moreupdates
[synfig.git] / synfig-core / trunk / src / synfig / mutex.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mutex.cpp
3 **      \brief Template File
4 **
5 **      $Id: mutex.cpp,v 1.3 2005/01/12 00:55:46 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
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.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "mutex.h"
32
33 #ifdef HAVE_LIBPTHREAD
34 #define USING_PTHREADS 1
35 #else
36 #ifdef _WIN32
37 #define USING_WIN32_THREADS 1
38 #endif
39 #endif
40
41 #ifdef USING_WIN32_THREADS
42 #include <windows.h>
43 #endif
44
45 #ifdef USING_PTHREADS
46 #include <pthread.h>
47 #endif
48
49 #endif
50
51 /* === U S I N G =========================================================== */
52
53 //using namespace std;
54 //using namespace etl;
55 using namespace synfig;
56
57 /* === M A C R O S ========================================================= */
58
59 /* === G L O B A L S ======================================================= */
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65
66
67
68
69
70 bool
71 Mutex::is_locked()
72 {
73         if(try_lock())
74         {
75                 unlock();
76                 return false;
77         }
78         return true;
79 }
80
81 void
82 RecMutex::unlock_all()
83 {
84         while(is_locked()) unlock();
85 }
86
87 #ifdef USING_PTHREADS
88 Mutex::Mutex()
89 {
90         pthread_mutex_t*const mtx_ptr(new pthread_mutex_t);
91
92         pthread_mutexattr_t attr;
93         pthread_mutexattr_init(&attr);
94
95         //#ifdef PTHREAD_PRIO_INHERIT
96         //pthread_mutexattr_setprioceiling(&attr,PTHREAD_PRIO_INHERIT);
97         //#endif
98
99         //#ifdef PTHREAD_MUTEX_RECURSIVE
100         //pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
101         //#endif
102
103         pthread_mutex_init(mtx_ptr,&attr);
104         pthread_mutexattr_destroy(&attr);
105         
106         blackbox=mtx_ptr;
107 }
108
109 Mutex::~Mutex()
110 {
111         pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
112         pthread_mutex_destroy(mtx_ptr);
113         delete mtx_ptr;
114 }
115
116 void
117 Mutex::lock()
118 {
119         pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
120         pthread_mutex_lock(mtx_ptr);
121 }
122
123 void
124 Mutex::unlock()
125 {
126         pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
127         pthread_mutex_unlock(mtx_ptr);
128 }
129
130 bool
131 Mutex::try_lock()
132 {
133         pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
134         return !(bool) pthread_mutex_trylock(mtx_ptr);
135 }
136
137
138 RecMutex::RecMutex()
139 {
140         pthread_mutex_t*const mtx_ptr(static_cast<pthread_mutex_t*>(blackbox));
141         pthread_mutexattr_t attr;
142
143         // Backtrack and get rid of the non-recursive mutex
144         pthread_mutex_destroy(mtx_ptr);
145
146         pthread_mutexattr_init(&attr);
147
148         pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
149
150         pthread_mutex_init(mtx_ptr,&attr);
151         pthread_mutexattr_destroy(&attr);
152 }
153
154
155
156 RWLock::RWLock()
157 {
158         pthread_rwlock_t*const rwlock_ptr(new pthread_rwlock_t);
159         
160         pthread_rwlock_init(rwlock_ptr, NULL);
161         
162         blackbox=rwlock_ptr;
163 }
164
165 RWLock::~RWLock()
166 {
167         pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
168
169         pthread_rwlock_destroy(rwlock_ptr);
170         
171         delete rwlock_ptr;
172 }
173
174 void
175 RWLock::reader_lock()
176 {
177         pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
178
179         pthread_rwlock_rdlock(rwlock_ptr);
180 }
181
182 void
183 RWLock::reader_unlock()
184 {
185         pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
186
187         pthread_rwlock_unlock(rwlock_ptr);
188 }
189
190 bool
191 RWLock::reader_trylock()
192 {
193         pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
194
195         return !pthread_rwlock_tryrdlock(rwlock_ptr);
196 }
197
198 void
199 RWLock::writer_lock()
200 {
201         pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
202
203         pthread_rwlock_wrlock(rwlock_ptr);
204 }
205
206 void
207 RWLock::writer_unlock()
208 {
209         pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
210
211         pthread_rwlock_unlock(rwlock_ptr);
212 }
213
214 bool
215 RWLock::writer_trylock()
216 {
217         pthread_rwlock_t*const rwlock_ptr(static_cast<pthread_rwlock_t*>(blackbox));
218
219         return !pthread_rwlock_trywrlock(rwlock_ptr);
220 }
221
222 #endif
223
224 #ifdef USING_WIN32_THREADS
225 Mutex::Mutex()
226 {
227         HANDLE& mtx(*reinterpret_cast<HANDLE*>(&blackbox));
228         mtx=CreateMutex(NULL, FALSE, NULL);
229 }
230
231 Mutex::~Mutex()
232 {
233         HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
234         CloseHandle(mtx);
235 }
236
237 void
238 Mutex::lock()
239 {
240         HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
241         WaitForSingleObject(mtx, INFINITE);
242 }
243
244 void
245 Mutex::unlock()
246 {
247         HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
248         ReleaseMutex(mtx);
249 }
250
251 bool
252 Mutex::try_lock()
253 {
254         HANDLE mtx(reinterpret_cast<HANDLE>(blackbox));
255         return WaitForSingleObject(mtx, 0)==WAIT_FAILED;
256 }
257
258
259 RecMutex::RecMutex()
260 {
261         // Win32 mutexes are recursive by default.
262 }
263
264
265 RWLock::RWLock()
266 {
267 }
268
269 RWLock::~RWLock()
270 {
271 }
272
273 void
274 RWLock::reader_lock()
275 {
276 }
277
278 void
279 RWLock::reader_unlock()
280 {
281 }
282
283 bool
284 RWLock::reader_trylock()
285 {
286 }
287
288 void
289 RWLock::writer_lock()
290 {
291 }
292
293 void
294 RWLock::writer_unlock()
295 {
296 }
297
298 bool
299 RWLock::writer_trylock()
300 {
301 }
302
303 #endif