]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Change the mutex interface to be pluggable. This is an incremental checkin, there...
authordanielk1977 <danielk1977@noemail.net>
Tue, 17 Jun 2008 17:21:18 +0000 (17:21 +0000)
committerdanielk1977 <danielk1977@noemail.net>
Tue, 17 Jun 2008 17:21:18 +0000 (17:21 +0000)
FossilOrigin-Name: 597283637bcdc2398bf249b2bbc6ded47ad2de22

manifest
manifest.uuid
src/mem2.c
src/mutex.c
src/mutex_unix.c
src/mutex_w32.c
src/sqlite.h.in
src/sqliteInt.h

index 06bfbcbf0d39992a6b732f016f6dec338e7b68d7..cff7a42fe9e4d5ec7712f4e69f2b94fc0d930f85 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sinternal\sinterfaces:\s\sPageMalloc/PageFree\sand\sScratchMalloc/ScratchFree.\s(CVS\s5226)
-D 2008-06-17T15:12:01
+C Change\sthe\smutex\sinterface\sto\sbe\spluggable.\sThis\sis\san\sincremental\scheckin,\sthere\sare\sstill\schanges\sto\scome.\s(CVS\s5227)
+D 2008-06-17T17:21:18
 F Makefile.arm-wince-mingw32ce-gcc ac5f7b2cef0cd850d6f755ba6ee4ab961b1fadf7
 F Makefile.in dc5608df93faf4406cfd7a1c8ed9ab93d8bfbfd5
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -119,15 +119,15 @@ F src/main.c 1f65b285a87f237e9e63c9628f220d10aef36b81
 F src/malloc.c 7b622d22f29e3be56d271a5a921f3b5788330189
 F src/md5.c 008216bbb5d34c6fbab5357aa68575ad8a31516a
 F src/mem1.c 159f10e280f2d9aea597cf938851e61652dd5c3d
-F src/mem2.c 809d0e311c38521779e92262f51bd847d12d0f4d
+F src/mem2.c 594270b8f0ed6c09ae5fb729eca2864139f9e293
 F src/mem3.c 617c2e2a72a10ecc5c01af14efc8c2596d2c2e87
 F src/mem4.c 45c328ec6dcb7e8d319cb383615b5fe547ca5409
 F src/mem5.c 3d2ff00c6e3bc37f5d82cd82e1a123b246fb7b39
-F src/mutex.c 20f6ec9e2d85e257cae9e2ecc40a6769fad9e9e7
+F src/mutex.c cbdaeeed05e3debdaf3ab4465ba32203cae44656
 F src/mutex.h b9b9baf7050f3bb1c723e1d22088a704783a2927
 F src/mutex_os2.c b8c1231319e966875f251a7ec137bea353546b87
-F src/mutex_unix.c 28588d08a2221623d66655e536880003fd5234d6
-F src/mutex_w32.c 36fbcf9f50bf0cd9be2c106cd69e5f59021402be
+F src/mutex_unix.c 74a6755912ad2a924026b54b4b5a64d327748aa5
+F src/mutex_w32.c e03baa369c5e8e7ea4dd2e5b3d098e047f7a2f06
 F src/os.c cf7e09d4215f35fc8269f0fa99f03fd2c2978658
 F src/os.h c9a7f94e80193fd4cf27f5c5698eb56753f1b05a
 F src/os_common.h 24525d8b7bce66c374dfc1810a6c9043f3359b60
@@ -143,9 +143,9 @@ F src/printf.c 8b063da9dcde26b7c500a01444b718d86f21bc6e
 F src/random.c 2b2db2de4ab491f5a14d3480466f8f4b5a5db74a
 F src/select.c 669687459e7d0193c89de06c5dbed55b4a41191c
 F src/shell.c a12ea645271b7876c8f080146f48e20b00d367ec
