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