Add the sqlite3_os_routine_set()/get() functions. (CVS 2818)

FossilOrigin-Name: c1ed79f594fb85009c2e9e5e281cbe66a9d2fa17
diff --git a/main.mk b/main.mk
index 133e93a..32ccb77 100644
--- a/main.mk
+++ b/main.mk
@@ -133,6 +133,7 @@
   $(TOP)/src/test4.c \
   $(TOP)/src/test5.c \
   $(TOP)/src/test6.c \
+  $(TOP)/src/test_async.c \
   $(TOP)/src/utf.c \
   $(TOP)/src/util.c \
   $(TOP)/src/vdbe.c \
diff --git a/manifest b/manifest
index 205fc55..d356e7c 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\smemory\sallocation\sproblems\son\sthe\sutf-16\sversions\sof\scollating\sfunction\ncontrol\sroutines.\s(CVS\s2817)
-D 2005-12-15T03:04:10
+C Add\sthe\ssqlite3_os_routine_set()/get()\sfunctions.\s(CVS\s2818)
+D 2005-12-15T10:11:31
 F Makefile.in e3c6b3a38d734d41574c04f2fc90d18de2b87102
 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092
 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -19,7 +19,7 @@
 F doc/report1.txt a031aaf37b185e4fa540223cb516d3bccec7eeac
 F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895
 F ltmain.sh f6b283068efa69f06eb8aa1fe4bddfdbdeb35826
-F main.mk 19f33320f3ce5fc000de87abafca07ef4b6dffb4
+F main.mk 1376cec5c19b087d00d679527cfab9fa6598c2c3
 F mkdll.sh 5ec23622515d5bf8969404e80cfb5e220ddf0512
 F mkopcodec.awk bd46ad001c98dfbab07b1713cb8e692fa0e5415d
 F mkopcodeh.awk 071dbba4eaf56c8d643baf4604a043af35683316
@@ -50,14 +50,14 @@
 F src/legacy.c 59757d857ab95fcbb0ac27692d3201e35f093dd7
 F src/main.c a12aa72335036bb22249418e222124abfc581b85
 F src/md5.c c5fdfa5c2593eaee2e32a5ce6c6927c986eaf217
-F src/os.c bdd3a2fd089777e7ad18b57c896f1141d5a0c1fd
-F src/os.h d5ae3f4c1c7731437b6cddec279b7c06f761c44e
+F src/os.c 7b4a002d9c9421580276db55d2329636a604e8ef
+F src/os.h df6babd4fd74bb659470c31fa5a27de31b1c474e
 F src/os_common.h d74a11728ad2444b6b695b94c28c06881f049e49
 F src/os_test.c 49833426101f99aee4bb5f6a44b7c4b2029fda1c
 F src/os_test.h 903c93554c23d88f34f667f1979e4a1cee792af3
-F src/os_unix.c f11eba69eea2ae2723f5ae1bdad8b1ef12e224b9
+F src/os_unix.c df6e4972099aa5263183009fb947083633236dd3
 F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e
-F src/os_win.c d962ac2dd0e482847e42b846d46cd044f97d1c32
+F src/os_win.c 2da77ddc03de8c51688cca648ced766b5dad8a6a
 F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b
 F src/pager.c 49f63a54b57164a70df0b1539141003fd27856c6
 F src/pager.h e7b41ce8e7b5f629d456708b7ad9a8c8ede37140
@@ -68,10 +68,10 @@
 F src/random.c ff5e9a8cad790e2a51cd4d2e7737dc8540e09d1d
 F src/select.c 2292b065bc6be61e01aad39a2e1b93e332fb7e57
 F src/shell.c 4872acee1d2a826c73c914961e469e563204b7f9
