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