]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Progress towards getting locking to work on windows. (CVS 1536)
authordrh <drh@noemail.net>
Mon, 7 Jun 2004 01:52:14 +0000 (01:52 +0000)
committerdrh <drh@noemail.net>
Mon, 7 Jun 2004 01:52:14 +0000 (01:52 +0000)
FossilOrigin-Name: 4f7c0961ad6cb7082bf7716f0c7ca16a8742c039

manifest
manifest.uuid
src/btree.c
src/os_common.h
src/os_win.c
src/printf.c
src/sqliteInt.h
test/attach.test

index b45e3a6b696dc8a0488e4dd4f92dc5cd398d5580..0fb31e989184ea2d80e8d987149c3106745ff704 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Performance\simprovements\sfor\sLIKE.\sIt\sis\sstill\stoo\sslow\sthough.\s(CVS\s1535)
-D 2004-06-06T12:41:50
+C Progress\stowards\sgetting\slocking\sto\swork\son\swindows.\s(CVS\s1536)
+D 2004-06-07T01:52:14
 F Makefile.in ab7b0d5118e2da97bac66be8684a1034e3500f5a
 F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
@@ -25,7 +25,7 @@ F sqlite.def fc4f5734786fe4743cfe2aa98eb2da4b089edb5f
 F sqlite.pc.in 30552343140c53304c2a658c080fbe810cd09ca2
 F src/attach.c c315c58cb16fd6e913b3bfa6412aedecb4567fa5
 F src/auth.c 5c2f0bea4729c98c2be3b69d6b466fc51448fe79
-F src/btree.c 5ee4cad91bdf4e2f0a5dba3a8750283a965d6d27
+F src/btree.c 3f0c22ab8c0c000ee5de4ad875836c111c4191c6
 F src/btree.h 589427ac13bb544d298cd99726e2572a6fe4bdaa
 F src/build.c e12e602f06e37a0fbcb49af17cba68ad85e101b6
 F src/date.c 8e6fa3173386fb29fdef012ee08a853c1e9908b2
@@ -40,23 +40,23 @@ F src/legacy.c ad23746f15f67e34577621b1875f639c94839e1f
 F src/main.c 2e4d37f0f8f31694b79823a530ea4b52c8e1a7fd
 F src/md5.c 4302e84ae516c616bb079c4e6d038c0addb33481
 F src/os.h 4e480eb92737ebcdd1e1136bdbf5cd22223bd1b4
-F src/os_common.h 744286a27de55c52f1b18921e8d17abbf7fafc0f
+F src/os_common.h 7b0f4ae0d9f66888f90ab28f126b42bfefe0bbd4
 F src/os_mac.c b823874690615ace0dd520d3ad1fe8bfd864b7e0
 F src/os_mac.h 51d2445f47e182ed32d3bd6937f81070c6fd9bd4
 F src/os_unix.c a4feb70b23fa5272f53cd2c74588484b54294800
 F src/os_unix.h 7999f2246c6347707e98f7078871ea8ca605df3f
-F src/os_win.c a5ab6da08ee90922c3e150afb814d8010383aa96
+F src/os_win.c 3c21eddf78e98d7be7dde1bf156d97e1d8ddd63d
 F src/os_win.h 004eec47b1780fcaf07420ddc2072294b698d48c
 F src/pager.c 944f6b071279887574081281f27bb2af88b42905
 F src/pager.h 0c7b5ac45c69e690c45d160d03bdc8fbd2d4657b
 F src/parse.y 27c1ce09f9d309be91f9e537df2fb00892990af4
 F src/pragma.c 1b58d852b84b36a8b84e2245dd29b63c377414ec
-F src/printf.c ef750e8e2398ca7e8b58be991075f08c6a7f0e53
+F src/printf.c 77ee9ec6dbf1b7512b17d63ccf8322ea9466278b
 F src/random.c eff68e3f257e05e81eae6c4d50a51eb88beb4ff3
 F src/select.c 02d711160100ef3a730060f7cfb5bc85fde06d72
 F src/shell.c 79af86d39b2149c7f16219fcbe636e7c2da9df8e
 F src/sqlite.h.in 4705697dd7213f322d59ffc69b48b8ac32b23373
