1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
5 ** $Id: mutex.cpp,v 1.3 2005/01/12 00:55:46 darco Exp $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
34 #ifdef HAVE_LIBPTHREAD
35 #define USING_PTHREADS 1
38 #define USING_WIN32_THREADS 1
42 #ifdef USING_WIN32_THREADS
52 /* === U S I N G =========================================================== */
54 //using namespace std;
55 //using namespace etl;
56 using namespace synfig;
58 /* === M A C R O S ========================================================= */
60 /* === G L O B A L S ======================================================= */
62 /* === P R O C E D U R E S ================================================= */
64 /* === M E T H O D S ======================================================= */
83 RecMutex::unlock_all()
85 while(is_locked()) unlock();
91 pthread_mutex_t*const mtx_ptr(new pthread_mutex_t);
93 pthread_mutexattr_t attr;
94 pthread_mutexattr_init(&attr);
96 //#ifdef PTHREAD_PRIO_INHERIT
97 //pthread_mutexattr_setprioceiling(&attr,PTHREAD_PRIO_INHERIT);
100 //#ifdef PTHREAD_MUTEX_RECURSIVE
101 //pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
104 pthread_mutex_init(mtx_ptr,&attr);
105 pthread_mutexattr_destroy(&attr);
112 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
113 pthread_mutex_destroy(mtx_ptr);
120 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
121 pthread_mutex_lock(mtx_ptr);
127 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
128 pthread_mutex_unlock(mtx_ptr);
134 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
135 return !(bool) pthread_mutex_trylock(mtx_ptr);
141 pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
142 pthread_mutexattr_t attr;
144 // Backtrack and get rid of the non-recursive mutex
145 pthread_mutex_destroy(mtx_ptr);
147 pthread_mutexattr_init(&attr);
149 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
151 pthread_mutex_init(mtx_ptr,&attr);
152 pthread_mutexattr_destroy(&attr);
159 pthread_rwlock_t*const rwlock_ptr(new pthread_rwlock_t);
161 pthread_rwlock_init(rwlock_ptr, NULL);
168 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
170 pthread_rwlock_destroy(rwlock_ptr);
176 RWLock::reader_lock()
178 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
180 pthread_rwlock_rdlock(rwlock_ptr);
184 RWLock::reader_unlock()
186 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
188 pthread_rwlock_unlock(rwlock_ptr);
192 RWLock::reader_trylock()
194 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
196 return !pthread_rwlock_tryrdlock(rwlock_ptr);
200 RWLock::writer_lock()
202 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
204 pthread_rwlock_wrlock(rwlock_ptr);
208 RWLock::writer_unlock()
210 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
212 pthread_rwlock_unlock(rwlock_ptr);
216 RWLock::writer_trylock()
218 pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
220 return !pthread_rwlock_trywrlock(rwlock_ptr);
225 #ifdef USING_WIN32_THREADS
228 HANDLE& mtx(*reinterpret_cast<HANDLE*>(&blackbox));
229 mtx=CreateMutex(NULL, FALSE, NULL);
234 HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
241 HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
242 WaitForSingleObject(mtx, INFINITE);
248 HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
255 HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
256 return WaitForSingleObject(mtx, 0)==WAIT_FAILED;
262 // Win32 mutexes are recursive by default.
275 RWLock::reader_lock()
280 RWLock::reader_unlock()
285 RWLock::reader_trylock()
290 RWLock::writer_lock()
295 RWLock::writer_unlock()
300 RWLock::writer_trylock()