SmartPDF - lightweight pdf viewer app for rosapps
[reactos.git] / rosapps / smartpdf / poppler / goo / GooMutex.h
1 //========================================================================
2 //
3 // GooMutex.h
4 //
5 // Portable mutex macros.
6 //
7 // Copyright 2002-2003 Glyph & Cog, LLC
8 //
9 //========================================================================
10
11 #ifndef GMUTEX_H
12 #define GMUTEX_H
13
14 // Usage:
15 //
16 // GooMutex m;
17 // gInitMutex(&m);
18 // ...
19 // gLockMutex(&m);
20 // ... critical section ...
21 // gUnlockMutex(&m);
22 // ...
23 // gDestroyMutex(&m);
24
25 #ifdef WIN32
26
27 #include <windows.h>
28
29 typedef CRITICAL_SECTION GooMutex;
30
31 #define gInitMutex(m) InitializeCriticalSection(m)
32 #define gInitMutexReentrant(m) InitializeCriticalSection(m)
33 #define gDestroyMutex(m) DeleteCriticalSection(m)
34 #define gLockMutex(m) EnterCriticalSection(m)
35 #define gUnlockMutex(m) LeaveCriticalSection(m)
36
37 #else // assume pthreads
38
39 #include <pthread.h>
40
41 typedef pthread_mutex_t GooMutex;
42
43 #define gInitMutex(m) pthread_mutex_init(m, NULL)
44 #define gDestroyMutex(m) pthread_mutex_destroy(m)
45 #define gLockMutex(m) pthread_mutex_lock(m)
46 #define gUnlockMutex(m) pthread_mutex_unlock(m)
47
48 #define gInitMutexReentrant(m) \
49 { \
50 pthread_mutexattr_t attr; \
51 pthread_mutexattr_init(&attr); \
52 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); \
53 pthread_mutex_init(mutex, &attr); \
54 }
55 #endif
56
57 class MutexAutoInitDestroy {
58 public:
59 MutexAutoInitDestroy(GooMutex *mutex) {
60 mMutex = mutex;
61 gInitMutexReentrant(mMutex);
62 }
63 ~MutexAutoInitDestroy() {
64 gDestroyMutex(mMutex);
65 }
66 private:
67 GooMutex *mMutex;
68 };
69
70 #endif