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.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.c` 389 lines ยท 10918 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. 13 ** 14 ** This file contains code that is common across all mutex implementations. 15 */ 16 #include "sqliteInt.h" 17 18 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT) 19 /* 20 ** For debugging purposes, record when the mutex subsystem is initialized 21 ** and uninitialized so that we can assert() if there is an attempt to 22 ** allocate a mutex while the system is uninitialized. 23 */ 24 static SQLITE_WSD int mutexIsInit = 0; 25 #endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */ 26 27 28 #ifndef SQLITE_MUTEX_OMIT 29 30 #ifdef SQLITE_THREAD_MISUSE_WARNINGS 31 /* 32 ** This block (enclosed by SQLITE_THREAD_MISUSE_WARNINGS) contains 33 ** the implementation of a wrapper around the system default mutex 34 ** implementation (sqlite3DefaultMutex()). 35 ** 36 ** Most calls are passed directly through to the underlying default 37 ** mutex implementation. Except, if a mutex is configured by calling 38 ** sqlite3MutexWarnOnContention() on it, then if contention is ever 39 ** encountered within xMutexEnter() then a warning is emitted via 40 ** sqlite3_log(). Furthermore, if SQLITE_THREAD_MISUSE_ABORT is 41 ** defined then abort() is called after the sqlite3_log() warning. 42 ** 43 ** This type of mutex is used on the database handle mutex when testing 44 ** apps that usually use SQLITE_CONFIG_MULTITHREAD mode. A failure 45 ** indicates that the app ought to be using SQLITE_OPEN_FULLMUTEX or 46 ** similar because it is trying to use the same database handle from 47 ** two different connections at the same time. 48 */ 49 50 /* 51 ** Type for all mutexes used when SQLITE_THREAD_MISUSE_WARNINGS 52 ** is defined. Variable CheckMutex.mutex is a pointer to the real mutex 53 ** allocated by the system mutex implementation. Variable iType is usually set 54 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST 55 ** or one of the static mutex identifiers. Or, if this is a recursive mutex 56 ** that has been configured using sqlite3MutexWarnOnContention(), it is 57 ** set to SQLITE_MUTEX_WARNONCONTENTION. 58 */ 59 typedef struct CheckMutex CheckMutex; 60 struct CheckMutex { 61 int iType; 62 sqlite3_mutex *mutex; 63 }; 64 65 #define SQLITE_MUTEX_WARNONCONTENTION (-1) 66 67 /* 68 ** Pointer to real mutex methods object used by the CheckMutex 69 ** implementation. Set by checkMutexInit(). 70 */ 71 static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods; 72 73 #ifdef SQLITE_DEBUG 74 static int checkMutexHeld(sqlite3_mutex *p){ 75 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); 76 } 77 static int checkMutexNotheld(sqlite3_mutex *p){ 78 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); 79 } 80 #endif 81 82 /* 83 ** Initialize and deinitialize the mutex subsystem. 84 */ 85 static int checkMutexInit(void){ 86 pGlobalMutexMethods = sqlite3DefaultMutex(); 87 return pGlobalMutexMethods->xMutexInit(); 88 } 89 static int checkMutexEnd(void){ 90 int rc = pGlobalMutexMethods->xMutexEnd(); 91 pGlobalMutexMethods = 0; 92 return rc; 93 } 94 95 /* 96 ** Allocate a mutex. 97 */ 98 static sqlite3_mutex *checkMutexAlloc(int iType){ 99 static CheckMutex staticMutexes[] = { 100 {2, 0}, {3, 0}, {4, 0}, {5, 0}, 101 {6, 0}, {7, 0}, {8, 0}, {9, 0}, 102 {10, 0}, {11, 0}, {12, 0}, {13, 0} 103 }; 104 CheckMutex *p = 0; 105 106 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 ); 107 if( iType<2 ){ 108 p = sqlite3MallocZero(sizeof(CheckMutex)); 109 if( p==0 ) return 0; 110 p->iType = iType; 111 }else{ 112 #ifdef SQLITE_ENABLE_API_ARMOR 113 if( iType-2>=ArraySize(staticMutexes) ){ 114 (void)SQLITE_MISUSE_BKPT; 115 return 0; 116 } 117 #endif 118 p = &staticMutexes[iType-2]; 119 } 120 121 if( p->mutex==0 ){ 122 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); 123 if( p->mutex==0 ){ 124 if( iType<2 ){ 125 sqlite3_free(p); 126 } 127 p = 0; 128 } 129 } 130 131 return (sqlite3_mutex*)p; 132 } 133 134 /* 135 ** Free a mutex. 136 */ 137 static void checkMutexFree(sqlite3_mutex *p){ 138 assert( SQLITE_MUTEX_RECURSIVE<2 ); 139 assert( SQLITE_MUTEX_FAST<2 ); 140 assert( SQLITE_MUTEX_WARNONCONTENTION<2 ); 141 142 #ifdef SQLITE_ENABLE_API_ARMOR 143 if( ((CheckMutex*)p)->iType<2 ) 144 #endif 145 { 146 CheckMutex *pCheck = (CheckMutex*)p; 147 pGlobalMutexMethods->xMutexFree(pCheck->mutex); 148 sqlite3_free(pCheck); 149 } 150 #ifdef SQLITE_ENABLE_API_ARMOR 151 else{ 152 (void)SQLITE_MISUSE_BKPT; 153 } 154 #endif 155 } 156 157 /* 158 ** Enter the mutex. 159 */ 160 static void checkMutexEnter(sqlite3_mutex *p){ 161 CheckMutex *pCheck = (CheckMutex*)p; 162 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ 163 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ 164 return; 165 } 166 sqlite3_log(SQLITE_MISUSE, 167 "illegal multi-threaded access to database connection" 168 ); 169 #if SQLITE_THREAD_MISUSE_ABORT 170 abort(); 171 #endif 172 } 173 pGlobalMutexMethods->xMutexEnter(pCheck->mutex); 174 } 175 176 /* 177 ** Enter the mutex (do not block). 178 */ 179 static int checkMutexTry(sqlite3_mutex *p){ 180 CheckMutex *pCheck = (CheckMutex*)p; 181 return pGlobalMutexMethods->xMutexTry(pCheck->mutex); 182 } 183 184 /* 185 ** Leave the mutex. 186 */ 187 static void checkMutexLeave(sqlite3_mutex *p){ 188 CheckMutex *pCheck = (CheckMutex*)p; 189 pGlobalMutexMethods->xMutexLeave(pCheck->mutex); 190 } 191 192 sqlite3_mutex_methods const *multiThreadedCheckMutex(void){ 193 static const sqlite3_mutex_methods sMutex = { 194 checkMutexInit, 195 checkMutexEnd, 196 checkMutexAlloc, 197 checkMutexFree, 198 checkMutexEnter, 199 checkMutexTry, 200 checkMutexLeave, 201 #ifdef SQLITE_DEBUG 202 checkMutexHeld, 203 checkMutexNotheld 204 #else 205 0, 206 0 207 #endif 208 }; 209 return &sMutex; 210 } 211 212 /* 213 ** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as 214 ** one on which there should be no contention. 215 */ 216 void sqlite3MutexWarnOnContention(sqlite3_mutex *p){ 217 if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){ 218 CheckMutex *pCheck = (CheckMutex*)p; 219 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); 220 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; 221 } 222 } 223 #endif /* ifdef SQLITE_THREAD_MISUSE_WARNINGS */ 224 225 /* 226 ** Initialize the mutex system. 227 */ 228 int sqlite3MutexInit(void){ 229 int rc = SQLITE_OK; 230 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){ 231 /* If the xMutexAlloc method has not been set, then the user did not 232 ** install a mutex implementation via sqlite3_config() prior to 233 ** sqlite3_initialize() being called. This block copies pointers to 234 ** the default implementation into the sqlite3GlobalConfig structure. 235 */ 236 sqlite3_mutex_methods const *pFrom; 237 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex; 238 239 if( sqlite3GlobalConfig.bCoreMutex ){ 240 #ifdef SQLITE_THREAD_MISUSE_WARNINGS 241 pFrom = multiThreadedCheckMutex(); 242 #else 243 pFrom = sqlite3DefaultMutex(); 244 #endif 245 }else{ 246 pFrom = sqlite3NoopMutex(); 247 } 248 pTo->xMutexInit = pFrom->xMutexInit; 249 pTo->xMutexEnd = pFrom->xMutexEnd; 250 pTo->xMutexFree = pFrom->xMutexFree; 251 pTo->xMutexEnter = pFrom->xMutexEnter; 252 pTo->xMutexTry = pFrom->xMutexTry; 253 pTo->xMutexLeave = pFrom->xMutexLeave; 254 pTo->xMutexHeld = pFrom->xMutexHeld; 255 pTo->xMutexNotheld = pFrom->xMutexNotheld; 256 sqlite3MemoryBarrier(); 257 pTo->xMutexAlloc = pFrom->xMutexAlloc; 258 }else{ 259 /* Another thread may have just installed the default methods above. 260 ** This barrier pairs with the one prior to setting xMutexAlloc, so 261 ** that the load of xMutexInit below cannot be satisfied before the 262 ** load of xMutexAlloc on processors with weak memory ordering. */ 263 sqlite3MemoryBarrier(); 264 } 265 assert( sqlite3GlobalConfig.mutex.xMutexInit ); 266 rc = sqlite3GlobalConfig.mutex.xMutexInit(); 267 268 #ifdef SQLITE_DEBUG 269 GLOBAL(int, mutexIsInit) = 1; 270 #endif 271 272 sqlite3MemoryBarrier(); 273 return rc; 274 } 275 276 /* 277 ** Shutdown the mutex system. This call frees resources allocated by 278 ** sqlite3MutexInit(). 279 */ 280 int sqlite3MutexEnd(void){ 281 int rc = SQLITE_OK; 282 if( sqlite3GlobalConfig.mutex.xMutexEnd ){ 283 rc = sqlite3GlobalConfig.mutex.xMutexEnd(); 284 } 285 286 #ifdef SQLITE_DEBUG 287 GLOBAL(int, mutexIsInit) = 0; 288 #endif 289 290 return rc; 291 } 292 293 /* 294 ** Retrieve a pointer to a static mutex or allocate a new dynamic one. 295 */ 296 sqlite3_mutex *sqlite3_mutex_alloc(int id){ 297 #ifndef SQLITE_OMIT_AUTOINIT 298 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; 299 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; 300 #endif 301 assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); 302 return sqlite3GlobalConfig.mutex.xMutexAlloc(id); 303 } 304 305 sqlite3_mutex *sqlite3MutexAlloc(int id){ 306 if( !sqlite3GlobalConfig.bCoreMutex ){ 307 return 0; 308 } 309 assert( GLOBAL(int, mutexIsInit) ); 310 assert( sqlite3GlobalConfig.mutex.xMutexAlloc ); 311 return sqlite3GlobalConfig.mutex.xMutexAlloc(id); 312 } 313 314 /* 315 ** Free a dynamic mutex. 316 */ 317 void sqlite3_mutex_free(sqlite3_mutex *p){ 318 if( p ){ 319 assert( sqlite3GlobalConfig.mutex.xMutexFree ); 320 sqlite3GlobalConfig.mutex.xMutexFree(p); 321 } 322 } 323 324 /* 325 ** Obtain the mutex p. If some other thread already has the mutex, block 326 ** until it can be obtained. 327 */ 328 void sqlite3_mutex_enter(sqlite3_mutex *p){ 329 if( p ){ 330 assert( sqlite3GlobalConfig.mutex.xMutexEnter ); 331 sqlite3GlobalConfig.mutex.xMutexEnter(p); 332 } 333 } 334 335 /* 336 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another 337 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. 338 */ 339 int sqlite3_mutex_try(sqlite3_mutex *p){ 340 int rc = SQLITE_OK; 341 if( p ){ 342 assert( sqlite3GlobalConfig.mutex.xMutexTry ); 343 return sqlite3GlobalConfig.mutex.xMutexTry(p); 344 } 345 return rc; 346 } 347 348 /* 349 ** The sqlite3_mutex_leave() routine exits a mutex that was previously 350 ** entered by the same thread. The behavior is undefined if the mutex 351 ** is not currently entered. If a NULL pointer is passed as an argument 352 ** this function is a no-op. 353 */ 354 void sqlite3_mutex_leave(sqlite3_mutex *p){ 355 if( p ){ 356 assert( sqlite3GlobalConfig.mutex.xMutexLeave ); 357 sqlite3GlobalConfig.mutex.xMutexLeave(p); 358 } 359 } 360 361 #ifndef NDEBUG 362 /* 363 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are 364 ** intended for use inside assert() statements. 365 ** 366 ** Because these routines raise false-positive alerts in TSAN, disable 367 ** them (make them always return 1) when compiling with TSAN. 368 */ 369 int sqlite3_mutex_held(sqlite3_mutex *p){ 370 # if defined(__has_feature) 371 # if __has_feature(thread_sanitizer) 372 p = 0; 373 # endif 374 # endif 375 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld ); 376 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p); 377 } 378 int sqlite3_mutex_notheld(sqlite3_mutex *p){ 379 # if defined(__has_feature) 380 # if __has_feature(thread_sanitizer) 381 p = 0; 382 # endif 383 # endif 384 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld ); 385 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p); 386 } 387 #endif /* NDEBUG */ 388 389 #endif /* !defined(SQLITE_MUTEX_OMIT) */