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