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