-F src/sqliteInt.h 67b1265bb461899409de0d56a2a0c335f119ff36
+F src/sqliteInt.h 306256532411cc7b9019b82d3697c534a322e2a7
 F src/table.c af14284fa36c8d41f6829e3f2819dce07d3e2de2
 F src/tclsqlite.c 3db6b868bd844bfb71720c8e573f4c9b0d536bd5
 F src/test1.c 4a3cc1b628a29f24c0a43227a035d0f2a96eb634
@@ -78,7 +78,7 @@ F src/vdbeaux.c c47fd5433a965b5f06de2498a56401861ce5ecbe
 F src/vdbemem.c 5d029d83bc60eaf9c45837fcbc0b03348ec95d7a
 F src/where.c 444a7c3a8b1eb7bba072e489af628555d21d92a4
 F test/all.test 569a92a8ee88f5300c057cc4a8f50fbbc69a3242
-F test/attach.test 5f7e21fce56e3055de15a86c58fa3d8ab74f5243
+F test/attach.test 1635022d7e1d95dc92fe381cc62f9bf25cb29d73
 F test/attach2.test e98aab312722d05fc1837bf103baeebc582c64f8
 F test/attach3.test d384ac2e59f305743f73aec4b3d97b36fa5c6975
 F test/auth.test 95809b8f6a9bec18b94d28cafd03fe27d2f8a9e9
@@ -215,7 +215,7 @@ F www/support.tcl 1801397edd271cc39a2aadd54e701184b5181248
 F www/tclsqlite.tcl 19191cf2a1010eaeff74c51d83fd5f5a4d899075
 F www/vdbe.tcl 59288db1ac5c0616296b26dce071c36cb611dfe9
 F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4
-P 92337d8f79b9754cd61c73e7db2e792a1f482f50
-R fd9052eaad985197e4f082c9d9a0d480
-U danielk1977
-Z a1980072adc559c578b2d7aa47186c8d
+P 30b81507fc404355751705c6f9856c178249eff1
+R f549135a3da153baeae9af57b4d41e2b
+U drh
+Z 564efee24b8b93a5d228cb5bc9c253cb
index 1a189a258b81e7861e33d47c3970d4639a066824..e8d06e14dfe82b1d6f4be99b97f7965de271ab9c 100644 (file)
@@ -1 +1 @@
-30b81507fc404355751705c6f9856c178249eff1
\ No newline at end of file
+4f7c0961ad6cb7082bf7716f0c7ca16a8742c039
\ No newline at end of file
index 98beb7b1850f2dd1c985a7f8305d080899df9439..fa4fe86a15ca9a3bde3619d588932b5695b0e25c 100644 (file)
@@ -9,7 +9,7 @@
 **    May you share freely, never taking more than you give.
 **
 *************************************************************************
-** $Id: btree.c,v 1.159 2004/06/06 00:42:26 drh Exp $
+** $Id: btree.c,v 1.160 2004/06/07 01:52:14 drh Exp $
 **
 ** This file implements a external (disk-based) database using BTrees.
 ** For a detailed discussion of BTrees, refer to
@@ -2266,7 +2266,8 @@ int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
 ** enabled.
 */
 #if SQLITE_TEST
-# define TRACE(X)   if( sqlite3_btree_trace ){ printf X; fflush(stdout); }
+# define TRACE(X)   if( sqlite3_btree_trace )\
+                        { sqlite3DebugPrintf X; fflush(stdout); }
 #else
 # define TRACE(X)
 #endif
index 70d0dd9d9e454f7041893817cf04c89194ac9c6b..8aa4ecc2127bed6870f2afc3254ab0661e6737a0 100644 (file)
@@ -37,11 +37,11 @@ static unsigned int elapse;
 #define TIMER_START       g_start=hwtime()
 #define TIMER_END         elapse=hwtime()-g_start
 #define SEEK(X)           last_page=(X)
