-C Fix\sa\sthreads/mutex\sproblem\sin\spcache.c.\s(CVS\s5630)
-D 2008-08-28T08:31:48
+C Enable\sthe\sdisabled\sasserts\sadded\sby\s(5629).\sAdd\sextra\stests\sto\sthread003.test.\sAnd\sthe\srequired\smodifications\sto\spcache.c.\s(CVS\s5631)
+D 2008-08-28T10:21:17
F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
F Makefile.in 689e14735f862a5553bceef206d8c13e29504e44
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
F src/pager.c 032d11049af4ec49bdbaa3584e7ce9887098b66a
F src/pager.h 914103bb62dbcc3d8e9f14baec812d027264d457
F src/parse.y d0f76d2cb8d6883d5600dc20beb961a6022b94b8
-F src/pcache.c 8a2d92fb12ede40cfc8532f062763f353ba2868a
+F src/pcache.c 23dba1a9f262946c931e3f122bf3ebfe8a6f3013
F src/pcache.h bd373ee3e4db310d6bbe7fa6d8d971de9678edd8
F src/pragma.c f5b271b090af7fcedd308d7c5807a5503f7a853d
F src/prepare.c c197041e0c4770672cda75e6bfe10242f885e510
F test/tester.tcl 12fd8394caeb71f7d961707da8668756389bc9d3
F test/thread001.test 3fb08080e1fe84d1bb7ec7bbc9e13743a77e5bc5
F test/thread002.test ed9b800460df01e3cf9428ee11dc4e3f04b9b896
-F test/thread003.test 6e612660f31c49f70bbe2e6c78af1eed54d89968
+F test/thread003.test d78038bf0ec882b61bd5d05c4e77affb23f5033f
F test/thread1.test 776c9e459b75ba905193b351926ac4019b049f35
F test/thread2.test 6d7b30102d600f51b4055ee3a5a19228799049fb
F test/thread_common.tcl 8a9d7a4500dfdbbd36679c977831b62c130b76b1
F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P da1777259f53c2e20c7ced06bf6f2a550f0ea0fc
-R 825acac92c02335d51391b9bead07be1
+P 1928f15b78eee0fbf0a8ecdbbdd38dbbde2942b8
+R 166d531ec6ddd69fe2db8781062339d3
U danielk1977
-Z ccfb17e90f1b78b85072c08441535cfa
+Z 853ee09750f4e2c171d372d7dd09456a
*************************************************************************
** This file implements that page cache.
**
-** @(#) $Id: pcache.c,v 1.20 2008/08/28 08:31:48 danielk1977 Exp $
+** @(#) $Id: pcache.c,v 1.21 2008/08/28 10:21:17 danielk1977 Exp $
*/
#include "sqliteInt.h"
assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
}
return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
- return 1;
}
#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
** Remove a page from its hash table (PCache.apHash[]).
*/
static void pcacheRemoveFromHash(PgHdr *pPage){
- /* assert( pcacheMutexHeld() ); *** FIXME ****/
+ assert( pcacheMutexHeld() );
if( pPage->pPrevHash ){
pPage->pPrevHash->pNextHash = pPage->pNextHash;
}else{
static void pcacheAddToHash(PgHdr *pPage){
PCache *pCache = pPage->pCache;
u32 h = pPage->pgno % pCache->nHash;
- /* assert( pcacheMutexHeld() ); *** FIXME *****/
+ assert( pcacheMutexHeld() );
pPage->pNextHash = pCache->apHash[h];
pPage->pPrevHash = 0;
if( pCache->apHash[h] ){
static int pcacheResizeHash(PCache *pCache, int nHash){
PgHdr *p;
PgHdr **pNew;
- /* assert( pcacheMutexHeld() ); **** FIXME *****/
+ assert( pcacheMutexHeld() );
#ifdef SQLITE_MALLOC_SOFT_LIMIT
if( nHash*sizeof(PgHdr*)>SQLITE_MALLOC_SOFT_LIMIT ){
nHash = SQLITE_MALLOC_SOFT_LIMIT/sizeof(PgHdr *);
}
#endif
- pNew = (PgHdr **)sqlite3_malloc(sizeof(PgHdr*)*nHash);
+ pcacheExitMutex();
+ pNew = (PgHdr **)sqlite3Malloc(sizeof(PgHdr*)*nHash);
+ pcacheEnterMutex();
if( !pNew ){
return SQLITE_NOMEM;
}
static void pcacheRemoveFromList(PgHdr **ppHead, PgHdr *pPage){
int isDirtyList = (ppHead==&pPage->pCache->pDirty);
assert( ppHead==&pPage->pCache->pClean || ppHead==&pPage->pCache->pDirty );
- /* assert( pcacheMutexHeld() || ppHead!=&pPage->pCache->pClean ); *** FIXME */
+ assert( pcacheMutexHeld() || ppHead!=&pPage->pCache->pClean );
if( pPage->pPrev ){
pPage->pPrev->pNext = pPage->pNext;
int szExtra = pCache->szExtra;
assert( pcache.isInit );
- assert( sqlite3_mutex_notheld(pcache.mutex) );
+ assert( sqlite3_mutex_held(pcache.mutex) );
*ppPage = 0;
- pcacheEnterMutex();
/* If we have reached the limit for pinned/dirty pages, and there is at
** least one dirty page, invoke the xStress callback to cause a page to
p = pcachePageAlloc(pCache);
}
- pcacheExitMutex();
*ppPage = p;
return (p?SQLITE_OK:SQLITE_NOMEM);
}
int createFlag, /* If true, create page if it does not exist already */
PgHdr **ppPage /* Write the page here */
){
- PgHdr *pPage;
+ int rc = SQLITE_OK;
+ PgHdr *pPage = 0;
+
assert( pcache.isInit );
assert( pCache!=0 );
assert( pgno>0 );
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
+ pcacheEnterMutex();
+
/* Search the hash table for the requested page. Exit early if it is found. */
if( pCache->apHash ){
u32 h = pgno % pCache->nHash;
- pcacheEnterMutex();
for(pPage=pCache->apHash[h]; pPage; pPage=pPage->pNextHash){
if( pPage->pgno==pgno ){
if( pPage->nRef==0 ){
pCache->nRef++;
}
pPage->nRef++;
- *ppPage = pPage;
- pcacheExitMutex();
- return SQLITE_OK;
+ break;
}
}
- pcacheExitMutex();
}
- if( createFlag ){
- int rc = SQLITE_OK;
+ if( !pPage && createFlag ){
if( pCache->nHash<=pCache->nPage ){
rc = pcacheResizeHash(pCache, pCache->nHash<256 ? 256 : pCache->nHash*2);
- if( rc!=SQLITE_OK ){
- return rc;
- }
}
-
- rc = pcacheRecycleOrAlloc(pCache, ppPage);
- if( rc!=SQLITE_OK ){
- return rc;
+ if( rc==SQLITE_OK ){
+ rc = pcacheRecycleOrAlloc(pCache, &pPage);
+ }
+ if( rc==SQLITE_OK ){
+ pPage->pPager = 0;
+ pPage->flags = 0;
+ pPage->pDirty = 0;
+ pPage->pgno = pgno;
+ pPage->pCache = pCache;
+ pPage->nRef = 1;
+ pCache->nRef++;
+ pCache->nPinned++;
+ pcacheAddToList(&pCache->pClean, pPage);
+ pcacheAddToHash(pPage);
}
- pPage = *ppPage;
- pPage->pPager = 0;
- pPage->flags = 0;
- pPage->pDirty = 0;
- pPage->pgno = pgno;
- pPage->pCache = pCache;
- pPage->nRef = 1;
- pCache->nRef++;
- pCache->nPinned++;
- pcacheAddToList(&pCache->pClean, pPage);
- pcacheAddToHash(pPage);
- }else{
- *ppPage = 0;
}
+ pcacheExitMutex();
+
+ *ppPage = pPage;
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
- return SQLITE_OK;
+ assert( pPage || !createFlag || rc!=SQLITE_OK );
+ return rc;
}
/*
pCache = p->pCache;
pCache->nRef--;
pCache->nPinned--;
+ pcacheEnterMutex();
pcacheRemoveFromList(&pCache->pClean, p);
pcacheRemoveFromHash(p);
- pcacheEnterMutex();
pcachePageFree(p);
pcacheExitMutex();
}
assert( (p->flags & PGHDR_DIRTY)==0 );
assert( p->nRef>0 );
pCache = p->pCache;
+ pcacheEnterMutex();
pcacheRemoveFromList(&pCache->pClean, p);
pcacheAddToList(&pCache->pDirty, p);
+ pcacheExitMutex();
p->flags |= PGHDR_DIRTY;
}
-/*
-** Make sure the page is marked as clean. If it isn't clean already,
-** make it so.
-*/
-void sqlite3PcacheMakeClean(PgHdr *p){
- PCache *pCache;
- if( (p->flags & PGHDR_DIRTY)==0 ) return;
+void pcacheMakeClean(PgHdr *p){
+ PCache *pCache = p->pCache;
assert( p->apSave[0]==0 && p->apSave[1]==0 );
assert( p->flags & PGHDR_DIRTY );
- pCache = p->pCache;
pcacheRemoveFromList(&pCache->pDirty, p);
- pcacheEnterMutex();
pcacheAddToList(&pCache->pClean, p);
p->flags &= ~PGHDR_DIRTY;
if( p->nRef==0 ){
pCache->nPinned--;
}
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
- pcacheExitMutex();
+}
+
+/*
+** Make sure the page is marked as clean. If it isn't clean already,
+** make it so.
+*/
+void sqlite3PcacheMakeClean(PgHdr *p){
+ if( (p->flags & PGHDR_DIRTY) ){
+ pcacheEnterMutex();
+ pcacheMakeClean(p);
+ pcacheExitMutex();
+ }
}
/*
*/
void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
assert( p->nRef>0 );
+ pcacheEnterMutex();
pcacheRemoveFromHash(p);
p->pgno = newPgno;
if( newPgno==0 ){
p->flags |= PGHDR_REUSE_UNLIKELY;
- pcacheEnterMutex();
pcacheFree(p->apSave[0]);
pcacheFree(p->apSave[1]);
- pcacheExitMutex();
p->apSave[0] = 0;
p->apSave[1] = 0;
- sqlite3PcacheMakeClean(p);
+ if( (p->flags & PGHDR_DIRTY) ){
+ pcacheMakeClean(p);
+ }
}
pcacheAddToHash(p);
+ pcacheExitMutex();
}
/*
# This file contains tests that attempt to break the pcache module
# by bombarding it with simultaneous requests from multiple threads.
#
-# $Id: thread003.test,v 1.1 2008/08/28 08:31:48 danielk1977 Exp $
+# $Id: thread003.test,v 1.2 2008/08/28 10:21:17 danielk1977 Exp $
set testdir [file dirname $argv0]
expr 0
} {0}
+# This test is the same as the test above, except that each thread also
+# writes to the database. This causes pages to be moved back and forth
+# between the caches internal dirty and clean lists, which is another
+# opportunity for a thread-related bug to present itself.
+#
+set nSecond 30
+puts "Starting thread003.3 (should run for ~$nSecond seconds)"
+do_test thread003.3 {
+ foreach zFile {test.db test2.db} {
+ set SCRIPT [format {
+ set iStart [clock seconds]
+ set iEnd [expr {[clock seconds] + %d}]
+ set ::DB [sqlthread open %s]
+
+ # Set the cache size to 15 pages per cache. 30 available globally.
+ execsql { PRAGMA cache_size = 15 }
+
+ while {[clock seconds] < $iEnd} {
+ set iQuery [expr {int(rand()*5000)}]
+ execsql "SELECT * FROM t1 WHERE a = $iQuery"
+ execsql "UPDATE t1 SET b = randomblob(200)
+ WHERE a < $iQuery AND a > $iQuery + 20
+ "
+ }
+
+ sqlite3_close $::DB
+ expr 1
+ } $nSecond $zFile]
+
+ unset -nocomplain finished($zFile)
+ thread_spawn finished($zFile) $thread_procs $SCRIPT
+ }
+ foreach zFile {test.db test2.db} {
+ if {![info exists finished($zFile)]} {
+ vwait finished($zFile)
+ }
+ }
+ expr 0
+} {0}
+
+# In this test case, one thread is continually querying the database.
+# The other thread does not have a database connection, but calls
+# sqlite3_release_memory() over and over again.
+#
+set nSecond 30
+puts "Starting thread003.3 (should run for ~$nSecond seconds)"
+do_test thread003.4 {
+ thread_spawn finished(1) $thread_procs [format {
+ set iEnd [expr {[clock seconds] + %d}]
+ set ::DB [sqlthread open test.db]
+
+ # Set the cache size to 15 pages per cache. 30 available globally.
+ execsql { PRAGMA cache_size = 15 }
+
+ while {[clock seconds] < $iEnd} {
+ set iQuery [expr {int(rand()*5000)}]
+ execsql "SELECT * FROM t1 WHERE a = $iQuery"
+ }
+
+ sqlite3_close $::DB
+ expr 1
+ } $nSecond]
+ thread_spawn finished(2) [format {
+ set iEnd [expr {[clock seconds] + %d}]
+
+ while {[clock seconds] < $iEnd} {
+ sqlite3_release_memory 1000
+ }
+ } $nSecond]
+
+ foreach ii {1 2} {
+ if {![info exists finished($ii)]} {
+ vwait finished($ii)
+ }
+ }
+ expr 0
+} {0}
+
finish_test