From abddb0c8c4121e6ad338fa27128b4b32a4072bb9 Mon Sep 17 00:00:00 2001 From: drh Date: Mon, 20 Aug 2007 13:14:28 +0000 Subject: [PATCH] Bug fix in BTree shared cache locking code. (CVS 4246) FossilOrigin-Name: 399d3e755253a7b4604a62b9f171e0f1154134e2 --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/btree.c | 7 +++---- src/btreeInt.h | 17 ++++++++++++++--- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/manifest b/manifest index e7053aa023..50b04ea252 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Get\smain.c\sto\scompile\swhen\sHAVE_USLEEP\sis\s0.\s(CVS\s4245) -D 2007-08-20T11:12:41 +C Bug\sfix\sin\sBTree\sshared\scache\slocking\scode.\s(CVS\s4246) +D 2007-08-20T13:14:29 F Makefile.in 0c0e53720f658c7a551046442dd7afba0b72bfbe F Makefile.linux-gcc 65241babba6faf1152bf86574477baab19190499 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -68,9 +68,9 @@ F src/alter.c f0aac0060ae8102e58f210b44d35b53438d53173 F src/analyze.c a14237d869c6bea0846493b59317e4097e81a0b6 F src/attach.c a52225c75b107be8c5bc144a2b6d20201be3f8f8 F src/auth.c 5ea90bc93dfea46e9fe4bf531e14c7cd98219ecb -F src/btree.c f00ed30c442b4934d042af84e065498c542d21aa +F src/btree.c f8a04f35eb81360773899983f7c2008145e13935 F src/btree.h 91ee529d581c1473d8e6e15299acc3b8de1d0674 -F src/btreeInt.h 6329e955a7dadd8628d5866e2465721b5fd25ef2 +F src/btreeInt.h 9b4ca8999e52f713420e5f297dd86887a7a9820f F src/build.c add67be992307b4b11849a6611bfd3352aacde92 F src/callback.c 143436453bb93e831c9574fea0b9b9eb90e40ff3 F src/complete.c ea63834e798a0ab14159bdc6e6cabc3df21aa346 @@ -529,7 +529,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5 -P 41f2175b1ed7eccf271b687ee5c3ea262a0cd096 -R 3e86c1deeec6de8a6818d829abf2506d +P b27f022fb924709f1c5e4642d5d59cab942e826d +R ee810a3824b25c07a2182b7d0dedc3f5 U drh -Z e107c976a8aac7848166a53955d0d290 +Z 6c13724b198dff1360ddd718c375cb8a diff --git a/manifest.uuid b/manifest.uuid index f5ed8cca12..b9a8e486e1 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -b27f022fb924709f1c5e4642d5d59cab942e826d \ No newline at end of file +399d3e755253a7b4604a62b9f171e0f1154134e2 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 7a623321fd..d5710b71b4 100644 --- a/src/btree.c +++ b/src/btree.c @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.401 2007/08/17 16:50:38 danielk1977 Exp $ +** $Id: btree.c,v 1.402 2007/08/20 13:14:29 drh Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** See the header comment on "btreeInt.h" for additional information. @@ -1207,7 +1207,7 @@ int sqlite3BtreeOpen( #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) /* If the new Btree uses a sharable pBtShared, then link the new ** Btree into the list of all sharable Btrees for the same connection. - ** The list is kept in ascending order by pBtShared address. + ** The list is kept in ascending order by pBt address. */ if( p->sharable ){ int i; @@ -1220,7 +1220,7 @@ int sqlite3BtreeOpen( p->pPrev = 0; pSib->pPrev = p; }else{ - while( pSib->pNext && pSib->pNext->pBt>p->pBt ){ + while( pSib->pNext && pSib->pNext->pBtpBt ){ pSib = pSib->pNext; } p->pNext = pSib->pNext; @@ -1323,7 +1323,6 @@ int sqlite3BtreeClose(Btree *p){ ** Clean out and delete the BtShared object. */ assert( !pBt->pCursor ); - assert( pBt->nRef==0 ); sqlite3PagerClose(pBt->pPager); if( pBt->xFreeSchema && pBt->pSchema ){ pBt->xFreeSchema(pBt->pSchema); diff --git a/src/btreeInt.h b/src/btreeInt.h index 179b1f219b..343d1eb6e8 100644 --- a/src/btreeInt.h +++ b/src/btreeInt.h @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btreeInt.h,v 1.6 2007/08/17 01:14:38 drh Exp $ +** $Id: btreeInt.h,v 1.7 2007/08/20 13:14:29 drh Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** For a detailed discussion of BTrees, refer to @@ -314,6 +314,9 @@ struct MemPage { ** points to the same BtShared object. The database cache and the ** schema associated with the database file are all contained within ** the BtShared object. +** +** All fields in this structure are accessed under the sqlite3.pMutex +** mutex. */ struct Btree { sqlite3 *pSqlite; /* The database connection holding this btree */ @@ -346,6 +349,10 @@ struct Btree { ** private Btree object for the file and each of those Btrees points ** to this one BtShared object. BtShared.nRef is the number of ** connections currently sharing this database file. +** +** Fields in this structure are accessed under the BtShared.mutex +** mutex, except for nRef and pNext which are accessed under the +** global SQLITE_MUTEX_STATIC_MASTER mutex. */ struct BtShared { Pager *pPager; /* The page cache */ @@ -370,14 +377,14 @@ struct BtShared { int minLeaf; /* Minimum local payload in a LEAFDATA table */ BusyHandler *pBusyHandler; /* Callback for when there is lock contention */ u8 inTransaction; /* Transaction state */ - int nRef; /* Number of references to this structure */ int nTransaction; /* Number of open transactions (read + write) */ void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ #ifndef SQLITE_OMIT_SHARED_CACHE + int nRef; /* Number of references to this structure */ + BtShared *pNext; /* Next on a list of sharable BtShared structs */ sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */ BtLock *pLock; /* List of locks held on this shared-btree struct */ - BtShared *pNext; /* Next in ThreadData.pBtree linked list */ #endif }; @@ -408,6 +415,10 @@ struct CellInfo { ** When a single database file can shared by two more database connections, ** but cursors cannot be shared. Each cursor is associated with a ** particular database connection identified BtCursor.pBtree.pSqlite. +** +** The fields in this structure are accessed under the sqlite3.pMutex +** mutex, specifically the BtCurser.pBtree->pSqlite->pMutex mutex. +** The pNext and pPrev fields also require the BtShared.mutex mutex. */ struct BtCursor { Btree *pBtree; /* The Btree to which this cursor belongs */