xref: /sqlite-3.40.0/src/test4.c (revision c7d7ebd7)
1 /*
2 ** 2003 December 18
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 ** Code for testing the SQLite library in a multithreaded environment.
13 */
14 #include "sqliteInt.h"
15 #if defined(INCLUDE_SQLITE_TCL_H)
16 #  include "sqlite_tcl.h"
17 #else
18 #  include "tcl.h"
19 #endif
20 #if SQLITE_OS_UNIX && SQLITE_THREADSAFE
21 #include <stdlib.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <sched.h>
25 #include <ctype.h>
26 
27 extern const char *sqlite3ErrName(int);
28 
29 /*
30 ** Each thread is controlled by an instance of the following
31 ** structure.
32 */
33 typedef struct Thread Thread;
34 struct Thread {
35   /* The first group of fields are writable by the leader and read-only
36   ** to the thread. */
37   char *zFilename;       /* Name of database file */
38   void (*xOp)(Thread*);  /* next operation to do */
39   char *zArg;            /* argument usable by xOp */
40   int opnum;             /* Operation number */
41   int busy;              /* True if this thread is in use */
42 
43   /* The next group of fields are writable by the thread but read-only to the
44   ** leader. */
45   int completed;        /* Number of operations completed */
46   sqlite3 *db;           /* Open database */
47   sqlite3_stmt *pStmt;     /* Pending operation */
48   char *zErr;           /* operation error */
49   char *zStaticErr;     /* Static error message */
50   int rc;               /* operation return code */
51   int argc;             /* number of columns in result */
52   const char *argv[100];    /* result columns */
53   const char *colv[100];    /* result column names */
54 };
55 
56 /*
57 ** There can be as many as 26 threads running at once.  Each is named
58 ** by a capital letter: A, B, C, ..., Y, Z.
59 */
60 #define N_THREAD 26
61 static Thread threadset[N_THREAD];
62 
test_barrier()63 static void test_barrier(){
64   sqlite3_mutex *pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_APP1);
65   sqlite3_mutex_enter(pMutex);
66   sqlite3_mutex_leave(pMutex);
67 }
68 
69 /*
70 ** The main loop for a thread.  Threads use busy waiting.
71 */
test_thread_main(void * pArg)72 static void *test_thread_main(void *pArg){
73   Thread *p = (Thread*)pArg;
74   if( p->db ){
75     sqlite3_close(p->db);
76   }
77   sqlite3_open(p->zFilename, &p->db);
78   if( SQLITE_OK!=sqlite3_errcode(p->db) ){
79     p->zErr = strdup(sqlite3_errmsg(p->db));
80     sqlite3_close(p->db);
81     p->db = 0;
82   }
83   p->pStmt = 0;
84   test_barrier();
85   p->completed = 1;
86   while( p->opnum<=p->completed ) sched_yield();
87   test_barrier();
88   while( p->xOp ){
89     if( p->zErr && p->zErr!=p->zStaticErr ){
90       sqlite3_free(p->zErr);
91       p->zErr = 0;
92     }
93     (*p->xOp)(p);
94     test_barrier();
95     p->completed++;
96     while( p->opnum<=p->completed ) sched_yield();
97     test_barrier();
98   }
99   if( p->pStmt ){
100     sqlite3_finalize(p->pStmt);
101     p->pStmt = 0;
102   }
103   if( p->db ){
104     sqlite3_close(p->db);
105     p->db = 0;
106   }
107   if( p->zErr && p->zErr!=p->zStaticErr ){
108     sqlite3_free(p->zErr);
109     p->zErr = 0;
110   }
111   test_barrier();
112   p->completed++;
113 #ifndef SQLITE_OMIT_DEPRECATED
114   sqlite3_thread_cleanup();
115 #endif
116   return 0;
117 }
118 
119 /*
120 ** Get a thread ID which is an upper case letter.  Return the index.
121 ** If the argument is not a valid thread ID put an error message in
122 ** the interpreter and return -1.
123 */
parse_thread_id(Tcl_Interp * interp,const char * zArg)124 static int parse_thread_id(Tcl_Interp *interp, const char *zArg){
125   if( zArg==0 || zArg[0]==0 || zArg[1]!=0 || !isupper((unsigned char)zArg[0]) ){
126     Tcl_AppendResult(interp, "thread ID must be an upper case letter", 0);
127     return -1;
128   }
129   return zArg[0] - 'A';
130 }
131 
132 /*
133 ** Usage:    thread_create NAME  FILENAME
134 **
135 ** NAME should be an upper case letter.  Start the thread running with
136 ** an open connection to the given database.
137 */
tcl_thread_create(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)138 static int SQLITE_TCLAPI tcl_thread_create(
139   void *NotUsed,
140   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
141   int argc,              /* Number of arguments */
142   const char **argv      /* Text of each argument */
143 ){
144   int i;
145   pthread_t x;
146   int rc;
147 
148   if( argc!=3 ){
149     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
150        " ID FILENAME", 0);
151     return TCL_ERROR;
152   }
153   i = parse_thread_id(interp, argv[1]);
154   if( i<0 ) return TCL_ERROR;
155   if( threadset[i].busy ){
156     Tcl_AppendResult(interp, "thread ", argv[1], " is already running", 0);
157     return TCL_ERROR;
158   }
159   threadset[i].busy = 1;
160   sqlite3_free(threadset[i].zFilename);
161   threadset[i].zFilename = sqlite3_mprintf("%s", argv[2]);
162   threadset[i].opnum = 1;
163   threadset[i].completed = 0;
164   rc = pthread_create(&x, 0, test_thread_main, &threadset[i]);
165   if( rc ){
166     Tcl_AppendResult(interp, "failed to create the thread", 0);
167     sqlite3_free(threadset[i].zFilename);
168     threadset[i].busy = 0;
169     return TCL_ERROR;
170   }
171   pthread_detach(x);
172   return TCL_OK;
173 }
174 
175 /*
176 ** Wait for a thread to reach its idle state.
177 */
test_thread_wait(Thread * p)178 static void test_thread_wait(Thread *p){
179   test_barrier();
180   while( p->opnum>p->completed ) sched_yield();
181   test_barrier();
182 }
183 
184 /*
185 ** Usage:  thread_wait ID
186 **
187 ** Wait on thread ID to reach its idle state.
188 */
tcl_thread_wait(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)189 static int SQLITE_TCLAPI tcl_thread_wait(
190   void *NotUsed,
191   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
192   int argc,              /* Number of arguments */
193   const char **argv      /* Text of each argument */
194 ){
195   int i;
196 
197   if( argc!=2 ){
198     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
199        " ID", 0);
200     return TCL_ERROR;
201   }
202   i = parse_thread_id(interp, argv[1]);
203   if( i<0 ) return TCL_ERROR;
204   if( !threadset[i].busy ){
205     Tcl_AppendResult(interp, "no such thread", 0);
206     return TCL_ERROR;
207   }
208   test_thread_wait(&threadset[i]);
209   return TCL_OK;
210 }
211 
212 /*
213 ** Stop a thread.
214 */
test_stop_thread(Thread * p)215 static void test_stop_thread(Thread *p){
216   test_thread_wait(p);
217   p->xOp = 0;
218   p->opnum++;
219   test_thread_wait(p);
220   sqlite3_free(p->zArg);
221   p->zArg = 0;
222   sqlite3_free(p->zFilename);
223   p->zFilename = 0;
224   p->busy = 0;
225 }
226 
227 /*
228 ** Usage:  thread_halt ID
229 **
230 ** Cause a thread to shut itself down.  Wait for the shutdown to be
231 ** completed.  If ID is "*" then stop all threads.
232 */
tcl_thread_halt(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)233 static int SQLITE_TCLAPI tcl_thread_halt(
234   void *NotUsed,
235   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
236   int argc,              /* Number of arguments */
237   const char **argv      /* Text of each argument */
238 ){
239   int i;
240 
241   if( argc!=2 ){
242     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
243        " ID", 0);
244     return TCL_ERROR;
245   }
246   if( argv[1][0]=='*' && argv[1][1]==0 ){
247     for(i=0; i<N_THREAD; i++){
248       if( threadset[i].busy ) test_stop_thread(&threadset[i]);
249     }
250   }else{
251     i = parse_thread_id(interp, argv[1]);
252     if( i<0 ) return TCL_ERROR;
253     if( !threadset[i].busy ){
254       Tcl_AppendResult(interp, "no such thread", 0);
255       return TCL_ERROR;
256     }
257     test_stop_thread(&threadset[i]);
258   }
259   return TCL_OK;
260 }
261 
262 /*
263 ** Usage: thread_argc  ID
264 **
265 ** Wait on the most recent thread_step to complete, then return the
266 ** number of columns in the result set.
267 */
tcl_thread_argc(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)268 static int SQLITE_TCLAPI tcl_thread_argc(
269   void *NotUsed,
270   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
271   int argc,              /* Number of arguments */
272   const char **argv      /* Text of each argument */
273 ){
274   int i;
275   char zBuf[100];
276 
277   if( argc!=2 ){
278     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
279        " ID", 0);
280     return TCL_ERROR;
281   }
282   i = parse_thread_id(interp, argv[1]);
283   if( i<0 ) return TCL_ERROR;
284   if( !threadset[i].busy ){
285     Tcl_AppendResult(interp, "no such thread", 0);
286     return TCL_ERROR;
287   }
288   test_thread_wait(&threadset[i]);
289   sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", threadset[i].argc);
290   Tcl_AppendResult(interp, zBuf, 0);
291   return TCL_OK;
292 }
293 
294 /*
295 ** Usage: thread_argv  ID   N
296 **
297 ** Wait on the most recent thread_step to complete, then return the
298 ** value of the N-th columns in the result set.
299 */
tcl_thread_argv(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)300 static int SQLITE_TCLAPI tcl_thread_argv(
301   void *NotUsed,
302   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
303   int argc,              /* Number of arguments */
304   const char **argv      /* Text of each argument */
305 ){
306   int i;
307   int n;
308 
309   if( argc!=3 ){
310     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
311        " ID N", 0);
312     return TCL_ERROR;
313   }
314   i = parse_thread_id(interp, argv[1]);
315   if( i<0 ) return TCL_ERROR;
316   if( !threadset[i].busy ){
317     Tcl_AppendResult(interp, "no such thread", 0);
318     return TCL_ERROR;
319   }
320   if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR;
321   test_thread_wait(&threadset[i]);
322   if( n<0 || n>=threadset[i].argc ){
323     Tcl_AppendResult(interp, "column number out of range", 0);
324     return TCL_ERROR;
325   }
326   Tcl_AppendResult(interp, threadset[i].argv[n], 0);
327   return TCL_OK;
328 }
329 
330 /*
331 ** Usage: thread_colname  ID   N
332 **
333 ** Wait on the most recent thread_step to complete, then return the
334 ** name of the N-th columns in the result set.
335 */
tcl_thread_colname(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)336 static int SQLITE_TCLAPI tcl_thread_colname(
337   void *NotUsed,
338   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
339   int argc,              /* Number of arguments */
340   const char **argv      /* Text of each argument */
341 ){
342   int i;
343   int n;
344 
345   if( argc!=3 ){
346     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
347        " ID N", 0);
348     return TCL_ERROR;
349   }
350   i = parse_thread_id(interp, argv[1]);
351   if( i<0 ) return TCL_ERROR;
352   if( !threadset[i].busy ){
353     Tcl_AppendResult(interp, "no such thread", 0);
354     return TCL_ERROR;
355   }
356   if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR;
357   test_thread_wait(&threadset[i]);
358   if( n<0 || n>=threadset[i].argc ){
359     Tcl_AppendResult(interp, "column number out of range", 0);
360     return TCL_ERROR;
361   }
362   Tcl_AppendResult(interp, threadset[i].colv[n], 0);
363   return TCL_OK;
364 }
365 
366 /*
367 ** Usage: thread_result  ID
368 **
369 ** Wait on the most recent operation to complete, then return the
370 ** result code from that operation.
371 */
tcl_thread_result(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)372 static int SQLITE_TCLAPI tcl_thread_result(
373   void *NotUsed,
374   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
375   int argc,              /* Number of arguments */
376   const char **argv      /* Text of each argument */
377 ){
378   int i;
379   const char *zName;
380 
381   if( argc!=2 ){
382     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
383        " ID", 0);
384     return TCL_ERROR;
385   }
386   i = parse_thread_id(interp, argv[1]);
387   if( i<0 ) return TCL_ERROR;
388   if( !threadset[i].busy ){
389     Tcl_AppendResult(interp, "no such thread", 0);
390     return TCL_ERROR;
391   }
392   test_thread_wait(&threadset[i]);
393   zName = sqlite3ErrName(threadset[i].rc);
394   Tcl_AppendResult(interp, zName, 0);
395   return TCL_OK;
396 }
397 
398 /*
399 ** Usage: thread_error  ID
400 **
401 ** Wait on the most recent operation to complete, then return the
402 ** error string.
403 */
tcl_thread_error(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)404 static int SQLITE_TCLAPI tcl_thread_error(
405   void *NotUsed,
406   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
407   int argc,              /* Number of arguments */
408   const char **argv      /* Text of each argument */
409 ){
410   int i;
411 
412   if( argc!=2 ){
413     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
414        " ID", 0);
415     return TCL_ERROR;
416   }
417   i = parse_thread_id(interp, argv[1]);
418   if( i<0 ) return TCL_ERROR;
419   if( !threadset[i].busy ){
420     Tcl_AppendResult(interp, "no such thread", 0);
421     return TCL_ERROR;
422   }
423   test_thread_wait(&threadset[i]);
424   Tcl_AppendResult(interp, threadset[i].zErr, 0);
425   return TCL_OK;
426 }
427 
428 /*
429 ** This procedure runs in the thread to compile an SQL statement.
430 */
do_compile(Thread * p)431 static void do_compile(Thread *p){
432   if( p->db==0 ){
433     p->zErr = p->zStaticErr = "no database is open";
434     p->rc = SQLITE_ERROR;
435     return;
436   }
437   if( p->pStmt ){
438     sqlite3_finalize(p->pStmt);
439     p->pStmt = 0;
440   }
441   p->rc = sqlite3_prepare(p->db, p->zArg, -1, &p->pStmt, 0);
442 }
443 
444 /*
445 ** Usage: thread_compile ID SQL
446 **
447 ** Compile a new virtual machine.
448 */
tcl_thread_compile(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)449 static int SQLITE_TCLAPI tcl_thread_compile(
450   void *NotUsed,
451   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
452   int argc,              /* Number of arguments */
453   const char **argv      /* Text of each argument */
454 ){
455   int i;
456   if( argc!=3 ){
457     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
458        " ID SQL", 0);
459     return TCL_ERROR;
460   }
461   i = parse_thread_id(interp, argv[1]);
462   if( i<0 ) return TCL_ERROR;
463   if( !threadset[i].busy ){
464     Tcl_AppendResult(interp, "no such thread", 0);
465     return TCL_ERROR;
466   }
467   test_thread_wait(&threadset[i]);
468   threadset[i].xOp = do_compile;
469   sqlite3_free(threadset[i].zArg);
470   threadset[i].zArg = sqlite3_mprintf("%s", argv[2]);
471   test_barrier();
472   threadset[i].opnum++;
473   return TCL_OK;
474 }
475 
476 /*
477 ** This procedure runs in the thread to step the virtual machine.
478 */
do_step(Thread * p)479 static void do_step(Thread *p){
480   int i;
481   if( p->pStmt==0 ){
482     p->zErr = p->zStaticErr = "no virtual machine available";
483     p->rc = SQLITE_ERROR;
484     return;
485   }
486   p->rc = sqlite3_step(p->pStmt);
487   if( p->rc==SQLITE_ROW ){
488     p->argc = sqlite3_column_count(p->pStmt);
489     for(i=0; i<sqlite3_data_count(p->pStmt); i++){
490       p->argv[i] = (char*)sqlite3_column_text(p->pStmt, i);
491     }
492     for(i=0; i<p->argc; i++){
493       p->colv[i] = sqlite3_column_name(p->pStmt, i);
494     }
495   }
496 }
497 
498 /*
499 ** Usage: thread_step ID
500 **
501 ** Advance the virtual machine by one step
502 */
tcl_thread_step(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)503 static int SQLITE_TCLAPI tcl_thread_step(
504   void *NotUsed,
505   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
506   int argc,              /* Number of arguments */
507   const char **argv      /* Text of each argument */
508 ){
509   int i;
510   if( argc!=2 ){
511     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
512        " IDL", 0);
513     return TCL_ERROR;
514   }
515   i = parse_thread_id(interp, argv[1]);
516   if( i<0 ) return TCL_ERROR;
517   if( !threadset[i].busy ){
518     Tcl_AppendResult(interp, "no such thread", 0);
519     return TCL_ERROR;
520   }
521   test_thread_wait(&threadset[i]);
522   threadset[i].xOp = do_step;
523   test_barrier();
524   threadset[i].opnum++;
525   return TCL_OK;
526 }
527 
528 /*
529 ** This procedure runs in the thread to finalize a virtual machine.
530 */
do_finalize(Thread * p)531 static void do_finalize(Thread *p){
532   if( p->pStmt==0 ){
533     p->zErr = p->zStaticErr = "no virtual machine available";
534     p->rc = SQLITE_ERROR;
535     return;
536   }
537   p->rc = sqlite3_finalize(p->pStmt);
538   p->pStmt = 0;
539 }
540 
541 /*
542 ** Usage: thread_finalize ID
543 **
544 ** Finalize the virtual machine.
545 */
tcl_thread_finalize(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)546 static int SQLITE_TCLAPI tcl_thread_finalize(
547   void *NotUsed,
548   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
549   int argc,              /* Number of arguments */
550   const char **argv      /* Text of each argument */
551 ){
552   int i;
553   if( argc!=2 ){
554     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
555        " IDL", 0);
556     return TCL_ERROR;
557   }
558   i = parse_thread_id(interp, argv[1]);
559   if( i<0 ) return TCL_ERROR;
560   if( !threadset[i].busy ){
561     Tcl_AppendResult(interp, "no such thread", 0);
562     return TCL_ERROR;
563   }
564   test_thread_wait(&threadset[i]);
565   threadset[i].xOp = do_finalize;
566   sqlite3_free(threadset[i].zArg);
567   threadset[i].zArg = 0;
568   test_barrier();
569   threadset[i].opnum++;
570   return TCL_OK;
571 }
572 
573 /*
574 ** Usage: thread_swap ID ID
575 **
576 ** Interchange the sqlite* pointer between two threads.
577 */
tcl_thread_swap(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)578 static int SQLITE_TCLAPI tcl_thread_swap(
579   void *NotUsed,
580   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
581   int argc,              /* Number of arguments */
582   const char **argv      /* Text of each argument */
583 ){
584   int i, j;
585   sqlite3 *temp;
586   if( argc!=3 ){
587     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
588        " ID1 ID2", 0);
589     return TCL_ERROR;
590   }
591   i = parse_thread_id(interp, argv[1]);
592   if( i<0 ) return TCL_ERROR;
593   if( !threadset[i].busy ){
594     Tcl_AppendResult(interp, "no such thread", 0);
595     return TCL_ERROR;
596   }
597   test_thread_wait(&threadset[i]);
598   j = parse_thread_id(interp, argv[2]);
599   if( j<0 ) return TCL_ERROR;
600   if( !threadset[j].busy ){
601     Tcl_AppendResult(interp, "no such thread", 0);
602     return TCL_ERROR;
603   }
604   test_thread_wait(&threadset[j]);
605   temp = threadset[i].db;
606   threadset[i].db = threadset[j].db;
607   threadset[j].db = temp;
608   return TCL_OK;
609 }
610 
611 /*
612 ** Usage: thread_db_get ID
613 **
614 ** Return the database connection pointer for the given thread.  Then
615 ** remove the pointer from the thread itself.  Afterwards, the thread
616 ** can be stopped and the connection can be used by the main thread.
617 */
tcl_thread_db_get(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)618 static int SQLITE_TCLAPI tcl_thread_db_get(
619   void *NotUsed,
620   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
621   int argc,              /* Number of arguments */
622   const char **argv      /* Text of each argument */
623 ){
624   int i;
625   char zBuf[100];
626   extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*);
627   if( argc!=2 ){
628     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
629        " ID", 0);
630     return TCL_ERROR;
631   }
632   i = parse_thread_id(interp, argv[1]);
633   if( i<0 ) return TCL_ERROR;
634   if( !threadset[i].busy ){
635     Tcl_AppendResult(interp, "no such thread", 0);
636     return TCL_ERROR;
637   }
638   test_thread_wait(&threadset[i]);
639   sqlite3TestMakePointerStr(interp, zBuf, threadset[i].db);
640   threadset[i].db = 0;
641   Tcl_AppendResult(interp, zBuf, (char*)0);
642   return TCL_OK;
643 }
644 
645 /*
646 ** Usage: thread_db_put ID DB
647 **
648 */
tcl_thread_db_put(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)649 static int SQLITE_TCLAPI tcl_thread_db_put(
650   void *NotUsed,
651   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
652   int argc,              /* Number of arguments */
653   const char **argv      /* Text of each argument */
654 ){
655   int i;
656   extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*);
657   extern void *sqlite3TestTextToPtr(const char *);
658   if( argc!=3 ){
659     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
660        " ID DB", 0);
661     return TCL_ERROR;
662   }
663   i = parse_thread_id(interp, argv[1]);
664   if( i<0 ) return TCL_ERROR;
665   if( !threadset[i].busy ){
666     Tcl_AppendResult(interp, "no such thread", 0);
667     return TCL_ERROR;
668   }
669   test_thread_wait(&threadset[i]);
670   assert( !threadset[i].db );
671   threadset[i].db = (sqlite3*)sqlite3TestTextToPtr(argv[2]);
672   return TCL_OK;
673 }
674 
675 /*
676 ** Usage: thread_stmt_get ID
677 **
678 ** Return the database stmt pointer for the given thread.  Then
679 ** remove the pointer from the thread itself.
680 */
tcl_thread_stmt_get(void * NotUsed,Tcl_Interp * interp,int argc,const char ** argv)681 static int SQLITE_TCLAPI tcl_thread_stmt_get(
682   void *NotUsed,
683   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
684   int argc,              /* Number of arguments */
685   const char **argv      /* Text of each argument */
686 ){
687   int i;
688   char zBuf[100];
689   extern int sqlite3TestMakePointerStr(Tcl_Interp*, char*, void*);
690   if( argc!=2 ){
691     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
692        " ID", 0);
693     return TCL_ERROR;
694   }
695   i = parse_thread_id(interp, argv[1]);
696   if( i<0 ) return TCL_ERROR;
697   if( !threadset[i].busy ){
698     Tcl_AppendResult(interp, "no such thread", 0);
699     return TCL_ERROR;
700   }
701   test_thread_wait(&threadset[i]);
702   sqlite3TestMakePointerStr(interp, zBuf, threadset[i].pStmt);
703   threadset[i].pStmt = 0;
704   Tcl_AppendResult(interp, zBuf, (char*)0);
705   return TCL_OK;
706 }
707 
708 /*
709 ** Register commands with the TCL interpreter.
710 */
Sqlitetest4_Init(Tcl_Interp * interp)711 int Sqlitetest4_Init(Tcl_Interp *interp){
712   static struct {
713      char *zName;
714      Tcl_CmdProc *xProc;
715   } aCmd[] = {
716      { "thread_create",     (Tcl_CmdProc*)tcl_thread_create     },
717      { "thread_wait",       (Tcl_CmdProc*)tcl_thread_wait       },
718      { "thread_halt",       (Tcl_CmdProc*)tcl_thread_halt       },
719      { "thread_argc",       (Tcl_CmdProc*)tcl_thread_argc       },
720      { "thread_argv",       (Tcl_CmdProc*)tcl_thread_argv       },
721      { "thread_colname",    (Tcl_CmdProc*)tcl_thread_colname    },
722      { "thread_result",     (Tcl_CmdProc*)tcl_thread_result     },
723      { "thread_error",      (Tcl_CmdProc*)tcl_thread_error      },
724      { "thread_compile",    (Tcl_CmdProc*)tcl_thread_compile    },
725      { "thread_step",       (Tcl_CmdProc*)tcl_thread_step       },
726      { "thread_finalize",   (Tcl_CmdProc*)tcl_thread_finalize   },
727      { "thread_swap",       (Tcl_CmdProc*)tcl_thread_swap       },
728      { "thread_db_get",     (Tcl_CmdProc*)tcl_thread_db_get     },
729      { "thread_db_put",     (Tcl_CmdProc*)tcl_thread_db_put     },
730      { "thread_stmt_get",   (Tcl_CmdProc*)tcl_thread_stmt_get   },
731   };
732   int i;
733 
734   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
735     Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
736   }
737   return TCL_OK;
738 }
739 #else
Sqlitetest4_Init(Tcl_Interp * interp)740 int Sqlitetest4_Init(Tcl_Interp *interp){ return TCL_OK; }
741 #endif /* SQLITE_OS_UNIX */
742