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