-F src/sqlite.h.in a52db91dfa4f90e8e42361f6c7824c09b4e101ad
-F src/sqliteInt.h babeb60d34dbaa95a0f1d336e65039ae7ade5aab
+F src/sqlite.h.in 9d587d1f78129f08f8923b544afb7774563125e1
+F src/sqliteInt.h 09f9addda8de78e2b632597840b0ec6bb70a7c99
 F src/table.c 486dcfce532685b53b5a2b5da8bba0ded6fb2316
-F src/tclsqlite.c c155bf5f9ce9ae8950037931f7f7efcc0d92d9c5
+F src/tclsqlite.c 5753ac020a9a6619190d8dc125cb00bb0bc61c96
 F src/test1.c d6924b182773b2ad3b22e435e4d3bfd5a846da9e
 F src/test2.c 36390cdfc70c08e5ee0b466d0654a117f398bbff
 F src/test3.c 7c97833e33496c2b69f4fe6b9882ac60a481da97
@@ -82,7 +82,7 @@
 F src/trigger.c 2925ba96d964d9b717e74006bf7e64b8a6b70d97
 F src/update.c ec8e540617b116725b5a55c8d6b4db8bc67fdd7d
 F src/utf.c d2360f55ecd666f3e472738191f8dae717b95e5e
-F src/util.c e525154652f7aecb8773cae55ada9f43024bb2c4
+F src/util.c 06ab728430ae55191076cdf4075be36f621a7183
 F src/vacuum.c fbfdd3967fd34e2f260fafed88dcbf3c10856b94
 F src/vdbe.c d09c185f4badac6c79f2a919cbf661e7b5618293
 F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13
@@ -93,7 +93,7 @@
 F src/vdbemem.c deba8d6e3727643924b210a8c531a496c2b8d386
 F src/where.c 269569f380ddc018518f67765fe2f0d3c8760e28
 F tclinstaller.tcl 046e3624671962dc50f0481d7c25b38ef803eb42
-F test/all.test 7f0988442ab811dfa41793b5b550f5828ce316f3
+F test/all.test 55706917a12cd616440d50c35323747b4a9f03c3
 F test/alter.test 9d6837a3d946b73df692b7cef2a7644d2e2f6bc6
 F test/alter2.test 60ba0a7057dc71ad630a1cc7c487104346849d50
 F test/alter3.test d4eecd8dbd008d0e66f1c201fa6dc2edca853c38
@@ -201,14 +201,14 @@
 F test/pragma.test 95ea907adf68459e1be6f310c9ae94d1d59c465b
 F test/printf.test 9e10c74e16bf889f8495ddb3d6f5f891e75ff1b7
 F test/progress.test 16496001da445e6534afb94562c286708316d82f x
-F test/quick.test 84138c0fcc959a5e7761625aa52d37d55e56b748
+F test/quick.test 4be1733beae790f4a61fb7c7827b396c95375d7e
 F test/quote.test 5891f2338980916cf7415484b4ce785294044adb
 F test/reindex.test 38b138abe36bf9a08c791ed44d9f76cd6b97b78b
 F test/rollback.test 94cd981ee3a627d9f6466f69dcf1f7dbfe695d7a
 F test/rowid.test 040a3bef06f970c45f5fcd14b2355f7f4d62f0cf
 F test/safety.test 907b64fee719554a3622853812af3886fddbbb4f
 F test/schema.test 8a2ae440fb15f5798a68059e8746402f3137be46
-F test/select1.test 20275797efe032a56e97687aa06ba65d7a0b9b2b
+F test/select1.test 386cc42da2d0fade5dfb25c85118009c0ef26637
 F test/select2.test f3c2678c3a9f3cf08ec4988a3845bda64be6d9e3
 F test/select3.test 8fece41cd8f2955131b3f973a7123bec60b6e65e
 F test/select4.test c239f516aa31f42f2ef7c6d7cd01105f08f934ca
@@ -327,7 +327,7 @@
 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
 F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
