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