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