-P ab6241af29b2e9f5f094b83c13afebe44a8ad6bc
-R 12c15b60aaac20b8ef3a98bcc49cb762
-U drh
-Z 29b97667c2714272591c690c9c9d58c8
+P ad292e27336b8c5afc0acdf111944a456bd23c32
+R 079783b8421fc6a8eada9a4aa8fe00c7
+U danielk1977
+Z 21e315959d3bb7c348f1a6f926708fc0
diff --git a/manifest.uuid b/manifest.uuid
index 74c7e5e..88ecd50 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-ad292e27336b8c5afc0acdf111944a456bd23c32
\ No newline at end of file
+c1ed79f594fb85009c2e9e5e281cbe66a9d2fa17
\ No newline at end of file
diff --git a/src/os.c b/src/os.c
index 9703674..a1acd76 100644
--- a/src/os.c
+++ b/src/os.c
@@ -69,3 +69,42 @@
 int sqlite3OsCheckReservedLock(OsFile *id){
   return id->pMethod->xCheckReservedLock(id);
 }
+
+static void**getOsRoutinePtr(int eRoutine){
+  switch( eRoutine ){
+    case SQLITE_OS_ROUTINE_OPENREADWRITE:
+      return (void **)(&sqlite3Os.xOpenReadWrite);
+    case SQLITE_OS_ROUTINE_OPENREADONLY:
+      return (void **)(&sqlite3Os.xOpenReadOnly);
+    case SQLITE_OS_ROUTINE_OPENEXCLUSIVE:
+      return (void **)(&sqlite3Os.xOpenExclusive);
+    case SQLITE_OS_ROUTINE_DELETE:
+      return (void **)(&sqlite3Os.xDelete);
+    case SQLITE_OS_ROUTINE_FILEEXISTS:
+      return (void **)(&sqlite3Os.xFileExists);
+    case SQLITE_OS_ROUTINE_SYNCDIRECTORY:
+      return (void **)(&sqlite3Os.xSyncDirectory);
+    default:
+      assert(!"Illegal eRoutine value");
+  }
+  return 0;
+}
+
+void *sqlite3_os_routine_get(int eRoutine){
+  return *getOsRoutinePtr(eRoutine);
+}
+
+void *sqlite3_os_routine_set(int eRoutine, void *pRoutine){
+  void **ppRet = getOsRoutinePtr(eRoutine);
+  void *pRet = *ppRet;
+  *ppRet = pRoutine;
+  return pRet;
+}
+
+void sqlite3_os_enter_mutex(){
+  sqlite3Os.xEnterMutex();
+}
+void sqlite3_os_leave_mutex(){
+  sqlite3Os.xLeaveMutex();
+}
+
diff --git a/src/os.h b/src/os.h
index dc0e8d8..739c8a9 100644
--- a/src/os.h
+++ b/src/os.h
@@ -222,8 +222,30 @@
   int  (*xCurrentTime)(double*);
   void (*xEnterMutex)(void);
   void (*xLeaveMutex)(void);
+  void *(*xThreadSpecificData)(int);
 } sqlite3Os;
 
+
+/*
+** The semi-published API for setting and getting methods from the 
+** global sqlite3OsVtbl structure. Neither sqlite3_os_routine_XXX() function
+** is intriniscally thread-safe.
+**
+** External get/set access is only provided to the routines identified 
+** by the hash-defined SQLITE_OS_ROUTINE symbols.
+*/
+#define SQLITE_OS_ROUTINE_OPENREADWRITE   1
+#define SQLITE_OS_ROUTINE_OPENREADONLY    2
+#define SQLITE_OS_ROUTINE_OPENEXCLUSIVE   3
+#define SQLITE_OS_ROUTINE_DELETE          4
+#define SQLITE_OS_ROUTINE_FILEEXISTS      5
+#define SQLITE_OS_ROUTINE_SYNCDIRECTORY   6
+void *sqlite3_os_routine_get(int);
+void *sqlite3_os_routine_set(int, void *);
+
+void sqlite3_os_enter_mutex();
+void sqlite3_os_leave_mutex();
+
 /*
 ** Prototypes for routines found in os.c
 */
