From: danielk1977 Date: Thu, 28 Aug 2008 08:31:48 +0000 (+0000) Subject: Fix a threads/mutex problem in pcache.c. (CVS 5630) X-Git-Tag: version-3.6.10~547 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51d2d03636c2ea65eb034172070bd43d7063211f;p=thirdparty%2Fsqlite.git Fix a threads/mutex problem in pcache.c. (CVS 5630) FossilOrigin-Name: 1928f15b78eee0fbf0a8ecdbbdd38dbbde2942b8 --- diff --git a/manifest b/manifest index ad116edebf..db5ca1a7dd 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Miscellaneous\scleanup\sin\sthe\snew\spcache\scode.\s(CVS\s5629) -D 2008-08-28T02:26:07 +C Fix\sa\sthreads/mutex\sproblem\sin\spcache.c.\s(CVS\s5630) +D 2008-08-28T08:31:48 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 689e14735f862a5553bceef206d8c13e29504e44 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -137,7 +137,7 @@ F src/os_win.c aefe9ee26430678a19a058a874e4e2bd91398142 F src/pager.c 032d11049af4ec49bdbaa3584e7ce9887098b66a F src/pager.h 914103bb62dbcc3d8e9f14baec812d027264d457 F src/parse.y d0f76d2cb8d6883d5600dc20beb961a6022b94b8 -F src/pcache.c aa609c9b1fc1a2b00f7d7bfb3d06b1e507767b93 +F src/pcache.c 8a2d92fb12ede40cfc8532f062763f353ba2868a F src/pcache.h bd373ee3e4db310d6bbe7fa6d8d971de9678edd8 F src/pragma.c f5b271b090af7fcedd308d7c5807a5503f7a853d F src/prepare.c c197041e0c4770672cda75e6bfe10242f885e510 @@ -450,7 +450,7 @@ F test/pragma2.test 5364893491b9231dd170e3459bfc2e2342658b47 F test/printf.test 262a5acd3158f788e9bdf7f18d718f3af32ff6ef F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 x F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc -F test/quick.test 4e702221947cf31a624f929502e55871bcb11dbc +F test/quick.test ab9399a71983f67113c04bab331edb59b950e2d6 F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6 F test/rdonly.test bd054831f8a3078e765a0657e247182486f0cb47 F test/reindex.test 44edd3966b474468b823d481eafef0c305022254 @@ -503,6 +503,7 @@ F test/temptable.test 19b851b9e3e64d91e9867619b2a3f5fffee6e125 F test/tester.tcl 12fd8394caeb71f7d961707da8668756389bc9d3 F test/thread001.test 3fb08080e1fe84d1bb7ec7bbc9e13743a77e5bc5 F test/thread002.test ed9b800460df01e3cf9428ee11dc4e3f04b9b896 +F test/thread003.test 6e612660f31c49f70bbe2e6c78af1eed54d89968 F test/thread1.test 776c9e459b75ba905193b351926ac4019b049f35 F test/thread2.test 6d7b30102d600f51b4055ee3a5a19228799049fb F test/thread_common.tcl 8a9d7a4500dfdbbd36679c977831b62c130b76b1 @@ -624,7 +625,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P ce9c74eaab459ddde213c828e821940f5d6cb354 -R 55c280ec697a3a6039a240cf488ad9ad -U drh -Z 7df830e688305e6be422e59df78b53fe +P da1777259f53c2e20c7ced06bf6f2a550f0ea0fc +R 825acac92c02335d51391b9bead07be1 +U danielk1977 +Z ccfb17e90f1b78b85072c08441535cfa diff --git a/manifest.uuid b/manifest.uuid index 609a5bfb32..88121b9c3d 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -da1777259f53c2e20c7ced06bf6f2a550f0ea0fc \ No newline at end of file +1928f15b78eee0fbf0a8ecdbbdd38dbbde2942b8 \ No newline at end of file diff --git a/src/pcache.c b/src/pcache.c index 50616bf88b..6a9c8b48ba 100644 --- a/src/pcache.c +++ b/src/pcache.c @@ -11,7 +11,7 @@ ************************************************************************* ** This file implements that page cache. ** -** @(#) $Id: pcache.c,v 1.19 2008/08/28 02:26:07 drh Exp $ +** @(#) $Id: pcache.c,v 1.20 2008/08/28 08:31:48 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -685,22 +685,23 @@ int sqlite3PcacheFetch( /* 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 ){ if( 0==(pPage->flags&PGHDR_DIRTY) ){ - pcacheEnterMutex(); pcacheRemoveFromLruList(pPage); - pcacheExitMutex(); pCache->nPinned++; } pCache->nRef++; } pPage->nRef++; *ppPage = pPage; + pcacheExitMutex(); return SQLITE_OK; } } + pcacheExitMutex(); } if( createFlag ){ diff --git a/test/quick.test b/test/quick.test index 7e90c32b9b..2310301c87 100644 --- a/test/quick.test +++ b/test/quick.test @@ -6,7 +6,7 @@ #*********************************************************************** # This file runs all tests. # -# $Id: quick.test,v 1.85 2008/08/27 18:56:36 drh Exp $ +# $Id: quick.test,v 1.86 2008/08/28 08:31:48 danielk1977 Exp $ proc lshift {lvar} { upvar $lvar l @@ -76,6 +76,7 @@ set EXCLUDE { tkt2686.test thread001.test thread002.test + thread003.test trans2.test vacuum3.test diff --git a/test/thread003.test b/test/thread003.test new file mode 100644 index 0000000000..22882b1c41 --- /dev/null +++ b/test/thread003.test @@ -0,0 +1,115 @@ +# 2007 September 10 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# 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 $ + +set testdir [file dirname $argv0] + +source $testdir/tester.tcl +source $testdir/thread_common.tcl +if {[info commands sqlthread] eq ""} { + finish_test + return +} + +# Set up a couple of different databases full of pseudo-randomly +# generated data. +# +do_test thread003.1.1 { + execsql { + BEGIN; + CREATE TABLE t1(a, b, c); + } + for {set ii 0} {$ii < 5000} {incr ii} { + execsql {INSERT INTO t1 VALUES($ii, randomblob(200), randomblob(200))} + } + execsql { + CREATE INDEX i1 ON t1(a, b); + COMMIT; + } +} {} +do_test thread003.1.2 { + expr {([file size test.db] / 1024) > 2000} +} {1} +do_test thread003.1.3 { + db close + file delete -force test2.db + sqlite3 db test2.db +} {} +do_test thread003.1.4 { + execsql { + BEGIN; + CREATE TABLE t1(a, b, c); + } + for {set ii 0} {$ii < 5000} {incr ii} { + execsql {INSERT INTO t1 VALUES($ii, randomblob(200), randomblob(200))} + } + execsql { + CREATE INDEX i1 ON t1(a, b); + COMMIT; + } +} {} +do_test thread003.1.5 { + expr {([file size test.db] / 1024) > 2000} +} {1} +do_test thread003.1.6 { + db close +} {} + + +# This test opens a connection on each of the large (>2MB) database files +# created by the previous block. The connections do not share a cache. +# Both "cache_size" parameters are set to 15, so there is a maximum of +# 30 pages available globally. +# +# Then, in separate threads, the databases are randomly queried over and +# over again. This will force the connections to recycle clean pages from +# each other. If there is a thread-safety problem, a segfault or assertion +# failure may eventually occur. +# +set nSecond 30 +puts "Starting thread003.2 (should run for ~$nSecond seconds)" +do_test thread003.2 { + 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 " + } + + 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} + +finish_test + +