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