diff --git a/src/os_unix.c b/src/os_unix.c
index 349db3d..a7f1271 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -1587,6 +1587,66 @@
 }
 
 /*
+** This function is called automatically when a thread exists to delete
+** the threads SqliteTsd structure. 
+**
+** Because the SqliteTsd structure is required by higher level routines
+** such as sqliteMalloc() we use OsFree() and OsMalloc() directly to
+** allocate the thread specific data.
+*/
+static void deleteTsd(void *pTsd){
+  sqlite3OsFree(pTsd);
+}
+
+/* 
+** The first time this function is called from a specific thread, nByte 
+** bytes of data area are allocated and zeroed. A pointer to the new 
+** allocation is returned to the caller. 
+**
+** Each subsequent call to this function from the thread returns the same
+** pointer. The argument is ignored in this case.
+*/
+static void *unixThreadSpecificData(int nByte){
+#ifdef SQLITE_UNIX_THREADS
+  static pthread_key_t key;
+  static int keyInit = 0;
+  void *pTsd;
+
+  if( !keyInit ){
+    sqlite3Os.xEnterMutex();
+    if( !keyInit ){
+      int rc;
+      rc = pthread_key_create(&key, deleteTsd);
+      if( rc ){
+        return 0;
+      }
+      keyInit = 1;
+    }
+    sqlite3Os.xLeaveMutex();
+  }
+
+  pTsd = (SqliteTsd *)pthread_getspecific(key);
+  if( !pTsd ){
+    pTsd = sqlite3OsMalloc(sizeof(SqliteTsd));
+    if( pTsd ){
+      memset(pTsd, 0, sizeof(SqliteTsd));
+      pthread_setspecific(key, pTsd);
+    }
+  }
+  return pTsd;
+#else
+  static char tsd[sizeof(SqliteTsd)];
+  static isInit = 0;
+  assert( nByte==sizeof(SqliteTsd) );
+  if( !isInit ){
+    memset(tsd, 0, sizeof(SqliteTsd));
+    isInit = 1;
+  }
+  return (void *)tsd;
+#endif
+}
+
+/*
 ** The following variable, if set to a non-zero value, becomes the result
 ** returned from sqlite3Os.xCurrentTime().  This is used for testing.
 */
@@ -1644,6 +1704,7 @@
   unixCurrentTime,
   unixEnterMutex,
   unixLeaveMutex,
+  unixThreadSpecificData
 };
 
 
diff --git a/src/os_win.c b/src/os_win.c
index be561b7..bfa0af0 100644
--- a/src/os_win.c
+++ b/src/os_win.c
@@ -1020,6 +1020,21 @@
   return 0;
 }
 
+
+/*
+** Todo: This is a place-holder only
+*/
+static void *winThreadSpecificData(int nByte){
+  static char tsd[sizeof(SqliteTsd)];
+  static isInit = 0;
+  assert( nByte==sizeof(SqliteTsd) );
+  if( !isInit ){
+    memset(tsd, 0, sizeof(SqliteTsd));
+    isInit = 1;
+  }
+  return (void *)tsd;
+}
+
 /* Macro used to comment out routines that do not exists when there is
 ** no disk I/O
 */
@@ -1047,6 +1062,7 @@
   winCurrentTime,
   winEnterMutex,
   winLeaveMutex,
+  winThreadSpecificData
 };
 
 #endif /* OS_WIN */
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index 8c9e95a..27551e5 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -12,7 +12,7 @@
 ** This header file defines the interface that the SQLite library
 ** presents to client programs.
 **
