xref: /sqlite-3.40.0/src/mutex_unix.c (revision ccb2113a)
1437b9013Sdrh /*
2437b9013Sdrh ** 2007 August 28
3437b9013Sdrh **
4437b9013Sdrh ** The author disclaims copyright to this source code.  In place of
5437b9013Sdrh ** a legal notice, here is a blessing:
6437b9013Sdrh **
7437b9013Sdrh **    May you do good and not evil.
8437b9013Sdrh **    May you find forgiveness for yourself and forgive others.
9437b9013Sdrh **    May you share freely, never taking more than you give.
10437b9013Sdrh **
11437b9013Sdrh *************************************************************************
12437b9013Sdrh ** This file contains the C functions that implement mutexes for pthreads
13437b9013Sdrh */
14437b9013Sdrh #include "sqliteInt.h"
15437b9013Sdrh 
16437b9013Sdrh /*
17437b9013Sdrh ** The code in this file is only used if we are compiling threadsafe
18437b9013Sdrh ** under unix with pthreads.
19437b9013Sdrh **
20437b9013Sdrh ** Note that this implementation requires a version of pthreads that
21437b9013Sdrh ** supports recursive mutexes.
22437b9013Sdrh */
23437b9013Sdrh #ifdef SQLITE_MUTEX_PTHREADS
24437b9013Sdrh 
25437b9013Sdrh #include <pthread.h>
26437b9013Sdrh 
2772339a3eSdrh /*
2872339a3eSdrh ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
2972339a3eSdrh ** are necessary under two condidtions:  (1) Debug builds and (2) using
3072339a3eSdrh ** home-grown mutexes.  Encapsulate these conditions into a single #define.
3172339a3eSdrh */
3272339a3eSdrh #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
3372339a3eSdrh # define SQLITE_MUTEX_NREF 1
3472339a3eSdrh #else
3572339a3eSdrh # define SQLITE_MUTEX_NREF 0
3672339a3eSdrh #endif
37ed05efbfSdrh 
38437b9013Sdrh /*
39437b9013Sdrh ** Each recursive mutex is an instance of the following structure.
40437b9013Sdrh */
41437b9013Sdrh struct sqlite3_mutex {
42437b9013Sdrh   pthread_mutex_t mutex;     /* Mutex controlling the lock */
4396c707a3Sdrh #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
44437b9013Sdrh   int id;                    /* Mutex type */
4596c707a3Sdrh #endif
4696c707a3Sdrh #if SQLITE_MUTEX_NREF
4772339a3eSdrh   volatile int nRef;         /* Number of entrances */
4872339a3eSdrh   volatile pthread_t owner;  /* Thread that is within this mutex */
49d0679edcSdrh   int trace;                 /* True to trace changes */
50d0679edcSdrh #endif
51437b9013Sdrh };
5272339a3eSdrh #if SQLITE_MUTEX_NREF
53c5515508Sdan # define SQLITE3_MUTEX_INITIALIZER(id) \
54c5515508Sdan      {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
553e6a1411Sdan #elif defined(SQLITE_ENABLE_API_ARMOR)
56c5515508Sdan # define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
5728f667fcSrse #else
58c5515508Sdan #define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
5928f667fcSrse #endif
60437b9013Sdrh 
61437b9013Sdrh /*
626d2ab0e4Sdanielk1977 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
636d2ab0e4Sdanielk1977 ** intended for use only inside assert() statements.  On some platforms,
646d2ab0e4Sdanielk1977 ** there might be race conditions that can cause these routines to
656d2ab0e4Sdanielk1977 ** deliver incorrect results.  In particular, if pthread_equal() is
666d2ab0e4Sdanielk1977 ** not an atomic operation, then these routines might delivery
676d2ab0e4Sdanielk1977 ** incorrect results.  On most platforms, pthread_equal() is a
686d2ab0e4Sdanielk1977 ** comparison of two integers and is therefore atomic.  But we are
696d2ab0e4Sdanielk1977 ** told that HPUX is not such a platform.  If so, then these routines
706d2ab0e4Sdanielk1977 ** will not always work correctly on HPUX.
716d2ab0e4Sdanielk1977 **
726d2ab0e4Sdanielk1977 ** On those platforms where pthread_equal() is not atomic, SQLite
736d2ab0e4Sdanielk1977 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
746d2ab0e4Sdanielk1977 ** make sure no assert() statements are evaluated and hence these
756d2ab0e4Sdanielk1977 ** routines are never called.
766d2ab0e4Sdanielk1977 */
7797185489Schw #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
pthreadMutexHeld(sqlite3_mutex * p)786d2ab0e4Sdanielk1977 static int pthreadMutexHeld(sqlite3_mutex *p){
796d2ab0e4Sdanielk1977   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
806d2ab0e4Sdanielk1977 }
pthreadMutexNotheld(sqlite3_mutex * p)816d2ab0e4Sdanielk1977 static int pthreadMutexNotheld(sqlite3_mutex *p){
826d2ab0e4Sdanielk1977   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
836d2ab0e4Sdanielk1977 }
846d2ab0e4Sdanielk1977 #endif
856d2ab0e4Sdanielk1977 
866d2ab0e4Sdanielk1977 /*
87539482b7Sdrh ** Try to provide a memory barrier operation, needed for initialization
88539482b7Sdrh ** and also for the implementation of xShmBarrier in the VFS in cases
89539482b7Sdrh ** where SQLite is compiled without mutexes.
906081c1dbSdrh */
sqlite3MemoryBarrier(void)916081c1dbSdrh void sqlite3MemoryBarrier(void){
922d64034bSdrh #if defined(SQLITE_MEMORY_BARRIER)
936081c1dbSdrh   SQLITE_MEMORY_BARRIER;
9404abf087Smistachkin #elif defined(__GNUC__) && GCC_VERSION>=4001000
952d64034bSdrh   __sync_synchronize();
966081c1dbSdrh #endif
976081c1dbSdrh }
986081c1dbSdrh 
996081c1dbSdrh /*
10040257ffdSdrh ** Initialize and deinitialize the mutex subsystem.
10140257ffdSdrh */
pthreadMutexInit(void)1026d2ab0e4Sdanielk1977 static int pthreadMutexInit(void){ return SQLITE_OK; }
pthreadMutexEnd(void)1036d2ab0e4Sdanielk1977 static int pthreadMutexEnd(void){ return SQLITE_OK; }
10440257ffdSdrh 
10540257ffdSdrh /*
106437b9013Sdrh ** The sqlite3_mutex_alloc() routine allocates a new
107437b9013Sdrh ** mutex and returns a pointer to it.  If it returns NULL
108437b9013Sdrh ** that means that a mutex could not be allocated.  SQLite
109437b9013Sdrh ** will unwind its stack and return an error.  The argument
110437b9013Sdrh ** to sqlite3_mutex_alloc() is one of these integer constants:
111437b9013Sdrh **
112437b9013Sdrh ** <ul>
113437b9013Sdrh ** <li>  SQLITE_MUTEX_FAST
114437b9013Sdrh ** <li>  SQLITE_MUTEX_RECURSIVE
115*ccb2113aSdrh ** <li>  SQLITE_MUTEX_STATIC_MAIN
116437b9013Sdrh ** <li>  SQLITE_MUTEX_STATIC_MEM
117d42d0bedSdrh ** <li>  SQLITE_MUTEX_STATIC_OPEN
118437b9013Sdrh ** <li>  SQLITE_MUTEX_STATIC_PRNG
119437b9013Sdrh ** <li>  SQLITE_MUTEX_STATIC_LRU
1206d4fb833Sdan ** <li>  SQLITE_MUTEX_STATIC_PMEM
121d42d0bedSdrh ** <li>  SQLITE_MUTEX_STATIC_APP1
122d42d0bedSdrh ** <li>  SQLITE_MUTEX_STATIC_APP2
123d42d0bedSdrh ** <li>  SQLITE_MUTEX_STATIC_APP3
12493de6538Smistachkin ** <li>  SQLITE_MUTEX_STATIC_VFS1
12593de6538Smistachkin ** <li>  SQLITE_MUTEX_STATIC_VFS2
12693de6538Smistachkin ** <li>  SQLITE_MUTEX_STATIC_VFS3
127437b9013Sdrh ** </ul>
128437b9013Sdrh **
129437b9013Sdrh ** The first two constants cause sqlite3_mutex_alloc() to create
130437b9013Sdrh ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
131437b9013Sdrh ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
132437b9013Sdrh ** The mutex implementation does not need to make a distinction
133437b9013Sdrh ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
134437b9013Sdrh ** not want to.  But SQLite will only request a recursive mutex in
135437b9013Sdrh ** cases where it really needs one.  If a faster non-recursive mutex
136437b9013Sdrh ** implementation is available on the host platform, the mutex subsystem
137437b9013Sdrh ** might return such a mutex in response to SQLITE_MUTEX_FAST.
138437b9013Sdrh **
139437b9013Sdrh ** The other allowed parameters to sqlite3_mutex_alloc() each return
1407c7c311dSshane ** a pointer to a static preexisting mutex.  Six static mutexes are
141437b9013Sdrh ** used by the current version of SQLite.  Future versions of SQLite
142437b9013Sdrh ** may add additional static mutexes.  Static mutexes are for internal
143437b9013Sdrh ** use by SQLite only.  Applications that use SQLite mutexes should
144437b9013Sdrh ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
145437b9013Sdrh ** SQLITE_MUTEX_RECURSIVE.
146437b9013Sdrh **
147437b9013Sdrh ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
148437b9013Sdrh ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
149437b9013Sdrh ** returns a different mutex on every call.  But for the static
150437b9013Sdrh ** mutex types, the same mutex is returned on every call that has
151437b9013Sdrh ** the same type number.
152437b9013Sdrh */
pthreadMutexAlloc(int iType)1536d2ab0e4Sdanielk1977 static sqlite3_mutex *pthreadMutexAlloc(int iType){
154437b9013Sdrh   static sqlite3_mutex staticMutexes[] = {
155c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(2),
156c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(3),
157c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(4),
158c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(5),
159c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(6),
160c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(7),
161c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(8),
162c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(9),
163c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(10),
164c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(11),
165c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(12),
166c5515508Sdan     SQLITE3_MUTEX_INITIALIZER(13)
167437b9013Sdrh   };
168437b9013Sdrh   sqlite3_mutex *p;
169437b9013Sdrh   switch( iType ){
170437b9013Sdrh     case SQLITE_MUTEX_RECURSIVE: {
171437b9013Sdrh       p = sqlite3MallocZero( sizeof(*p) );
172437b9013Sdrh       if( p ){
1730167f285Sdrh #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
1740167f285Sdrh         /* If recursive mutexes are not available, we will have to
1750167f285Sdrh         ** build our own.  See below. */
1760167f285Sdrh         pthread_mutex_init(&p->mutex, 0);
1770167f285Sdrh #else
178ed05efbfSdrh         /* Use a recursive mutex if it is available */
179437b9013Sdrh         pthread_mutexattr_t recursiveAttr;
180437b9013Sdrh         pthread_mutexattr_init(&recursiveAttr);
181437b9013Sdrh         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
182437b9013Sdrh         pthread_mutex_init(&p->mutex, &recursiveAttr);
183437b9013Sdrh         pthread_mutexattr_destroy(&recursiveAttr);
184ed05efbfSdrh #endif
185c5515508Sdan #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
186c5515508Sdan         p->id = SQLITE_MUTEX_RECURSIVE;
187c5515508Sdan #endif
188437b9013Sdrh       }
189437b9013Sdrh       break;
190437b9013Sdrh     }
191437b9013Sdrh     case SQLITE_MUTEX_FAST: {
192437b9013Sdrh       p = sqlite3MallocZero( sizeof(*p) );
193437b9013Sdrh       if( p ){
194437b9013Sdrh         pthread_mutex_init(&p->mutex, 0);
195c5515508Sdan #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
196c5515508Sdan         p->id = SQLITE_MUTEX_FAST;
197c5515508Sdan #endif
198437b9013Sdrh       }
199437b9013Sdrh       break;
200437b9013Sdrh     }
201437b9013Sdrh     default: {
2029ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
2039ca95730Sdrh       if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
2049ca95730Sdrh         (void)SQLITE_MISUSE_BKPT;
2059ca95730Sdrh         return 0;
2069ca95730Sdrh       }
2079ca95730Sdrh #endif
208437b9013Sdrh       p = &staticMutexes[iType-2];
209437b9013Sdrh       break;
210437b9013Sdrh     }
211437b9013Sdrh   }
21296c707a3Sdrh #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
213c5515508Sdan   assert( p==0 || p->id==iType );
21496c707a3Sdrh #endif
215437b9013Sdrh   return p;
216437b9013Sdrh }
217437b9013Sdrh 
218437b9013Sdrh 
219437b9013Sdrh /*
220437b9013Sdrh ** This routine deallocates a previously
221437b9013Sdrh ** allocated mutex.  SQLite is careful to deallocate every
222437b9013Sdrh ** mutex that it allocates.
223437b9013Sdrh */
pthreadMutexFree(sqlite3_mutex * p)2246d2ab0e4Sdanielk1977 static void pthreadMutexFree(sqlite3_mutex *p){
225437b9013Sdrh   assert( p->nRef==0 );
22696c707a3Sdrh #if SQLITE_ENABLE_API_ARMOR
22796c707a3Sdrh   if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
22896c707a3Sdrh #endif
22996c707a3Sdrh   {
230437b9013Sdrh     pthread_mutex_destroy(&p->mutex);
231437b9013Sdrh     sqlite3_free(p);
232437b9013Sdrh   }
23396c707a3Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
23496c707a3Sdrh   else{
23596c707a3Sdrh     (void)SQLITE_MISUSE_BKPT;
23696c707a3Sdrh   }
23796c707a3Sdrh #endif
23896c707a3Sdrh }
239437b9013Sdrh 
240437b9013Sdrh /*
241437b9013Sdrh ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
242437b9013Sdrh ** to enter a mutex.  If another thread is already within the mutex,
243437b9013Sdrh ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
244437b9013Sdrh ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
245437b9013Sdrh ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
246437b9013Sdrh ** be entered multiple times by the same thread.  In such cases the,
247437b9013Sdrh ** mutex must be exited an equal number of times before another thread
248437b9013Sdrh ** can enter.  If the same thread tries to enter any other kind of mutex
249437b9013Sdrh ** more than once, the behavior is undefined.
250437b9013Sdrh */
pthreadMutexEnter(sqlite3_mutex * p)2516d2ab0e4Sdanielk1977 static void pthreadMutexEnter(sqlite3_mutex *p){
2526d2ab0e4Sdanielk1977   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
253ed05efbfSdrh 
2540167f285Sdrh #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
255ed05efbfSdrh   /* If recursive mutexes are not available, then we have to grow
256ed05efbfSdrh   ** our own.  This implementation assumes that pthread_equal()
257ed05efbfSdrh   ** is atomic - that it cannot be deceived into thinking self
258ed05efbfSdrh   ** and p->owner are equal if p->owner changes between two values
259ed05efbfSdrh   ** that are not equal to self while the comparison is taking place.
2605f3d6523Sdrh   ** This implementation also assumes a coherent cache - that
2615f3d6523Sdrh   ** separate processes cannot read different values from the same
2625f3d6523Sdrh   ** address at the same time.  If either of these two conditions
2635f3d6523Sdrh   ** are not met, then the mutexes will fail and problems will result.
264ed05efbfSdrh   */
265ed05efbfSdrh   {
266ed05efbfSdrh     pthread_t self = pthread_self();
267ed05efbfSdrh     if( p->nRef>0 && pthread_equal(p->owner, self) ){
268ed05efbfSdrh       p->nRef++;
269ed05efbfSdrh     }else{
270ed05efbfSdrh       pthread_mutex_lock(&p->mutex);
271ed05efbfSdrh       assert( p->nRef==0 );
272ed05efbfSdrh       p->owner = self;
273ed05efbfSdrh       p->nRef = 1;
274ed05efbfSdrh     }
275ed05efbfSdrh   }
2760167f285Sdrh #else
2770167f285Sdrh   /* Use the built-in recursive mutexes if they are available.
2780167f285Sdrh   */
2790167f285Sdrh   pthread_mutex_lock(&p->mutex);
28072339a3eSdrh #if SQLITE_MUTEX_NREF
28184612fecSdan   assert( p->nRef>0 || p->owner==0 );
2820167f285Sdrh   p->owner = pthread_self();
2830167f285Sdrh   p->nRef++;
284ed05efbfSdrh #endif
28572339a3eSdrh #endif
286ed05efbfSdrh 
287d0679edcSdrh #ifdef SQLITE_DEBUG
288d0679edcSdrh   if( p->trace ){
289d0679edcSdrh     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
290d0679edcSdrh   }
291d0679edcSdrh #endif
292437b9013Sdrh }
pthreadMutexTry(sqlite3_mutex * p)2936d2ab0e4Sdanielk1977 static int pthreadMutexTry(sqlite3_mutex *p){
294437b9013Sdrh   int rc;
2956d2ab0e4Sdanielk1977   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
296ed05efbfSdrh 
2970167f285Sdrh #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
298ed05efbfSdrh   /* If recursive mutexes are not available, then we have to grow
299ed05efbfSdrh   ** our own.  This implementation assumes that pthread_equal()
300ed05efbfSdrh   ** is atomic - that it cannot be deceived into thinking self
301ed05efbfSdrh   ** and p->owner are equal if p->owner changes between two values
302ed05efbfSdrh   ** that are not equal to self while the comparison is taking place.
3035f3d6523Sdrh   ** This implementation also assumes a coherent cache - that
3045f3d6523Sdrh   ** separate processes cannot read different values from the same
3055f3d6523Sdrh   ** address at the same time.  If either of these two conditions
3065f3d6523Sdrh   ** are not met, then the mutexes will fail and problems will result.
307ed05efbfSdrh   */
308ed05efbfSdrh   {
309ed05efbfSdrh     pthread_t self = pthread_self();
310ed05efbfSdrh     if( p->nRef>0 && pthread_equal(p->owner, self) ){
311ed05efbfSdrh       p->nRef++;
312ed05efbfSdrh       rc = SQLITE_OK;
3133bbc0e7bSdrh     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
314ed05efbfSdrh       assert( p->nRef==0 );
315ed05efbfSdrh       p->owner = self;
316ed05efbfSdrh       p->nRef = 1;
317ed05efbfSdrh       rc = SQLITE_OK;
318ed05efbfSdrh     }else{
319ed05efbfSdrh       rc = SQLITE_BUSY;
320ed05efbfSdrh     }
321ed05efbfSdrh   }
3220167f285Sdrh #else
3230167f285Sdrh   /* Use the built-in recursive mutexes if they are available.
3240167f285Sdrh   */
3250167f285Sdrh   if( pthread_mutex_trylock(&p->mutex)==0 ){
32672339a3eSdrh #if SQLITE_MUTEX_NREF
3270167f285Sdrh     p->owner = pthread_self();
3280167f285Sdrh     p->nRef++;
32972339a3eSdrh #endif
3300167f285Sdrh     rc = SQLITE_OK;
3310167f285Sdrh   }else{
3320167f285Sdrh     rc = SQLITE_BUSY;
3330167f285Sdrh   }
334ed05efbfSdrh #endif
335ed05efbfSdrh 
336ed05efbfSdrh #ifdef SQLITE_DEBUG
337ed05efbfSdrh   if( rc==SQLITE_OK && p->trace ){
338ed05efbfSdrh     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
339ed05efbfSdrh   }
340ed05efbfSdrh #endif
341437b9013Sdrh   return rc;
342437b9013Sdrh }
343437b9013Sdrh 
344437b9013Sdrh /*
345437b9013Sdrh ** The sqlite3_mutex_leave() routine exits a mutex that was
346437b9013Sdrh ** previously entered by the same thread.  The behavior
347437b9013Sdrh ** is undefined if the mutex is not currently entered or
348437b9013Sdrh ** is not currently allocated.  SQLite will never do either.
349437b9013Sdrh */
pthreadMutexLeave(sqlite3_mutex * p)3506d2ab0e4Sdanielk1977 static void pthreadMutexLeave(sqlite3_mutex *p){
3511a9ed0b2Sdanielk1977   assert( pthreadMutexHeld(p) );
35272339a3eSdrh #if SQLITE_MUTEX_NREF
353437b9013Sdrh   p->nRef--;
35484612fecSdan   if( p->nRef==0 ) p->owner = 0;
35572339a3eSdrh #endif
356437b9013Sdrh   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
357ed05efbfSdrh 
3580167f285Sdrh #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
359ed05efbfSdrh   if( p->nRef==0 ){
360ed05efbfSdrh     pthread_mutex_unlock(&p->mutex);
361ed05efbfSdrh   }
3620167f285Sdrh #else
3630167f285Sdrh   pthread_mutex_unlock(&p->mutex);
364ed05efbfSdrh #endif
365ed05efbfSdrh 
366d0679edcSdrh #ifdef SQLITE_DEBUG
367d0679edcSdrh   if( p->trace ){
368d0679edcSdrh     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
369d0679edcSdrh   }
370d0679edcSdrh #endif
371437b9013Sdrh }
372437b9013Sdrh 
sqlite3DefaultMutex(void)373558814f8Sdan sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
374558814f8Sdan   static const sqlite3_mutex_methods sMutex = {
3756d2ab0e4Sdanielk1977     pthreadMutexInit,
3764a9d1f66Sdanielk1977     pthreadMutexEnd,
3776d2ab0e4Sdanielk1977     pthreadMutexAlloc,
3786d2ab0e4Sdanielk1977     pthreadMutexFree,
3796d2ab0e4Sdanielk1977     pthreadMutexEnter,
3806d2ab0e4Sdanielk1977     pthreadMutexTry,
3816d2ab0e4Sdanielk1977     pthreadMutexLeave,
382a418980bSdrh #ifdef SQLITE_DEBUG
3836d2ab0e4Sdanielk1977     pthreadMutexHeld,
3846d2ab0e4Sdanielk1977     pthreadMutexNotheld
3851875f7a3Sdrh #else
3861875f7a3Sdrh     0,
3871875f7a3Sdrh     0
388a418980bSdrh #endif
3896d2ab0e4Sdanielk1977   };
3906d2ab0e4Sdanielk1977 
3916d2ab0e4Sdanielk1977   return &sMutex;
392437b9013Sdrh }
3936d2ab0e4Sdanielk1977 
394e4c88c0cSdrh #endif /* SQLITE_MUTEX_PTHREADS */
395