mutex_unix.c

Go to the documentation of this file.
00001 /*
00002 ** 2007 August 28
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains the C functions that implement mutexes for pthreads
00013 **
00014 ** $Id: mutex_unix.c,v 1.13 2008/07/16 12:33:24 drh Exp $
00015 */
00016 #include "sqliteInt.h"
00017 
00018 /*
00019 ** The code in this file is only used if we are compiling threadsafe
00020 ** under unix with pthreads.
00021 **
00022 ** Note that this implementation requires a version of pthreads that
00023 ** supports recursive mutexes.
00024 */
00025 #ifdef SQLITE_MUTEX_PTHREADS
00026 
00027 #include <pthread.h>
00028 
00029 
00030 /*
00031 ** Each recursive mutex is an instance of the following structure.
00032 */
00033 struct sqlite3_mutex {
00034   pthread_mutex_t mutex;     /* Mutex controlling the lock */
00035   int id;                    /* Mutex type */
00036   int nRef;                  /* Number of entrances */
00037   pthread_t owner;           /* Thread that is within this mutex */
00038 #ifdef SQLITE_DEBUG
00039   int trace;                 /* True to trace changes */
00040 #endif
00041 };
00042 #ifdef SQLITE_DEBUG
00043 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
00044 #else
00045 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 }
00046 #endif
00047 
00048 /*
00049 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
00050 ** intended for use only inside assert() statements.  On some platforms,
00051 ** there might be race conditions that can cause these routines to
00052 ** deliver incorrect results.  In particular, if pthread_equal() is
00053 ** not an atomic operation, then these routines might delivery
00054 ** incorrect results.  On most platforms, pthread_equal() is a 
00055 ** comparison of two integers and is therefore atomic.  But we are
00056 ** told that HPUX is not such a platform.  If so, then these routines
00057 ** will not always work correctly on HPUX.
00058 **
00059 ** On those platforms where pthread_equal() is not atomic, SQLite
00060 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
00061 ** make sure no assert() statements are evaluated and hence these
00062 ** routines are never called.
00063 */
00064 #ifndef NDEBUG
00065 static int pthreadMutexHeld(sqlite3_mutex *p){
00066   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
00067 }
00068 static int pthreadMutexNotheld(sqlite3_mutex *p){
00069   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
00070 }
00071 #endif
00072 
00073 /*
00074 ** Initialize and deinitialize the mutex subsystem.
00075 */
00076 static int pthreadMutexInit(void){ return SQLITE_OK; }
00077 static int pthreadMutexEnd(void){ return SQLITE_OK; }
00078 
00079 /*
00080 ** The sqlite3_mutex_alloc() routine allocates a new
00081 ** mutex and returns a pointer to it.  If it returns NULL
00082 ** that means that a mutex could not be allocated.  SQLite
00083 ** will unwind its stack and return an error.  The argument
00084 ** to sqlite3_mutex_alloc() is one of these integer constants:
00085 **
00086 ** <ul>
00087 ** <li>  SQLITE_MUTEX_FAST
00088 ** <li>  SQLITE_MUTEX_RECURSIVE
00089 ** <li>  SQLITE_MUTEX_STATIC_MASTER
00090 ** <li>  SQLITE_MUTEX_STATIC_MEM
00091 ** <li>  SQLITE_MUTEX_STATIC_MEM2
00092 ** <li>  SQLITE_MUTEX_STATIC_PRNG
00093 ** <li>  SQLITE_MUTEX_STATIC_LRU
00094 ** </ul>
00095 **
00096 ** The first two constants cause sqlite3_mutex_alloc() to create
00097 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
00098 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
00099 ** The mutex implementation does not need to make a distinction
00100 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
00101 ** not want to.  But SQLite will only request a recursive mutex in
00102 ** cases where it really needs one.  If a faster non-recursive mutex
00103 ** implementation is available on the host platform, the mutex subsystem
00104 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
00105 **
00106 ** The other allowed parameters to sqlite3_mutex_alloc() each return
00107 ** a pointer to a static preexisting mutex.  Three static mutexes are
00108 ** used by the current version of SQLite.  Future versions of SQLite
00109 ** may add additional static mutexes.  Static mutexes are for internal
00110 ** use by SQLite only.  Applications that use SQLite mutexes should
00111 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
00112 ** SQLITE_MUTEX_RECURSIVE.
00113 **
00114 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
00115 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
00116 ** returns a different mutex on every call.  But for the static 
00117 ** mutex types, the same mutex is returned on every call that has
00118 ** the same type number.
00119 */
00120 static sqlite3_mutex *pthreadMutexAlloc(int iType){
00121   static sqlite3_mutex staticMutexes[] = {
00122     SQLITE3_MUTEX_INITIALIZER,
00123     SQLITE3_MUTEX_INITIALIZER,
00124     SQLITE3_MUTEX_INITIALIZER,
00125     SQLITE3_MUTEX_INITIALIZER,
00126     SQLITE3_MUTEX_INITIALIZER,
00127     SQLITE3_MUTEX_INITIALIZER
00128   };
00129   sqlite3_mutex *p;
00130   switch( iType ){
00131     case SQLITE_MUTEX_RECURSIVE: {
00132       p = sqlite3MallocZero( sizeof(*p) );
00133       if( p ){
00134 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
00135         /* If recursive mutexes are not available, we will have to
00136         ** build our own.  See below. */
00137         pthread_mutex_init(&p->mutex, 0);
00138 #else
00139         /* Use a recursive mutex if it is available */
00140         pthread_mutexattr_t recursiveAttr;
00141         pthread_mutexattr_init(&recursiveAttr);
00142         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
00143         pthread_mutex_init(&p->mutex, &recursiveAttr);
00144         pthread_mutexattr_destroy(&recursiveAttr);
00145 #endif
00146         p->id = iType;
00147       }
00148       break;
00149     }
00150     case SQLITE_MUTEX_FAST: {
00151       p = sqlite3MallocZero( sizeof(*p) );
00152       if( p ){
00153         p->id = iType;
00154         pthread_mutex_init(&p->mutex, 0);
00155       }
00156       break;
00157     }
00158     default: {
00159       assert( iType-2 >= 0 );
00160       assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
00161       p = &staticMutexes[iType-2];
00162       p->id = iType;
00163       break;
00164     }
00165   }
00166   return p;
00167 }
00168 
00169 
00170 /*
00171 ** This routine deallocates a previously
00172 ** allocated mutex.  SQLite is careful to deallocate every
00173 ** mutex that it allocates.
00174 */
00175 static void pthreadMutexFree(sqlite3_mutex *p){
00176   assert( p->nRef==0 );
00177   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
00178   pthread_mutex_destroy(&p->mutex);
00179   sqlite3_free(p);
00180 }
00181 
00182 /*
00183 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
00184 ** to enter a mutex.  If another thread is already within the mutex,
00185 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
00186 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
00187 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
00188 ** be entered multiple times by the same thread.  In such cases the,
00189 ** mutex must be exited an equal number of times before another thread
00190 ** can enter.  If the same thread tries to enter any other kind of mutex
00191 ** more than once, the behavior is undefined.
00192 */
00193 static void pthreadMutexEnter(sqlite3_mutex *p){
00194   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
00195 
00196 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
00197   /* If recursive mutexes are not available, then we have to grow
00198   ** our own.  This implementation assumes that pthread_equal()
00199   ** is atomic - that it cannot be deceived into thinking self
00200   ** and p->owner are equal if p->owner changes between two values
00201   ** that are not equal to self while the comparison is taking place.
00202   ** This implementation also assumes a coherent cache - that 
00203   ** separate processes cannot read different values from the same
00204   ** address at the same time.  If either of these two conditions
00205   ** are not met, then the mutexes will fail and problems will result.
00206   */
00207   {
00208     pthread_t self = pthread_self();
00209     if( p->nRef>0 && pthread_equal(p->owner, self) ){
00210       p->nRef++;
00211     }else{
00212       pthread_mutex_lock(&p->mutex);
00213       assert( p->nRef==0 );
00214       p->owner = self;
00215       p->nRef = 1;
00216     }
00217   }
00218 #else
00219   /* Use the built-in recursive mutexes if they are available.
00220   */
00221   pthread_mutex_lock(&p->mutex);
00222   p->owner = pthread_self();
00223   p->nRef++;
00224 #endif
00225 
00226 #ifdef SQLITE_DEBUG
00227   if( p->trace ){
00228     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
00229   }
00230 #endif
00231 }
00232 static int pthreadMutexTry(sqlite3_mutex *p){
00233   int rc;
00234   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
00235 
00236 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
00237   /* If recursive mutexes are not available, then we have to grow
00238   ** our own.  This implementation assumes that pthread_equal()
00239   ** is atomic - that it cannot be deceived into thinking self
00240   ** and p->owner are equal if p->owner changes between two values
00241   ** that are not equal to self while the comparison is taking place.
00242   ** This implementation also assumes a coherent cache - that 
00243   ** separate processes cannot read different values from the same
00244   ** address at the same time.  If either of these two conditions
00245   ** are not met, then the mutexes will fail and problems will result.
00246   */
00247   {
00248     pthread_t self = pthread_self();
00249     if( p->nRef>0 && pthread_equal(p->owner, self) ){
00250       p->nRef++;
00251       rc = SQLITE_OK;
00252     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
00253       assert( p->nRef==0 );
00254       p->owner = self;
00255       p->nRef = 1;
00256       rc = SQLITE_OK;
00257     }else{
00258       rc = SQLITE_BUSY;
00259     }
00260   }
00261 #else
00262   /* Use the built-in recursive mutexes if they are available.
00263   */
00264   if( pthread_mutex_trylock(&p->mutex)==0 ){
00265     p->owner = pthread_self();
00266     p->nRef++;
00267     rc = SQLITE_OK;
00268   }else{
00269     rc = SQLITE_BUSY;
00270   }
00271 #endif
00272 
00273 #ifdef SQLITE_DEBUG
00274   if( rc==SQLITE_OK && p->trace ){
00275     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
00276   }
00277 #endif
00278   return rc;
00279 }
00280 
00281 /*
00282 ** The sqlite3_mutex_leave() routine exits a mutex that was
00283 ** previously entered by the same thread.  The behavior
00284 ** is undefined if the mutex is not currently entered or
00285 ** is not currently allocated.  SQLite will never do either.
00286 */
00287 static void pthreadMutexLeave(sqlite3_mutex *p){
00288   assert( pthreadMutexHeld(p) );
00289   p->nRef--;
00290   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
00291 
00292 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
00293   if( p->nRef==0 ){
00294     pthread_mutex_unlock(&p->mutex);
00295   }
00296 #else
00297   pthread_mutex_unlock(&p->mutex);
00298 #endif
00299 
00300 #ifdef SQLITE_DEBUG
00301   if( p->trace ){
00302     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
00303   }
00304 #endif
00305 }
00306 
00307 sqlite3_mutex_methods *sqlite3DefaultMutex(void){
00308   static sqlite3_mutex_methods sMutex = {
00309     pthreadMutexInit,
00310     pthreadMutexEnd,
00311     pthreadMutexAlloc,
00312     pthreadMutexFree,
00313     pthreadMutexEnter,
00314     pthreadMutexTry,
00315     pthreadMutexLeave,
00316 #ifdef SQLITE_DEBUG
00317     pthreadMutexHeld,
00318     pthreadMutexNotheld
00319 #endif
00320   };
00321 
00322   return &sMutex;
00323 }
00324 
00325 #endif /* SQLITE_MUTEX_PTHREAD */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:55 2011 by Doxygen 1.6.1