-** @(#) $Id: sqlite.h.in,v 1.144 2005/12/12 06:53:05 danielk1977 Exp $
+** @(#) $Id: sqlite.h.in,v 1.145 2005/12/15 10:11:32 danielk1977 Exp $
 */
 #ifndef _SQLITE3_H_
 #define _SQLITE3_H_
@@ -1291,6 +1291,10 @@
 */
 void sqlite3_soft_heap_limit(int);
 
+
+int sqlite3_set_io_routine(int, void *);
+void *sqlite3_get_io_routine(int);
+
 /*
 ** Undo the hack that converts floating point types to integer for
 ** builds on processors without floating point support.
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index aa1bf3e..daf146d 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.435 2005/12/15 03:04:11 drh Exp $
+** @(#) $Id: sqliteInt.h,v 1.436 2005/12/15 10:11:32 danielk1977 Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -268,6 +268,7 @@
 */
 typedef struct SqliteTsd SqliteTsd;
 struct SqliteTsd {
+  int isInit;                     /* True if structure has been initialised */
   int mallocFailed;               /* True after a malloc() has failed */
 #ifndef SQLITE_OMIT_SOFTHEAPLIMIT
   unsigned int nSoftHeapLimit;    /* (uint)-1 for unlimited */
diff --git a/src/tclsqlite.c b/src/tclsqlite.c
index 3b58df7..af6c910 100644
--- a/src/tclsqlite.c
+++ b/src/tclsqlite.c
@@ -11,7 +11,7 @@
 *************************************************************************
 ** A TCL Interface to SQLite
 **
-** $Id: tclsqlite.c,v 1.137 2005/12/12 06:53:05 danielk1977 Exp $
+** $Id: tclsqlite.c,v 1.138 2005/12/15 10:11:32 danielk1977 Exp $
 */
 #ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */
 
@@ -2092,6 +2092,7 @@
     extern int Sqlitetest4_Init(Tcl_Interp*);
     extern int Sqlitetest5_Init(Tcl_Interp*);
     extern int Sqlitetest6_Init(Tcl_Interp*);
+    extern int Sqlitetestasync_Init(Tcl_Interp*);
     extern int Md5_Init(Tcl_Interp*);
     extern int Sqlitetestsse_Init(Tcl_Interp*);
 
@@ -2101,6 +2102,7 @@
     Sqlitetest4_Init(interp);
     Sqlitetest5_Init(interp);
     Sqlitetest6_Init(interp);
+    Sqlitetestasync_Init(interp);
     Md5_Init(interp);
 #ifdef SQLITE_SSE
     Sqlitetestsse_Init(interp);
diff --git a/src/util.c b/src/util.c
index 49b2c76..b91339f 100644
--- a/src/util.c
+++ b/src/util.c
@@ -14,9 +14,10 @@
 ** This file contains functions for allocating memory, comparing
 ** strings, and stuff like that.
 **
-** $Id: util.c,v 1.152 2005/12/12 06:53:05 danielk1977 Exp $
+** $Id: util.c,v 1.153 2005/12/15 10:11:32 danielk1977 Exp $
 */
 #include "sqliteInt.h"
+#include "os.h"
 #include <stdarg.h>
 #include <ctype.h>
 
@@ -107,6 +108,10 @@
 ** Begin code for memory allocation system test layer.
 **
 ** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro.
+**
+** SQLITE_MEMDEBUG==1    -> Fence-posting only (thread safe) 
+** SQLITE_MEMDEBUG==2    -> Fence-posting + linked list of allocations (not ts)
+** SQLITE_MEMDEBUG==3    -> Above + backtraces (not thread safe, req. glibc)
 */
 
 /* Figure out whether or not to store backtrace() information for each malloc.
@@ -114,7 +119,7 @@
 ** greater and glibc is in use. If we don't want to use backtrace(), then just
 ** define it as an empty macro and set the amount of space reserved to 0.
 */
-#if defined(__GLIBC__) && SQLITE_MEMDEBUG>1
+#if defined(__GLIBC__) && SQLITE_MEMDEBUG>2
   extern int backtrace(void **, int);
   #define TESTALLOC_STACKSIZE 128
   #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*))
@@ -302,6 +307,8 @@
   return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]);
 }
 