-F src/sqlite.h.in 1444cdd69bd9377fe33fd273a4554b75fb0b5ede
+F src/sqlite.h.in b0aacae57d08cd3909f999a0801f764287b1817f
 F src/sqlite3ext.h faacd0e6a81aabee0861c6d7883c9172e74ef5b3
-F src/sqliteInt.h e579f90f7d070f604f79bb85029c424d273cc20b
+F src/sqliteInt.h f465a6d45e9cabadad5c6c097d17df3c054616d0
 F src/sqliteLimit.h f435e728c6b620ef7312814d660a81f9356eb5c8
 F src/table.c 1fa8f8113ac9cbc09ae4801c6d2a7f0af82c5822
 F src/tclsqlite.c c57e740e30bd6dda678796eed62c7f0e64689834
@@ -594,7 +594,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P d49e5020019093a5b8a4fbe09caeb84ecb538629
-R 25fbb9097db5d9ffe1b1d6302c5755df
-U drh
-Z e3fe71a0b1fa8d45526d8e8aaa39339c
+P 3e797d0ffe1af6f7512b3fb7b387418ac05671e0
+R e1c01b11e23adfbe56a06238817523c5
+U danielk1977
+Z 7e1766078375e85380a4af4034e888e3
index 9ddef60bb463704aa96d4a61f8a38753354a8bda..e96ed556087fedf43ab674e72f57bfd990cb6022 100644 (file)
@@ -1 +1 @@
-3e797d0ffe1af6f7512b3fb7b387418ac05671e0
\ No newline at end of file
+597283637bcdc2398bf249b2bbc6ded47ad2de22
\ No newline at end of file
index 311f02dbdbd67be23f2e23b5d4377ff2665e2f40..4f97ba2cecc9298f44f1aaf80506a0f1c84f1b68 100644 (file)
@@ -19,7 +19,7 @@
 ** This file contains implementations of the low-level memory allocation
 ** routines specified in the sqlite3_mem_methods object.
 **
