1c713bdc7Sdrh /* 2c713bdc7Sdrh ** 2004 January 13 3c713bdc7Sdrh ** 4c713bdc7Sdrh ** The author disclaims copyright to this source code. In place of 5c713bdc7Sdrh ** a legal notice, here is a blessing: 6c713bdc7Sdrh ** 7c713bdc7Sdrh ** May you do good and not evil. 8c713bdc7Sdrh ** May you find forgiveness for yourself and forgive others. 9c713bdc7Sdrh ** May you share freely, never taking more than you give. 10c713bdc7Sdrh ** 11c713bdc7Sdrh ************************************************************************* 12c713bdc7Sdrh ** This file implements a simple standalone program used to test whether 13c713bdc7Sdrh ** or not the SQLite library is threadsafe. 14c713bdc7Sdrh ** 15c713bdc7Sdrh ** This file is NOT part of the standard SQLite library. It is used for 16c713bdc7Sdrh ** testing only. 17c713bdc7Sdrh */ 18c713bdc7Sdrh #include <stdio.h> 19c713bdc7Sdrh #include <unistd.h> 20c713bdc7Sdrh #include <pthread.h> 21c713bdc7Sdrh #include <string.h> 22c713bdc7Sdrh #include <stdlib.h> 23c713bdc7Sdrh #include "sqlite.h" 24c713bdc7Sdrh 25c713bdc7Sdrh /* 26c713bdc7Sdrh ** Name of the database 27c713bdc7Sdrh */ 28c713bdc7Sdrh #define DB_FILE "test.db" 29c713bdc7Sdrh 30c713bdc7Sdrh /* 31c713bdc7Sdrh ** When this variable becomes non-zero, all threads stop 32c713bdc7Sdrh ** what they are doing. 33c713bdc7Sdrh */ 34c713bdc7Sdrh volatile int all_stop = 0; 35c713bdc7Sdrh 36c713bdc7Sdrh /* 37c713bdc7Sdrh ** Callback from the integrity check. If the result is anything other 38c713bdc7Sdrh ** than "ok" it means the integrity check has failed. Set the "all_stop" 39c713bdc7Sdrh ** global variable to stop all other activity. Print the error message 40c713bdc7Sdrh ** or print OK if the string "ok" is seen. 41c713bdc7Sdrh */ 42*ba92a2ebSdrh int check_callback(void *pid, int argc, char **argv, char **notUsed2){ 43*ba92a2ebSdrh int id = (int)pid; 44c713bdc7Sdrh if( strcmp(argv[0],"ok") ){ 45c713bdc7Sdrh all_stop = 1; 46*ba92a2ebSdrh fprintf(stderr,"id: %s\n", id, argv[0]); 47c713bdc7Sdrh }else{ 48*ba92a2ebSdrh /* fprintf(stderr,"%d: OK\n", id); */ 49c713bdc7Sdrh } 50c713bdc7Sdrh return 0; 51c713bdc7Sdrh } 52c713bdc7Sdrh 53c713bdc7Sdrh /* 54c713bdc7Sdrh ** Do an integrity check on the database. If the first integrity check 55c713bdc7Sdrh ** fails, try it a second time. 56c713bdc7Sdrh */ 57*ba92a2ebSdrh int integrity_check(sqlite *db, int id){ 58c713bdc7Sdrh int rc; 59c713bdc7Sdrh if( all_stop ) return 0; 60*ba92a2ebSdrh /* fprintf(stderr,"%d: CHECK\n", id); */ 615fdae771Sdrh rc = sqlite3_exec(db, "pragma integrity_check", check_callback, 0, 0); 62c713bdc7Sdrh if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ 63*ba92a2ebSdrh fprintf(stderr,"%d, Integrity check returns %d\n", id, rc); 64c713bdc7Sdrh } 65c713bdc7Sdrh if( all_stop ){ 665fdae771Sdrh sqlite3_exec(db, "pragma integrity_check", check_callback, 0, 0); 67c713bdc7Sdrh } 68c713bdc7Sdrh return 0; 69c713bdc7Sdrh } 70c713bdc7Sdrh 71c713bdc7Sdrh /* 72c713bdc7Sdrh ** This is the worker thread 73c713bdc7Sdrh */ 74*ba92a2ebSdrh void *worker(void *workerArg){ 75c713bdc7Sdrh sqlite *db; 76*ba92a2ebSdrh int id = (int)workerArg; 77c713bdc7Sdrh int rc; 78c713bdc7Sdrh int cnt = 0; 79*ba92a2ebSdrh fprintf(stderr, "Starting worker %d\n", id); 80c713bdc7Sdrh while( !all_stop && cnt++<10000 ){ 81*ba92a2ebSdrh if( cnt%100==0 ) printf("%d: %d\n", id, cnt); 825fdae771Sdrh while( (sqlite3_open(DB_FILE, &db))!=SQLITE_OK ) sched_yield(); 835fdae771Sdrh sqlite3_exec(db, "PRAGMA synchronous=OFF", 0, 0, 0); 84*ba92a2ebSdrh /* integrity_check(db, id); */ 855fdae771Sdrh if( all_stop ){ sqlite3_close(db); break; } 86*ba92a2ebSdrh /* fprintf(stderr, "%d: BEGIN\n", id); */ 875fdae771Sdrh rc = sqlite3_exec(db, "INSERT INTO t1 VALUES('bogus data')", 0, 0, 0); 88*ba92a2ebSdrh /* fprintf(stderr, "%d: END rc=%d\n", id, rc); */ 895fdae771Sdrh sqlite3_close(db); 90c713bdc7Sdrh } 91*ba92a2ebSdrh fprintf(stderr, "Worker %d finished\n", id); 92c713bdc7Sdrh return 0; 93c713bdc7Sdrh } 94c713bdc7Sdrh 95c713bdc7Sdrh /* 96c713bdc7Sdrh ** Initialize the database and start the threads 97c713bdc7Sdrh */ 98c713bdc7Sdrh int main(int argc, char **argv){ 99c713bdc7Sdrh sqlite *db; 100c713bdc7Sdrh int i, rc; 101c713bdc7Sdrh pthread_t aThread[5]; 102c713bdc7Sdrh 1032b444853Sdanielk1977 if( strcmp(DB_FILE,":memory:") ){ 1042b444853Sdanielk1977 char *zJournal = sqlite3_mprintf("%s-journal", DB_FILE); 1052b444853Sdanielk1977 unlink(DB_FILE); 1062b444853Sdanielk1977 unlink(zJournal); 107*ba92a2ebSdrh sqlite3_free(zJournal); 1082b444853Sdanielk1977 } 1095fdae771Sdrh sqlite3_open(DB_FILE, &db); 110c713bdc7Sdrh if( db==0 ){ 111c713bdc7Sdrh fprintf(stderr,"unable to initialize database\n"); 112c713bdc7Sdrh exit(1); 113c713bdc7Sdrh } 1145fdae771Sdrh rc = sqlite3_exec(db, "CREATE TABLE t1(x);", 0,0,0); 115c713bdc7Sdrh if( rc ){ 116c713bdc7Sdrh fprintf(stderr,"cannot create table t1: %d\n", rc); 117c713bdc7Sdrh exit(1); 118c713bdc7Sdrh } 1195fdae771Sdrh sqlite3_close(db); 120c713bdc7Sdrh for(i=0; i<sizeof(aThread)/sizeof(aThread[0]); i++){ 121*ba92a2ebSdrh pthread_create(&aThread[i], 0, worker, (void*)i); 122c713bdc7Sdrh } 123c713bdc7Sdrh for(i=0; i<sizeof(aThread)/sizeof(aThread[i]); i++){ 124c713bdc7Sdrh pthread_join(aThread[i], 0); 125c713bdc7Sdrh } 126c713bdc7Sdrh if( !all_stop ){ 127c713bdc7Sdrh printf("Everything seems ok.\n"); 128c713bdc7Sdrh return 0; 129c713bdc7Sdrh }else{ 130c713bdc7Sdrh printf("We hit an error.\n"); 131c713bdc7Sdrh return 1; 132c713bdc7Sdrh } 133c713bdc7Sdrh } 134