From: drh Date: Mon, 18 Feb 2002 22:50:26 +0000 (+0000) Subject: Added a C wrapper program to test threading under Unix. (CVS 379) X-Git-Tag: version-3.6.10~5639 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1feb840dff40c3c73fe22253775b1c1128628bd;p=thirdparty%2Fsqlite.git Added a C wrapper program to test threading under Unix. (CVS 379) FossilOrigin-Name: 8d5634a4470a8145c0fbd0b843d55371d2f95e81 --- diff --git a/manifest b/manifest index 14ab63d573..7e6f0e7a64 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Bug\sfix:\sCREATE\sTABLE,\sfollowed\sby\sDROP\sTABLE\swithin\sthe\ssame\stransaction\sis\nworking\snow.\s(CVS\s378) -D 2002-02-18T22:49:59 +C Added\sa\sC\swrapper\sprogram\sto\stest\sthreading\sunder\sUnix.\s(CVS\s379) +D 2002-02-18T22:50:27 F Makefile.in 9fa4277413bf1d9cf91365f07d4108d7d87ed2af F Makefile.template 3372d45f8853afdb70bd30cc6fb50a3cd9069834 F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0 @@ -46,6 +46,7 @@ F src/tclsqlite.c b9cf346e95291cb4c4f1bf5ac1d77db6b8ad023d F src/test1.c 33efd350dca27c52c58c553c04fd3a6a51f13c1f F src/test2.c d410dbd8a90faa466c3ab694fa0aa57f5a773aa6 F src/test3.c d6775f95fd91f5b3cf0e2382a28e5aaeb68f745b +F src/threadtest.c 81f0598e0f031c1bd506af337fdc1b7e8dff263f F src/tokenize.c 9e98f94469694a763992860596137e78dbae0cc0 F src/update.c 95459f94a061860bf8e5716b3426a5ba85c79103 F src/util.c f31f3d6198a0d1296a16f5a6ceec423a932cbbf6 @@ -123,7 +124,7 @@ F www/speed.tcl 83457b2bf6bb430900bd48ca3dd98264d9a916a5 F www/sqlite.tcl 8b5884354cb615049aed83039f8dfe1552a44279 F www/tclsqlite.tcl 829b393d1ab187fd7a5e978631b3429318885c49 F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218 -P 78a50971e9adc8739e7888201c79465a40e1a152 -R ec588117a78704be8b36ea36b54ef170 +P 553579f936b3a4477c6adfd991adccd06280bfd2 +R 68572ee64467deeafdfca67210cb5aaa U drh -Z 76dfc832e7d97e5e1ad7b5a7cf543e04 +Z d4976e5cb119f987def7a2de13fb4da7 diff --git a/manifest.uuid b/manifest.uuid index 64e0809ae8..f5c7f23b56 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -553579f936b3a4477c6adfd991adccd06280bfd2 \ No newline at end of file +8d5634a4470a8145c0fbd0b843d55371d2f95e81 \ No newline at end of file diff --git a/src/threadtest.c b/src/threadtest.c new file mode 100644 index 0000000000..7255ba9196 --- /dev/null +++ b/src/threadtest.c @@ -0,0 +1,236 @@ +/* +** 2002 January 15 +** +** 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 implements a simple standalone program used to test whether +** or not the SQLite library is threadsafe. +** +** Testing the thread safety of SQLite is difficult because there are very +** few places in the code that are even potentially unsafe, and those +** places execute for very short periods of time. So even if the library +** is compiled with its mutexes disabled, it is likely to work correctly +** in a multi-threaded program most of the time. +*/ +#include "sqlite.h" +#include +#include +#include +#include +#include + +#define sqliteFree(X) sqliteFree_(X,__FILE__,__LINE__) + +/* +** Come here to die. +*/ +static void Exit(int rc){ + exit(rc); +} + +extern char *sqlite_mprintf(const char *zFormat, ...); +extern char *sqlite_vmprintf(const char *zFormat, va_list); + +/* +** Used to accumulate query results by db_query() +*/ +struct QueryResult { + const char *zFile; /* Filename - used for error reporting */ + int nElem; /* Number of used entries in azElem[] */ + int nAlloc; /* Number of slots allocated for azElem[] */ + char **azElem; /* The result of the query */ +}; + +/* +** The callback function for db_query +*/ +static int db_query_callback( + void *pUser, /* Pointer to the QueryResult structure */ + int nArg, /* Number of columns in this result row */ + char **azArg, /* Text of data in all columns */ + char **NotUsed /* Names of the columns */ +){ + struct QueryResult *pResult = (struct QueryResult*)pUser; + int i; + if( pResult->nElem + nArg >= pResult->nAlloc ){ + if( pResult->nAlloc==0 ){ + pResult->nAlloc = nArg+1; + }else{ + pResult->nAlloc = pResult->nAlloc*2 + nArg + 1; + } + pResult->azElem = realloc( pResult->azElem, pResult->nAlloc*sizeof(char*)); + if( pResult->azElem==0 ){ + fprintf(stderr,"%s: malloc failed\n", pResult->zFile); + return 1; + } + } + if( azArg==0 ) return 0; + for(i=0; iazElem[pResult->nElem++] = + sqlite_mprintf("%s",azArg[i] ? azArg[i] : ""); + } + return 0; +} + +/* +** Execute a query against the database. NULL values are returned +** as an empty string. The list is terminated by a single NULL pointer. +*/ +char **db_query(sqlite *db, const char *zFile, const char *zFormat, ...){ + char *zSql; + int rc; + char *zErrMsg = 0; + va_list ap; + struct QueryResult sResult; + va_start(ap, zFormat); + zSql = sqlite_vmprintf(zFormat, ap); + va_end(ap); + memset(&sResult, 0, sizeof(sResult)); + sResult.zFile = zFile; + rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg); + if( zErrMsg ){ + fprintf(stderr,"%s: query failed: %s - %s\n", zFile, zSql, zErrMsg); + free(zErrMsg); + free(zSql); + Exit(1); + } + sqliteFree(zSql); + if( sResult.azElem==0 ){ + db_query_callback(&sResult, 0, 0, 0); + } + sResult.azElem[sResult.nElem] = 0; + return sResult.azElem; +} + +/* +** Execute an SQL statement. +*/ +void db_execute(sqlite *db, const char *zFile, const char *zFormat, ...){ + char *zSql; + int rc; + char *zErrMsg = 0; + va_list ap; + va_start(ap, zFormat); + zSql = sqlite_vmprintf(zFormat, ap); + va_end(ap); + rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg); + if( zErrMsg ){ + fprintf(stderr,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg); + free(zErrMsg); + sqliteFree(zSql); + Exit(1); + } + sqliteFree(zSql); +} + +/* +** Free the results of a db_query() call. +*/ +void db_query_free(char **az){ + int i; + for(i=0; az[i]; i++){ + sqliteFree(az[i]); + } + free(az); +} + +/* +** Check results +*/ +void db_check(const char *zFile, const char *zMsg, char **az, ...){ + va_list ap; + int i; + char *z; + va_start(ap, az); + for(i=0; (z = va_arg(ap, char*))!=0; i++){ + if( az[i]==0 || strcmp(az[i],z)!=0 ){ + fprintf(stderr,"%s: %s: bad result in column %d: %s\n", + zFile, zMsg, i+1, az[i]); + db_query_free(az); + Exit(1); + } + } + va_end(ap); + db_query_free(az); +} + +pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t sig = PTHREAD_COND_INITIALIZER; +int thread_cnt = 0; + +static void *worker_bee(void *pArg){ + const char *zFilename = (char*)pArg; + char *azErr; + int i, cnt; + char **az; + sqlite *db; + + pthread_mutex_lock(&lock); + thread_cnt++; + pthread_mutex_unlock(&lock); + printf("%s: START\n", zFilename); + fflush(stdout); + for(cnt=0; cnt<10; cnt++){ + db = sqlite_open(zFilename, 0, &azErr); + if( db==0 ){ + fprintf(stderr,"%s: can't open\n", zFilename); + Exit(1); + } + db_execute(db, zFilename, "BEGIN; CREATE TABLE t1(a,b,c);"); + for(i=1; i<=100; i++){ + db_execute(db, zFilename, "INSERT INTO t1 VALUES(%d,%d,%d);", + i, i*2, i*i); + } + az = db_query(db, zFilename, "SELECT count(*) FROM t1"); + db_check(zFilename, "t1 size", az, "100", 0); + az = db_query(db, zFilename, "SELECT avg(b) FROM t1"); + db_check(zFilename, "t1 avg", az, "101", 0); + db_execute(db, zFilename, "DELETE FROM t1 WHERE a>50"); + az = db_query(db, zFilename, "SELECT avg(b) FROM t1"); + db_check(zFilename, "t1 avg2", az, "51", 0); + for(i=1; i<=50; i++){ + char z1[30], z2[30]; + az = db_query(db, zFilename, "SELECT b, c FROM t1 WHERE a=%d", i); + sprintf(z1, "%d", i*2); + sprintf(z2, "%d", i*i); + db_check(zFilename, "readback", az, z1, z2, 0); + } + db_execute(db, zFilename, "COMMIT; DROP TABLE t1;"); + sqlite_close(db); + } + printf("%s: END\n", zFilename); + unlink(zFilename); + fflush(stdout); + pthread_mutex_lock(&lock); + thread_cnt--; + if( thread_cnt<=0 ){ + pthread_cond_signal(&sig); + } + pthread_mutex_unlock(&lock); + return 0; +} + +int main(int argc, char **argv){ + char *zFile; + int i, n; + pthread_t id; + if( argc<2 || (n=atoi(argv[1]))<1 ) n = 10; + for(i=0; i0 ){ + pthread_cond_wait(&sig, &lock); + } + pthread_mutex_unlock(&lock); + return 0; +}