langpackedit v0.13 -- from 8f9f0878
[wdl.git] / WDL / mutex.h
bloba83c24f476f7e2d0b3deb9d0072ce1ef51dd30b8
1 /*
2 WDL - mutex.h
3 Copyright (C) 2005 and later, Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
26 This file provides a simple class that abstracts a mutex or critical section object.
27 On Windows it uses CRITICAL_SECTION, on everything else it uses pthread's mutex library.
28 It simulates the Critical Section behavior on non-Windows, as well (meaning a thread can
29 safely Enter the mutex multiple times, provided it Leaves the same number of times)
33 #ifndef _WDL_MUTEX_H_
34 #define _WDL_MUTEX_H_
36 #ifdef _WIN32
37 #include <windows.h>
38 #else
40 #include <unistd.h>
41 // define this if you wish to use carbon critical sections on OS X
42 // #define WDL_MAC_USE_CARBON_CRITSEC
44 #ifdef WDL_MAC_USE_CARBON_CRITSEC
45 #include <Carbon/Carbon.h>
46 #else
47 #include <pthread.h>
48 #endif
50 #endif
52 #include "wdltypes.h"
53 #include "wdlatomic.h"
55 #ifdef _DEBUG
56 #include <assert.h>
57 #endif
59 class WDL_Mutex {
60 public:
61 WDL_Mutex()
63 #ifdef _WIN32
64 InitializeCriticalSection(&m_cs);
65 #elif defined( WDL_MAC_USE_CARBON_CRITSEC)
66 MPCreateCriticalRegion(&m_cr);
67 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER) && !defined(__linux__)
68 const pthread_mutex_t tmp = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
69 m_mutex = tmp;
70 #else
71 pthread_mutexattr_t attr;
72 pthread_mutexattr_init(&attr);
73 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
74 #ifdef __linux__
75 // todo: macos too?
76 pthread_mutexattr_setprotocol(&attr,PTHREAD_PRIO_INHERIT);
77 #endif
78 pthread_mutex_init(&m_mutex,&attr);
79 pthread_mutexattr_destroy(&attr);
80 #endif
82 ~WDL_Mutex()
84 #ifdef _WIN32
85 DeleteCriticalSection(&m_cs);
86 #elif defined(WDL_MAC_USE_CARBON_CRITSEC)
87 MPDeleteCriticalRegion(m_cr);
88 #else
89 pthread_mutex_destroy(&m_mutex);
90 #endif
93 void Enter()
95 #ifdef _WIN32
96 EnterCriticalSection(&m_cs);
97 #elif defined(WDL_MAC_USE_CARBON_CRITSEC)
98 MPEnterCriticalRegion(m_cr,kDurationForever);
99 #else
100 pthread_mutex_lock(&m_mutex);
101 #endif
104 void Leave()
106 #ifdef _WIN32
107 LeaveCriticalSection(&m_cs);
108 #elif defined(WDL_MAC_USE_CARBON_CRITSEC)
109 MPExitCriticalRegion(m_cr);
110 #else
111 pthread_mutex_unlock(&m_mutex);
112 #endif
115 private:
116 #ifdef _WIN32
117 CRITICAL_SECTION m_cs;
118 #elif defined(WDL_MAC_USE_CARBON_CRITSEC)
119 MPCriticalRegionID m_cr;
120 #else
121 pthread_mutex_t m_mutex;
122 #endif
124 // prevent callers from copying mutexes accidentally
125 WDL_Mutex(const WDL_Mutex &cp)
127 #ifdef _DEBUG
128 assert(sizeof(WDL_Mutex) == 0);
129 #endif
131 WDL_Mutex &operator=(const WDL_Mutex &cp)
133 #ifdef _DEBUG
134 assert(sizeof(WDL_Mutex) == 0);
135 #endif
136 return *this;
139 } WDL_FIXALIGN;
141 class WDL_MutexLock {
142 public:
143 WDL_MutexLock(WDL_Mutex *m) : m_m(m) { if (m) m->Enter(); }
144 ~WDL_MutexLock() { if (m_m) m_m->Leave(); }
146 // the caller modifies this, make sure it unlocks the mutex first and locks the new mutex!
147 WDL_Mutex *m_m;
148 } WDL_FIXALIGN;
150 class WDL_SharedMutex
152 public:
153 WDL_SharedMutex() { m_sharedcnt=0; }
154 ~WDL_SharedMutex() { }
156 void LockExclusive() // note: the calling thread must NOT have any shared locks, or deadlock WILL occur
158 m_mutex.Enter();
159 #ifdef _WIN32
160 while (m_sharedcnt>0) Sleep(1);
161 #else
162 while (m_sharedcnt>0) usleep(100);
163 #endif
165 void UnlockExclusive() { m_mutex.Leave(); }
167 void LockShared()
169 m_mutex.Enter();
170 wdl_atomic_incr(&m_sharedcnt);
171 m_mutex.Leave();
173 void UnlockShared()
175 wdl_atomic_decr(&m_sharedcnt);
178 void SharedToExclusive() // assumes a SINGLE shared lock by this thread!
180 m_mutex.Enter();
181 #ifdef _WIN32
182 while (m_sharedcnt>1) Sleep(1);
183 #else
184 while (m_sharedcnt>1) usleep(100);
185 #endif
186 UnlockShared();
189 void ExclusiveToShared() // assumes exclusive locked returns with shared locked
191 // already have exclusive lock
192 wdl_atomic_incr(&m_sharedcnt);
193 m_mutex.Leave();
196 private:
197 WDL_Mutex m_mutex;
198 volatile int m_sharedcnt;
200 // prevent callers from copying accidentally
201 WDL_SharedMutex(const WDL_SharedMutex &cp)
203 #ifdef _DEBUG
204 assert(sizeof(WDL_SharedMutex) == 0);
205 #endif
207 WDL_SharedMutex &operator=(const WDL_SharedMutex &cp)
209 #ifdef _DEBUG
210 assert(sizeof(WDL_SharedMutex) == 0);
211 #endif
212 return *this;
216 } WDL_FIXALIGN;
220 class WDL_MutexLockShared {
221 public:
222 WDL_MutexLockShared(WDL_SharedMutex *m) : m_m(m) { if (m) m->LockShared(); }
223 ~WDL_MutexLockShared() { if (m_m) m_m->UnlockShared(); }
224 private:
225 WDL_SharedMutex *m_m;
226 } WDL_FIXALIGN;
228 class WDL_MutexLockExclusive {
229 public:
230 WDL_MutexLockExclusive(WDL_SharedMutex *m) : m_m(m) { if (m) m->LockExclusive(); }
231 ~WDL_MutexLockExclusive() { if (m_m) m_m->UnlockExclusive(); }
232 private:
233 WDL_SharedMutex *m_m;
234 } WDL_FIXALIGN;
237 #endif