4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This file implements a simple standalone program used to test whether
13 ** or not the SQLite library is threadsafe.
15 ** This file is NOT part of the standard SQLite library. It is used for
26 ** Name of the database
28 #define DB_FILE "test.db"
31 ** When this variable becomes non-zero, all threads stop
32 ** what they are doing.
34 volatile int all_stop
= 0;
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.
42 int check_callback(void *pid
, int argc
, char **argv
, char **notUsed2
){
44 if( strcmp(argv
[0],"ok") ){
46 fprintf(stderr
,"%d: %s\n", id
, argv
[0]);
48 /* fprintf(stderr,"%d: OK\n", id); */
54 ** Do an integrity check on the database. If the first integrity check
55 ** fails, try it a second time.
57 int integrity_check(sqlite
*db
, int id
){
59 if( all_stop
) return 0;
60 /* fprintf(stderr,"%d: CHECK\n", id); */
61 rc
= sqlite3_exec(db
, "pragma integrity_check", check_callback
, 0, 0);
62 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_BUSY
){
63 fprintf(stderr
,"%d, Integrity check returns %d\n", id
, rc
);
66 sqlite3_exec(db
, "pragma integrity_check", check_callback
, 0, 0);
72 ** This is the worker thread
74 void *worker(void *workerArg
){
76 int id
= (int)workerArg
;
79 fprintf(stderr
, "Starting worker %d\n", id
);
80 while( !all_stop
&& cnt
++<10000 ){
81 if( cnt
%100==0 ) printf("%d: %d\n", id
, cnt
);
82 while( (sqlite3_open(DB_FILE
, &db
))!=SQLITE_OK
) sched_yield();
83 sqlite3_exec(db
, "PRAGMA synchronous=OFF", 0, 0, 0);
84 /* integrity_check(db, id); */
85 if( all_stop
){ sqlite3_close(db
); break; }
86 /* fprintf(stderr, "%d: BEGIN\n", id); */
87 rc
= sqlite3_exec(db
, "INSERT INTO t1 VALUES('bogus data')", 0, 0, 0);
88 /* fprintf(stderr, "%d: END rc=%d\n", id, rc); */
91 fprintf(stderr
, "Worker %d finished\n", id
);
96 ** Initialize the database and start the threads
98 int main(int argc
, char **argv
){
101 pthread_t aThread
[5];
103 if( strcmp(DB_FILE
,":memory:") ){
104 char *zJournal
= sqlite3_mprintf("%s-journal", DB_FILE
);
107 sqlite3_free(zJournal
);
109 sqlite3_open(DB_FILE
, &db
);
111 fprintf(stderr
,"unable to initialize database\n");
114 rc
= sqlite3_exec(db
, "CREATE TABLE t1(x);", 0,0,0);
116 fprintf(stderr
,"cannot create table t1: %d\n", rc
);
120 for(i
=0; i
<sizeof(aThread
)/sizeof(aThread
[0]); i
++){
121 pthread_create(&aThread
[i
], 0, worker
, (void*)i
);
123 for(i
=0; i
<sizeof(aThread
)/sizeof(aThread
[i
]); i
++){
124 pthread_join(aThread
[i
], 0);
127 printf("Everything seems ok.\n");
130 printf("We hit an error.\n");