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