Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/mutex_w32.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / mutex_w32.c` 389 lines ยท 11314 bytes Raw Blame History 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 #if MSVC_VERSION>0 35 # define ALIGN128 __declspec(align(128)) 36 #else 37 # define ALIGN128 38 #endif 39 40 #ifdef _MSC_VER 41 #pragma warning(push) 42 #pragma warning(disable: 4324) 43 #endif /* _MSC_VER */ 44 45 /* 46 ** Each SQLite mutex is an instance of the following structure. 47 ** 48 ** The ALIGN128 macro attempts to force 128-byte alignment on mutexes, 49 ** so that adjacent mutex objects are always on different cache lines 50 ** in the CPU. This is CPU-dependent, of course, but 128-byte alignment 51 ** seems to work well for all contemporary processors. Experiments show 52 ** that 64 works just as well most of the time, but the internet says 53 ** that 128-byte alignment works better. 54 */ 55 struct sqlite3_mutex { 56 union { 57 CRITICAL_SECTION cs; /* The CRITICAL_SECTION mutex. id==1 */ 58 SRWLOCK srwl; /* The Slim reader/writer lock. id!=1 */ 59 } u; 60 int id; /* Mutex type */ 61 #ifdef SQLITE_DEBUG 62 volatile int nRef; /* Number of entrances */ 63 volatile DWORD owner; /* Thread holding this mutex */ 64 volatile LONG trace; /* True to trace changes */ 65 #endif 66 }; 67 #ifdef _MSC_VER 68 #pragma warning(pop) 69 #endif /* _MSC_VER */ 70 71 #ifdef SQLITE_DEBUG 72 /* 73 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are 74 ** intended for use only inside assert() statements. 75 */ 76 static int winMutexHeld(sqlite3_mutex *p){ 77 return p->nRef!=0 && p->owner==GetCurrentThreadId(); 78 } 79 80 static int winMutexNotheld(sqlite3_mutex *p){ 81 return p->nRef==0 || p->owner!=GetCurrentThreadId(); 82 } 83 #endif 84 85 /* 86 ** Try to provide a memory barrier operation, needed for initialization 87 ** and also for the xShmBarrier method of the VFS in cases when SQLite is 88 ** compiled without mutexes (SQLITE_THREADSAFE=0). 89 */ 90 void sqlite3MemoryBarrier(void){ 91 #if defined(SQLITE_MEMORY_BARRIER) 92 SQLITE_MEMORY_BARRIER; 93 #elif defined(__GNUC__) 94 __sync_synchronize(); 95 #elif MSVC_VERSION>=1400 96 _ReadWriteBarrier(); 97 #elif defined(MemoryBarrier) 98 MemoryBarrier(); 99 #endif 100 } 101 102 /* 103 ** Static mutexes. 104 ** 105 ** The ALIGN128 macro on the static mutex array forces 128-byte alignment 106 ** on the mutexes so that adjacent mutex objects are always on different 107 ** cache lines in the CPU. This is CPU-dependent, of course, but 128-byte 108 ** alignment seems to work well for all contemporary processors. 109 ** Experiments show that 64 works just as well most of the time, but 110 ** the internet says that 128-byte alignment works better. 111 */ 112 static ALIGN128 union staticMutexes { 113 sqlite3_mutex m; 114 char spacer[128]; 115 } aWindowsMutex[12]; 116 static int winMutex_isInit = 0; 117 118 /* As the winMutexInit() and winMutexEnd() functions are called as part 119 ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the 120 ** "interlocked" magic used here is probably not strictly necessary. 121 */ 122 static LONG volatile winMutex_lock = 0; 123 124 void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */ 125 126 static int winMutexInit(void){ 127 /* The first to increment to 1 does actual initialization */ 128 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){ 129 int i; 130 for(i=0; iid = i+2; 133 InitializeSRWLock(&p->u.srwl); 134 #ifdef SQLITE_DEBUG 135 p->nRef = 0; 136 p->owner = 0; 137 p->trace = 0; 138 #endif 139 } 140 winMutex_isInit = 1; 141 }else{ 142 /* Another thread is (in the process of) initializing the static 143 ** mutexes */ 144 while( !winMutex_isInit ){ 145 sqlite3_win32_sleep(1); 146 } 147 } 148 return SQLITE_OK; 149 } 150 151 static int winMutexEnd(void){ 152 /* The first to decrement to 0 does actual shutdown 153 ** (which should be the last to shutdown.) */ 154 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){ 155 if( winMutex_isInit==1 ){ 156 winMutex_isInit = 0; 157 } 158 } 159 return SQLITE_OK; 160 } 161 162 /* 163 ** The sqlite3_mutex_alloc() routine allocates a new 164 ** mutex and returns a pointer to it. If it returns NULL 165 ** that means that a mutex could not be allocated. SQLite 166 ** will unwind its stack and return an error. The argument 167 ** to sqlite3_mutex_alloc() is one of these integer constants: 168 ** 169 ** 185 ** 186 ** The first two constants cause sqlite3_mutex_alloc() to create 187 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE 188 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. 189 ** The mutex implementation does not need to make a distinction 190 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does 191 ** not want to. But SQLite will only request a recursive mutex in 192 ** cases where it really needs one. If a faster non-recursive mutex 193 ** implementation is available on the host platform, the mutex subsystem 194 ** might return such a mutex in response to SQLITE_MUTEX_FAST. 195 ** 196 ** The other allowed parameters to sqlite3_mutex_alloc() each return 197 ** a pointer to a static preexisting mutex. Six static mutexes are 198 ** used by the current version of SQLite. Future versions of SQLite 199 ** may add additional static mutexes. Static mutexes are for internal 200 ** use by SQLite only. Applications that use SQLite mutexes should 201 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or 202 ** SQLITE_MUTEX_RECURSIVE. 203 ** 204 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST 205 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() 206 ** returns a different mutex on every call. But for the static 207 ** mutex types, the same mutex is returned on every call that has 208 ** the same type number. 209 */ 210 static sqlite3_mutex *winMutexAlloc(int iType){ 211 sqlite3_mutex *p; 212 213 switch( iType ){ 214 case SQLITE_MUTEX_FAST: 215 case SQLITE_MUTEX_RECURSIVE: { 216 p = sqlite3MallocZero( sizeof(*p) ); 217 if( p ){ 218 p->id = iType; 219 #ifdef SQLITE_DEBUG 220 #ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC 221 p->trace = 1; 222 #endif 223 #endif 224 if( iType==SQLITE_MUTEX_RECURSIVE ){ 225 InitializeCriticalSection(&p->u.cs); 226 }else{ 227 InitializeSRWLock(&p->u.srwl); 228 } 229 } 230 break; 231 } 232 default: { 233 #ifdef SQLITE_ENABLE_API_ARMOR 234 if( iType-2<0 || iType-2>=ArraySize(aWindowsMutex) ){ 235 (void)SQLITE_MISUSE_BKPT; 236 return 0; 237 } 238 #endif 239 p = &aWindowsMutex[iType-2].m; 240 #ifdef SQLITE_DEBUG 241 #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC 242 InterlockedCompareExchange(&p->trace, 1, 0); 243 #endif 244 #endif 245 break; 246 } 247 } 248 assert( p==0 || p->id==iType ); 249 return p; 250 } 251 252 253 /* 254 ** This routine deallocates a previously 255 ** allocated mutex. SQLite is careful to deallocate every 256 ** mutex that it allocates. 257 */ 258 static void winMutexFree(sqlite3_mutex *p){ 259 assert( p ); 260 assert( p->nRef==0 && p->owner==0 ); 261 if( p->id==SQLITE_MUTEX_FAST ){ 262 sqlite3_free(p); 263 }else if( p->id==SQLITE_MUTEX_RECURSIVE ){ 264 DeleteCriticalSection(&p->u.cs); 265 sqlite3_free(p); 266 }else{ 267 #ifdef SQLITE_ENABLE_API_ARMOR 268 (void)SQLITE_MISUSE_BKPT; 269 #endif 270 } 271 } 272 273 /* 274 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt 275 ** to enter a mutex. If another thread is already within the mutex, 276 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return 277 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK 278 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can 279 ** be entered multiple times by the same thread. In such cases the, 280 ** mutex must be exited an equal number of times before another thread 281 ** can enter. If the same thread tries to enter any other kind of mutex 282 ** more than once, the behavior is undefined. 283 */ 284 static void winMutexEnter(sqlite3_mutex *p){ 285 assert( p ); 286 #ifdef SQLITE_DEBUG 287 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld(p) ); 288 #endif 289 assert( winMutex_isInit==1 ); 290 if( p->id==SQLITE_MUTEX_RECURSIVE ){ 291 EnterCriticalSection(&p->u.cs); 292 }else{ 293 AcquireSRWLockExclusive(&p->u.srwl); 294 } 295 #ifdef SQLITE_DEBUG 296 assert( p->nRef>0 || p->owner==0 ); 297 p->owner = GetCurrentThreadId(); 298 p->nRef++; 299 if( p->trace ){ 300 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", 301 GetCurrentThreadId(), p->id, p, p->trace, p->nRef)); 302 } 303 #endif 304 } 305 306 static int winMutexTry(sqlite3_mutex *p){ 307 int rc = SQLITE_BUSY; 308 assert( p ); 309 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld(p) ); 310 /* 311 ** The sqlite3_mutex_try() routine is seldom used, and when it is 312 ** used it is merely an optimization. So it is OK for it to always 313 ** fail. 314 */ 315 if( p->id==SQLITE_MUTEX_RECURSIVE ){ 316 rc = TryEnterCriticalSection(&p->u.cs); 317 }else{ 318 rc = TryAcquireSRWLockExclusive(&p->u.srwl); 319 } 320 if( rc ){ 321 #ifdef SQLITE_DEBUG 322 p->owner = GetCurrentThreadId(); 323 p->nRef++; 324 #endif 325 rc = SQLITE_OK; 326 }else{ 327 rc = SQLITE_BUSY; 328 } 329 #ifdef SQLITE_DEBUG 330 if( p->trace ){ 331 OSTRACE(( 332 "TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n", 333 GetCurrentThreadId(), p->id, p, p->trace, p->owner, p->nRef, 334 sqlite3ErrName(rc))); 335 } 336 #endif 337 return rc; 338 } 339 340 /* 341 ** The sqlite3_mutex_leave() routine exits a mutex that was 342 ** previously entered by the same thread. The behavior 343 ** is undefined if the mutex is not currently entered or 344 ** is not currently allocated. SQLite will never do either. 345 */ 346 static void winMutexLeave(sqlite3_mutex *p){ 347 assert( p ); 348 #ifdef SQLITE_DEBUG 349 assert( p->nRef>0 ); 350 assert( p->owner==GetCurrentThreadId() ); 351 p->nRef--; 352 if( p->nRef==0 ) p->owner = 0; 353 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); 354 #endif 355 assert( winMutex_isInit==1 ); 356 if( p->id==SQLITE_MUTEX_RECURSIVE ){ 357 LeaveCriticalSection(&p->u.cs); 358 }else{ 359 ReleaseSRWLockExclusive(&p->u.srwl); 360 } 361 #ifdef SQLITE_DEBUG 362 if( p->trace ){ 363 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", 364 GetCurrentThreadId(), p->id, p, p->trace, p->nRef)); 365 } 366 #endif 367 } 368 369 sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ 370 static const sqlite3_mutex_methods sMutex = { 371 winMutexInit, 372 winMutexEnd, 373 winMutexAlloc, 374 winMutexFree, 375 winMutexEnter, 376 winMutexTry, 377 winMutexLeave, 378 #ifdef SQLITE_DEBUG 379 winMutexHeld, 380 winMutexNotheld 381 #else 382 0, 383 0 384 #endif 385 }; 386 return &sMutex; 387 } 388 389 #endif /* SQLITE_MUTEX_W32 */