33 : mCount(0), mValidID( false )
35 pthread_mutexattr_t attr;
39 res = pthread_mutexattr_init( &attr );
40 CLAM_ASSERT( res == 0,
"pthread_mutexattr_init failed" );
42 res = pthread_mutex_init( &mMutex, &attr );
43 CLAM_ASSERT( res == 0,
"pthread_mutex_init failed");
45 res = pthread_cond_init( &mUnlocked, 0 );
49 pthread_mutex_destroy( &mMutex );
57 res = pthread_mutex_destroy( &mMutex );
59 CLAM_ASSERT( res == 0,
"pthread_mutex_destroy failed" );
61 res = pthread_cond_destroy( &mUnlocked );
63 CLAM_ASSERT( res == 0,
"pthread_cond_destroy failed" );
67 void RecursiveMutex::DoLock()
71 res = pthread_mutex_lock( &mMutex );
73 pthread_t tid = pthread_self();
75 if ( mValidID && pthread_equal( mThreadID, tid ) )
81 res = pthread_cond_wait( &mUnlocked, &mMutex );
82 CLAM_ASSERT( res == 0,
"pthread_cond_wait failed" );
90 res = pthread_mutex_unlock( &mMutex );
91 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
94 void RecursiveMutex::DoUnlock( )
97 res = pthread_mutex_lock( &mMutex );
98 CLAM_ASSERT( res == 0,
"pthread_mutex_lock failed" );
100 pthread_t tid = pthread_self();
102 if ( mValidID && !pthread_equal( mThreadID, tid ) )
104 res = pthread_mutex_unlock( &mMutex );
105 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
113 res = pthread_cond_signal( &mUnlocked );
114 CLAM_ASSERT( res==0,
"pthread_cond_signal failed" );
117 res = pthread_mutex_unlock( &mMutex );
118 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
121 void RecursiveMutex::DoLock( ConditionVar& state )
127 res = pthread_cond_wait( &mUnlocked, &mMutex );
128 CLAM_ASSERT( res == 0,
"pthread_cond_wait failed!" );
131 mThreadID = pthread_self();
133 mCount = state.count;
135 res = pthread_mutex_unlock( &mMutex );
136 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
139 void RecursiveMutex::DoUnlock( ConditionVar& state )
143 res = pthread_mutex_lock( &mMutex );
144 CLAM_ASSERT( res == 0,
"pthread_mutex_lock failed" );
149 res = pthread_cond_signal( &mUnlocked );
150 CLAM_ASSERT( res == 0,
"pthread_cond_signal failed!" );
152 state.pmutex = &mMutex;
153 state.count = mCount;
157 : mCount(0), mValidID( false )
159 pthread_mutexattr_t attr;
163 res = pthread_mutexattr_init( &attr );
164 CLAM_ASSERT( res == 0,
"pthread_mutexattr_init failed" );
166 res = pthread_mutex_init( &mMutex, &attr );
167 CLAM_ASSERT( res == 0,
"pthread_mutex_init failed");
169 res = pthread_cond_init( &mUnlocked, 0 );
173 pthread_mutex_destroy( &mMutex );
181 res = pthread_mutex_destroy( &mMutex );
183 CLAM_ASSERT( res == 0,
"pthread_mutex_destroy failed" );
185 res = pthread_cond_destroy( &mUnlocked );
187 CLAM_ASSERT( res == 0,
"pthread_cond_destroy failed" );
191 void RecursiveTryMutex::DoLock()
195 res = pthread_mutex_lock( &mMutex );
197 pthread_t tid = pthread_self();
199 if ( mValidID && pthread_equal( mThreadID, tid ) )
205 res = pthread_cond_wait( &mUnlocked, &mMutex );
206 CLAM_ASSERT( res == 0,
"pthread_cond_wait failed" );
214 res = pthread_mutex_unlock( &mMutex );
215 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
218 bool RecursiveTryMutex::DoTryLock()
222 res = pthread_mutex_lock( &mMutex );
223 CLAM_ASSERT( res == 0,
" pthread_mutex_lock failed " );
227 pthread_t tid = pthread_self();
229 if (mValidID && pthread_equal(mThreadID, tid ) )
234 else if ( !mValidID )
242 res = pthread_mutex_unlock(&mMutex );
243 CLAM_ASSERT( res==0,
"pthread_mutex_unlock failed" );
248 void RecursiveTryMutex::DoUnlock( )
251 res = pthread_mutex_lock( &mMutex );
252 CLAM_ASSERT( res == 0,
"pthread_mutex_lock failed" );
254 pthread_t tid = pthread_self();
256 if ( mValidID && !pthread_equal( mThreadID, tid ) )
258 res = pthread_mutex_unlock( &mMutex );
259 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
267 res = pthread_cond_signal( &mUnlocked );
268 CLAM_ASSERT( res==0,
"pthread_cond_signal failed" );
271 res = pthread_mutex_unlock( &mMutex );
272 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
275 void RecursiveTryMutex::DoLock( ConditionVar& state )
281 res = pthread_cond_wait( &mUnlocked, &mMutex );
282 CLAM_ASSERT( res == 0,
"pthread_cond_wait failed!" );
285 mThreadID = pthread_self();
287 mCount = state.count;
289 res = pthread_mutex_unlock( &mMutex );
290 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
293 void RecursiveTryMutex::DoUnlock( ConditionVar& state )
297 res = pthread_mutex_lock( &mMutex );
298 CLAM_ASSERT( res == 0,
"pthread_mutex_lock failed" );
303 res = pthread_cond_signal( &mUnlocked );
304 CLAM_ASSERT( res == 0,
"pthread_cond_signal failed!" );
306 state.pmutex = &mMutex;
307 state.count = mCount;
312 : mCount(0), mValidID( false )
314 pthread_mutexattr_t attr;
318 res = pthread_mutexattr_init( &attr );
319 CLAM_ASSERT( res == 0,
"pthread_mutexattr_init failed" );
322 res = pthread_mutex_init( &mMutex, &attr );
323 CLAM_ASSERT( res == 0,
"pthread_mutex_init failed");
325 res = pthread_cond_init( &mUnlocked, 0 );
329 pthread_mutex_destroy( &mMutex );
337 res = pthread_mutex_destroy( &mMutex );
339 CLAM_ASSERT( res == 0,
"pthread_mutex_destroy failed" );
341 res = pthread_cond_destroy( &mUnlocked );
343 CLAM_ASSERT( res == 0,
"pthread_cond_destroy failed" );
347 void RecursiveTimedMutex::DoLock()
351 res = pthread_mutex_lock( &mMutex );
353 pthread_t tid = pthread_self();
355 if ( mValidID && pthread_equal( mThreadID, tid ) )
361 res = pthread_cond_wait( &mUnlocked, &mMutex );
362 CLAM_ASSERT( res == 0,
"pthread_cond_wait failed" );
370 res = pthread_mutex_unlock( &mMutex );
371 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
374 bool RecursiveTimedMutex::DoTryLock()
378 res = pthread_mutex_lock( &mMutex );
379 CLAM_ASSERT( res == 0,
" pthread_mutex_lock failed " );
383 pthread_t tid = pthread_self();
385 if (mValidID && pthread_equal(mThreadID, tid ) )
390 else if ( !mValidID )
398 res = pthread_mutex_unlock(&mMutex );
399 CLAM_ASSERT( res==0,
"pthread_mutex_unlock failed" );
404 bool RecursiveTimedMutex::DoTimedLock(
const xtime& xt )
408 res = pthread_mutex_lock( &mMutex );
409 CLAM_ASSERT( res == 0 ,
"pthread_mutex_lock failed" );
413 pthread_t tid = pthread_self();
415 if ( mValidID && pthread_equal( mThreadID, tid ) )
428 res = pthread_cond_timedwait( &mUnlocked, &mMutex, &ts );
429 if ( res == ETIMEDOUT )
431 CLAM_ASSERT( res==0,
"pthread_cond_timedwait failed" );
443 res = pthread_mutex_unlock( &mMutex );
444 CLAM_ASSERT( res==0,
"pthread_mutex_unlock failed" );
449 void RecursiveTimedMutex::DoUnlock( )
452 res = pthread_mutex_lock( &mMutex );
453 CLAM_ASSERT( res == 0,
"pthread_mutex_lock failed" );
455 pthread_t tid = pthread_self();
457 if ( mValidID && !pthread_equal( mThreadID, tid ) )
459 res = pthread_mutex_unlock( &mMutex );
460 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
468 res = pthread_cond_signal( &mUnlocked );
469 CLAM_ASSERT( res == 0,
"pthread_cond_signal failed" );
472 res = pthread_mutex_unlock( &mMutex );
473 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
476 void RecursiveTimedMutex::DoLock( ConditionVar& state )
482 res = pthread_cond_wait( &mUnlocked, &mMutex );
483 CLAM_ASSERT( res == 0,
"pthread_cond_wait failed!" );
486 mThreadID = pthread_self();
488 mCount = state.count;
490 res = pthread_mutex_unlock( &mMutex );
491 CLAM_ASSERT( res == 0,
"pthread_mutex_unlock failed" );
494 void RecursiveTimedMutex::DoUnlock( ConditionVar& state )
498 res = pthread_mutex_lock( &mMutex );
499 CLAM_ASSERT( res == 0,
"pthread_mutex_lock failed" );
504 res = pthread_cond_signal( &mUnlocked );
505 CLAM_ASSERT( res == 0,
"pthread_cond_signal failed!" );
507 state.pmutex = &mMutex;
508 state.count = mCount;