-#define TRACE1(X)         fprintf(stderr,X)
-#define TRACE2(X,Y)       fprintf(stderr,X,Y)
-#define TRACE3(X,Y,Z)     fprintf(stderr,X,Y,Z)
-#define TRACE4(X,Y,Z,A)   fprintf(stderr,X,Y,Z,A)
-#define TRACE5(X,Y,Z,A,B) fprintf(stderr,X,Y,Z,A,B)
+#define TRACE1(X)         sqlite3DebugPrintf(X)
+#define TRACE2(X,Y)       sqlite3DebugPrintf(X,Y)
+#define TRACE3(X,Y,Z)     sqlite3DebugPrintf(X,Y,Z)
+#define TRACE4(X,Y,Z,A)   sqlite3DebugPrintf(X,Y,Z,A)
+#define TRACE5(X,Y,Z,A,B) sqlite3DebugPrintf(X,Y,Z,A,B)
 #else
 #define TIMER_START
 #define TIMER_END
index 4e36f9127c92b7df1639b2222642a44bfd36b980..1134eeea2da29c772a6f24e2431b737f4cfe326c 100644 (file)
@@ -90,6 +90,7 @@ int sqlite3OsOpenReadWrite(
   }
   id->h = h;
   id->locktype = NO_LOCK;
+  id->sharedLockByte = 0;
   OpenCounter(+1);
   TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
   return SQLITE_OK;
@@ -132,6 +133,7 @@ int sqlite3OsOpenExclusive(const char *zFilename, OsFile *id, int delFlag){
   }
   id->h = h;
   id->locktype = NO_LOCK;
+  id->sharedLockByte = 0;
   OpenCounter(+1);
   TRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
   return SQLITE_OK;
@@ -158,6 +160,7 @@ int sqlite3OsOpenReadOnly(const char *zFilename, OsFile *id){
   }
   id->h = h;
   id->locktype = NO_LOCK;
+  id->sharedLockByte = 0;
   OpenCounter(+1);
   TRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
   return SQLITE_OK;