-** $Id: mem2.c,v 1.30 2008/06/17 15:12:01 drh Exp $
+** $Id: mem2.c,v 1.31 2008/06/17 17:21:18 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -321,7 +321,7 @@ void sqlite3MemSetDefault(void){
 
 /*
 ** Set the number of backtrace levels kept for each allocation.
-** A value of zero turns of backtracing.  The number is always rounded
+** A value of zero turns off backtracing.  The number is always rounded
 ** up to a multiple of 2.
 */
 void sqlite3MemdebugBacktrace(int depth){
index 23c0422f665f35a0b992bd5aeba136163973facf..3eded653bb67b0cc67b16527f4b15bbfa1f29084 100644 (file)
 ** implementation is suitable for testing.
 ** debugging purposes
 **
-** $Id: mutex.c,v 1.20 2008/06/15 02:51:48 drh Exp $
+** $Id: mutex.c,v 1.21 2008/06/17 17:21:18 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
+#ifndef SQLITE_MUTEX_NOOP
+/*
+** Initialize the mutex system.
+*/
+int sqlite3_mutex_init(void){ 
+  int rc;
+  if( !sqlite3Config.mutex.xMutexAlloc ){
+    sqlite3_mutex_methods *p = sqlite3DefaultMutex();
+    sqlite3_mutex *pMaster;
+
+    rc = p->xMutexInit();
+    if( rc==SQLITE_OK ){
+      pMaster = p->xMutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
+      p->xMutexEnter(pMaster);
+      assert( sqlite3Config.mutex.xMutexAlloc==0 
+           || sqlite3Config.mutex.xMutexAlloc==p->xMutexAlloc
+      );
+      if( !sqlite3Config.mutex.xMutexAlloc ){
+        sqlite3Config.mutex = *p;
+      }
+      p->xMutexLeave(pMaster);
+    }
+
+  }else{
+    rc = sqlite3Config.mutex.xMutexInit();
+  }
+
+  return rc;
+}
+
+/*
+** Shutdown the mutex system. This call frees resources allocated by
+** sqlite3_mutex_init().
+*/
+int sqlite3_mutex_end(void){
+  int rc = SQLITE_OK;
+  rc = sqlite3Config.mutex.xMutexEnd();
+  return rc;
+}
+
+/*
+** Retrieve a pointer to a static mutex or allocate a new dynamic one.
+*/
+sqlite3_mutex *sqlite3_mutex_alloc(int id){
+  return sqlite3Config.mutex.xMutexAlloc(id);
+}
+
+/*
+** Free a dynamic mutex.
+*/
+void sqlite3_mutex_free(sqlite3_mutex *p){
+  if( p ){
+    sqlite3Config.mutex.xMutexFree(p);
+  }
+}
+
+/*
+** Obtain the mutex p. If some other thread already has the mutex, block
+** until it can be obtained.
+*/
+void sqlite3_mutex_enter(sqlite3_mutex *p){
+  if( p ){
+    sqlite3Config.mutex.xMutexEnter(p);
+  }
+}
+
+/*
+** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
+** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
+*/
+int sqlite3_mutex_try(sqlite3_mutex *p){
+  int rc = SQLITE_OK;
+  if( p ){
+    return sqlite3Config.mutex.xMutexTry(p);
+  }
+  return rc;
+}
+
+/*
+** The sqlite3_mutex_leave() routine exits a mutex that was previously
+** entered by the same thread.  The behavior is undefined if the mutex 
+** is not currently entered. If a NULL pointer is passed as an argument
+** this function is a no-op.
+*/
+void sqlite3_mutex_leave(sqlite3_mutex *p){
+  if( p ){
+    sqlite3Config.mutex.xMutexLeave(p);
+  }
+}
+
+#ifndef NDEBUG
+/*
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
+** intended for use inside assert() statements.
+*/
+int sqlite3_mutex_held(sqlite3_mutex *p){
+  return p==0 || sqlite3Config.mutex.xMutexHeld(p);
+}
+int sqlite3_mutex_notheld(sqlite3_mutex *p){
+  return p==0 || sqlite3Config.mutex.xMutexNotheld(p);
+}
+#endif
+
+#endif
+
 #ifdef SQLITE_MUTEX_NOOP_DEBUG
 /*
 ** In this implementation, mutexes do not provide any mutual exclusion.
@@ -38,18 +143,29 @@ struct sqlite3_mutex {
   int cnt;    /* Number of entries without a matching leave */
 };
 
+/*
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
+** intended for use inside assert() statements.
+*/
+static int noopMutexHeld(sqlite3_mutex *p){
+  return p==0 || p->cnt>0;
+}
+static int noopMutexNotheld(sqlite3_mutex *p){
+  return p==0 || p->cnt==0;
+}
+
 /*
 ** Initialize and deinitialize the mutex subsystem.
 */
-int sqlite3_mutex_init(void){ return SQLITE_OK; }
-int sqlite3_mutex_end(void){ return SQLITE_OK; }
+static int noopMutexInit(void){ return SQLITE_OK; }
+static int noopMutexEnd(void){ return SQLITE_OK; }
 
 /*
 ** The sqlite3_mutex_alloc() routine allocates a new
 ** mutex and returns a pointer to it.  If it returns NULL
 ** that means that a mutex could not be allocated. 
 */
-sqlite3_mutex *sqlite3_mutex_alloc(int id){
+static sqlite3_mutex *noopMutexAlloc(int id){
   static sqlite3_mutex aStatic[6];
   sqlite3_mutex *pNew = 0;
   switch( id ){
@@ -76,8 +192,7 @@ sqlite3_mutex *sqlite3_mutex_alloc(int id){
 /*
 ** This routine deallocates a previously allocated mutex.
 */
-void sqlite3_mutex_free(sqlite3_mutex *p){
-  if( p==0 ) return;
+static void noopMutexFree(sqlite3_mutex *p){
   assert( p->cnt==0 );
   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   sqlite3_free(p);
@@ -94,17 +209,13 @@ void sqlite3_mutex_free(sqlite3_mutex *p){
 ** can enter.  If the same thread tries to enter any other kind of mutex
 ** more than once, the behavior is undefined.
 */
-void sqlite3_mutex_enter(sqlite3_mutex *p){
-  if( p ){
-    assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
-    p->cnt++;
-  }
+static void noopMutexEnter(sqlite3_mutex *p){
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || noopMutexNotheld(p) );
+  p->cnt++;
 }
-int sqlite3_mutex_try(sqlite3_mutex *p){
-  if( p ){
-    assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
-    p->cnt++;
-  }
+static int noopMutexTry(sqlite3_mutex *p){
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || noopMutexNotheld(p) );
+  p->cnt++;
   return SQLITE_OK;
 }
 
@@ -114,22 +225,26 @@ int sqlite3_mutex_try(sqlite3_mutex *p){
 ** is undefined if the mutex is not currently entered or
 ** is not currently allocated.  SQLite will never do either.
 */
-void sqlite3_mutex_leave(sqlite3_mutex *p){
-  if( p ){
-    assert( sqlite3_mutex_held(p) );
-    p->cnt--;
-    assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
-  }
+static void noopMutexLeave(sqlite3_mutex *p){
+  assert( sqlite3_mutex_held(p) );
+  p->cnt--;
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
 }
 
-/*
-** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
-** intended for use inside assert() statements.
-*/
-int sqlite3_mutex_held(sqlite3_mutex *p){
-  return p==0 || p->cnt>0;
-}
-int sqlite3_mutex_notheld(sqlite3_mutex *p){
-  return p==0 || p->cnt==0;
+sqlite3_mutex_methods *sqlite3DefaultMutex(void){
+  static sqlite3_mutex_methods sMutex = {
+    noopMutexInit,
+    noopMutexAlloc,
+    noopMutexFree,
+    noopMutexEnter,
+    noopMutexTry,
+    noopMutexLeave,
+    noopMutexEnd,
+
+    noopMutexHeld,
+    noopMutexNotheld
+  };
+
+  return &sMutex;
 }
 #endif /* SQLITE_MUTEX_NOOP_DEBUG */
index 4639a84277e272cecfcbba3920a98c9baea3c551..2ec52a1a98ec4d813facc2aff999b5158dbdf1cf 100644 (file)
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains the C functions that implement mutexes for pthreads
 **
-** $Id: mutex_unix.c,v 1.8 2008/06/13 18:24:27 drh Exp $
+** $Id: mutex_unix.c,v 1.9 2008/06/17 17:21:18 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -45,11 +45,36 @@ struct sqlite3_mutex {
 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 }
 #endif
 
+/*
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
+** intended for use only inside assert() statements.  On some platforms,
+** there might be race conditions that can cause these routines to
+** deliver incorrect results.  In particular, if pthread_equal() is
+** not an atomic operation, then these routines might delivery
+** incorrect results.  On most platforms, pthread_equal() is a 
+** comparison of two integers and is therefore atomic.  But we are
+** told that HPUX is not such a platform.  If so, then these routines
+** will not always work correctly on HPUX.
+**
+** On those platforms where pthread_equal() is not atomic, SQLite
+** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
+** make sure no assert() statements are evaluated and hence these
+** routines are never called.
+*/
+#ifndef NDEBUG
+static int pthreadMutexHeld(sqlite3_mutex *p){
+  return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
+}
+static int pthreadMutexNotheld(sqlite3_mutex *p){
+  return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
+}
+#endif
+
 /*
 ** Initialize and deinitialize the mutex subsystem.
 */
-int sqlite3_mutex_init(void){ return SQLITE_OK; }
-int sqlite3_mutex_end(void){ return SQLITE_OK; }
+static int pthreadMutexInit(void){ return SQLITE_OK; }
+static int pthreadMutexEnd(void){ return SQLITE_OK; }
 
 /*
 ** The sqlite3_mutex_alloc() routine allocates a new
@@ -92,7 +117,7 @@ int sqlite3_mutex_end(void){ return SQLITE_OK; }
 ** mutex types, the same mutex is returned on every call that has
 ** the same type number.
 */
-sqlite3_mutex *sqlite3_mutex_alloc(int iType){
+static sqlite3_mutex *pthreadMutexAlloc(int iType){
   static sqlite3_mutex staticMutexes[] = {
     SQLITE3_MUTEX_INITIALIZER,
     SQLITE3_MUTEX_INITIALIZER,
@@ -147,13 +172,11 @@ sqlite3_mutex *sqlite3_mutex_alloc(int iType){
 ** allocated mutex.  SQLite is careful to deallocate every
 ** mutex that it allocates.
 */
-void sqlite3_mutex_free(sqlite3_mutex *p){
-  if( p ){
-    assert( p->nRef==0 );
-    assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
-    pthread_mutex_destroy(&p->mutex);
-    sqlite3_free(p);
-  }
+static void pthreadMutexFree(sqlite3_mutex *p){
+  assert( p->nRef==0 );
+  assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
+  pthread_mutex_destroy(&p->mutex);
+  sqlite3_free(p);
 }
 
 /*
@@ -167,9 +190,8 @@ void sqlite3_mutex_free(sqlite3_mutex *p){
 ** can enter.  If the same thread tries to enter any other kind of mutex
 ** more than once, the behavior is undefined.
 */
-void sqlite3_mutex_enter(sqlite3_mutex *p){
-  if( p==0 ) return;
-  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+static void pthreadMutexEnter(sqlite3_mutex *p){
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
 
 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   /* If recursive mutexes are not available, then we have to grow
@@ -207,10 +229,9 @@ void sqlite3_mutex_enter(sqlite3_mutex *p){
   }
 #endif
 }
-int sqlite3_mutex_try(sqlite3_mutex *p){
+static int pthreadMutexTry(sqlite3_mutex *p){
   int rc;
-  if( p==0 ) return SQLITE_OK;
-  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
 
 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   /* If recursive mutexes are not available, then we have to grow
@@ -263,8 +284,7 @@ int sqlite3_mutex_try(sqlite3_mutex *p){
 ** is undefined if the mutex is not currently entered or
 ** is not currently allocated.  SQLite will never do either.
 */
-void sqlite3_mutex_leave(sqlite3_mutex *p){
-  if( p==0 ) return;
+static void pthreadMutexLeave(sqlite3_mutex *p){
   assert( sqlite3_mutex_held(p) );
   p->nRef--;
   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
@@ -284,28 +304,21 @@ void sqlite3_mutex_leave(sqlite3_mutex *p){
 #endif
 }
 
-/*
-** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
-** intended for use only inside assert() statements.  On some platforms,
-** there might be race conditions that can cause these routines to
-** deliver incorrect results.  In particular, if pthread_equal() is
-** not an atomic operation, then these routines might delivery
-** incorrect results.  On most platforms, pthread_equal() is a 
-** comparison of two integers and is therefore atomic.  But we are
-** told that HPUX is not such a platform.  If so, then these routines
-** will not always work correctly on HPUX.
-**
-** On those platforms where pthread_equal() is not atomic, SQLite
-** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
-** make sure no assert() statements are evaluated and hence these
-** routines are never called.
-*/
-#ifndef NDEBUG
-int sqlite3_mutex_held(sqlite3_mutex *p){
-  return p==0 || (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
-}
-int sqlite3_mutex_notheld(sqlite3_mutex *p){
-  return p==0 || p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
+sqlite3_mutex_methods *sqlite3DefaultMutex(void){
+  static sqlite3_mutex_methods sMutex = {
+    pthreadMutexInit,
+    pthreadMutexAlloc,
+    pthreadMutexFree,
+    pthreadMutexEnter,
+    pthreadMutexTry,
+    pthreadMutexLeave,
+    pthreadMutexEnd,
+
+    pthreadMutexHeld,
+    pthreadMutexNotheld
+  };
+
+  return &sMutex;
 }
-#endif
+
 #endif /* SQLITE_MUTEX_PTHREAD */
index fcb468aec5be9155688d01c9487dfcda2ce93a8c..fa3f86a085166a82dd6c400e573b858d6ced1a28 100644 (file)
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains the C functions that implement mutexes for win32
 **
-** $Id: mutex_w32.c,v 1.7 2008/06/13 18:24:27 drh Exp $
+** $Id: mutex_w32.c,v 1.8 2008/06/17 17:21:18 danielk1977 Exp $
 */
 #include "sqliteInt.h"
 
@@ -57,12 +57,23 @@ struct sqlite3_mutex {
   }
 #endif /* OS_WINCE */
 
+/*
+** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
+** intended for use only inside assert() statements.
+*/
+static int winMutexHeld(sqlite3_mutex *p){
+  return p->nRef!=0 && p->owner==GetCurrentThreadId();
+}
+static int winMutexNotheld(sqlite3_mutex *p){
+  return p->nRef==0 || p->owner!=GetCurrentThreadId();
+}
+
 
 /*
 ** Initialize and deinitialize the mutex subsystem.
 */
-int sqlite3_mutex_init(void){ return SQLITE_OK; }
-int sqlite3_mutex_end(void){ return SQLITE_OK; }
+static int winMutexInit(void){ return SQLITE_OK; }
+static int winMutexEnd(void){ return SQLITE_OK; }
 
 /*
 ** The sqlite3_mutex_alloc() routine allocates a new
@@ -103,7 +114,7 @@ int sqlite3_mutex_end(void){ return SQLITE_OK; }
 ** mutex types, the same mutex is returned on every call that has
 ** the same type number.
 */
-sqlite3_mutex *sqlite3_mutex_alloc(int iType){
+static sqlite3_mutex *winMutexAlloc(int iType){
   sqlite3_mutex *p;
 
   switch( iType ){
@@ -147,7 +158,7 @@ sqlite3_mutex *sqlite3_mutex_alloc(int iType){
 ** allocated mutex.  SQLite is careful to deallocate every
 ** mutex that it allocates.
 */
-void sqlite3_mutex_free(sqlite3_mutex *p){
+static void winMutexFree(sqlite3_mutex *p){
   assert( p );
   assert( p->nRef==0 );
   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
@@ -166,17 +177,15 @@ void sqlite3_mutex_free(sqlite3_mutex *p){
 ** can enter.  If the same thread tries to enter any other kind of mutex
 ** more than once, the behavior is undefined.
 */
-void sqlite3_mutex_enter(sqlite3_mutex *p){
-  if( p==0 ) return;
-  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+static void winMutexEnter(sqlite3_mutex *p){
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld(p) );
   EnterCriticalSection(&p->mutex);
   p->owner = GetCurrentThreadId(); 
   p->nRef++;
 }
-int sqlite3_mutex_try(sqlite3_mutex *p){
+static int winMutexTry(sqlite3_mutex *p){
   int rc = SQLITE_BUSY;
-  if( p==0 ) return SQLITE_OK;
-  assert( p->id==SQLITE_MUTEX_RECURSIVE || sqlite3_mutex_notheld(p) );
+  assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld(p) );
   /*
   ** The sqlite3_mutex_try() routine is very rarely used, and when it
   ** is used it is merely an optimization.  So it is OK for it to always
@@ -204,8 +213,7 @@ int sqlite3_mutex_try(sqlite3_mutex *p){
 ** is undefined if the mutex is not currently entered or
 ** is not currently allocated.  SQLite will never do either.
 */
-void sqlite3_mutex_leave(sqlite3_mutex *p){
-  if( p==0 ) return;
+static void winMutexLeave(sqlite3_mutex *p){
   assert( p->nRef>0 );
   assert( p->owner==GetCurrentThreadId() );
   p->nRef--;
@@ -213,14 +221,21 @@ void sqlite3_mutex_leave(sqlite3_mutex *p){
   LeaveCriticalSection(&p->mutex);
 }
 
-/*
-** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
-** intended for use only inside assert() statements.
-*/
-int sqlite3_mutex_held(sqlite3_mutex *p){
-  return p==0 || (p->nRef!=0 && p->owner==GetCurrentThreadId());
-}
-int sqlite3_mutex_notheld(sqlite3_mutex *p){
-  return p==0 || p->nRef==0 || p->owner!=GetCurrentThreadId();
+sqlite3_mutex_methods *sqlite3DefaultMutex(void){
+  static sqlite3_mutex_methods sMutex = {
+    winMutexInit,
+    winMutexAlloc,
+    winMutexFree,
+    winMutexEnter,
+    winMutexTry,
+    winMutexLeave,
+    winMutexEnd,
+
+    winMutexHeld,
+    winMutexNotheld
+  };
+
+  return &sMutex;
 }
 #endif /* SQLITE_MUTEX_W32 */
+
index 52e7f55376e421ae869854610be33209b7c776a1..592b1e17490976c3c250f437e0b8a6b07e7c47ab 100644 (file)
@@ -30,7 +30,7 @@
 ** the version number) and changes its name to "sqlite3.h" as
 ** part of the build process.
 **
-** @(#) $Id: sqlite.h.in,v 1.329 2008/06/17 15:12:01 drh Exp $
+** @(#) $Id: sqlite.h.in,v 1.330 2008/06/17 17:21:18 danielk1977 Exp $
 */
 #ifndef _SQLITE3_H_
 #define _SQLITE3_H_
@@ -5784,6 +5784,20 @@ int sqlite3_mutex_try(sqlite3_mutex*);
 void sqlite3_mutex_leave(sqlite3_mutex*);
 int sqlite3_mutex_end(void);
 
+typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
+struct sqlite3_mutex_methods {
+  int (*xMutexInit)(void);
+  sqlite3_mutex *(*xMutexAlloc)(int);
+  void (*xMutexFree)(sqlite3_mutex *);
+  void (*xMutexEnter)(sqlite3_mutex *);
+  int (*xMutexTry)(sqlite3_mutex *);
+  void (*xMutexLeave)(sqlite3_mutex *);
+  int (*xMutexEnd)(void);
+  int (*xMutexHeld)(sqlite3_mutex *);
+  int (*xMutexNotheld)(sqlite3_mutex *);
+};
+
+
 /*
 ** CAPI3REF: Mutex Verifcation Routines {F17080}
 **
index e0b9c4c05a5f847f026d5de15ecfecccaf7c89c2..8513074bab0a192d00096d16e84716a1699e1f2f 100644 (file)
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.711 2008/06/17 15:12:01 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.712 2008/06/17 17:21:18 danielk1977 Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -1739,6 +1739,7 @@ struct Sqlite3Config {
   int bCoreMutex;                   /* True to enable core mutexing */
   int bFullMutex;                   /* True to enable full mutexing */
   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
+  sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
   void *pHeap;                      /* Heap storage space */
   sqlite3_int64 nHeap;              /* Size of pHeap[] */
   int mnReq, mxReq;                 /* Min and max memory request sizes */
@@ -1797,6 +1798,8 @@ void *sqlite3PageMalloc(int);
 void sqlite3PageFree(void*);
 void sqlite3MemSetDefault(void);
 
+sqlite3_mutex_methods *sqlite3DefaultMutex(void);
+
 int sqlite3IsNaN(double);
 
 char *sqlite3MPrintf(sqlite3*,const char*, ...);