From 72339a3e241dd15e20623ecb7fb94670ddee2afc Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 13 May 2010 20:19:17 +0000 Subject: [PATCH] Make debugging elements of the sqlite3_mutex object volatile and make them only appear when compiling with SQLITE_DEBUG. Ticket [51914f6acd2cb462]. FossilOrigin-Name: e823c60ca4c3d515b8b12dada4631fe8f44975e9 --- manifest | 20 ++++++++++---------- manifest.uuid | 2 +- src/mutex_unix.c | 32 +++++++++++++++++++++++++++----- src/mutex_w32.c | 16 +++++++++++----- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/manifest b/manifest index 55dc301c59..61dbec0e0c 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Allow\ssqlite3WalCheckpoint()\sto\sbe\scalled\seven\sif\sthe\sWAL\sis\sin\sthe\nSQLITE_SHM_READ\sstate,\sas\ssometimes\shappens\safter\san\serror. -D 2010-05-13T15:44:00 +C Make\sdebugging\selements\sof\sthe\ssqlite3_mutex\sobject\svolatile\sand\smake\sthem\nonly\sappear\swhen\scompiling\swith\sSQLITE_DEBUG.\s\sTicket\s[51914f6acd2cb462]. +D 2010-05-13T20:19:17 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in a5cad1f8f3e021356bfcc6c77dc16f6f1952bbc3 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -148,8 +148,8 @@ F src/mutex.c e2358d8f9a9021ab0bba4d511bb701e471e6e5f6 F src/mutex.h 6fde601e55fa6c3fae768783c439797ab84c87c6 F src/mutex_noop.c 10ae943d26ba86ea92319f72adbb501a89c563e0 F src/mutex_os2.c 20477db50cf3817c2f1cd3eb61e5c177e50231db -F src/mutex_unix.c 04a25238abce7e3d06b358dcf706e26624270809 -F src/mutex_w32.c 4cc201c1bfd11d1562810554ff5500e735559d7e +F src/mutex_unix.c becb8c4e07616abf84650d3687d62a1461d5d9cd +F src/mutex_w32.c fb1cf87c5a88b56c7df0d9ddb796ed9641046c3d F src/notify.c cbfa66a836da3a51567209636e6a94059c137930 F src/os.c c0a5dfce2a214dacb679425632d04f8a2021f364 F src/os.h 8a7e2456237ecf3a2e55b02f9fe6091f1ad36902 @@ -817,14 +817,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P e8c10d3cf601cc81a620f6c0ba5f195945a3f725 -R c616cb93d23b0cdfd59e536067b43dbd +P 175b296f9b9680f605537f52a8a53944deaa5391 +R d5299c39eacf8ef75cb22ef8e896c85b U drh -Z c6979d7b9231c984649ced9e5922d0be +Z c4de85f29400b70f3ccfc19d9b6ef1ea -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFL7B5DoxKgR168RlERAqIyAJ94od8cLks28tVsbhFMyAj89ZqJdwCfYpyj -EkxyMfbMhJhN1sSlNDztuWo= -=g0yE +iD8DBQFL7F7IoxKgR168RlERAl/rAKCJQmVtoXSuuKkoZ5WmLfYvUU8Y6QCePanm +qTdCHBH6Ih1hf+/7njgzXD0= +=TMY3 -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index cd71b30437..db34614c8e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -175b296f9b9680f605537f52a8a53944deaa5391 \ No newline at end of file +e823c60ca4c3d515b8b12dada4631fe8f44975e9 \ No newline at end of file diff --git a/src/mutex_unix.c b/src/mutex_unix.c index 402757fdf7..b1037c4bf6 100644 --- a/src/mutex_unix.c +++ b/src/mutex_unix.c @@ -24,23 +24,33 @@ #include +/* +** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields +** are necessary under two condidtions: (1) Debug builds and (2) using +** home-grown mutexes. Encapsulate these conditions into a single #define. +*/ +#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX) +# define SQLITE_MUTEX_NREF 1 +#else +# define SQLITE_MUTEX_NREF 0 +#endif /* ** Each recursive mutex is an instance of the following structure. */ struct sqlite3_mutex { pthread_mutex_t mutex; /* Mutex controlling the lock */ +#if SQLITE_MUTEX_NREF int id; /* Mutex type */ - int nRef; /* Number of entrances */ - pthread_t owner; /* Thread that is within this mutex */ -#ifdef SQLITE_DEBUG + volatile int nRef; /* Number of entrances */ + volatile pthread_t owner; /* Thread that is within this mutex */ int trace; /* True to trace changes */ #endif }; -#ifdef SQLITE_DEBUG +#if SQLITE_MUTEX_NREF #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 } #else -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 } +#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } #endif /* @@ -142,14 +152,18 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ pthread_mutex_init(&p->mutex, &recursiveAttr); pthread_mutexattr_destroy(&recursiveAttr); #endif +#if SQLITE_MUTEX_NREF p->id = iType; +#endif } break; } case SQLITE_MUTEX_FAST: { p = sqlite3MallocZero( sizeof(*p) ); if( p ){ +#if SQLITE_MUTEX_NREF p->id = iType; +#endif pthread_mutex_init(&p->mutex, 0); } break; @@ -158,7 +172,9 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){ assert( iType-2 >= 0 ); assert( iType-2 < ArraySize(staticMutexes) ); p = &staticMutexes[iType-2]; +#if SQLITE_MUTEX_NREF p->id = iType; +#endif break; } } @@ -218,9 +234,11 @@ static void pthreadMutexEnter(sqlite3_mutex *p){ /* Use the built-in recursive mutexes if they are available. */ pthread_mutex_lock(&p->mutex); +#if SQLITE_MUTEX_NREF p->owner = pthread_self(); p->nRef++; #endif +#endif #ifdef SQLITE_DEBUG if( p->trace ){ @@ -261,8 +279,10 @@ static int pthreadMutexTry(sqlite3_mutex *p){ /* Use the built-in recursive mutexes if they are available. */ if( pthread_mutex_trylock(&p->mutex)==0 ){ +#if SQLITE_MUTEX_NREF p->owner = pthread_self(); p->nRef++; +#endif rc = SQLITE_OK; }else{ rc = SQLITE_BUSY; @@ -285,7 +305,9 @@ static int pthreadMutexTry(sqlite3_mutex *p){ */ static void pthreadMutexLeave(sqlite3_mutex *p){ assert( pthreadMutexHeld(p) ); +#if SQLITE_MUTEX_NREF p->nRef--; +#endif assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX diff --git a/src/mutex_w32.c b/src/mutex_w32.c index 237d24eff8..7f0fd04e61 100644 --- a/src/mutex_w32.c +++ b/src/mutex_w32.c @@ -25,9 +25,9 @@ struct sqlite3_mutex { CRITICAL_SECTION mutex; /* Mutex controlling the lock */ int id; /* Mutex type */ - int nRef; /* Number of enterances */ - DWORD owner; /* Thread holding this mutex */ #ifdef SQLITE_DEBUG + volatile int nRef; /* Number of enterances */ + volatile DWORD owner; /* Thread holding this mutex */ int trace; /* True to trace changes */ #endif }; @@ -35,7 +35,7 @@ struct sqlite3_mutex { #ifdef SQLITE_DEBUG #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 } #else -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0 } +#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 } #endif /* @@ -191,7 +191,9 @@ static sqlite3_mutex *winMutexAlloc(int iType){ case SQLITE_MUTEX_RECURSIVE: { p = sqlite3MallocZero( sizeof(*p) ); if( p ){ +#ifdef SQLITE_DEBUG p->id = iType; +#endif InitializeCriticalSection(&p->mutex); } break; @@ -201,7 +203,9 @@ static sqlite3_mutex *winMutexAlloc(int iType){ assert( iType-2 >= 0 ); assert( iType-2 < ArraySize(winMutex_staticMutexes) ); p = &winMutex_staticMutexes[iType-2]; +#ifdef SQLITE_DEBUG p->id = iType; +#endif break; } } @@ -234,12 +238,14 @@ static void winMutexFree(sqlite3_mutex *p){ ** more than once, the behavior is undefined. */ static void winMutexEnter(sqlite3_mutex *p){ +#ifdef SQLITE_DEBUG DWORD tid = GetCurrentThreadId(); assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); +#endif EnterCriticalSection(&p->mutex); +#ifdef SQLITE_DEBUG p->owner = tid; p->nRef++; -#ifdef SQLITE_DEBUG if( p->trace ){ printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); } @@ -288,11 +294,11 @@ static int winMutexTry(sqlite3_mutex *p){ static void winMutexLeave(sqlite3_mutex *p){ #ifndef NDEBUG DWORD tid = GetCurrentThreadId(); -#endif assert( p->nRef>0 ); assert( p->owner==tid ); p->nRef--; assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); +#endif LeaveCriticalSection(&p->mutex); #ifdef SQLITE_DEBUG if( p->trace ){