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 */ 26 27 28 #ifndef SQLITE_MUTEX_OMIT 29 /* 30 ** Initialize the mutex system. 31 */ 32 int sqlite3MutexInit(void){ 33 int rc = SQLITE_OK; 34 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){ 35 /* If the xMutexAlloc method has not been set, then the user did not 36 ** install a mutex implementation via sqlite3_config() prior to 37 ** sqlite3_initialize() being called. This block copies pointers to 38 ** the default implementation into the sqlite3GlobalConfig structure. 39 */ 40 sqlite3_mutex_methods const *pFrom; 41 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex; 42 43 if( sqlite3GlobalConfig.bCoreMutex ){ 44 pFrom = sqlite3DefaultMutex(); 45 }else{ 46 pFrom = sqlite3NoopMutex(); 47 } 48 memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc)); 49 memcpy(&pTo->xMutexFree, &pFrom->xMutexFree, 50 sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree)); 51 pTo->xMutexAlloc = pFrom->xMutexAlloc; 52 } 53 rc = sqlite3GlobalConfig.mutex.xMutexInit(); 54 55 #ifdef SQLITE_DEBUG 56 GLOBAL(int, mutexIsInit) = 1; 57 #endif 58 59 return rc; 60 } 61 62 /* 63 ** Shutdown the mutex system. This call frees resources allocated by 64 ** sqlite3MutexInit(). 65 */ 66 int sqlite3MutexEnd(void){ 67 int rc = SQLITE_OK; 68 if( sqlite3GlobalConfig.mutex.xMutexEnd ){ 69 rc = sqlite3GlobalConfig.mutex.xMutexEnd(); 70 } 71 72 #ifdef SQLITE_DEBUG 73 GLOBAL(int, mutexIsInit) = 0; 74 #endif 75 76 return rc; 77 } 78 79 /* 80 ** Retrieve a pointer to a static mutex or allocate a new dynamic one. 81 */ 82 sqlite3_mutex *sqlite3_mutex_alloc(int id){ 83 #ifndef SQLITE_OMIT_AUTOINIT 84 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0; 85 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0; 86 #endif 87 return sqlite3GlobalConfig.mutex.xMutexAlloc(id); 88 } 89 90 sqlite3_mutex *sqlite3MutexAlloc(int id){ 91 if( !sqlite3GlobalConfig.bCoreMutex ){ 92 return 0; 93 } 94 assert( GLOBAL(int, mutexIsInit) ); 95 return sqlite3GlobalConfig.mutex.xMutexAlloc(id); 96 } 97 98 /* 99 ** Free a dynamic mutex. 100 */ 101 void sqlite3_mutex_free(sqlite3_mutex *p){ 102 if( p ){ 103 sqlite3GlobalConfig.mutex.xMutexFree(p); 104 } 105 } 106 107 /* 108 ** Obtain the mutex p. If some other thread already has the mutex, block 109 ** until it can be obtained. 110 */ 111 void sqlite3_mutex_enter(sqlite3_mutex *p){ 112 if( p ){ 113 sqlite3GlobalConfig.mutex.xMutexEnter(p); 114 } 115 } 116 117 /* 118 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another 119 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY. 120 */ 121 int sqlite3_mutex_try(sqlite3_mutex *p){ 122 int rc = SQLITE_OK; 123 if( p ){ 124 return sqlite3GlobalConfig.mutex.xMutexTry(p); 125 } 126 return rc; 127 } 128 129 /* 130 ** The sqlite3_mutex_leave() routine exits a mutex that was previously 131 ** entered by the same thread. The behavior is undefined if the mutex 132 ** is not currently entered. If a NULL pointer is passed as an argument 133 ** this function is a no-op. 134 */ 135 void sqlite3_mutex_leave(sqlite3_mutex *p){ 136 if( p ){ 137 sqlite3GlobalConfig.mutex.xMutexLeave(p); 138 } 139 } 140 141 #ifndef NDEBUG 142 /* 143 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are 144 ** intended for use inside assert() statements. 145 */ 146 int sqlite3_mutex_held(sqlite3_mutex *p){ 147 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p); 148 } 149 int sqlite3_mutex_notheld(sqlite3_mutex *p){ 150 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p); 151 } 152 #endif 153 154 #endif /* !defined(SQLITE_MUTEX_OMIT) */ 155