+
+#if SQLITE_MEMDEBUG>1
 /*
 ** The argument points to an Os level allocation. Link it into the threads list
 ** of allocations.
@@ -363,6 +370,11 @@
     ((void **)(pp[1]))[0] = p;
   }
 }
+#else
+#define linkAlloc(x)
+#define relinkAlloc(x)
+#define unlinkAlloc(x)
+#endif
 
 /*
 ** This function sets the result of the Tcl interpreter passed as an argument
@@ -1248,26 +1260,17 @@
 
 /*
 ** Return a pointer to the SqliteTsd associated with the calling thread.
-** TODO: Actually return thread-specific-data instead of this global pointer.
 */
 SqliteTsd *sqlite3Tsd(){
-  static SqliteTsd tsd = {
-    0                    /* mallocFailed flag */
-  #ifndef SQLITE_OMIT_SOFTHEAPLIMIT
-    , 0xFFFFFFFF         /* nSoftHeapLimit */
-    , 0                  /* nAlloc */
-  #endif
-  #ifndef NDEBUG
-    , 1                  /* mallocAllowed flag */
-  #endif
-  #ifdef SQLITE_MEMDEBUG
-    , 0
-    , 0
-    , 0
-    , 0
-  #endif
-  };
-  return &tsd;
+  SqliteTsd *pTsd = sqlite3Os.xThreadSpecificData(sizeof(SqliteTsd));
+  if( pTsd && !pTsd->isInit ){
+    pTsd->nSoftHeapLimit = 0xFFFFFFFF;
+#ifndef NDEBUG
+    pTsd->mallocAllowed = 1;
+#endif
+    pTsd->isInit = 1;
+  }
+  return pTsd;
 }
 
 /*
diff --git a/test/all.test b/test/all.test
index 2143b5f..95e16a6 100644
--- a/test/all.test
+++ b/test/all.test
@@ -10,7 +10,7 @@
 #***********************************************************************
 # This file runs all tests.
 #
-# $Id: all.test,v 1.30 2005/01/17 07:53:44 danielk1977 Exp $
+# $Id: all.test,v 1.31 2005/12/15 10:11:32 danielk1977 Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -49,6 +49,7 @@
 
 set EXCLUDE {
   all.test
+  async.test
   crash.test
   autovacuum_crash.test
   quick.test
diff --git a/test/quick.test b/test/quick.test
index 61bfcb0..6d81d4d 100644
--- a/test/quick.test
+++ b/test/quick.test
@@ -6,7 +6,7 @@
 #***********************************************************************
 # This file runs all tests.
 #
-# $Id: quick.test,v 1.37 2005/12/07 06:27:45 danielk1977 Exp $
+# $Id: quick.test,v 1.38 2005/12/15 10:11:32 danielk1977 Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -16,6 +16,7 @@
 
 set EXCLUDE {
   all.test
+  async.test
   btree2.test
   btree3.test
   btree4.test
diff --git a/test/select1.test b/test/select1.test
index 7445ed1..c51cbe1 100644
--- a/test/select1.test
+++ b/test/select1.test
@@ -11,7 +11,7 @@
 # This file implements regression tests for SQLite library.  The
 # focus of this file is testing the SELECT statement.
 #
-# $Id: select1.test,v 1.44 2005/12/09 14:25:12 danielk1977 Exp $
+# $Id: select1.test,v 1.45 2005/12/15 10:11:32 danielk1977 Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -23,6 +23,7 @@
   lappend v $msg
 } {1 {no such table: test1}}
 
+
 execsql {CREATE TABLE test1(f1 int, f2 int)}
 
 do_test select1-1.2 {
@@ -799,4 +800,5 @@
   } {x 1 x 3}
 } ;# ifcapable compound
 
+
 finish_test