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