@@ -218,6 +221,7 @@ int sqlite3OsTempFileName(char *zBuf){
 ** Close a file.
 */
 int sqlite3OsClose(OsFile *id){
+  TRACE2("CLOSE %d\n", id->h);
   CloseHandle(id->h);
   OpenCounter(-1);
   return SQLITE_OK;
@@ -231,7 +235,7 @@ int sqlite3OsClose(OsFile *id){
 int sqlite3OsRead(OsFile *id, void *pBuf, int amt){
   DWORD got;
   SimulateIOError(SQLITE_IOERR);
-  TRACE2("READ %d\n", id->h);
+  TRACE3("READ %d lock=%d\n", id->h, id->locktype);
   if( !ReadFile(id->h, pBuf, amt, &got, 0) ){
     got = 0;
   }
@@ -250,7 +254,7 @@ int sqlite3OsWrite(OsFile *id, const void *pBuf, int amt){
   int rc;
   DWORD wrote;
   SimulateIOError(SQLITE_IOERR);
-  TRACE2("WRITE %d\n", id->h);
+  TRACE3("WRITE %d lock=%d\n", id->h, id->locktype);
   while( amt>0 && (rc = WriteFile(id->h, pBuf, amt, &wrote, 0))!=0 && wrote>0 ){
     amt -= wrote;
     pBuf = &((char*)pBuf)[wrote];
@@ -278,7 +282,7 @@ int sqlite3OsSeek(OsFile *id, off_t offset){
 ** Make sure all writes to a particular file are committed to disk.
 */
 int sqlite3OsSync(OsFile *id){
-  TRACE2("SYNC %d\n", id->h);
+  TRACE3("SYNC %d lock=%d\n", id->h, id->locktype);
   if( FlushFileBuffers(id->h) ){
     return SQLITE_OK;
   }else{
@@ -346,7 +350,7 @@ int sqlite3OsFileSize(OsFile *id, off_t *pSize){
 ** shared locks begins at SHARED_FIRST.  
 */
 #define SHARED_SIZE       10238
-#define SHARED_FIRST      (0xffffffff - SHARED_SIZE + 1)
+#define SHARED_FIRST      (0x3fffffff - (SHARED_SIZE - 1))
 #define RESERVED_BYTE     (SHARED_FIRST - 1)
 #define PENDING_BYTE      (RESERVED_BYTE - 1)
 
@@ -413,8 +417,11 @@ static int unlockReadLock(OsFile *id){
 int sqlite3OsLock(OsFile *id, int locktype){
   int rc = SQLITE_OK;    /* Return code from subroutines */
   int res = 1;           /* Result of a windows lock call */
+  int newLocktype;       /* Set id->locktype to this value before exiting */
+  int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
 
-  TRACE4("LOCK %d %d was %d\n", id->h, locktype, id->locktype);
+  TRACE5("LOCK %d %d was %d(%d)\n",
+          id->h, locktype, id->locktype, id->sharedLockByte);
 
   /* If there is already a lock of this type or more restrictive on the
   ** OsFile, do nothing. Don't use the end_lock: exit path, as
@@ -428,14 +435,18 @@ int sqlite3OsLock(OsFile *id, int locktype){
   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
   ** the PENDING_LOCK byte is temporary.
   */
-  if( id->locktype==NO_LOCK || locktype==PENDING_LOCK ){
+  if( id->locktype==NO_LOCK
+   || (locktype>=PENDING_LOCK && id->locktype<PENDING_LOCK)
+  ){
     int cnt = 4;
     while( cnt-->0 && (res = LockFile(id->h, PENDING_BYTE, 0, 1, 0))==0 ){
       /* Try 4 times to get the pending lock.  The pending lock might be
       ** held by another reader process who will release it momentarily.
       */
+      TRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
       Sleep(1);
     }
+    gotPendingLock = res;
   }
 
   /* Acquire a shared lock
@@ -449,40 +460,65 @@ int sqlite3OsLock(OsFile *id, int locktype){
       id->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
       res = LockFile(id->h, SHARED_FIRST+id->sharedLockByte, 0, 1, 0);
     }
-    if( locktype<PENDING_LOCK ){
-      UnlockFile(id->h, PENDING_BYTE, 0, 1, 0);
+    if( res ){
+      newLocktype = SHARED_LOCK;
     }
   }
 
   /* Acquire a RESERVED lock
   */
   if( locktype>=RESERVED_LOCK && id->locktype<RESERVED_LOCK && res ){
-    res = getReadLock(id->h, RESERVED_BYTE, 1);
+    res = LockFile(id->h, RESERVED_BYTE, 0, 1, 0);
+    if( res ){
+      newLocktype = RESERVED_LOCK;
+    }
+  }
+
+  /* Acquire a PENDING lock
+  */
+  if( locktype>=PENDING_LOCK && res ){
+    newLocktype = PENDING_LOCK;
+    gotPendingLock = 0;
   }
 
   /* Acquire an EXCLUSIVE lock
   */
-  if( locktype==EXCLUSIVE_LOCK ){
+  if( locktype==EXCLUSIVE_LOCK && res ){
     if( id->locktype>=SHARED_LOCK ){
       res = unlockReadLock(id);
+      TRACE2("unreadlock = %d\n", res);
     }
     if( res ){
       res = LockFile(id->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
     }else{
+      TRACE2("LOCK FAILED due to failure to unlock read %d\n", id->h);
       res = 0;
     }
+    if( res ){
+      newLocktype = EXCLUSIVE_LOCK;
+    }else{
+      TRACE2("error-code = %d\n", GetLastError());
+    }
+  }
+
+  /* If we are holding a PENDING lock that ought to be released, then
+  ** release it now.
+  */
+  if( gotPendingLock && (res==0 || locktype<PENDING_LOCK) ){
+    UnlockFile(id->h, PENDING_BYTE, 0, 1, 0);
   }
 
   /* Update the state of the lock has held in the file descriptor then
   ** return the appropriate result code.
   */
   if( res ){
-    id->locktype = locktype;
     rc = SQLITE_OK;
   }else{
-    TRACE2("LOCK FAILED %d\n", id->h);
+    TRACE4("LOCK FAILED %d trying for %d but got %d\n", id->h,
+           locktype, newLocktype);
     rc = SQLITE_BUSY;
   }
+  id->locktype = newLocktype;
   return rc;
 }
 
@@ -496,7 +532,7 @@ int sqlite3OsCheckWriteLock(OsFile *id){
   if( id->locktype>=RESERVED_LOCK ){
     rc = 1;
   }else{
-    rc = getReadLock(id->h, RESERVED_BYTE, 1);
+    rc = LockFile(id->h, RESERVED_BYTE, 0, 1, 0);
     if( rc ){
       UnlockFile(id->h, RESERVED_BYTE, 0, 1, 0);
     }
@@ -511,18 +547,19 @@ int sqlite3OsCheckWriteLock(OsFile *id){
 ** available on the host, then an SQLITE_NOLFS is returned.
 */
 int sqlite3OsUnlock(OsFile *id){
-  int rc;
-  TRACE3("UNLOCK %d was %d\n", id->h, id->locktype);
-  if( id->locktype>=EXCLUSIVE_LOCK ){
+  int rc, type;
+  TRACE4("UNLOCK %d was %d(%d)\n", id->h, id->locktype, id->sharedLockByte);
+  type = id->locktype;
+  if( type>=EXCLUSIVE_LOCK ){
     UnlockFile(id->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
   }
-  if( id->locktype>=PENDING_LOCK ){
+  if( type>=PENDING_LOCK ){
     UnlockFile(id->h, PENDING_BYTE, 0, 1, 0);
   }
-  if( id->locktype>=RESERVED_LOCK ){
+  if( type>=RESERVED_LOCK ){
     UnlockFile(id->h, RESERVED_BYTE, 0, 1, 0);
   }
-  if( id->locktype==SHARED_LOCK ){
+  if( type>=SHARED_LOCK && type<EXCLUSIVE_LOCK ){
     unlockReadLock(id);
   }
   id->locktype = NO_LOCK;
index 5db2ca37925b39b423f498502811869b039a46db..9cc625f663f9649886671b656654102b57de3c72 100644 (file)
@@ -798,6 +798,22 @@ char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
   return z;
 }
 
+#ifndef NDEBUG
+/*
+** A version of printf() that understands %lld.  Used for debugging.
+** The printf() built into some versions of windows does not understand %lld
+** and segfaults if you give it a long long int.
+*/
+void sqlite3DebugPrintf(const char *zFormat, ...){
+  va_list ap;
+  char zBuf[500];
+  va_start(ap, zFormat);
+  base_vprintf(0, 0, zBuf, sizeof(zBuf), zFormat, ap);
+  va_end(ap);
+  fprintf(stderr,"%s", zBuf);
+}
+#endif
+
 /*
 ** The following four routines implement the varargs versions of the
 ** sqlite3_exec() and sqlite3_get_table() interfaces.  See the sqlite.h
@@ -872,6 +888,3 @@ int sqlite3_get_table_vprintf(
   free(zSql);
   return rc;
 }
-
-
-
index 8d9815f6c488041871a7ffcf7fd85ef075486b30..ba9231158a71856cadc92f5908228fd23707628d 100644 (file)
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.269 2004/06/06 09:44:05 danielk1977 Exp $
+** @(#) $Id: sqliteInt.h,v 1.270 2004/06/07 01:52:14 drh Exp $
 */
 #include "config.h"
 #include "sqlite3.h"
@@ -1195,6 +1195,7 @@ void sqlite3RealToSortable(double r, char *);
 #endif
 char *sqlite3MPrintf(const char*, ...);
 char *sqlite3VMPrintf(const char*, va_list);
+void sqlite3DebugPrintf(const char*, ...);
 void sqlite3SetString(char **, const char *, ...);
 void sqlite3SetNString(char **, ...);
 void sqlite3ErrorMsg(Parse*, const char*, ...);
index e0f704b12b4683dfadffd2ac65b3cf741539dbb5..af3f529d6abb656732457e2d93d9682c46e90eb1 100644 (file)
@@ -12,7 +12,7 @@
 # focus of this script is testing the ATTACH and DETACH commands
 # and related functionality.
 #
-# $Id: attach.test,v 1.21 2004/05/31 18:21:55 drh Exp $
+# $Id: attach.test,v 1.22 2004/06/07 01:52:15 drh Exp $
 #
 
 set testdir [file dirname $argv0]
@@ -380,6 +380,8 @@ do_test attach-3.15 {
   execsql {SELECT * FROM t1}
 } {1 2 3 4}
 
+#set btree_trace 1
+#puts stderr "###################"; flush stderr
 # Ticket #323
 do_test attach-4.1 {
   execsql {DETACH db2}