Mixe for Privacy and Anonymity in the Internet
CAConditionVariable.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #ifndef __CACONVAR__
29 #define __CACONVAR__
30 #if !defined ONLY_LOCAL_PROXY || defined INCLUDE_MIDDLE_MIX
31 
32 #include "CAMutex.hpp"
33 #include "CAUtil.hpp"
34 #include "CASemaphore.hpp"
35 
37  {
38  public:
40  {
41  #ifdef HAVE_PTHREAD_CV
42  m_pCondVar=new pthread_cond_t;
43  pthread_cond_init(m_pCondVar,NULL);
44  #else
45  m_pMutex=new CAMutex();
46  m_pSemaphore=new CASemaphore(0);
47  m_iSleepers=0;
48  #endif
49  }
50 
52  {
53  #ifdef HAVE_PTHREAD_CV
54  pthread_cond_destroy(m_pCondVar);
55  delete m_pCondVar;
56  m_pCondVar = NULL;
57  #else
58  delete m_pMutex;
59  m_pMutex = NULL;
60  delete m_pSemaphore;
61  m_pSemaphore = NULL;
62  #endif
63  }
64 
72  {
73  #ifdef HAVE_PTHREAD_CV
74  if(pthread_cond_wait(m_pCondVar,m_pMutex)==0)
75  return E_SUCCESS;
76  return E_UNKNOWN;
77  #else
78  m_pMutex->lock();
79  unlock(); // Release the lock that is associated with our cv
80  m_iSleepers++;
81  m_pMutex->unlock();
82  m_pSemaphore->down();
83  return lock();
84  #endif
85  }
86 
89  SINT32 wait(CAMutex& oMutex)
90  {
91  #ifdef HAVE_PTHREAD_CV
92  if(pthread_cond_wait(m_pCondVar,oMutex.m_pMutex)==0)
93  return E_SUCCESS;
94  return E_UNKNOWN;
95  #else
96  m_pMutex->lock();
97  oMutex.unlock(); // Release the lock that is associated with our cv
98  m_iSleepers++;
99  m_pMutex->unlock();
100  m_pSemaphore->down();
101  return oMutex.lock();
102  #endif
103  }
104 
107  SINT32 wait(CAMutex* pMutex)
108  {
109  #ifdef HAVE_PTHREAD_CV
110  if(pthread_cond_wait(m_pCondVar,pMutex->m_pMutex)==0)
111  return E_SUCCESS;
112  return E_UNKNOWN;
113  #else
114  m_pMutex->lock();
115  pMutex->unlock(); // Release the lock that is associated with our cv
116  m_iSleepers++;
117  m_pMutex->unlock();
118  m_pSemaphore->down();
119  return pMutex->lock();
120  #endif
121  }
122 
131  SINT32 wait(UINT32 msTimeout)
132  {
133  #ifdef HAVE_PTHREAD_CV
134  timespec to;
135  getcurrentTime(to);
136  to.tv_nsec+=(msTimeout%1000)*1000000;
137  to.tv_sec+=msTimeout/1000;
138  if(to.tv_nsec>999999999)
139  {
140  to.tv_sec++;
141  to.tv_nsec-=1000000000;
142  }
143  int ret=pthread_cond_timedwait(m_pCondVar,m_pMutex,&to);
144  if(ret==0)
145  return E_SUCCESS;
146  else if(ret==ETIMEDOUT)
147  return E_TIMEDOUT;
148  return E_UNKNOWN;
149  #else
151  return wait();
152  #endif
153  }
154 
160  {
161  #ifdef HAVE_PTHREAD_CV
162  if(pthread_cond_signal(m_pCondVar)==0)
163  return E_SUCCESS;
164  return E_UNKNOWN;
165  #else
166  m_pMutex->lock();
167  if( m_iSleepers > 0 )
168  {
169  m_pSemaphore->up();
170  m_iSleepers--;
171  }
172  return m_pMutex->unlock();
173  #endif
174  }
175 
181  {
182  #ifdef HAVE_PTHREAD_CV
183  if(pthread_cond_broadcast(m_pCondVar)==0)
184  return E_SUCCESS;
185  return E_UNKNOWN;
186  #else
187  m_pMutex->lock();
188  while( m_iSleepers > 0 )
189  {
190  m_pSemaphore->up();
191  m_iSleepers--;
192  }
193  return m_pMutex->unlock();
194  #endif
195  }
196 
197  private:
198  #ifdef HAVE_PTHREAD_CV
199  pthread_cond_t* m_pCondVar;
200  #else
202  CASemaphore* m_pSemaphore;
204  #endif
205  };
206 #endif
207 #endif //ONLY_LOCAL_PROXY
SINT32 getcurrentTime(timespec &t)
Gets the current Systemtime in milli seconds.
Definition: CAUtil.cpp:227
signed int SINT32
Definition: basetypedefs.h:132
unsigned int UINT32
Definition: basetypedefs.h:131
SINT32 signal()
Signals this object.
SINT32 wait(CAMutex *pMutex)
Very ugly shortly to be deleted, uncommented function!
SINT32 broadcast()
Signals this object.
SINT32 wait()
Waits for a signal or for a timeout.
SINT32 wait(UINT32 msTimeout)
Waits for a signal or for a timeout.
SINT32 wait(CAMutex &oMutex)
Very ugly shortly to be deleted, uncommented function!
CASemaphore * m_pMutex
Definition: CAMutex.hpp:69
CAMutex()
Definition: CAMutex.cpp:33
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
#define E_TIMEDOUT
Definition: errorcodes.hpp:10