1 /* === S I N F G =========================================================== */
3 ** \brief Template File
5 ** $Id: mutex.cpp,v 1.3 2005/01/12 00:55:46 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
33 #ifdef HAVE_LIBPTHREAD
34 #define USING_PTHREADS 1
37 #define USING_WIN32_THREADS 1
41 #ifdef USING_WIN32_THREADS
51 /* === U S I N G =========================================================== */
53 //using namespace std;
54 //using namespace etl;
55 using namespace sinfg;
57 /* === M A C R O S ========================================================= */
59 /* === G L O B A L S ======================================================= */
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
82 RecMutex::unlock_all()
84 while(is_locked()) unlock();
90 pthread_mutex_t*const mtx_ptr(new pthread_mutex_t);
92 pthread_mutexattr_t attr;
93 pthread_mutexattr_init(&attr);
95 //#ifdef PTHREAD_PRIO_INHERIT
96 //pthread_mutexattr_setprioceiling(&attr,PTHREAD_PRIO_INHERIT);
99 //#ifdef PTHREAD_MUTEX_RECURSIVE
100 //pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
103 pthread_mutex_init(mtx_ptr,&attr);
104 pthread_mutexattr_destroy(&attr);
111 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
112 pthread_mutex_destroy(mtx_ptr);
119 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
120 pthread_mutex_lock(mtx_ptr);
126 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
127 pthread_mutex_unlock(mtx_ptr);
133 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
134 return !(bool) pthread_mutex_trylock(mtx_ptr);
140 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
141 pthread_mutexattr_t attr;
143 // Backtrack and get rid of the non-recursive mutex
144 pthread_mutex_destroy(mtx_ptr);
146 pthread_mutexattr_init(&attr);
148 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
150 pthread_mutex_init(mtx_ptr,&attr);
151 pthread_mutexattr_destroy(&attr);
158 pthread_rwlock_t*const rwlock_ptr(new pthread_rwlock_t);
160 pthread_rwlock_init(rwlock_ptr, NULL);
167 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
169 pthread_rwlock_destroy(rwlock_ptr);
175 RWLock::reader_lock()
177 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
179 pthread_rwlock_rdlock(rwlock_ptr);
183 RWLock::reader_unlock()
185 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
187 pthread_rwlock_unlock(rwlock_ptr);
191 RWLock::reader_trylock()
193 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
195 return !pthread_rwlock_tryrdlock(rwlock_ptr);
199 RWLock::writer_lock()
201 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
203 pthread_rwlock_wrlock(rwlock_ptr);
207 RWLock::writer_unlock()
209 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
211 pthread_rwlock_unlock(rwlock_ptr);
215 RWLock::writer_trylock()
217 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
219 return !pthread_rwlock_trywrlock(rwlock_ptr);
224 #ifdef USING_WIN32_THREADS
227 HANDLE& mtx(*reinterpret_cast<HANDLE*>(&blackbox));
228 mtx=CreateMutex(NULL, FALSE, NULL);
233 HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
240 HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
241 WaitForSingleObject(mtx, INFINITE);
247 HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
254 HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
255 return WaitForSingleObject(mtx, 0)==WAIT_FAILED;
261 // Win32 mutexes are recursive by default.
274 RWLock::reader_lock()
279 RWLock::reader_unlock()
284 RWLock::reader_trylock()
289 RWLock::writer_lock()
294 RWLock::writer_unlock()
299 RWLock::writer_trylock()