sl@0: /* sl@0: ** 2004 January 13 sl@0: ** sl@0: ** The author disclaims copyright to this source code. In place of sl@0: ** a legal notice, here is a blessing: sl@0: ** sl@0: ** May you do good and not evil. sl@0: ** May you find forgiveness for yourself and forgive others. sl@0: ** May you share freely, never taking more than you give. sl@0: ** sl@0: ************************************************************************* sl@0: ** This file implements a simple standalone program used to test whether sl@0: ** or not the SQLite library is threadsafe. sl@0: ** sl@0: ** This file is NOT part of the standard SQLite library. It is used for sl@0: ** testing only. sl@0: */ sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "sqlite.h" sl@0: sl@0: /* sl@0: ** Name of the database sl@0: */ sl@0: #define DB_FILE "test.db" sl@0: sl@0: /* sl@0: ** When this variable becomes non-zero, all threads stop sl@0: ** what they are doing. sl@0: */ sl@0: volatile int all_stop = 0; sl@0: sl@0: /* sl@0: ** Callback from the integrity check. If the result is anything other sl@0: ** than "ok" it means the integrity check has failed. Set the "all_stop" sl@0: ** global variable to stop all other activity. Print the error message sl@0: ** or print OK if the string "ok" is seen. sl@0: */ sl@0: int check_callback(void *pid, int argc, char **argv, char **notUsed2){ sl@0: int id = (int)pid; sl@0: if( strcmp(argv[0],"ok") ){ sl@0: all_stop = 1; sl@0: fprintf(stderr,"id: %s\n", id, argv[0]); sl@0: }else{ sl@0: /* fprintf(stderr,"%d: OK\n", id); */ sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** Do an integrity check on the database. If the first integrity check sl@0: ** fails, try it a second time. sl@0: */ sl@0: int integrity_check(sqlite *db, int id){ sl@0: int rc; sl@0: if( all_stop ) return 0; sl@0: /* fprintf(stderr,"%d: CHECK\n", id); */ sl@0: rc = sqlite3_exec(db, "pragma integrity_check", check_callback, 0, 0); sl@0: if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ sl@0: fprintf(stderr,"%d, Integrity check returns %d\n", id, rc); sl@0: } sl@0: if( all_stop ){ sl@0: sqlite3_exec(db, "pragma integrity_check", check_callback, 0, 0); sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** This is the worker thread sl@0: */ sl@0: void *worker(void *workerArg){ sl@0: sqlite *db; sl@0: int id = (int)workerArg; sl@0: int rc; sl@0: int cnt = 0; sl@0: fprintf(stderr, "Starting worker %d\n", id); sl@0: while( !all_stop && cnt++<10000 ){ sl@0: if( cnt%100==0 ) printf("%d: %d\n", id, cnt); sl@0: while( (sqlite3_open(DB_FILE, &db))!=SQLITE_OK ) sched_yield(); sl@0: sqlite3_exec(db, "PRAGMA synchronous=OFF", 0, 0, 0); sl@0: /* integrity_check(db, id); */ sl@0: if( all_stop ){ sqlite3_close(db); break; } sl@0: /* fprintf(stderr, "%d: BEGIN\n", id); */ sl@0: rc = sqlite3_exec(db, "INSERT INTO t1 VALUES('bogus data')", 0, 0, 0); sl@0: /* fprintf(stderr, "%d: END rc=%d\n", id, rc); */ sl@0: sqlite3_close(db); sl@0: } sl@0: fprintf(stderr, "Worker %d finished\n", id); sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** Initialize the database and start the threads sl@0: */ sl@0: int main(int argc, char **argv){ sl@0: sqlite *db; sl@0: int i, rc; sl@0: pthread_t aThread[5]; sl@0: sl@0: if( strcmp(DB_FILE,":memory:") ){ sl@0: char *zJournal = sqlite3_mprintf("%s-journal", DB_FILE); sl@0: unlink(DB_FILE); sl@0: unlink(zJournal); sl@0: sqlite3_free(zJournal); sl@0: } sl@0: sqlite3_open(DB_FILE, &db); sl@0: if( db==0 ){ sl@0: fprintf(stderr,"unable to initialize database\n"); sl@0: exit(1); sl@0: } sl@0: rc = sqlite3_exec(db, "CREATE TABLE t1(x);", 0,0,0); sl@0: if( rc ){ sl@0: fprintf(stderr,"cannot create table t1: %d\n", rc); sl@0: exit(1); sl@0: } sl@0: sqlite3_close(db); sl@0: for(i=0; i