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