4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains the C functions that implement mutexes for win32
14 #include "sqliteInt.h"
17 ** The code in this file is only used if we are compiling multithreaded
20 #ifdef SQLITE_MUTEX_W32
23 ** Each recursive mutex is an instance of the following structure.
25 struct sqlite3_mutex
{
26 CRITICAL_SECTION mutex
; /* Mutex controlling the lock */
27 int id
; /* Mutex type */
29 volatile int nRef
; /* Number of enterances */
30 volatile DWORD owner
; /* Thread holding this mutex */
31 int trace
; /* True to trace changes */
34 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
36 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
38 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
42 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
43 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
45 ** Here is an interesting observation: Win95, Win98, and WinME lack
46 ** the LockFileEx() API. But we can still statically link against that
47 ** API as long as we don't call it win running Win95/98/ME. A call to
48 ** this routine is used to determine if the host is Win95/98/ME or
49 ** WinNT/2K/XP so that we will know whether or not we can safely call
50 ** the LockFileEx() API.
52 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
53 ** which is only available if your application was compiled with
54 ** _WIN32_WINNT defined to a value >= 0x0400. Currently, the only
55 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
59 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
60 # define mutexIsNT() (1)
62 static int mutexIsNT(void){
63 static int osType
= 0;
66 sInfo
.dwOSVersionInfoSize
= sizeof(sInfo
);
68 osType
= sInfo
.dwPlatformId
==VER_PLATFORM_WIN32_NT
? 2 : 1;
72 #endif /* SQLITE_OS_WINCE */
77 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
78 ** intended for use only inside assert() statements.
80 static int winMutexHeld(sqlite3_mutex
*p
){
81 return p
->nRef
!=0 && p
->owner
==GetCurrentThreadId();
83 static int winMutexNotheld2(sqlite3_mutex
*p
, DWORD tid
){
84 return p
->nRef
==0 || p
->owner
!=tid
;
86 static int winMutexNotheld(sqlite3_mutex
*p
){
87 DWORD tid
= GetCurrentThreadId();
88 return winMutexNotheld2(p
, tid
);
94 ** Initialize and deinitialize the mutex subsystem.
96 static sqlite3_mutex winMutex_staticMutexes
[6] = {
97 SQLITE3_MUTEX_INITIALIZER
,
98 SQLITE3_MUTEX_INITIALIZER
,
99 SQLITE3_MUTEX_INITIALIZER
,
100 SQLITE3_MUTEX_INITIALIZER
,
101 SQLITE3_MUTEX_INITIALIZER
,
102 SQLITE3_MUTEX_INITIALIZER
104 static int winMutex_isInit
= 0;
105 /* As winMutexInit() and winMutexEnd() are called as part
106 ** of the sqlite3_initialize and sqlite3_shutdown()
107 ** processing, the "interlocked" magic is probably not
108 ** strictly necessary.
110 static long winMutex_lock
= 0;
112 void sqlite3_win32_sleep(DWORD milliseconds
); /* os_win.c */
114 static int winMutexInit(void){
115 /* The first to increment to 1 does actual initialization */
116 if( InterlockedCompareExchange(&winMutex_lock
, 1, 0)==0 ){
118 for(i
=0; i
<ArraySize(winMutex_staticMutexes
); i
++){
120 InitializeCriticalSectionEx(&winMutex_staticMutexes
[i
].mutex
, 0, 0);
122 InitializeCriticalSection(&winMutex_staticMutexes
[i
].mutex
);
127 /* Someone else is in the process of initing the static mutexes */
128 while( !winMutex_isInit
){
129 sqlite3_win32_sleep(1);
135 static int winMutexEnd(void){
136 /* The first to decrement to 0 does actual shutdown
137 ** (which should be the last to shutdown.) */
138 if( InterlockedCompareExchange(&winMutex_lock
, 0, 1)==1 ){
139 if( winMutex_isInit
==1 ){
141 for(i
=0; i
<ArraySize(winMutex_staticMutexes
); i
++){
142 DeleteCriticalSection(&winMutex_staticMutexes
[i
].mutex
);
151 ** The sqlite3_mutex_alloc() routine allocates a new
152 ** mutex and returns a pointer to it. If it returns NULL
153 ** that means that a mutex could not be allocated. SQLite
154 ** will unwind its stack and return an error. The argument
155 ** to sqlite3_mutex_alloc() is one of these integer constants:
158 ** <li> SQLITE_MUTEX_FAST
159 ** <li> SQLITE_MUTEX_RECURSIVE
160 ** <li> SQLITE_MUTEX_STATIC_MASTER
161 ** <li> SQLITE_MUTEX_STATIC_MEM
162 ** <li> SQLITE_MUTEX_STATIC_MEM2
163 ** <li> SQLITE_MUTEX_STATIC_PRNG
164 ** <li> SQLITE_MUTEX_STATIC_LRU
165 ** <li> SQLITE_MUTEX_STATIC_PMEM
168 ** The first two constants cause sqlite3_mutex_alloc() to create
169 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
170 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
171 ** The mutex implementation does not need to make a distinction
172 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
173 ** not want to. But SQLite will only request a recursive mutex in
174 ** cases where it really needs one. If a faster non-recursive mutex
175 ** implementation is available on the host platform, the mutex subsystem
176 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
178 ** The other allowed parameters to sqlite3_mutex_alloc() each return
179 ** a pointer to a static preexisting mutex. Six static mutexes are
180 ** used by the current version of SQLite. Future versions of SQLite
181 ** may add additional static mutexes. Static mutexes are for internal
182 ** use by SQLite only. Applications that use SQLite mutexes should
183 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
184 ** SQLITE_MUTEX_RECURSIVE.
186 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
187 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
188 ** returns a different mutex on every call. But for the static
189 ** mutex types, the same mutex is returned on every call that has
190 ** the same type number.
192 static sqlite3_mutex
*winMutexAlloc(int iType
){
196 case SQLITE_MUTEX_FAST
:
197 case SQLITE_MUTEX_RECURSIVE
: {
198 p
= sqlite3MallocZero( sizeof(*p
) );
204 InitializeCriticalSectionEx(&p
->mutex
, 0, 0);
206 InitializeCriticalSection(&p
->mutex
);
212 assert( winMutex_isInit
==1 );
213 assert( iType
-2 >= 0 );
214 assert( iType
-2 < ArraySize(winMutex_staticMutexes
) );
215 p
= &winMutex_staticMutexes
[iType
-2];
227 ** This routine deallocates a previously
228 ** allocated mutex. SQLite is careful to deallocate every
229 ** mutex that it allocates.
231 static void winMutexFree(sqlite3_mutex
*p
){
233 assert( p
->nRef
==0 && p
->owner
==0 );
234 assert( p
->id
==SQLITE_MUTEX_FAST
|| p
->id
==SQLITE_MUTEX_RECURSIVE
);
235 DeleteCriticalSection(&p
->mutex
);
240 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
241 ** to enter a mutex. If another thread is already within the mutex,
242 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
243 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
244 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
245 ** be entered multiple times by the same thread. In such cases the,
246 ** mutex must be exited an equal number of times before another thread
247 ** can enter. If the same thread tries to enter any other kind of mutex
248 ** more than once, the behavior is undefined.
250 static void winMutexEnter(sqlite3_mutex
*p
){
252 DWORD tid
= GetCurrentThreadId();
253 assert( p
->id
==SQLITE_MUTEX_RECURSIVE
|| winMutexNotheld2(p
, tid
) );
255 EnterCriticalSection(&p
->mutex
);
257 assert( p
->nRef
>0 || p
->owner
==0 );
261 printf("enter mutex %p (%d) with nRef=%d\n", p
, p
->trace
, p
->nRef
);
265 static int winMutexTry(sqlite3_mutex
*p
){
267 DWORD tid
= GetCurrentThreadId();
269 int rc
= SQLITE_BUSY
;
270 assert( p
->id
==SQLITE_MUTEX_RECURSIVE
|| winMutexNotheld2(p
, tid
) );
272 ** The sqlite3_mutex_try() routine is very rarely used, and when it
273 ** is used it is merely an optimization. So it is OK for it to always
276 ** The TryEnterCriticalSection() interface is only available on WinNT.
277 ** And some windows compilers complain if you try to use it without
278 ** first doing some #defines that prevent SQLite from building on Win98.
279 ** For that reason, we will omit this optimization for now. See
283 if( mutexIsNT() && TryEnterCriticalSection(&p
->mutex
) ){
292 if( rc
==SQLITE_OK
&& p
->trace
){
293 printf("try mutex %p (%d) with nRef=%d\n", p
, p
->trace
, p
->nRef
);
300 ** The sqlite3_mutex_leave() routine exits a mutex that was
301 ** previously entered by the same thread. The behavior
302 ** is undefined if the mutex is not currently entered or
303 ** is not currently allocated. SQLite will never do either.
305 static void winMutexLeave(sqlite3_mutex
*p
){
307 DWORD tid
= GetCurrentThreadId();
309 assert( p
->owner
==tid
);
311 if( p
->nRef
==0 ) p
->owner
= 0;
312 assert( p
->nRef
==0 || p
->id
==SQLITE_MUTEX_RECURSIVE
);
314 LeaveCriticalSection(&p
->mutex
);
317 printf("leave mutex %p (%d) with nRef=%d\n", p
, p
->trace
, p
->nRef
);
322 sqlite3_mutex_methods
const *sqlite3DefaultMutex(void){
323 static const sqlite3_mutex_methods sMutex
= {
342 #endif /* SQLITE_MUTEX_W32 */