1
0

test4.c 19 KB

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