4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** Code for testing the the SQLite library in a multithreaded environment.
14 #include "sqliteInt.h"
16 #if SQLITE_OS_UNIX && SQLITE_THREADSAFE
24 ** Each thread is controlled by an instance of the following
27 typedef struct Thread Thread
;
29 /* The first group of fields are writable by the master and read-only
31 char *zFilename
; /* Name of database file */
32 void (*xOp
)(Thread
*); /* next operation to do */
33 char *zArg
; /* argument usable by xOp */
34 int opnum
; /* Operation number */
35 int busy
; /* True if this thread is in use */
37 /* The next group of fields are writable by the thread but read-only to the
39 int completed
; /* Number of operations completed */
40 sqlite3
*db
; /* Open database */
41 sqlite3_stmt
*pStmt
; /* Pending operation */
42 char *zErr
; /* operation error */
43 char *zStaticErr
; /* Static error message */
44 int rc
; /* operation return code */
45 int argc
; /* number of columns in result */
46 const char *argv
[100]; /* result columns */
47 const char *colv
[100]; /* result column names */
51 ** There can be as many as 26 threads running at once. Each is named
52 ** by a capital letter: A, B, C, ..., Y, Z.
55 static Thread threadset
[N_THREAD
];
59 ** The main loop for a thread. Threads use busy waiting.
61 static void *thread_main(void *pArg
){
62 Thread
*p
= (Thread
*)pArg
;
66 sqlite3_open(p
->zFilename
, &p
->db
);
67 if( SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
68 p
->zErr
= strdup(sqlite3_errmsg(p
->db
));
74 while( p
->opnum
<=p
->completed
) sched_yield();
76 if( p
->zErr
&& p
->zErr
!=p
->zStaticErr
){
77 sqlite3_free(p
->zErr
);
82 while( p
->opnum
<=p
->completed
) sched_yield();
85 sqlite3_finalize(p
->pStmt
);
92 if( p
->zErr
&& p
->zErr
!=p
->zStaticErr
){
93 sqlite3_free(p
->zErr
);
97 #ifndef SQLITE_OMIT_DEPRECATED
98 sqlite3_thread_cleanup();
104 ** Get a thread ID which is an upper case letter. Return the index.
105 ** If the argument is not a valid thread ID put an error message in
106 ** the interpreter and return -1.
108 static int parse_thread_id(Tcl_Interp
*interp
, const char *zArg
){
109 if( zArg
==0 || zArg
[0]==0 || zArg
[1]!=0 || !isupper((unsigned char)zArg
[0]) ){
110 Tcl_AppendResult(interp
, "thread ID must be an upper case letter", 0);
113 return zArg
[0] - 'A';
117 ** Usage: thread_create NAME FILENAME
119 ** NAME should be an upper case letter. Start the thread running with
120 ** an open connection to the given database.
122 static int tcl_thread_create(
124 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
125 int argc
, /* Number of arguments */
126 const char **argv
/* Text of each argument */
133 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
137 i
= parse_thread_id(interp
, argv
[1]);
138 if( i
<0 ) return TCL_ERROR
;
139 if( threadset
[i
].busy
){
140 Tcl_AppendResult(interp
, "thread ", argv
[1], " is already running", 0);
143 threadset
[i
].busy
= 1;
144 sqlite3_free(threadset
[i
].zFilename
);
145 threadset
[i
].zFilename
= sqlite3_mprintf("%s", argv
[2]);
146 threadset
[i
].opnum
= 1;
147 threadset
[i
].completed
= 0;
148 rc
= pthread_create(&x
, 0, thread_main
, &threadset
[i
]);
150 Tcl_AppendResult(interp
, "failed to create the thread", 0);
151 sqlite3_free(threadset
[i
].zFilename
);
152 threadset
[i
].busy
= 0;
160 ** Wait for a thread to reach its idle state.
162 static void thread_wait(Thread
*p
){
163 while( p
->opnum
>p
->completed
) sched_yield();
167 ** Usage: thread_wait ID
169 ** Wait on thread ID to reach its idle state.
171 static int tcl_thread_wait(
173 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
174 int argc
, /* Number of arguments */
175 const char **argv
/* Text of each argument */
180 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
184 i
= parse_thread_id(interp
, argv
[1]);
185 if( i
<0 ) return TCL_ERROR
;
186 if( !threadset
[i
].busy
){
187 Tcl_AppendResult(interp
, "no such thread", 0);
190 thread_wait(&threadset
[i
]);
197 static void stop_thread(Thread
*p
){
202 sqlite3_free(p
->zArg
);
204 sqlite3_free(p
->zFilename
);
210 ** Usage: thread_halt ID
212 ** Cause a thread to shut itself down. Wait for the shutdown to be
213 ** completed. If ID is "*" then stop all threads.
215 static int tcl_thread_halt(
217 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
218 int argc
, /* Number of arguments */
219 const char **argv
/* Text of each argument */
224 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
228 if( argv
[1][0]=='*' && argv
[1][1]==0 ){
229 for(i
=0; i
<N_THREAD
; i
++){
230 if( threadset
[i
].busy
) stop_thread(&threadset
[i
]);
233 i
= parse_thread_id(interp
, argv
[1]);
234 if( i
<0 ) return TCL_ERROR
;
235 if( !threadset
[i
].busy
){
236 Tcl_AppendResult(interp
, "no such thread", 0);
239 stop_thread(&threadset
[i
]);
245 ** Usage: thread_argc ID
247 ** Wait on the most recent thread_step to complete, then return the
248 ** number of columns in the result set.
250 static int tcl_thread_argc(
252 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
253 int argc
, /* Number of arguments */
254 const char **argv
/* Text of each argument */
260 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
264 i
= parse_thread_id(interp
, argv
[1]);
265 if( i
<0 ) return TCL_ERROR
;
266 if( !threadset
[i
].busy
){
267 Tcl_AppendResult(interp
, "no such thread", 0);
270 thread_wait(&threadset
[i
]);
271 sprintf(zBuf
, "%d", threadset
[i
].argc
);
272 Tcl_AppendResult(interp
, zBuf
, 0);
277 ** Usage: thread_argv ID N
279 ** Wait on the most recent thread_step to complete, then return the
280 ** value of the N-th columns in the result set.
282 static int tcl_thread_argv(
284 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
285 int argc
, /* Number of arguments */
286 const char **argv
/* Text of each argument */
292 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
296 i
= parse_thread_id(interp
, argv
[1]);
297 if( i
<0 ) return TCL_ERROR
;
298 if( !threadset
[i
].busy
){
299 Tcl_AppendResult(interp
, "no such thread", 0);
302 if( Tcl_GetInt(interp
, argv
[2], &n
) ) return TCL_ERROR
;
303 thread_wait(&threadset
[i
]);
304 if( n
<0 || n
>=threadset
[i
].argc
){
305 Tcl_AppendResult(interp
, "column number out of range", 0);
308 Tcl_AppendResult(interp
, threadset
[i
].argv
[n
], 0);
313 ** Usage: thread_colname ID N
315 ** Wait on the most recent thread_step to complete, then return the
316 ** name of the N-th columns in the result set.
318 static int tcl_thread_colname(
320 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
321 int argc
, /* Number of arguments */
322 const char **argv
/* Text of each argument */
328 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
332 i
= parse_thread_id(interp
, argv
[1]);
333 if( i
<0 ) return TCL_ERROR
;
334 if( !threadset
[i
].busy
){
335 Tcl_AppendResult(interp
, "no such thread", 0);
338 if( Tcl_GetInt(interp
, argv
[2], &n
) ) return TCL_ERROR
;
339 thread_wait(&threadset
[i
]);
340 if( n
<0 || n
>=threadset
[i
].argc
){
341 Tcl_AppendResult(interp
, "column number out of range", 0);
344 Tcl_AppendResult(interp
, threadset
[i
].colv
[n
], 0);
349 ** Usage: thread_result ID
351 ** Wait on the most recent operation to complete, then return the
352 ** result code from that operation.
354 static int tcl_thread_result(
356 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
357 int argc
, /* Number of arguments */
358 const char **argv
/* Text of each argument */
364 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
368 i
= parse_thread_id(interp
, argv
[1]);
369 if( i
<0 ) return TCL_ERROR
;
370 if( !threadset
[i
].busy
){
371 Tcl_AppendResult(interp
, "no such thread", 0);
374 thread_wait(&threadset
[i
]);
375 switch( threadset
[i
].rc
){
376 case SQLITE_OK
: zName
= "SQLITE_OK"; break;
377 case SQLITE_ERROR
: zName
= "SQLITE_ERROR"; break;
378 case SQLITE_PERM
: zName
= "SQLITE_PERM"; break;
379 case SQLITE_ABORT
: zName
= "SQLITE_ABORT"; break;
380 case SQLITE_BUSY
: zName
= "SQLITE_BUSY"; break;
381 case SQLITE_LOCKED
: zName
= "SQLITE_LOCKED"; break;
382 case SQLITE_NOMEM
: zName
= "SQLITE_NOMEM"; break;
383 case SQLITE_READONLY
: zName
= "SQLITE_READONLY"; break;
384 case SQLITE_INTERRUPT
: zName
= "SQLITE_INTERRUPT"; break;
385 case SQLITE_IOERR
: zName
= "SQLITE_IOERR"; break;
386 case SQLITE_CORRUPT
: zName
= "SQLITE_CORRUPT"; break;
387 case SQLITE_FULL
: zName
= "SQLITE_FULL"; break;
388 case SQLITE_CANTOPEN
: zName
= "SQLITE_CANTOPEN"; break;
389 case SQLITE_PROTOCOL
: zName
= "SQLITE_PROTOCOL"; break;
390 case SQLITE_EMPTY
: zName
= "SQLITE_EMPTY"; break;
391 case SQLITE_SCHEMA
: zName
= "SQLITE_SCHEMA"; break;
392 case SQLITE_CONSTRAINT
: zName
= "SQLITE_CONSTRAINT"; break;
393 case SQLITE_MISMATCH
: zName
= "SQLITE_MISMATCH"; break;
394 case SQLITE_MISUSE
: zName
= "SQLITE_MISUSE"; break;
395 case SQLITE_NOLFS
: zName
= "SQLITE_NOLFS"; break;
396 case SQLITE_AUTH
: zName
= "SQLITE_AUTH"; break;
397 case SQLITE_FORMAT
: zName
= "SQLITE_FORMAT"; break;
398 case SQLITE_RANGE
: zName
= "SQLITE_RANGE"; break;
399 case SQLITE_ROW
: zName
= "SQLITE_ROW"; break;
400 case SQLITE_DONE
: zName
= "SQLITE_DONE"; break;
401 default: zName
= "SQLITE_Unknown"; break;
403 Tcl_AppendResult(interp
, zName
, 0);
408 ** Usage: thread_error ID
410 ** Wait on the most recent operation to complete, then return the
413 static int tcl_thread_error(
415 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
416 int argc
, /* Number of arguments */
417 const char **argv
/* Text of each argument */
422 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
426 i
= parse_thread_id(interp
, argv
[1]);
427 if( i
<0 ) return TCL_ERROR
;
428 if( !threadset
[i
].busy
){
429 Tcl_AppendResult(interp
, "no such thread", 0);
432 thread_wait(&threadset
[i
]);
433 Tcl_AppendResult(interp
, threadset
[i
].zErr
, 0);
438 ** This procedure runs in the thread to compile an SQL statement.
440 static void do_compile(Thread
*p
){
442 p
->zErr
= p
->zStaticErr
= "no database is open";
443 p
->rc
= SQLITE_ERROR
;
447 sqlite3_finalize(p
->pStmt
);
450 p
->rc
= sqlite3_prepare(p
->db
, p
->zArg
, -1, &p
->pStmt
, 0);
454 ** Usage: thread_compile ID SQL
456 ** Compile a new virtual machine.
458 static int tcl_thread_compile(
460 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
461 int argc
, /* Number of arguments */
462 const char **argv
/* Text of each argument */
466 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
470 i
= parse_thread_id(interp
, argv
[1]);
471 if( i
<0 ) return TCL_ERROR
;
472 if( !threadset
[i
].busy
){
473 Tcl_AppendResult(interp
, "no such thread", 0);
476 thread_wait(&threadset
[i
]);
477 threadset
[i
].xOp
= do_compile
;
478 sqlite3_free(threadset
[i
].zArg
);
479 threadset
[i
].zArg
= sqlite3_mprintf("%s", argv
[2]);
480 threadset
[i
].opnum
++;
485 ** This procedure runs in the thread to step the virtual machine.
487 static void do_step(Thread
*p
){
490 p
->zErr
= p
->zStaticErr
= "no virtual machine available";
491 p
->rc
= SQLITE_ERROR
;
494 p
->rc
= sqlite3_step(p
->pStmt
);
495 if( p
->rc
==SQLITE_ROW
){
496 p
->argc
= sqlite3_column_count(p
->pStmt
);
497 for(i
=0; i
<sqlite3_data_count(p
->pStmt
); i
++){
498 p
->argv
[i
] = (char*)sqlite3_column_text(p
->pStmt
, i
);
500 for(i
=0; i
<p
->argc
; i
++){
501 p
->colv
[i
] = sqlite3_column_name(p
->pStmt
, i
);
507 ** Usage: thread_step ID
509 ** Advance the virtual machine by one step
511 static int tcl_thread_step(
513 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
514 int argc
, /* Number of arguments */
515 const char **argv
/* Text of each argument */
519 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
523 i
= parse_thread_id(interp
, argv
[1]);
524 if( i
<0 ) return TCL_ERROR
;
525 if( !threadset
[i
].busy
){
526 Tcl_AppendResult(interp
, "no such thread", 0);
529 thread_wait(&threadset
[i
]);
530 threadset
[i
].xOp
= do_step
;
531 threadset
[i
].opnum
++;
536 ** This procedure runs in the thread to finalize a virtual machine.
538 static void do_finalize(Thread
*p
){
540 p
->zErr
= p
->zStaticErr
= "no virtual machine available";
541 p
->rc
= SQLITE_ERROR
;
544 p
->rc
= sqlite3_finalize(p
->pStmt
);
549 ** Usage: thread_finalize ID
551 ** Finalize the virtual machine.
553 static int tcl_thread_finalize(
555 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
556 int argc
, /* Number of arguments */
557 const char **argv
/* Text of each argument */
561 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
565 i
= parse_thread_id(interp
, argv
[1]);
566 if( i
<0 ) return TCL_ERROR
;
567 if( !threadset
[i
].busy
){
568 Tcl_AppendResult(interp
, "no such thread", 0);
571 thread_wait(&threadset
[i
]);
572 threadset
[i
].xOp
= do_finalize
;
573 sqlite3_free(threadset
[i
].zArg
);
574 threadset
[i
].zArg
= 0;
575 threadset
[i
].opnum
++;
580 ** Usage: thread_swap ID ID
582 ** Interchange the sqlite* pointer between two threads.
584 static int tcl_thread_swap(
586 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
587 int argc
, /* Number of arguments */
588 const char **argv
/* Text of each argument */
593 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
597 i
= parse_thread_id(interp
, argv
[1]);
598 if( i
<0 ) return TCL_ERROR
;
599 if( !threadset
[i
].busy
){
600 Tcl_AppendResult(interp
, "no such thread", 0);
603 thread_wait(&threadset
[i
]);
604 j
= parse_thread_id(interp
, argv
[2]);
605 if( j
<0 ) return TCL_ERROR
;
606 if( !threadset
[j
].busy
){
607 Tcl_AppendResult(interp
, "no such thread", 0);
610 thread_wait(&threadset
[j
]);
611 temp
= threadset
[i
].db
;
612 threadset
[i
].db
= threadset
[j
].db
;
613 threadset
[j
].db
= temp
;
618 ** Usage: thread_db_get ID
620 ** Return the database connection pointer for the given thread. Then
621 ** remove the pointer from the thread itself. Afterwards, the thread
622 ** can be stopped and the connection can be used by the main thread.
624 static int tcl_thread_db_get(
626 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
627 int argc
, /* Number of arguments */
628 const char **argv
/* Text of each argument */
632 extern int sqlite3TestMakePointerStr(Tcl_Interp
*, char*, void*);
634 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
638 i
= parse_thread_id(interp
, argv
[1]);
639 if( i
<0 ) return TCL_ERROR
;
640 if( !threadset
[i
].busy
){
641 Tcl_AppendResult(interp
, "no such thread", 0);
644 thread_wait(&threadset
[i
]);
645 sqlite3TestMakePointerStr(interp
, zBuf
, threadset
[i
].db
);
647 Tcl_AppendResult(interp
, zBuf
, (char*)0);
652 ** Usage: thread_db_put ID DB
655 static int tcl_thread_db_put(
657 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
658 int argc
, /* Number of arguments */
659 const char **argv
/* Text of each argument */
662 extern int sqlite3TestMakePointerStr(Tcl_Interp
*, char*, void*);
663 extern void *sqlite3TestTextToPtr(const char *);
665 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
669 i
= parse_thread_id(interp
, argv
[1]);
670 if( i
<0 ) return TCL_ERROR
;
671 if( !threadset
[i
].busy
){
672 Tcl_AppendResult(interp
, "no such thread", 0);
675 thread_wait(&threadset
[i
]);
676 assert( !threadset
[i
].db
);
677 threadset
[i
].db
= (sqlite3
*)sqlite3TestTextToPtr(argv
[2]);
682 ** Usage: thread_stmt_get ID
684 ** Return the database stmt pointer for the given thread. Then
685 ** remove the pointer from the thread itself.
687 static int tcl_thread_stmt_get(
689 Tcl_Interp
*interp
, /* The TCL interpreter that invoked this command */
690 int argc
, /* Number of arguments */
691 const char **argv
/* Text of each argument */
695 extern int sqlite3TestMakePointerStr(Tcl_Interp
*, char*, void*);
697 Tcl_AppendResult(interp
, "wrong # args: should be \"", argv
[0],
701 i
= parse_thread_id(interp
, argv
[1]);
702 if( i
<0 ) return TCL_ERROR
;
703 if( !threadset
[i
].busy
){
704 Tcl_AppendResult(interp
, "no such thread", 0);
707 thread_wait(&threadset
[i
]);
708 sqlite3TestMakePointerStr(interp
, zBuf
, threadset
[i
].pStmt
);
709 threadset
[i
].pStmt
= 0;
710 Tcl_AppendResult(interp
, zBuf
, (char*)0);
715 ** Register commands with the TCL interpreter.
717 int Sqlitetest4_Init(Tcl_Interp
*interp
){
722 { "thread_create", (Tcl_CmdProc
*)tcl_thread_create
},
723 { "thread_wait", (Tcl_CmdProc
*)tcl_thread_wait
},
724 { "thread_halt", (Tcl_CmdProc
*)tcl_thread_halt
},
725 { "thread_argc", (Tcl_CmdProc
*)tcl_thread_argc
},
726 { "thread_argv", (Tcl_CmdProc
*)tcl_thread_argv
},
727 { "thread_colname", (Tcl_CmdProc
*)tcl_thread_colname
},
728 { "thread_result", (Tcl_CmdProc
*)tcl_thread_result
},
729 { "thread_error", (Tcl_CmdProc
*)tcl_thread_error
},
730 { "thread_compile", (Tcl_CmdProc
*)tcl_thread_compile
},
731 { "thread_step", (Tcl_CmdProc
*)tcl_thread_step
},
732 { "thread_finalize", (Tcl_CmdProc
*)tcl_thread_finalize
},
733 { "thread_swap", (Tcl_CmdProc
*)tcl_thread_swap
},
734 { "thread_db_get", (Tcl_CmdProc
*)tcl_thread_db_get
},
735 { "thread_db_put", (Tcl_CmdProc
*)tcl_thread_db_put
},
736 { "thread_stmt_get", (Tcl_CmdProc
*)tcl_thread_stmt_get
},
740 for(i
=0; i
<sizeof(aCmd
)/sizeof(aCmd
[0]); i
++){
741 Tcl_CreateCommand(interp
, aCmd
[i
].zName
, aCmd
[i
].xProc
, 0, 0);
746 int Sqlitetest4_Init(Tcl_Interp
*interp
){ return TCL_OK
; }
747 #endif /* SQLITE_OS_UNIX */