OLD | NEW |
| (Empty) |
1 /* | |
2 ** 2007 August 14 | |
3 ** | |
4 ** The author disclaims copyright to this source code. In place of | |
5 ** a legal notice, here is a blessing: | |
6 ** | |
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. | |
10 ** | |
11 ************************************************************************* | |
12 ** This file contains the C functions that implement mutexes for Win32. | |
13 */ | |
14 #include "sqliteInt.h" | |
15 | |
16 #if SQLITE_OS_WIN | |
17 /* | |
18 ** Include code that is common to all os_*.c files | |
19 */ | |
20 #include "os_common.h" | |
21 | |
22 /* | |
23 ** Include the header file for the Windows VFS. | |
24 */ | |
25 #include "os_win.h" | |
26 #endif | |
27 | |
28 /* | |
29 ** The code in this file is only used if we are compiling multithreaded | |
30 ** on a Win32 system. | |
31 */ | |
32 #ifdef SQLITE_MUTEX_W32 | |
33 | |
34 /* | |
35 ** Each recursive mutex is an instance of the following structure. | |
36 */ | |
37 struct sqlite3_mutex { | |
38 CRITICAL_SECTION mutex; /* Mutex controlling the lock */ | |
39 int id; /* Mutex type */ | |
40 #ifdef SQLITE_DEBUG | |
41 volatile int nRef; /* Number of enterances */ | |
42 volatile DWORD owner; /* Thread holding this mutex */ | |
43 volatile int trace; /* True to trace changes */ | |
44 #endif | |
45 }; | |
46 | |
47 /* | |
48 ** These are the initializer values used when declaring a "static" mutex | |
49 ** on Win32. It should be noted that all mutexes require initialization | |
50 ** on the Win32 platform. | |
51 */ | |
52 #define SQLITE_W32_MUTEX_INITIALIZER { 0 } | |
53 | |
54 #ifdef SQLITE_DEBUG | |
55 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \ | |
56 0L, (DWORD)0, 0 } | |
57 #else | |
58 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 } | |
59 #endif | |
60 | |
61 #ifdef SQLITE_DEBUG | |
62 /* | |
63 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are | |
64 ** intended for use only inside assert() statements. | |
65 */ | |
66 static int winMutexHeld(sqlite3_mutex *p){ | |
67 return p->nRef!=0 && p->owner==GetCurrentThreadId(); | |
68 } | |
69 | |
70 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){ | |
71 return p->nRef==0 || p->owner!=tid; | |
72 } | |
73 | |
74 static int winMutexNotheld(sqlite3_mutex *p){ | |
75 DWORD tid = GetCurrentThreadId(); | |
76 return winMutexNotheld2(p, tid); | |
77 } | |
78 #endif | |
79 | |
80 /* | |
81 ** Initialize and deinitialize the mutex subsystem. | |
82 */ | |
83 static sqlite3_mutex winMutex_staticMutexes[] = { | |
84 SQLITE3_MUTEX_INITIALIZER, | |
85 SQLITE3_MUTEX_INITIALIZER, | |
86 SQLITE3_MUTEX_INITIALIZER, | |
87 SQLITE3_MUTEX_INITIALIZER, | |
88 SQLITE3_MUTEX_INITIALIZER, | |
89 SQLITE3_MUTEX_INITIALIZER, | |
90 SQLITE3_MUTEX_INITIALIZER, | |
91 SQLITE3_MUTEX_INITIALIZER, | |
92 SQLITE3_MUTEX_INITIALIZER | |
93 }; | |
94 | |
95 static int winMutex_isInit = 0; | |
96 static int winMutex_isNt = -1; /* <0 means "need to query" */ | |
97 | |
98 /* As the winMutexInit() and winMutexEnd() functions are called as part | |
99 ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the | |
100 ** "interlocked" magic used here is probably not strictly necessary. | |
101 */ | |
102 static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0; | |
103 | |
104 int sqlite3_win32_is_nt(void); /* os_win.c */ | |
105 void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */ | |
106 | |
107 static int winMutexInit(void){ | |
108 /* The first to increment to 1 does actual initialization */ | |
109 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){ | |
110 int i; | |
111 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){ | |
112 #if SQLITE_OS_WINRT | |
113 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0); | |
114 #else | |
115 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex); | |
116 #endif | |
117 } | |
118 winMutex_isInit = 1; | |
119 }else{ | |
120 /* Another thread is (in the process of) initializing the static | |
121 ** mutexes */ | |
122 while( !winMutex_isInit ){ | |
123 sqlite3_win32_sleep(1); | |
124 } | |
125 } | |
126 return SQLITE_OK; | |
127 } | |
128 | |
129 static int winMutexEnd(void){ | |
130 /* The first to decrement to 0 does actual shutdown | |
131 ** (which should be the last to shutdown.) */ | |
132 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){ | |
133 if( winMutex_isInit==1 ){ | |
134 int i; | |
135 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){ | |
136 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex); | |
137 } | |
138 winMutex_isInit = 0; | |
139 } | |
140 } | |
141 return SQLITE_OK; | |
142 } | |
143 | |
144 /* | |
145 ** The sqlite3_mutex_alloc() routine allocates a new | |
146 ** mutex and returns a pointer to it. If it returns NULL | |
147 ** that means that a mutex could not be allocated. SQLite | |
148 ** will unwind its stack and return an error. The argument | |
149 ** to sqlite3_mutex_alloc() is one of these integer constants: | |
150 ** | |
151 ** <ul> | |
152 ** <li> SQLITE_MUTEX_FAST | |
153 ** <li> SQLITE_MUTEX_RECURSIVE | |
154 ** <li> SQLITE_MUTEX_STATIC_MASTER | |
155 ** <li> SQLITE_MUTEX_STATIC_MEM | |
156 ** <li> SQLITE_MUTEX_STATIC_OPEN | |
157 ** <li> SQLITE_MUTEX_STATIC_PRNG | |
158 ** <li> SQLITE_MUTEX_STATIC_LRU | |
159 ** <li> SQLITE_MUTEX_STATIC_PMEM | |
160 ** <li> SQLITE_MUTEX_STATIC_APP1 | |
161 ** <li> SQLITE_MUTEX_STATIC_APP2 | |
162 ** <li> SQLITE_MUTEX_STATIC_APP3 | |
163 ** </ul> | |
164 ** | |
165 ** The first two constants cause sqlite3_mutex_alloc() to create | |
166 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE | |
167 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. | |
168 ** The mutex implementation does not need to make a distinction | |
169 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does | |
170 ** not want to. But SQLite will only request a recursive mutex in | |
171 ** cases where it really needs one. If a faster non-recursive mutex | |
172 ** implementation is available on the host platform, the mutex subsystem | |
173 ** might return such a mutex in response to SQLITE_MUTEX_FAST. | |
174 ** | |
175 ** The other allowed parameters to sqlite3_mutex_alloc() each return | |
176 ** a pointer to a static preexisting mutex. Six static mutexes are | |
177 ** used by the current version of SQLite. Future versions of SQLite | |
178 ** may add additional static mutexes. Static mutexes are for internal | |
179 ** use by SQLite only. Applications that use SQLite mutexes should | |
180 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or | |
181 ** SQLITE_MUTEX_RECURSIVE. | |
182 ** | |
183 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST | |
184 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() | |
185 ** returns a different mutex on every call. But for the static | |
186 ** mutex types, the same mutex is returned on every call that has | |
187 ** the same type number. | |
188 */ | |
189 static sqlite3_mutex *winMutexAlloc(int iType){ | |
190 sqlite3_mutex *p; | |
191 | |
192 switch( iType ){ | |
193 case SQLITE_MUTEX_FAST: | |
194 case SQLITE_MUTEX_RECURSIVE: { | |
195 p = sqlite3MallocZero( sizeof(*p) ); | |
196 if( p ){ | |
197 #ifdef SQLITE_DEBUG | |
198 p->id = iType; | |
199 #ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC | |
200 p->trace = 1; | |
201 #endif | |
202 #endif | |
203 #if SQLITE_OS_WINRT | |
204 InitializeCriticalSectionEx(&p->mutex, 0, 0); | |
205 #else | |
206 InitializeCriticalSection(&p->mutex); | |
207 #endif | |
208 } | |
209 break; | |
210 } | |
211 default: { | |
212 assert( iType-2 >= 0 ); | |
213 assert( iType-2 < ArraySize(winMutex_staticMutexes) ); | |
214 assert( winMutex_isInit==1 ); | |
215 p = &winMutex_staticMutexes[iType-2]; | |
216 #ifdef SQLITE_DEBUG | |
217 p->id = iType; | |
218 #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC | |
219 p->trace = 1; | |
220 #endif | |
221 #endif | |
222 break; | |
223 } | |
224 } | |
225 return p; | |
226 } | |
227 | |
228 | |
229 /* | |
230 ** This routine deallocates a previously | |
231 ** allocated mutex. SQLite is careful to deallocate every | |
232 ** mutex that it allocates. | |
233 */ | |
234 static void winMutexFree(sqlite3_mutex *p){ | |
235 assert( p ); | |
236 #ifdef SQLITE_DEBUG | |
237 assert( p->nRef==0 && p->owner==0 ); | |
238 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); | |
239 #endif | |
240 assert( winMutex_isInit==1 ); | |
241 DeleteCriticalSection(&p->mutex); | |
242 sqlite3_free(p); | |
243 } | |
244 | |
245 /* | |
246 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt | |
247 ** to enter a mutex. If another thread is already within the mutex, | |
248 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return | |
249 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK | |
250 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can | |
251 ** be entered multiple times by the same thread. In such cases the, | |
252 ** mutex must be exited an equal number of times before another thread | |
253 ** can enter. If the same thread tries to enter any other kind of mutex | |
254 ** more than once, the behavior is undefined. | |
255 */ | |
256 static void winMutexEnter(sqlite3_mutex *p){ | |
257 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) | |
258 DWORD tid = GetCurrentThreadId(); | |
259 #endif | |
260 #ifdef SQLITE_DEBUG | |
261 assert( p ); | |
262 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); | |
263 #else | |
264 assert( p ); | |
265 #endif | |
266 assert( winMutex_isInit==1 ); | |
267 EnterCriticalSection(&p->mutex); | |
268 #ifdef SQLITE_DEBUG | |
269 assert( p->nRef>0 || p->owner==0 ); | |
270 p->owner = tid; | |
271 p->nRef++; | |
272 if( p->trace ){ | |
273 OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", | |
274 tid, p, p->trace, p->nRef)); | |
275 } | |
276 #endif | |
277 } | |
278 | |
279 static int winMutexTry(sqlite3_mutex *p){ | |
280 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) | |
281 DWORD tid = GetCurrentThreadId(); | |
282 #endif | |
283 int rc = SQLITE_BUSY; | |
284 assert( p ); | |
285 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); | |
286 /* | |
287 ** The sqlite3_mutex_try() routine is very rarely used, and when it | |
288 ** is used it is merely an optimization. So it is OK for it to always | |
289 ** fail. | |
290 ** | |
291 ** The TryEnterCriticalSection() interface is only available on WinNT. | |
292 ** And some windows compilers complain if you try to use it without | |
293 ** first doing some #defines that prevent SQLite from building on Win98. | |
294 ** For that reason, we will omit this optimization for now. See | |
295 ** ticket #2685. | |
296 */ | |
297 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 | |
298 assert( winMutex_isInit==1 ); | |
299 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); | |
300 if( winMutex_isNt<0 ){ | |
301 winMutex_isNt = sqlite3_win32_is_nt(); | |
302 } | |
303 assert( winMutex_isNt==0 || winMutex_isNt==1 ); | |
304 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ | |
305 #ifdef SQLITE_DEBUG | |
306 p->owner = tid; | |
307 p->nRef++; | |
308 #endif | |
309 rc = SQLITE_OK; | |
310 } | |
311 #else | |
312 UNUSED_PARAMETER(p); | |
313 #endif | |
314 #ifdef SQLITE_DEBUG | |
315 if( p->trace ){ | |
316 OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n", | |
317 tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); | |
318 } | |
319 #endif | |
320 return rc; | |
321 } | |
322 | |
323 /* | |
324 ** The sqlite3_mutex_leave() routine exits a mutex that was | |
325 ** previously entered by the same thread. The behavior | |
326 ** is undefined if the mutex is not currently entered or | |
327 ** is not currently allocated. SQLite will never do either. | |
328 */ | |
329 static void winMutexLeave(sqlite3_mutex *p){ | |
330 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) | |
331 DWORD tid = GetCurrentThreadId(); | |
332 #endif | |
333 assert( p ); | |
334 #ifdef SQLITE_DEBUG | |
335 assert( p->nRef>0 ); | |
336 assert( p->owner==tid ); | |
337 p->nRef--; | |
338 if( p->nRef==0 ) p->owner = 0; | |
339 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); | |
340 #endif | |
341 assert( winMutex_isInit==1 ); | |
342 LeaveCriticalSection(&p->mutex); | |
343 #ifdef SQLITE_DEBUG | |
344 if( p->trace ){ | |
345 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", | |
346 tid, p, p->trace, p->nRef)); | |
347 } | |
348 #endif | |
349 } | |
350 | |
351 sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ | |
352 static const sqlite3_mutex_methods sMutex = { | |
353 winMutexInit, | |
354 winMutexEnd, | |
355 winMutexAlloc, | |
356 winMutexFree, | |
357 winMutexEnter, | |
358 winMutexTry, | |
359 winMutexLeave, | |
360 #ifdef SQLITE_DEBUG | |
361 winMutexHeld, | |
362 winMutexNotheld | |
363 #else | |
364 0, | |
365 0 | |
366 #endif | |
367 }; | |
368 return &sMutex; | |
369 } | |
370 | |
371 #endif /* SQLITE_MUTEX_W32 */ | |
OLD | NEW |