1 /*
2 ** 2002 January 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
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.
10 **
11 *************************************************************************
12 ** This file implements a simple standalone program used to test whether
13 ** or not the SQLite library is threadsafe.
14 **
15 ** Testing the thread safety of SQLite is difficult because there are very
16 ** few places in the code that are even potentially unsafe, and those
17 ** places execute for very short periods of time.  So even if the library
18 ** is compiled with its mutexes disabled, it is likely to work correctly
19 ** in a multi-threaded program most of the time.
20 **
21 ** This file is NOT part of the standard SQLite library.  It is used for
22 ** testing only.
23 */
24 #include "sqlite.h"
25 #include <pthread.h>
26 #include <sched.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 /*
33 ** Enable for tracing
34 */
35 static int verbose = 0;
36 
37 /*
38 ** Come here to die.
39 */
Exit(int rc)40 static void Exit(int rc){
41   exit(rc);
42 }
43 
44 extern char *sqlite_mprintf(const char *zFormat, ...);
45 extern char *sqlite_vmprintf(const char *zFormat, va_list);
46 
47 /*
48 ** When a lock occurs, yield.
49 */
db_is_locked(void * NotUsed,const char * zNotUsed,int iNotUsed)50 static int db_is_locked(void *NotUsed, const char *zNotUsed, int iNotUsed){
51   /* sched_yield(); */
52   if( verbose ) printf("BUSY %s\n", (char*)NotUsed);
53   usleep(100);
54   return 1;
55 }
56 
57 /*
58 ** Used to accumulate query results by db_query()
59 */
60 struct QueryResult {
61   const char *zFile;  /* Filename - used for error reporting */
62   int nElem;          /* Number of used entries in azElem[] */
63   int nAlloc;         /* Number of slots allocated for azElem[] */
64   char **azElem;      /* The result of the query */
65 };
66 
67 /*
68 ** The callback function for db_query
69 */
db_query_callback(void * pUser,int nArg,char ** azArg,char ** NotUsed)70 static int db_query_callback(
71   void *pUser,     /* Pointer to the QueryResult structure */
72   int nArg,        /* Number of columns in this result row */
73   char **azArg,    /* Text of data in all columns */
74   char **NotUsed   /* Names of the columns */
75 ){
76   struct QueryResult *pResult = (struct QueryResult*)pUser;
77   int i;
78   if( pResult->nElem + nArg >= pResult->nAlloc ){
79     if( pResult->nAlloc==0 ){
80       pResult->nAlloc = nArg+1;
81     }else{
82       pResult->nAlloc = pResult->nAlloc*2 + nArg + 1;
83     }
84     pResult->azElem = realloc( pResult->azElem, pResult->nAlloc*sizeof(char*));
85     if( pResult->azElem==0 ){
86       fprintf(stdout,"%s: malloc failed\n", pResult->zFile);
87       return 1;
88     }
89   }
90   if( azArg==0 ) return 0;
91   for(i=0; i<nArg; i++){
92     pResult->azElem[pResult->nElem++] =
93         sqlite_mprintf("%s",azArg[i] ? azArg[i] : "");
94   }
95   return 0;
96 }
97 
98 /*
99 ** Execute a query against the database.  NULL values are returned
100 ** as an empty string.  The list is terminated by a single NULL pointer.
101 */
db_query(sqlite * db,const char * zFile,const char * zFormat,...)102 char **db_query(sqlite *db, const char *zFile, const char *zFormat, ...){
103   char *zSql;
104   int rc;
105   char *zErrMsg = 0;
106   va_list ap;
107   struct QueryResult sResult;
108   va_start(ap, zFormat);
109   zSql = sqlite_vmprintf(zFormat, ap);
110   va_end(ap);
111   memset(&sResult, 0, sizeof(sResult));
112   sResult.zFile = zFile;
113   if( verbose ) printf("QUERY %s: %s\n", zFile, zSql);
114   rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
115   if( rc==SQLITE_SCHEMA ){
116     if( zErrMsg ) free(zErrMsg);
117     rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
118   }
119   if( verbose ) printf("DONE %s %s\n", zFile, zSql);
120   if( zErrMsg ){
121     fprintf(stdout,"%s: query failed: %s - %s\n", zFile, zSql, zErrMsg);
122     free(zErrMsg);
123     free(zSql);
124     Exit(1);
125   }
126   sqlite_freemem(zSql);
127   if( sResult.azElem==0 ){
128     db_query_callback(&sResult, 0, 0, 0);
129   }
130   sResult.azElem[sResult.nElem] = 0;
131   return sResult.azElem;
132 }
133 
134 /*
135 ** Execute an SQL statement.
136 */
db_execute(sqlite * db,const char * zFile,const char * zFormat,...)137 void db_execute(sqlite *db, const char *zFile, const char *zFormat, ...){
138   char *zSql;
139   int rc;
140   char *zErrMsg = 0;
141   va_list ap;
142   va_start(ap, zFormat);
143   zSql = sqlite_vmprintf(zFormat, ap);
144   va_end(ap);
145   if( verbose ) printf("EXEC %s: %s\n", zFile, zSql);
146   rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
147   while( rc==SQLITE_SCHEMA ){
148     if( zErrMsg ) free(zErrMsg);
149     rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
150   }
151   if( verbose ) printf("DONE %s: %s\n", zFile, zSql);
152   if( zErrMsg ){
153     fprintf(stdout,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg);
154     free(zErrMsg);
155     sqlite_freemem(zSql);
156     Exit(1);
157   }
158   sqlite_freemem(zSql);
159 }
160 
161 /*
162 ** Free the results of a db_query() call.
163 */
db_query_free(char ** az)164 void db_query_free(char **az){
165   int i;
166   for(i=0; az[i]; i++){
167     sqlite_freemem(az[i]);
168   }
169   free(az);
170 }
171 
172 /*
173 ** Check results
174 */
db_check(const char * zFile,const char * zMsg,char ** az,...)175 void db_check(const char *zFile, const char *zMsg, char **az, ...){
176   va_list ap;
177   int i;
178   char *z;
179   va_start(ap, az);
180   for(i=0; (z = va_arg(ap, char*))!=0; i++){
181     if( az[i]==0 || strcmp(az[i],z)!=0 ){
182       fprintf(stdout,"%s: %s: bad result in column %d: %s\n",
183         zFile, zMsg, i+1, az[i]);
184       db_query_free(az);
185       Exit(1);
186     }
187   }
188   va_end(ap);
189   db_query_free(az);
190 }
191 
192 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
193 pthread_cond_t sig = PTHREAD_COND_INITIALIZER;
194 int thread_cnt = 0;
195 
worker_bee(void * pArg)196 static void *worker_bee(void *pArg){
197   const char *zFilename = (char*)pArg;
198   char *azErr;
199   int i, cnt;
200   int t = atoi(zFilename);
201   char **az;
202   sqlite *db;
203 
204   pthread_mutex_lock(&lock);
205   thread_cnt++;
206   pthread_mutex_unlock(&lock);
207   printf("%s: START\n", zFilename);
208   fflush(stdout);
209   for(cnt=0; cnt<10; cnt++){
210     db = sqlite_open(&zFilename[2], 0, &azErr);
211     if( db==0 ){
212       fprintf(stdout,"%s: can't open\n", zFilename);
213       Exit(1);
214     }
215     sqlite_busy_handler(db, db_is_locked, zFilename);
216     db_execute(db, zFilename, "CREATE TABLE t%d(a,b,c);", t);
217     for(i=1; i<=100; i++){
218       db_execute(db, zFilename, "INSERT INTO t%d VALUES(%d,%d,%d);",
219          t, i, i*2, i*i);
220     }
221     az = db_query(db, zFilename, "SELECT count(*) FROM t%d", t);
222     db_check(zFilename, "tX size", az, "100", 0);
223     az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
224     db_check(zFilename, "tX avg", az, "101", 0);
225     db_execute(db, zFilename, "DELETE FROM t%d WHERE a>50", t);
226     az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
227     db_check(zFilename, "tX avg2", az, "51", 0);
228     for(i=1; i<=50; i++){
229       char z1[30], z2[30];
230       az = db_query(db, zFilename, "SELECT b, c FROM t%d WHERE a=%d", t, i);
231       sprintf(z1, "%d", i*2);
232       sprintf(z2, "%d", i*i);
233       db_check(zFilename, "readback", az, z1, z2, 0);
234     }
235     db_execute(db, zFilename, "DROP TABLE t%d;", t);
236     sqlite_close(db);
237   }
238   printf("%s: END\n", zFilename);
239   /* unlink(zFilename); */
240   fflush(stdout);
241   pthread_mutex_lock(&lock);
242   thread_cnt--;
243   if( thread_cnt<=0 ){
244     pthread_cond_signal(&sig);
245   }
246   pthread_mutex_unlock(&lock);
247   return 0;
248 }
249 
main(int argc,char ** argv)250 int main(int argc, char **argv){
251   char *zFile;
252   int i, n;
253   pthread_t id;
254   if( argc>2 && strcmp(argv[1], "-v")==0 ){
255     verbose = 1;
256     argc--;
257     argv++;
258   }
259   if( argc<2 || (n=atoi(argv[1]))<1 ) n = 10;
260   for(i=0; i<n; i++){
261     char zBuf[200];
262     sprintf(zBuf, "testdb-%d", (i+1)/2);
263     unlink(zBuf);
264   }
265   for(i=0; i<n; i++){
266     zFile = sqlite_mprintf("%d.testdb-%d", i%2+1, (i+2)/2);
267     unlink(zFile);
268     pthread_create(&id, 0, worker_bee, (void*)zFile);
269     pthread_detach(id);
270   }
271   pthread_mutex_lock(&lock);
272   while( thread_cnt>0 ){
273     pthread_cond_wait(&sig, &lock);
274   }
275   pthread_mutex_unlock(&lock);
276   for(i=0; i<n; i++){
277     char zBuf[200];
278     sprintf(zBuf, "testdb-%d", (i+1)/2);
279     unlink(zBuf);
280   }
281   return 0;
282 }
283