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 ** Try to provide a memory barrier operation, needed for initialization 82 ** and also for the xShmBarrier method of the VFS in cases when SQLite is 83 ** compiled without mutexes (SQLITE_THREADSAFE=0). 84 */ 85 void sqlite3MemoryBarrier(void){ 86 #if defined(SQLITE_MEMORY_BARRIER) 87 SQLITE_MEMORY_BARRIER; 88 #elif defined(__GNUC__) 89 __sync_synchronize(); 90 #elif !defined(SQLITE_DISABLE_INTRINSIC) && \ 91 defined(_MSC_VER) && _MSC_VER>=1300 92 _ReadWriteBarrier(); 93 #elif defined(MemoryBarrier) 94 MemoryBarrier(); 95 #endif 96 } 97 98 /* 99 ** Initialize and deinitialize the mutex subsystem. 100 */ 101 static sqlite3_mutex winMutex_staticMutexes[] = { 102 SQLITE3_MUTEX_INITIALIZER, 103 SQLITE3_MUTEX_INITIALIZER, 104 SQLITE3_MUTEX_INITIALIZER, 105 SQLITE3_MUTEX_INITIALIZER, 106 SQLITE3_MUTEX_INITIALIZER, 107 SQLITE3_MUTEX_INITIALIZER, 108 SQLITE3_MUTEX_INITIALIZER, 109 SQLITE3_MUTEX_INITIALIZER, 110 SQLITE3_MUTEX_INITIALIZER, 111 SQLITE3_MUTEX_INITIALIZER, 112 SQLITE3_MUTEX_INITIALIZER, 113 SQLITE3_MUTEX_INITIALIZER 114 }; 115 116 static int winMutex_isInit = 0; 117 static int winMutex_isNt = -1; /* <0 means "need to query" */ 118 119 /* As the winMutexInit() and winMutexEnd() functions are called as part 120 ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the 121 ** "interlocked" magic used here is probably not strictly necessary. 122 */ 123 static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0; 124 125 int sqlite3_win32_is_nt(void); /* os_win.c */ 126 void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */ 127 128 static int winMutexInit(void){ 129 /* The first to increment to 1 does actual initialization */ 130 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){ 131 int i; 132 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){ 133 #if SQLITE_OS_WINRT 134 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0); 135 #else 136 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex); 137 #endif 138 } 139 winMutex_isInit = 1; 140 }else{ 141 /* Another thread is (in the process of) initializing the static 142 ** mutexes */ 143 while( !winMutex_isInit ){ 144 sqlite3_win32_sleep(1); 145 } 146 } 147 return SQLITE_OK; 148 } 149 150 static int winMutexEnd(void){ 151 /* The first to decrement to 0 does actual shutdown 152 ** (which should be the last to shutdown.) */ 153 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){ 154 if( winMutex_isInit==1 ){ 155 int i; 156 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){ 157 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex); 158 } 159 winMutex_isInit = 0; 160 } 161 } 162 return SQLITE_OK; 163 } 164 165 /* 166 ** The sqlite3_mutex_alloc() routine allocates a new 167 ** mutex and returns a pointer to it. If it returns NULL 168 ** that means that a mutex could not be allocated. SQLite 169 ** will unwind its stack and return an error. The argument 170 ** to sqlite3_mutex_alloc() is one of these integer constants: 171 ** 172 ** <ul> 173 ** <li> SQLITE_MUTEX_FAST 174 ** <li> SQLITE_MUTEX_RECURSIVE 175 ** <li> SQLITE_MUTEX_STATIC_MASTER 176 ** <li> SQLITE_MUTEX_STATIC_MEM 177 ** <li> SQLITE_MUTEX_STATIC_OPEN 178 ** <li> SQLITE_MUTEX_STATIC_PRNG 179 ** <li> SQLITE_MUTEX_STATIC_LRU 180 ** <li> SQLITE_MUTEX_STATIC_PMEM 181 ** <li> SQLITE_MUTEX_STATIC_APP1 182 ** <li> SQLITE_MUTEX_STATIC_APP2 183 ** <li> SQLITE_MUTEX_STATIC_APP3 184 ** <li> SQLITE_MUTEX_STATIC_VFS1 185 ** <li> SQLITE_MUTEX_STATIC_VFS2 186 ** <li> SQLITE_MUTEX_STATIC_VFS3 187 ** </ul> 188 ** 189 ** The first two constants cause sqlite3_mutex_alloc() to create 190 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE 191 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. 192 ** The mutex implementation does not need to make a distinction 193 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does 194 ** not want to. But SQLite will only request a recursive mutex in 195 ** cases where it really needs one. If a faster non-recursive mutex 196 ** implementation is available on the host platform, the mutex subsystem 197 ** might return such a mutex in response to SQLITE_MUTEX_FAST. 198 ** 199 ** The other allowed parameters to sqlite3_mutex_alloc() each return 200 ** a pointer to a static preexisting mutex. Six static mutexes are 201 ** used by the current version of SQLite. Future versions of SQLite 202 ** may add additional static mutexes. Static mutexes are for internal 203 ** use by SQLite only. Applications that use SQLite mutexes should 204 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or 205 ** SQLITE_MUTEX_RECURSIVE. 206 ** 207 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST 208 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() 209 ** returns a different mutex on every call. But for the static 210 ** mutex types, the same mutex is returned on every call that has 211 ** the same type number. 212 */ 213 static sqlite3_mutex *winMutexAlloc(int iType){ 214 sqlite3_mutex *p; 215 216 switch( iType ){ 217 case SQLITE_MUTEX_FAST: 218 case SQLITE_MUTEX_RECURSIVE: { 219 p = sqlite3MallocZero( sizeof(*p) ); 220 if( p ){ 221 p->id = iType; 222 #ifdef SQLITE_DEBUG 223 #ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC 224 p->trace = 1; 225 #endif 226 #endif 227 #if SQLITE_OS_WINRT 228 InitializeCriticalSectionEx(&p->mutex, 0, 0); 229 #else 230 InitializeCriticalSection(&p->mutex); 231 #endif 232 } 233 break; 234 } 235 default: { 236 #ifdef SQLITE_ENABLE_API_ARMOR 237 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){ 238 (void)SQLITE_MISUSE_BKPT; 239 return 0; 240 } 241 #endif 242 p = &winMutex_staticMutexes[iType-2]; 243 p->id = iType; 244 #ifdef SQLITE_DEBUG 245 #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC 246 p->trace = 1; 247 #endif 248 #endif 249 break; 250 } 251 } 252 return p; 253 } 254 255 256 /* 257 ** This routine deallocates a previously 258 ** allocated mutex. SQLite is careful to deallocate every 259 ** mutex that it allocates. 260 */ 261 static void winMutexFree(sqlite3_mutex *p){ 262 assert( p ); 263 assert( p->nRef==0 && p->owner==0 ); 264 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){ 265 DeleteCriticalSection(&p->mutex); 266 sqlite3_free(p); 267 }else{ 268 #ifdef SQLITE_ENABLE_API_ARMOR 269 (void)SQLITE_MISUSE_BKPT; 270 #endif 271 } 272 } 273 274 /* 275 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt 276 ** to enter a mutex. If another thread is already within the mutex, 277 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return 278 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK 279 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can 280 ** be entered multiple times by the same thread. In such cases the, 281 ** mutex must be exited an equal number of times before another thread 282 ** can enter. If the same thread tries to enter any other kind of mutex 283 ** more than once, the behavior is undefined. 284 */ 285 static void winMutexEnter(sqlite3_mutex *p){ 286 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) 287 DWORD tid = GetCurrentThreadId(); 288 #endif 289 #ifdef SQLITE_DEBUG 290 assert( p ); 291 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); 292 #else 293 assert( p ); 294 #endif 295 assert( winMutex_isInit==1 ); 296 EnterCriticalSection(&p->mutex); 297 #ifdef SQLITE_DEBUG 298 assert( p->nRef>0 || p->owner==0 ); 299 p->owner = tid; 300 p->nRef++; 301 if( p->trace ){ 302 OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", 303 tid, p, p->trace, p->nRef)); 304 } 305 #endif 306 } 307 308 static int winMutexTry(sqlite3_mutex *p){ 309 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) 310 DWORD tid = GetCurrentThreadId(); 311 #endif 312 int rc = SQLITE_BUSY; 313 assert( p ); 314 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); 315 /* 316 ** The sqlite3_mutex_try() routine is very rarely used, and when it 317 ** is used it is merely an optimization. So it is OK for it to always 318 ** fail. 319 ** 320 ** The TryEnterCriticalSection() interface is only available on WinNT. 321 ** And some windows compilers complain if you try to use it without 322 ** first doing some #defines that prevent SQLite from building on Win98. 323 ** For that reason, we will omit this optimization for now. See 324 ** ticket #2685. 325 */ 326 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 327 assert( winMutex_isInit==1 ); 328 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); 329 if( winMutex_isNt<0 ){ 330 winMutex_isNt = sqlite3_win32_is_nt(); 331 } 332 assert( winMutex_isNt==0 || winMutex_isNt==1 ); 333 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ 334 #ifdef SQLITE_DEBUG 335 p->owner = tid; 336 p->nRef++; 337 #endif 338 rc = SQLITE_OK; 339 } 340 #else 341 UNUSED_PARAMETER(p); 342 #endif 343 #ifdef SQLITE_DEBUG 344 if( p->trace ){ 345 OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n", 346 tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); 347 } 348 #endif 349 return rc; 350 } 351 352 /* 353 ** The sqlite3_mutex_leave() routine exits a mutex that was 354 ** previously entered by the same thread. The behavior 355 ** is undefined if the mutex is not currently entered or 356 ** is not currently allocated. SQLite will never do either. 357 */ 358 static void winMutexLeave(sqlite3_mutex *p){ 359 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) 360 DWORD tid = GetCurrentThreadId(); 361 #endif 362 assert( p ); 363 #ifdef SQLITE_DEBUG 364 assert( p->nRef>0 ); 365 assert( p->owner==tid ); 366 p->nRef--; 367 if( p->nRef==0 ) p->owner = 0; 368 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); 369 #endif 370 assert( winMutex_isInit==1 ); 371 LeaveCriticalSection(&p->mutex); 372 #ifdef SQLITE_DEBUG 373 if( p->trace ){ 374 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", 375 tid, p, p->trace, p->nRef)); 376 } 377 #endif 378 } 379 380 sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ 381 static const sqlite3_mutex_methods sMutex = { 382 winMutexInit, 383 winMutexEnd, 384 winMutexAlloc, 385 winMutexFree, 386 winMutexEnter, 387 winMutexTry, 388 winMutexLeave, 389 #ifdef SQLITE_DEBUG 390 winMutexHeld, 391 winMutexNotheld 392 #else 393 0, 394 0 395 #endif 396 }; 397 return &sMutex; 398 } 399 400 #endif /* SQLITE_MUTEX_W32 */ 401