From 9e2d64b8d38b012e653ceee5d245de4555a3b552 Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 26 Sep 2008 20:02:50 +0000 Subject: [PATCH] Make sure the queueMutex is held prior to writing the pQueueLast field of the write queue in the async demonstration code. Ticket #3405. (CVS 5744) FossilOrigin-Name: 5622a1e285fc4d5720f7180a0eb551952f2df331 --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/test_async.c | 27 ++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index d52ce8976e..910fb3ee23 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Performance\senhancement:\savoid\scalling\sreparentChildPages()\sfrom\sbalance_nonroot().\s(CVS\s5743) -D 2008-09-26T17:31:55 +C Make\ssure\sthe\squeueMutex\sis\sheld\sprior\sto\swriting\sthe\spQueueLast\sfield\nof\sthe\swrite\squeue\sin\sthe\sasync\sdemonstration\scode.\s\sTicket\s#3405.\s(CVS\s5744) +D 2008-09-26T20:02:50 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in e4ab842f9a64ef61d57093539a8aab76b12810db F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -165,7 +165,7 @@ F src/test6.c 0a0304a69cfa4962a429d084c6d451ff9e4fb572 F src/test7.c 475b1fa7e3275408b40a3cbdc9508cbdc41ffa02 F src/test8.c 3637439424d0d21ff2dcf9b015c30fcc1e7bcb24 F src/test9.c 904ebe0ed1472d6bad17a81e2ecbfc20017dc237 -F src/test_async.c de6661370d96f660e0f0d604582c193c6144b19c +F src/test_async.c 45024094ed7cf780c5d5dccda645145f95cf78ef F src/test_autoext.c f53b0cdf7bf5f08100009572a5d65cdb540bd0ad F src/test_btree.c 8d5b835054f1dd15992e09864a8bc04386bab701 F src/test_config.c db72e95bafdd53c05ceb8735f833cc5dc1f48782 @@ -637,7 +637,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P 5debf12fa46520946ac5da44c03448fffbc9940c -R ad8baa95733fbb9a312a15654e608ce1 -U danielk1977 -Z 410e2e4a76b6dc0451fd1f6a24aebc75 +P 28fd0a50ca8529892f5b1ababd38d494889eed6d +R 42f79e8d6bad8dc05f28b70884a1cf00 +U drh +Z c1f39a8806852672a67f740f0e33cd9d diff --git a/manifest.uuid b/manifest.uuid index 2f22cb8c72..1f441bd728 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -28fd0a50ca8529892f5b1ababd38d494889eed6d \ No newline at end of file +5622a1e285fc4d5720f7180a0eb551952f2df331 \ No newline at end of file diff --git a/src/test_async.c b/src/test_async.c index 77da3149c0..44e445ea1f 100644 --- a/src/test_async.c +++ b/src/test_async.c @@ -10,7 +10,7 @@ ** ************************************************************************* ** -** $Id: test_async.c,v 1.47 2008/09/15 15:49:34 danielk1977 Exp $ +** $Id: test_async.c,v 1.48 2008/09/26 20:02:50 drh Exp $ ** ** This file contains an example implementation of an asynchronous IO ** backend for SQLite. @@ -571,12 +571,31 @@ static int async_cond_wait(pthread_cond_t *pCond, pthread_mutex_t *pMutex){ return rc; } +/* +** Assert that the mutex is held by the current thread. +*/ +static void assert_mutex_is_held(pthread_mutex_t *pMutex){ + int iIdx; + pthread_mutex_t *aMutex = (pthread_mutex_t *)(&async); + pthread_t *aHolder = (pthread_t *)(&asyncdebug); + + for(iIdx=0; iIdx<3; iIdx++){ + if( pMutex==&aMutex[iIdx] ) break; + } + assert(iIdx<3); + assert( aHolder[iIdx]==pthread_self() ); +} + /* Call our async_XX wrappers instead of selected pthread_XX functions */ #define pthread_mutex_lock async_mutex_lock #define pthread_mutex_unlock async_mutex_unlock #define pthread_mutex_trylock async_mutex_trylock #define pthread_cond_wait async_cond_wait +#else /* if defined(NDEBUG) */ + +#define assert_mutex_is_held(X) /* A no-op when not debugging */ + #endif /* !defined(NDEBUG) */ /* @@ -1482,6 +1501,11 @@ static void *asyncWriterThread(void *pIsStarted){ rc = unlinkAsyncFile(pData); pthread_mutex_unlock(&async.lockMutex); + if( !holdingMutex ){ + pthread_mutex_lock(&async.queueMutex); + holdingMutex = 1; + } + assert_mutex_is_held(&async.queueMutex); async.pQueueFirst = p->pNext; sqlite3_free(pData); doNotFree = 1; @@ -1534,6 +1558,7 @@ static void *asyncWriterThread(void *pIsStarted){ async.pQueueLast = 0; } if( !doNotFree ){ + assert_mutex_is_held(&async.queueMutex); async.pQueueFirst = p->pNext; sqlite3_free(p); } -- 2.47.2