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 | ** |
| 14 | ** $Id: mutex_unix.c,v 1.1 2007/08/28 16:34:43 drh 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 | ** Each recursive mutex is an instance of the following structure. |
| 31 | */ |
| 32 | struct sqlite3_mutex { |
| 33 | pthread_mutex_t mutex; /* Mutex controlling the lock */ |
| 34 | int id; /* Mutex type */ |
| 35 | int nRef; /* Number of entrances */ |
| 36 | pthread_t owner; /* Thread that is within this mutex */ |
| 37 | }; |
| 38 | |
| 39 | /* |
| 40 | ** The sqlite3_mutex_alloc() routine allocates a new |
| 41 | ** mutex and returns a pointer to it. If it returns NULL |
| 42 | ** that means that a mutex could not be allocated. SQLite |
| 43 | ** will unwind its stack and return an error. The argument |
| 44 | ** to sqlite3_mutex_alloc() is one of these integer constants: |
| 45 | ** |
| 46 | ** <ul> |
| 47 | ** <li> SQLITE_MUTEX_FAST |
| 48 | ** <li> SQLITE_MUTEX_RECURSIVE |
| 49 | ** <li> SQLITE_MUTEX_STATIC_MASTER |
| 50 | ** <li> SQLITE_MUTEX_STATIC_MEM |
| 51 | ** <li> SQLITE_MUTEX_STATIC_MEM2 |
| 52 | ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 53 | ** <li> SQLITE_MUTEX_STATIC_LRU |
| 54 | ** </ul> |
| 55 | ** |
| 56 | ** The first two constants cause sqlite3_mutex_alloc() to create |
| 57 | ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 58 | ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 59 | ** The mutex implementation does not need to make a distinction |
| 60 | ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 61 | ** not want to. But SQLite will only request a recursive mutex in |
| 62 | ** cases where it really needs one. If a faster non-recursive mutex |
| 63 | ** implementation is available on the host platform, the mutex subsystem |
| 64 | ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 65 | ** |
| 66 | ** The other allowed parameters to sqlite3_mutex_alloc() each return |
| 67 | ** a pointer to a static preexisting mutex. Three static mutexes are |
| 68 | ** used by the current version of SQLite. Future versions of SQLite |
| 69 | ** may add additional static mutexes. Static mutexes are for internal |
| 70 | ** use by SQLite only. Applications that use SQLite mutexes should |
| 71 | ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 72 | ** SQLITE_MUTEX_RECURSIVE. |
| 73 | ** |
| 74 | ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 75 | ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 76 | ** returns a different mutex on every call. But for the static |
| 77 | ** mutex types, the same mutex is returned on every call that has |
| 78 | ** the same type number. |
| 79 | */ |
| 80 | sqlite3_mutex *sqlite3_mutex_alloc(int iType){ |
| 81 | static sqlite3_mutex staticMutexes[] = { |
| 82 | { PTHREAD_MUTEX_INITIALIZER, }, |
| 83 | { PTHREAD_MUTEX_INITIALIZER, }, |
| 84 | { PTHREAD_MUTEX_INITIALIZER, }, |
| 85 | { PTHREAD_MUTEX_INITIALIZER, }, |
| 86 | { PTHREAD_MUTEX_INITIALIZER, }, |
| 87 | }; |
| 88 | sqlite3_mutex *p; |
| 89 | switch( iType ){ |
| 90 | case SQLITE_MUTEX_RECURSIVE: { |
| 91 | p = sqlite3MallocZero( sizeof(*p) ); |
| 92 | if( p ){ |
| 93 | pthread_mutexattr_t recursiveAttr; |
| 94 | pthread_mutexattr_init(&recursiveAttr); |
| 95 | pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE); |
| 96 | pthread_mutex_init(&p->mutex, &recursiveAttr); |
| 97 | pthread_mutexattr_destroy(&recursiveAttr); |
| 98 | p->id = iType; |
| 99 | } |
| 100 | break; |
| 101 | } |
| 102 | case SQLITE_MUTEX_FAST: { |
| 103 | p = sqlite3MallocZero( sizeof(*p) ); |
| 104 | if( p ){ |
| 105 | p->id = iType; |
| 106 | pthread_mutex_init(&p->mutex, 0); |
| 107 | } |
| 108 | break; |
| 109 | } |
| 110 | default: { |
| 111 | assert( iType-2 >= 0 ); |
| 112 | assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) ); |
| 113 | p = &staticMutexes[iType-2]; |
| 114 | p->id = iType; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | return p; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /* |
| 123 | ** This routine deallocates a previously |
| 124 | ** allocated mutex. SQLite is careful to deallocate every |
| 125 | ** mutex that it allocates. |
| 126 | */ |
| 127 | void sqlite3_mutex_free(sqlite3_mutex *p){ |
| 128 | assert( p ); |
| 129 | assert( p->nRef==0 ); |
| 130 | assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 131 | pthread_mutex_destroy(&p->mutex); |
| 132 | sqlite3_free(p); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 137 | ** to enter a mutex. If another thread is already within the mutex, |
| 138 | ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 139 | ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK |
| 140 | ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can |
| 141 | ** be entered multiple times by the same thread. In such cases the, |
| 142 | ** mutex must be exited an equal number of times before another thread |
| 143 | ** can enter. If the same thread tries to enter any other kind of mutex |
| 144 | ** more than once, the behavior is undefined. |
| 145 | */ |
| 146 | void sqlite3_mutex_enter(sqlite3_mutex *p){ |
| 147 | assert( p ); |
| 148 | assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); |
| 149 | pthread_mutex_lock(&p->mutex); |
| 150 | p->owner = pthread_self(); |
| 151 | p->nRef++; |
| 152 | } |
| 153 | int sqlite3_mutex_try(sqlite3_mutex *p){ |
| 154 | int rc; |
| 155 | assert( p ); |
| 156 | assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) ); |
| 157 | if( pthread_mutex_trylock(&p->mutex)==0 ){ |
| 158 | p->owner = pthread_self(); |
| 159 | p->nRef++; |
| 160 | rc = SQLITE_OK; |
| 161 | }else{ |
| 162 | rc = SQLITE_BUSY; |
| 163 | } |
| 164 | return rc; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | ** The sqlite3_mutex_leave() routine exits a mutex that was |
| 169 | ** previously entered by the same thread. The behavior |
| 170 | ** is undefined if the mutex is not currently entered or |
| 171 | ** is not currently allocated. SQLite will never do either. |
| 172 | */ |
| 173 | void sqlite3_mutex_leave(sqlite3_mutex *p){ |
| 174 | assert( p ); |
| 175 | assert( sqlite3_mutex_held(p) ); |
| 176 | p->nRef--; |
| 177 | assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 178 | pthread_mutex_unlock(&p->mutex); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are |
| 183 | ** intended for use only inside assert() statements. On some platforms, |
| 184 | ** there might be race conditions that can cause these routines to |
| 185 | ** deliver incorrect results. In particular, if pthread_equal() is |
| 186 | ** not an atomic operation, then these routines might delivery |
| 187 | ** incorrect results. On most platforms, pthread_equal() is a |
| 188 | ** comparison of two integers and is therefore atomic. But we are |
| 189 | ** told that HPUX is not such a platform. If so, then these routines |
| 190 | ** will not always work correctly on HPUX. |
| 191 | ** |
| 192 | ** On those platforms where pthread_equal() is not atomic, SQLite |
| 193 | ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to |
| 194 | ** make sure no assert() statements are evaluated and hence these |
| 195 | ** routines are never called. |
| 196 | */ |
| 197 | #ifndef NDEBUG |
| 198 | int sqlite3_mutex_held(sqlite3_mutex *p){ |
| 199 | return p==0 || (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); |
| 200 | } |
| 201 | int sqlite3_mutex_notheld(sqlite3_mutex *p){ |
| 202 | return p==0 || p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; |
| 203 | } |
| 204 | #endif |
| 205 | #endif /* SQLITE_MUTEX_PTHREAD */ |