1 /* 2 ** 2007 August 28 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 pthreads 13 ** 14 ** $Id: mutex_unix.c,v 1.7 2008/03/29 12:47:27 rse Exp $ 15 */ 16 #include "sqliteInt.h" 17 18 /* 19 ** The code in this file is only used if we are compiling threadsafe 20 ** under unix with pthreads. 21 ** 22 ** Note that this implementation requires a version of pthreads that 23 ** supports recursive mutexes. 24 */ 25 #ifdef SQLITE_MUTEX_PTHREADS 26 27 #include <pthread.h> 28 29 30 /* 31 ** Each recursive mutex is an instance of the following structure. 32 */ 33 struct sqlite3_mutex { 34 pthread_mutex_t mutex; /* Mutex controlling the lock */ 35 int id; /* Mutex type */ 36 int nRef; /* Number of entrances */ 37 pthread_t owner; /* Thread that is within this mutex */ 38 #ifdef SQLITE_DEBUG 39 int trace; /* True to trace changes */ 40 #endif 41 }; 42 #ifdef SQLITE_DEBUG 43 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 } 44 #else 45 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 } 46 #endif 47 48 /* 49 ** The sqlite3_mutex_alloc() routine allocates a new 50 ** mutex and returns a pointer to it. If it returns NULL 51 ** that means that a mutex could not be allocated. SQLite 52 ** will unwind its stack and return an error. The argument 53 ** to sqlite3_mutex_alloc() is one of these integer constants: 54 ** 55 ** <ul> 56 ** <li> SQLITE_MUTEX_FAST 57 ** <li> SQLITE_MUTEX_RECURSIVE 58 ** <li> SQLITE_MUTEX_STATIC_MASTER 59 ** <li> SQLITE_MUTEX_STATIC_MEM 60 ** <li> SQLITE_MUTEX_STATIC_MEM2 61 ** <li> SQLITE_MUTEX_STATIC_PRNG 62 ** <li> SQLITE_MUTEX_STATIC_LRU 63 ** </ul> 64 ** 65 ** The first two constants cause sqlite3_mutex_alloc() to create 66 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE 67 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. 68 ** The mutex implementation does not need to make a distinction 69 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does 70 ** not want to. But SQLite will only request a recursive mutex in 71 ** cases where it really needs one. If a faster non-recursive mutex 72 ** implementation is available on the host platform, the mutex subsystem 73 ** might return such a mutex in response to SQLITE_MUTEX_FAST. 74 ** 75 ** The other allowed parameters to sqlite3_mutex_alloc() each return 76 ** a pointer to a static preexisting mutex. Three static mutexes are 77 ** used by the current version of SQLite. Future versions of SQLite 78 ** may add additional static mutexes. Static mutexes are for internal 79 ** use by SQLite only. Applications that use SQLite mutexes should 80 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or 81 ** SQLITE_MUTEX_RECURSIVE. 82 ** 83 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST 84 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() 85 ** returns a different mutex on every call. But for the static 86 ** mutex types, the same mutex is returned on every call that has 87 ** the same type number. 88 */ 89 sqlite3_mutex *sqlite3_mutex_alloc(int iType){ 90 static sqlite3_mutex staticMutexes[] = { 91 SQLITE3_MUTEX_INITIALIZER, 92 SQLITE3_MUTEX_INITIALIZER, 93 SQLITE3_MUTEX_INITIALIZER, 94 SQLITE3_MUTEX_INITIALIZER, 95 SQLITE3_MUTEX_INITIALIZER, 96 SQLITE3_MUTEX_INITIALIZER 97 }; 98 sqlite3_mutex *p; 99 switch( iType ){ 100 case SQLITE_MUTEX_RECURSIVE: { 101 p = sqlite3MallocZero( sizeof(*p) ); 102 if( p ){ 103 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX 104 /* If recursive mutexes are not available, we will have to 105 ** build our own. See below. */ 106 pthread_mutex_init(&p->mutex, 0); 107 #else 108 /* Use a recursive mutex if it is available */ 109 pthread_mutexattr_t recursiveAttr; 110 pthread_mutexattr_init(&recursiveAttr); 111 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE); 112 pthread_mutex_init(&p->mutex, &recursiveAttr); 113 pthread_mutexattr_destroy(&recursiveAttr); 114 #endif 115 p->id = iType; 116 } 117 break; 118 } 119 case SQLITE_MUTEX_FAST: { 120 p = sqlite3MallocZero( sizeof(*p) ); 121 if( p ){ 122 p->id = iType; 123 pthread_mutex_init(&p->mutex, 0); 124 } 125 break; 126 } 127 default: { 128 assert( iType-2 >= 0 ); 129 assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) ); 130 p = &staticMutexes[iType-2]; 131 p->id = iType; 132 break; 133 } 134 } 135 return p; 136 } 137 138 139 /* 140 ** This routine deallocates a previously 141 ** allocated mutex. SQLite is careful to deallocate every 142 ** mutex that it allocates. 143 */ 144 void sqlite3_mutex_free(sqlite3_mutex *p){ 145 assert( p ); 146 assert( p->nRef==0 ); 147 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); 148 pthread_mutex_destroy(&p->mutex); 149 sqlite3_free(p); 150 } 151 152 /* 153 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt 154 ** to enter a mutex. If another thread is already within the mutex, 155 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return 156 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK 157 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can 158 ** be entered multiple times by the same thread. In such cases the, 159 ** mutex must be exited an equal number of times before another thread 160 ** can enter. If the same thread tries to enter any other kind of mutex 161 ** more than once, the behavior is undefined. 162 */ 163 void sqlite3_mutex_enter(sqlite3_mutex *p){ 164 assert( p ); 165 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); 166 167 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX 168 /* If recursive mutexes are not available, then we have to grow 169 ** our own. This implementation assumes that pthread_equal() 170 ** is atomic - that it cannot be deceived into thinking self 171 ** and p->owner are equal if p->owner changes between two values 172 ** that are not equal to self while the comparison is taking place. 173 ** This implementation also assumes a coherent cache - that 174 ** separate processes cannot read different values from the same 175 ** address at the same time. If either of these two conditions 176 ** are not met, then the mutexes will fail and problems will result. 177 */ 178 { 179 pthread_t self = pthread_self(); 180 if( p->nRef>0 && pthread_equal(p->owner, self) ){ 181 p->nRef++; 182 }else{ 183 pthread_mutex_lock(&p->mutex); 184 assert( p->nRef==0 ); 185 p->owner = self; 186 p->nRef = 1; 187 } 188 } 189 #else 190 /* Use the built-in recursive mutexes if they are available. 191 */ 192 pthread_mutex_lock(&p->mutex); 193 p->owner = pthread_self(); 194 p->nRef++; 195 #endif 196 197 #ifdef SQLITE_DEBUG 198 if( p->trace ){ 199 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); 200 } 201 #endif 202 } 203 int sqlite3_mutex_try(sqlite3_mutex *p){ 204 int rc; 205 assert( p ); 206 assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); 207 208 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX 209 /* If recursive mutexes are not available, then we have to grow 210 ** our own. This implementation assumes that pthread_equal() 211 ** is atomic - that it cannot be deceived into thinking self 212 ** and p->owner are equal if p->owner changes between two values 213 ** that are not equal to self while the comparison is taking place. 214 ** This implementation also assumes a coherent cache - that 215 ** separate processes cannot read different values from the same 216 ** address at the same time. If either of these two conditions 217 ** are not met, then the mutexes will fail and problems will result. 218 */ 219 { 220 pthread_t self = pthread_self(); 221 if( p->nRef>0 && pthread_equal(p->owner, self) ){ 222 p->nRef++; 223 rc = SQLITE_OK; 224 }else if( pthread_mutex_lock(&p->mutex)==0 ){ 225 assert( p->nRef==0 ); 226 p->owner = self; 227 p->nRef = 1; 228 rc = SQLITE_OK; 229 }else{ 230 rc = SQLITE_BUSY; 231 } 232 } 233 #else 234 /* Use the built-in recursive mutexes if they are available. 235 */ 236 if( pthread_mutex_trylock(&p->mutex)==0 ){ 237 p->owner = pthread_self(); 238 p->nRef++; 239 rc = SQLITE_OK; 240 }else{ 241 rc = SQLITE_BUSY; 242 } 243 #endif 244 245 #ifdef SQLITE_DEBUG 246 if( rc==SQLITE_OK && p->trace ){ 247 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); 248 } 249 #endif 250 return rc; 251 } 252 253 /* 254 ** The sqlite3_mutex_leave() routine exits a mutex that was 255 ** previously entered by the same thread. The behavior 256 ** is undefined if the mutex is not currently entered or 257 ** is not currently allocated. SQLite will never do either. 258 */ 259 void sqlite3_mutex_leave(sqlite3_mutex *p){ 260 assert( p ); 261 assert( sqlite3_mutex_held(p) ); 262 p->nRef--; 263 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); 264 265 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX 266 if( p->nRef==0 ){ 267 pthread_mutex_unlock(&p->mutex); 268 } 269 #else 270 pthread_mutex_unlock(&p->mutex); 271 #endif 272 273 #ifdef SQLITE_DEBUG 274 if( p->trace ){ 275 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); 276 } 277 #endif 278 } 279 280 /* 281 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are 282 ** intended for use only inside assert() statements. On some platforms, 283 ** there might be race conditions that can cause these routines to 284 ** deliver incorrect results. In particular, if pthread_equal() is 285 ** not an atomic operation, then these routines might delivery 286 ** incorrect results. On most platforms, pthread_equal() is a 287 ** comparison of two integers and is therefore atomic. But we are 288 ** told that HPUX is not such a platform. If so, then these routines 289 ** will not always work correctly on HPUX. 290 ** 291 ** On those platforms where pthread_equal() is not atomic, SQLite 292 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to 293 ** make sure no assert() statements are evaluated and hence these 294 ** routines are never called. 295 */ 296 #ifndef NDEBUG 297 int sqlite3_mutex_held(sqlite3_mutex *p){ 298 return p==0 || (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); 299 } 300 int sqlite3_mutex_notheld(sqlite3_mutex *p){ 301 return p==0 || p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; 302 } 303 #endif 304 #endif /* SQLITE_MUTEX_PTHREAD */ 305