CLAM-Development  1.4.0
Mutex.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "Mutex.hxx"
23 #include "Assert.hxx"
24 #include "xtime.hxx"
25 #include "ErrSystem.hxx"
26 
27 namespace CLAM
28 {
29 
31  {
32  int res = 0;
33 
34  res = pthread_mutex_init( &mMutex, 0 );
35 
36  CLAM_ASSERT( res == 0, "Not able to initialize mutex" );
37  }
38 
40  {
41  int res = 0;
42 
43  res = pthread_mutex_destroy( &mMutex );
44 
45  CLAM_ASSERT( res == 0, "Not able to destroy mutex" );
46  }
47 
48  void Mutex::DoLock()
49  {
50  int res = 0;
51 
52  res = pthread_mutex_lock( &mMutex );
53 
54  if ( res == EDEADLK )
55  throw LockError(); // What is EDEADLK????
56 
57  CLAM_ASSERT( res == 0, "Not able to lock the mutex" );
58  }
59 
60  void Mutex::DoUnlock()
61  {
62  int res = 0;
63 
64  res = pthread_mutex_unlock( &mMutex );
65 
66  if ( res == EPERM )
67  throw LockError(); // What is EPERM?????
68 
69  CLAM_ASSERT( res == 0, "Not able to Unlock the mutex" );
70  }
71 
72  void Mutex::DoLock( ConditionVar& state )
73  {
74  // Does nothing
75  }
76 
77  void Mutex::DoUnlock( ConditionVar& state )
78  {
79  state.pmutex = &mMutex;
80  }
81 
83  {
84  int res = 0;
85 
86  res = pthread_mutex_init( &mMutex, 0 );
87 
88  CLAM_ASSERT( res == 0, "Not able to initialize mutex" );
89 
90  }
91 
93  {
94  int res = 0;
95 
96  res = pthread_mutex_destroy( &mMutex );
97 
98  CLAM_ASSERT( res == 0, "Not able to destroy mutex" );
99 
100  }
101 
102  void TryMutex::DoLock()
103  {
104  int res = 0;
105  res = pthread_mutex_lock( &mMutex );
106  if ( res == EDEADLK )
107  throw LockError();
108 
109  CLAM_ASSERT( res == 0, "Not able to lock mutex" );
110  }
111 
112  bool TryMutex::DoTryLock()
113  {
114  int res = 0;
115 
116  res = pthread_mutex_trylock(&mMutex );
117 
118  if ( res == EDEADLK )
119  throw LockError();
120 
121  CLAM_ASSERT ( (res == 0) || (res == EBUSY), "Not able to try-lock the mutex" );
122 
123  return res == 0;
124  }
125 
126  void TryMutex::DoUnlock()
127  {
128  int res = 0;
129 
130  res = pthread_mutex_unlock( &mMutex );
131 
132  if ( res == EPERM )
133  throw LockError(); // What is EPERM?????
134 
135  CLAM_ASSERT( res == 0, "Not able to Unlock the mutex" );
136  }
137 
138  void TryMutex::DoLock( ConditionVar& state )
139  {
140  // Does nothing
141  }
142 
143  void TryMutex::DoUnlock( ConditionVar& state )
144  {
145  state.pmutex = &mMutex;
146  }
147 
149  : mLocked( false )
150  {
151  int res = 0;
152 
153  res = pthread_mutex_init( &mMutex, 0 );
154 
155  CLAM_ASSERT( res==0, "Not able to initilize the mutex" );
156 
157  res = pthread_cond_init( &mCondition, 0 );
158 
159  if ( res!= 0 )
160  {
161  pthread_mutex_destroy( &mMutex );
162  throw ThreadResourceError("Unable to initialize Condition " );
163  }
164  }
165 
167  {
168  CLAM_ASSERT( !mLocked, "Mutex was locked while attempting to destroy it!" );
169 
170  int res = 0;
171 
172  res = pthread_mutex_destroy(&mMutex );
173 
174  CLAM_ASSERT( res == 0, "Unable to destroy the mutex!" );
175 
176  res = pthread_cond_destroy( &mCondition );
177 
178  CLAM_ASSERT( res == 0, "Unable to destroy the Condition variable" );
179  }
180 
181  void TimedMutex::DoLock( )
182  {
183  int res = 0;
184 
185  res = pthread_mutex_lock( &mMutex );
186  CLAM_ASSERT( res == 0, "Unable to lock mutex (already locked) " );
187 
188  while( mLocked )
189  {
190  res = pthread_cond_wait( &mCondition, &mMutex );
191  CLAM_ASSERT( res == 0, "Wait failed" );
192  }
193 
194  CLAM_ASSERT( !mLocked, "Spurious value for loop condition" );
195  mLocked = true;
196 
197  res = pthread_mutex_unlock( &mMutex );
198  CLAM_ASSERT( res ==0, "Unable to unlock the mutex" );
199  }
200 
201  bool TimedMutex::DoTryLock()
202  {
203  int res = 0;
204 
205  res = pthread_mutex_lock( &mMutex );
206 
207  CLAM_ASSERT( res == 0, "Unable to lock the mutex" );
208 
209  bool ret = false;
210 
211  if ( !mLocked )
212  {
213  mLocked = true;
214  ret = true;
215  }
216 
217  res = pthread_mutex_unlock( &mMutex );
218  CLAM_ASSERT( res==0, "Unable to unlock the mutex" );
219 
220  return ret;
221  }
222 
223  bool TimedMutex::DoTimedLock( const xtime& xt )
224  {
225  int res = 0;
226  res = pthread_mutex_lock( &mMutex );
227  CLAM_ASSERT( res == 0, "Unable to lock the mutex" );
228 
229  timespec ts;
230  to_timespec(xt, ts);
231 
232  while (mLocked)
233  {
234  res = pthread_cond_timedwait(&mCondition, &mMutex, &ts);
235  CLAM_ASSERT(res == 0 || res == ETIMEDOUT, "Low level error");
236 
237  if (res == ETIMEDOUT)
238  break;
239  }
240 
241  bool ret = false;
242  if (!mLocked)
243  {
244  mLocked = true;
245  ret = true;
246  }
247 
248  res = pthread_mutex_unlock(&mMutex);
249  CLAM_ASSERT(res == 0, "Something low level failed!");
250  return ret;
251  }
252 
253  void TimedMutex::DoUnlock()
254  {
255  int res = 0;
256  res = pthread_mutex_lock(&mMutex);
257  CLAM_ASSERT(res == 0, "Unable to lock the mutex");
258 
259  CLAM_ASSERT(mLocked, "No condition spurious value change");
260  mLocked = false;
261 
262  res = pthread_cond_signal(&mCondition);
263  CLAM_ASSERT(res == 0, "Not able to change the condition var value");
264 
265  res = pthread_mutex_unlock(&mMutex);
266  CLAM_ASSERT(res == 0, "Unable to unlock the mutex");
267  }
268 
269  void TimedMutex::DoLock(ConditionVar& v)
270  {
271  int res = 0;
272  while (mLocked)
273  {
274  res = pthread_cond_wait(&mCondition, &mMutex);
275  CLAM_ASSERT(res == 0, "pthread_cond_wait call failed!");
276  }
277 
278  CLAM_ASSERT(!mLocked, "Spurious value change");
279  mLocked = true;
280 
281  res = pthread_mutex_unlock(&mMutex);
282  CLAM_ASSERT(res == 0, "pthread_mutex_unlock call failed!");
283  }
284 
285  void TimedMutex::DoUnlock(ConditionVar& state)
286  {
287  int res = 0;
288  res = pthread_mutex_lock(&mMutex);
289  CLAM_ASSERT(res == 0, "pthread_mutex_lock call failed");
290 
291  CLAM_ASSERT(mLocked, "Spurious value change!");
292  mLocked = false;
293 
294  res = pthread_cond_signal(&mCondition);
295  CLAM_ASSERT(res == 0, "pthread_cond_signal call failed!");
296 
297  state.pmutex = &mMutex;
298  }
299 
300 } // end of namespace CLAM
301