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)
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>
53 #include "wdlatomic.h"
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
;
71 pthread_mutexattr_t attr
;
72 pthread_mutexattr_init(&attr
);
73 pthread_mutexattr_settype(&attr
,PTHREAD_MUTEX_RECURSIVE
);
76 pthread_mutexattr_setprotocol(&attr
,PTHREAD_PRIO_INHERIT
);
78 pthread_mutex_init(&m_mutex
,&attr
);
79 pthread_mutexattr_destroy(&attr
);
85 DeleteCriticalSection(&m_cs
);
86 #elif defined(WDL_MAC_USE_CARBON_CRITSEC)
87 MPDeleteCriticalRegion(m_cr
);
89 pthread_mutex_destroy(&m_mutex
);
96 EnterCriticalSection(&m_cs
);
97 #elif defined(WDL_MAC_USE_CARBON_CRITSEC)
98 MPEnterCriticalRegion(m_cr
,kDurationForever
);
100 pthread_mutex_lock(&m_mutex
);
107 LeaveCriticalSection(&m_cs
);
108 #elif defined(WDL_MAC_USE_CARBON_CRITSEC)
109 MPExitCriticalRegion(m_cr
);
111 pthread_mutex_unlock(&m_mutex
);
117 CRITICAL_SECTION m_cs
;
118 #elif defined(WDL_MAC_USE_CARBON_CRITSEC)
119 MPCriticalRegionID m_cr
;
121 pthread_mutex_t m_mutex
;
124 // prevent callers from copying mutexes accidentally
125 WDL_Mutex(const WDL_Mutex
&cp
)
128 assert(sizeof(WDL_Mutex
) == 0);
131 WDL_Mutex
&operator=(const WDL_Mutex
&cp
)
134 assert(sizeof(WDL_Mutex
) == 0);
141 class WDL_MutexLock
{
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!
150 class WDL_SharedMutex
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
160 while (m_sharedcnt
>0) Sleep(1);
162 while (m_sharedcnt
>0) usleep(100);
165 void UnlockExclusive() { m_mutex
.Leave(); }
170 wdl_atomic_incr(&m_sharedcnt
);
175 wdl_atomic_decr(&m_sharedcnt
);
178 void SharedToExclusive() // assumes a SINGLE shared lock by this thread!
182 while (m_sharedcnt
>1) Sleep(1);
184 while (m_sharedcnt
>1) usleep(100);
189 void ExclusiveToShared() // assumes exclusive locked returns with shared locked
191 // already have exclusive lock
192 wdl_atomic_incr(&m_sharedcnt
);
198 volatile int m_sharedcnt
;
200 // prevent callers from copying accidentally
201 WDL_SharedMutex(const WDL_SharedMutex
&cp
)
204 assert(sizeof(WDL_SharedMutex
) == 0);
207 WDL_SharedMutex
&operator=(const WDL_SharedMutex
&cp
)
210 assert(sizeof(WDL_SharedMutex
) == 0);
220 class WDL_MutexLockShared
{
222 WDL_MutexLockShared(WDL_SharedMutex
*m
) : m_m(m
) { if (m
) m
->LockShared(); }
223 ~WDL_MutexLockShared() { if (m_m
) m_m
->UnlockShared(); }
225 WDL_SharedMutex
*m_m
;
228 class WDL_MutexLockExclusive
{
230 WDL_MutexLockExclusive(WDL_SharedMutex
*m
) : m_m(m
) { if (m
) m
->LockExclusive(); }
231 ~WDL_MutexLockExclusive() { if (m_m
) m_m
->UnlockExclusive(); }
233 WDL_SharedMutex
*m_m
;