All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Gate.cpp
Go to the documentation of this file.
1 /*
2  * Gate.cpp
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <HSEP/Gate.h>
23 
24 #include <thread>
25 
26 #include <iostream>
27 
28 using namespace std;
29 
30 namespace HSEP {
31 
32  Gate::Gate(bool pShared) {
33 
34 #ifdef POSIX_BASE
35  pthread_mutexattr_t vMutexAttr;
36 // pthread_condattr_t vCondAttr;
37  pthread_mutexattr_init(&vMutexAttr);
38 // pthread_condattr_init(&vCondAttr);
39  if (pShared) {
40  pthread_mutexattr_setpshared(&vMutexAttr,PTHREAD_PROCESS_SHARED);
41 // pthread_condattr_setpshared (&vCondAttr,PTHREAD_PROCESS_SHARED);
42  }
43  else {
44  pthread_mutexattr_setpshared(&vMutexAttr,PTHREAD_PROCESS_PRIVATE);
45 // pthread_condattr_setpshared (&vCondAttr,PTHREAD_PROCESS_PRIVATE);
46  }
47  pthread_mutex_init(&aMutexHandler,&vMutexAttr);
48 // pthread_cond_init(&aConditionHandler,&vCondAttr);
49 // pthread_condattr_destroy(&vCondAttr);
50  pthread_mutexattr_destroy(&vMutexAttr);
51 #else
52  aMutexPtr = new mutex();
53 // aConditionPtr = new condition_variable();
54 #endif
55  aWaiting = true;
56  pthread_mutex_lock(&aMutexHandler);
57 
58  } // Gate constructor
59 
60  Gate::~Gate() {
61 #ifdef POSIX_BASE
62 // pthread_cond_destroy(&aConditionHandler);
63  pthread_mutex_destroy (&aMutexHandler);
64 #else
65  delete aMutexPtr;
66  delete aConditionPtr;
67 #endif
68  } // Gate destructor
69 
70  void Gate::signal() {
71 #ifdef POSIX_BASE
72 // pthread_mutex_lock(&aMutexHandler);
73  aWaiting = false;
74 // pthread_cond_signal(&aConditionHandler);
75  pthread_mutex_unlock(&aMutexHandler);
76 #else
77  // ANSI -- needs to be checked
78  aWaiting = false;
79  aConditionPtr->notify_one();
80 #endif
81  } // Gate::open
82 
83  void Gate::wait() {
84 #ifdef POSIX_BASE
85 
86  bool vEndLoop = false;
87  do {
88  pthread_mutex_lock(&aMutexHandler);
89  if (!aWaiting) {
90  vEndLoop = true;
91  aWaiting = true;
92  }
93  // pthread_mutex_unlock(&aMutexHandler);
94  } while (!vEndLoop);
95 
100 // pthread_mutex_lock(&aMutexHandler);
101 // while (aWaiting) {
102 // pthread_cond_wait(&aConditionHandler,&aMutexHandler);
103 // if (aWaiting) {
104 // cout << "Beep on" << endl;
105 // }
106 // else {
107 // cout << "Beep off" << endl;
108 // }
109 // }
110 // aWaiting = true; // for next call
111 // pthread_mutex_unlock(&aMutexHandler);
112 #else
113 
114  unique_lock<mutex> vLock(*aMutexPtr);
123  aConditionPtr->wait(vLock,[&](){
124  if (!aWaiting) {
125  aWaiting = true;
126  return true;
127  }
128  else {
129  return false;
130  }
131  }); // lambda
132 #endif
133  } // Gate::wait
134 
135 } // HSEP namespace
136