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 *notUsed
, int argc
, char **argv
, char **notUsed2
){
43 if( strcmp(argv
[0],"ok") ){
45 fprintf(stderr
,"pid=%d. %s\n", getpid(), argv
[0]);
47 /* fprintf(stderr,"pid=%d. OK\n", getpid()); */
53 ** Do an integrity check on the database. If the first integrity check
54 ** fails, try it a second time.
56 int integrity_check(sqlite
*db
){
58 if( all_stop
) return 0;
59 /* fprintf(stderr,"pid=%d: CHECK\n", getpid()); */
60 rc
= sqlite3_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
);
65 sqlite3_exec(db
, "pragma integrity_check", check_callback
, 0, 0);
71 ** This is the worker thread
73 void *worker(void *notUsed
){
77 while( !all_stop
&& cnt
++<10000 ){
78 if( cnt
%1000==0 ) printf("pid=%d: %d\n", getpid(), cnt
);
79 while( (sqlite3_open(DB_FILE
, &db
))!=SQLITE_OK
) sched_yield();
80 sqlite3_exec(db
, "PRAGMA synchronous=OFF", 0, 0, 0);
82 if( all_stop
){ sqlite3_close(db
); break; }
83 /* fprintf(stderr, "pid=%d: BEGIN\n", getpid()); */
84 rc
= sqlite3_exec(db
, "INSERT INTO t1 VALUES('bogus data')", 0, 0, 0);
85 /* fprintf(stderr, "pid=%d: END rc=%d\n", getpid(), rc); */
92 ** Initialize the database and start the threads
94 int main(int argc
, char **argv
){
99 if( strcmp(DB_FILE
,":memory:") ){
100 char *zJournal
= sqlite3_mprintf("%s-journal", DB_FILE
);
105 sqlite3_open(DB_FILE
, &db
);
107 fprintf(stderr
,"unable to initialize database\n");
110 rc
= sqlite3_exec(db
, "CREATE TABLE t1(x);", 0,0,0);
112 fprintf(stderr
,"cannot create table t1: %d\n", rc
);
116 for(i
=0; i
<sizeof(aThread
)/sizeof(aThread
[0]); i
++){
117 pthread_create(&aThread
[i
], 0, worker
, 0);
119 for(i
=0; i
<sizeof(aThread
)/sizeof(aThread
[i
]); i
++){
120 pthread_join(aThread
[i
], 0);
123 printf("Everything seems ok.\n");
126 printf("We hit an error.\n");