From: drh Date: Tue, 21 Aug 2012 17:36:44 +0000 (+0000) Subject: Cherry-pick [555fc07]: X-Git-Tag: version-3.8.7~132^2~101 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6ddecb753a7023d2ec6053cd01fac90c7a4f5679;p=thirdparty%2Fsqlite.git Cherry-pick [555fc07]: Changes to the thread routines to disable them when threading is turned off using sqlite3_config(). Also merge all recent trunk changes. FossilOrigin-Name: c92b0fe1371e7c20a5fbdf5fa96e30da14c40880 --- 6ddecb753a7023d2ec6053cd01fac90c7a4f5679 diff --cc manifest index 8609fc7751,4e50d4dc33..401aeb8a57 --- a/manifest +++ b/manifest @@@ -1,9 -1,9 +1,9 @@@ - C Update\sthe\sthreads\sbranch\sto\sinclude\sall\sthe\slatest\strunk\schanges. - D 2012-08-16T11:24:22.980 -C Update\sthe\sspellfix\svirtual\stable\sso\sthat\sall\sOOM\serrors\sare\sreported\sout\nto\sthe\sapplication. -D 2012-08-21T13:08:15.734 ++C Cherry-pick\s[555fc07]:\s\nChanges\sto\sthe\sthread\sroutines\sto\sdisable\sthem\swhen\sthreading\sis\sturned\noff\susing\ssqlite3_config().\s\sAlso\smerge\sall\srecent\strunk\schanges. ++D 2012-08-21T17:36:44.651 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f -F Makefile.in abd5c10d21d1395f140d9e50ea999df8fa4d6376 +F Makefile.in adec39f15a9c7000f634b87a535b95279b0cbd09 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 - F Makefile.msc abd0ffddd856f9251a563cfa5e430a046d73e359 -F Makefile.msc e89bdb795a841e58169ef9e26e543096dfc17c24 ++F Makefile.msc d2f83cd5e74d54f9e3bcd7a505ca5b356d7e7c68 F Makefile.vxworks 879f034a64062a364b21000266bbd5bc6e0c19b9 F README cd04a36fbc7ea56932a4052d7d0b7f09f27c33d6 F VERSION a71848df48082f1d6585d4b0819d530fc455485d @@@ -162,8 -162,8 +162,8 @@@ F src/notify.c 976dd0f6171d4588e89e874f F src/os.c e1acdc09ff3ac2412945cca9766e2dcf4675f31c F src/os.h 027491c77d2404c0a678bb3fb06286f331eb9b57 F src/os_common.h 92815ed65f805560b66166e3583470ff94478f04 - F src/os_unix.c d7c96b5d140f550f07345870112fae5d7ef99757 - F src/os_win.c 904f85152f806084826f5e6d7aae05d3cf4dc5d1 + F src/os_unix.c b5149a3343a6acd6c9df4e3acf5085a6501c1f68 -F src/os_win.c b8fc659987a678c7924796585f5ae293ba5c896d ++F src/os_win.c a7878531bf2c592218266f139d152cbe648fcc05 F src/pager.c e381c118b77dc22021a1a59d3fec24815e91df78 F src/pager.h 8b8c9bc065a3c66769df8724dfdf492ee1aab3c5 F src/parse.y f29df90bd3adc64b33114ab1de9fb7768fcf2099 @@@ -178,9 -178,9 +178,9 @@@ F src/resolve.c b3c70ab28cac60de33684c9 F src/rowset.c f6a49f3e9579428024662f6e2931832511f831a1 F src/select.c a365da6d7a6d7d8a10ad60ca71837ab5e9369466 F src/shell.c 076e1c90d594644f36027c8ecff9a392cf2d3a06 - F src/sqlite.h.in 3e8035bc406b1571a5cc8ea46bcc831201676f1a + F src/sqlite.h.in f664797c68ced43c2ea2c541d4ec8e1e04ec68ac F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477 -F src/sqliteInt.h c8169801f8bbfdf5873cc6fa45cb5df720c04db4 +F src/sqliteInt.h 1ef5f8b64d204fe5deec83c58297493b2f5fbfa0 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d F src/status.c 35939e7e03abf1b7577ce311f48f682c40de3208 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e @@@ -221,7 -221,7 +221,7 @@@ F src/test_quota.h 8761e463b25e75ebc078 F src/test_rtree.c aba603c949766c4193f1068b91c787f57274e0d9 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0 F src/test_server.c 2f99eb2837dfa06a4aacf24af24c6affdf66a84f - F src/test_spellfix.c 3a260d237fabbf5884389aa8c0e516b4e61ab98a -F src/test_spellfix.c 5cc2bfe3fe4b3e6d7b0b0807d36a485a3a15a1ae ++F src/test_spellfix.c fa83c9b4c4bdd1d41be4ad1e9241bf5a4fc9190f F src/test_stat.c d1569c7a4839f13e80187e2c26b2ab4da2d03935 F src/test_superlock.c 2b97936ca127d13962c3605dbc9a4ef269c424cd F src/test_syscall.c a992d8c80ea91fbf21fb2dd570db40e77dd7e6ae @@@ -231,7 -231,6 +231,7 @@@ F src/test_vfs.c c6260ef238c1142c8f8bd4 F src/test_vfstrace.c 6b28adb2a0e8ecd0f2e3581482e1f658b11b4067 F src/test_wholenumber.c 3d2b9ed1505c40ad5c5ca2ad16ae7a289d6cc251 F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9 - F src/threads.c cde5bd24ab5b33bb694c59f4236ecd56ad8da9b5 ++F src/threads.c 2b918d1f4f0b0831e8f41c49bcaa097f01490120 F src/tokenize.c 1e86210d3976717a19238ea7b047fac481fe8c12 F src/trigger.c ee7e178fb9188f44b532cebd449a7c1df90fb684 F src/update.c d3076782c887c10e882996550345da9c4c9f9dea @@@ -1011,7 -1011,7 +1012,7 @@@ F tool/vdbe-compress.tcl d70ea6d8a19e35 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 F tool/win/sqlite.vsix 67d8a99aceb56384a81b3f30d6c71743146d2cc9 - P ed3dc7a89f3416622fcd741ae5fba437929d06d6 31c07db2560ee867723c41cdb634e2aa7993634d - R 8983882407637c60aee542aa1d4c9bca -P e799222f3b8246e65657a758437914ece7069ba9 -R 41231569c08717226d41316ed2c512be ++P f4125771e21f1ca29d5442b5441dacfc06b8032b c2cf498513c2633bd2b08372772eaa0f3b3ab25f ++R d53679dd7e2c9cf4e3b63c09756c305f U drh - Z 9b3c8e4e1baa5816be48b30862221038 -Z 09392d5c35b740ca7443d8f9f40e84b9 ++Z 171195171b662a876210fc11cf6e5f1e diff --cc manifest.uuid index 0a036ded25,51c86e8c1f..1e3159f0c4 --- a/manifest.uuid +++ b/manifest.uuid @@@ -1,1 -1,1 +1,1 @@@ - f4125771e21f1ca29d5442b5441dacfc06b8032b -c2cf498513c2633bd2b08372772eaa0f3b3ab25f ++c92b0fe1371e7c20a5fbdf5fa96e30da14c40880 diff --cc src/test_spellfix.c index 92755cd1d6,fe6992e535..68247fed0f --- a/src/test_spellfix.c +++ b/src/test_spellfix.c @@@ -2508,10 -2508,12 +2508,12 @@@ static int spellfix1Filter */ static int spellfix1Next(sqlite3_vtab_cursor *cur){ spellfix1_cursor *pCur = (spellfix1_cursor *)cur; - rc = SQLITE_OK; ++ int rc = SQLITE_OK; if( pCur->iRow < pCur->nRow ){ if( pCur->pFullScan ){ - int rc = sqlite3_step(pCur->pFullScan); + rc = sqlite3_step(pCur->pFullScan); if( rc!=SQLITE_ROW ) pCur->iRow = pCur->nRow; + if( rc==SQLITE_ROW || rc==SQLITE_DONE ) rc = SQLITE_OK; }else{ pCur->iRow++; } diff --cc src/threads.c index 61e58f494b,0000000000..33781a7aac mode 100644,000000..100644 --- a/src/threads.c +++ b/src/threads.c @@@ -1,198 -1,0 +1,218 @@@ +/* +** 2012 July 21 +** +** 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 presents a simple cross-platform threading interface for +** use internally by SQLite. +** +** A "thread" can be created using sqlite3ThreadCreate(). This thread +** runs independently of its creator until it is joined using +** sqlite3ThreadJoin(), at which point it terminates. +** +** Threads do not have to be real. It could be that the work of the +** "thread" is done by the main thread at either the sqlite3ThreadCreate() +** or sqlite3ThreadJoin() call. This is, in fact, what happens in +** single threaded systems. Nothing in SQLite requires multiple threads. +** This interface exists so that applications that want to take advantage +** of multiple cores can do so, while also allowing applications to stay +** single-threaded if desired. +*/ +#include "sqliteInt.h" + +/********************************* Unix Pthreads ****************************/ +#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) + +#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ +#include + +/* A running thread */ +struct SQLiteThread { + pthread_t tid; ++ int done; ++ void *pOut; +}; + +/* Create a new thread */ +int sqlite3ThreadCreate( + SQLiteThread **ppThread, /* OUT: Write the thread object here */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + SQLiteThread *p; + int rc; + + assert( ppThread!=0 ); + assert( xTask!=0 ); + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); + if( p==0 ) return SQLITE_NOMEM; - rc = pthread_create(&p->tid, 0, xTask, pIn); - if( rc ){ - sqlite3_free(p); - return SQLITE_ERROR; ++ memset(p, 0, sizeof(*p)); ++ if( sqlite3GlobalConfig.bCoreMutex==0 ++ || pthread_create(&p->tid, 0, xTask, pIn)!=0 ++ ){ ++ p->done = 1; ++ p->pOut = xTask(pIn); + } + *ppThread = p; + return SQLITE_OK; +} + +/* Get the results of the thread */ +int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + int rc; + + assert( ppOut!=0 ); + if( p==0 ) return SQLITE_NOMEM; - rc = pthread_join(p->tid, ppOut); ++ if( p->done ){ ++ *ppOut = p->pOut; ++ rc = SQLITE_OK; ++ }else{ ++ rc = pthread_join(p->tid, ppOut); ++ } + sqlite3_free(p); + return rc ? SQLITE_ERROR : SQLITE_OK; +} + +#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */ +/******************************** End Unix Pthreads *************************/ + + +/********************************* Win32 Threads ****************************/ +#if SQLITE_OS_WIN && !SQLITE_OS_WINRT + +#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ +#include + +/* A running thread */ +struct SQLiteThread { + uintptr_t tid; /* The thread handle */ + void *(*xTask)(void*); /* The routine to run as a thread */ + void *pIn; /* Argument to xTask */ + void *pResult; /* Result of xTask */ +}; + +/* Thread procedure Win32 compatibility shim */ +static void sqlite3ThreadProc( + void *pArg /* IN: Pointer to the SQLiteThread structure */ +){ + SQLiteThread *p = (SQLiteThread *)pArg; + + assert( p!=0 ); + assert( p->xTask!=0 ); + p->pResult = p->xTask(p->pIn); + _endthread(); +} + +/* Create a new thread */ +int sqlite3ThreadCreate( + SQLiteThread **ppThread, /* OUT: Write the thread object here */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + SQLiteThread *p; + + assert( ppThread!=0 ); + assert( xTask!=0 ); + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); + if( p==0 ) return SQLITE_NOMEM; - p->xTask = xTask; p->pIn = pIn; - p->tid = _beginthread(sqlite3ThreadProc, 0, p); - if( p->tid==(uintptr_t)-1 ){ - sqlite3_free(p); - return SQLITE_ERROR; ++ if( sqlite3GlobalConfig.bCoreMutex==0 ){ ++ memset(p, 0, sizeof(*p)); ++ }else{ ++ p->xTask = xTask; ++ p->pIn = pIn; ++ p->tid = _beginthread(sqlite3ThreadProc, 0, p); ++ if( p->tid==(uintptr_t)-1 ){ ++ memset(p, 0, sizeof(*p)); ++ } ++ } ++ if( p->xTask==0 ){ ++ p->pResult = xTask(pIn); + } + *ppThread = p; + return SQLITE_OK; +} + +/* Get the results of the thread */ +int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + DWORD rc; + + assert( ppOut!=0 ); + if( p==0 ) return SQLITE_NOMEM; - rc = sqlite3Win32Wait((HANDLE)p->tid); - assert( rc!=WAIT_IO_COMPLETION ); ++ if( p->xTask==0 ){ ++ rc = WAIT_OBJECT_O; ++ }else{ ++ rc = sqlite3Win32Wait((HANDLE)p->tid); ++ assert( rc!=WAIT_IO_COMPLETION ); ++ } + if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; + sqlite3_free(p); + return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR; +} + +#endif /* SQLITE_OS_WIN && !SQLITE_OS_WINRT */ +/******************************** End Win32 Threads *************************/ + + +/********************************* Single-Threaded **************************/ +#ifndef SQLITE_THREADS_IMPLEMENTED +/* +** This implementation does not actually create a new thread. It does the +** work of the thread in the main thread, when either the thread is created +** or when it is joined +*/ + +/* A running thread */ +struct SQLiteThread { + void *(*xTask)(void*); /* The routine to run as a thread */ + void *pIn; /* Argument to xTask */ + void *pResult; /* Result of xTask */ +}; + +/* Create a new thread */ +int sqlite3ThreadCreate( + SQLiteThread **ppThread, /* OUT: Write the thread object here */ + void *(*xTask)(void*), /* Routine to run in a separate thread */ + void *pIn /* Argument passed into xTask() */ +){ + SQLiteThread *p; + + assert( ppThread!=0 ); + assert( xTask!=0 ); + *ppThread = 0; + p = sqlite3Malloc(sizeof(*p)); + if( p==0 ) return SQLITE_NOMEM; + if( (SQLITE_PTR_TO_INT(p)/17)&1 ){ + p->xTask = xTask; + p->pIn = pIn; + }else{ + p->xTask = 0; + p->pResult = xTask(pIn); + } + *ppThread = p; + return SQLITE_OK; +} + +/* Get the results of the thread */ +int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ + assert( ppOut!=0 ); + if( p==0 ) return SQLITE_NOMEM; + if( p->xTask ){ + *ppOut = p->xTask(p->pIn); + }else{ + *ppOut = p->pResult; + } + sqlite3_free(p); + return SQLITE_OK; +} + +#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */ +/****************************** End Single-Threaded *************************/