test_thread.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. ** 2007 September 9
  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. **
  13. ** This file contains the implementation of some Tcl commands used to
  14. ** test that sqlite3 database handles may be concurrently accessed by
  15. ** multiple threads. Right now this only works on unix.
  16. */
  17. #include "sqliteInt.h"
  18. #include <tcl.h>
  19. #if SQLITE_THREADSAFE
  20. #include <errno.h>
  21. #if !defined(_MSC_VER)
  22. #include <unistd.h>
  23. #endif
  24. /*
  25. ** One of these is allocated for each thread created by [sqlthread spawn].
  26. */
  27. typedef struct SqlThread SqlThread;
  28. struct SqlThread {
  29. Tcl_ThreadId parent; /* Thread id of parent thread */
  30. Tcl_Interp *interp; /* Parent interpreter */
  31. char *zScript; /* The script to execute. */
  32. char *zVarname; /* Varname in parent script */
  33. };
  34. /*
  35. ** A custom Tcl_Event type used by this module. When the event is
  36. ** handled, script zScript is evaluated in interpreter interp. If
  37. ** the evaluation throws an exception (returns TCL_ERROR), then the
  38. ** error is handled by Tcl_BackgroundError(). If no error occurs,
  39. ** the result is simply discarded.
  40. */
  41. typedef struct EvalEvent EvalEvent;
  42. struct EvalEvent {
  43. Tcl_Event base; /* Base class of type Tcl_Event */
  44. char *zScript; /* The script to execute. */
  45. Tcl_Interp *interp; /* The interpreter to execute it in. */
  46. };
  47. static Tcl_ObjCmdProc sqlthread_proc;
  48. static Tcl_ObjCmdProc clock_seconds_proc;
  49. #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
  50. static Tcl_ObjCmdProc blocking_step_proc;
  51. static Tcl_ObjCmdProc blocking_prepare_v2_proc;
  52. #endif
  53. int Sqlitetest1_Init(Tcl_Interp *);
  54. int Sqlite3_Init(Tcl_Interp *);
  55. /* Functions from main.c */
  56. extern const char *sqlite3ErrName(int);
  57. /* Functions from test1.c */
  58. extern void *sqlite3TestTextToPtr(const char *);
  59. extern int getDbPointer(Tcl_Interp *, const char *, sqlite3 **);
  60. extern int sqlite3TestMakePointerStr(Tcl_Interp *, char *, void *);
  61. extern int sqlite3TestErrCode(Tcl_Interp *, sqlite3 *, int);
  62. /*
  63. ** Handler for events of type EvalEvent.
  64. */
  65. static int tclScriptEvent(Tcl_Event *evPtr, int flags){
  66. int rc;
  67. EvalEvent *p = (EvalEvent *)evPtr;
  68. rc = Tcl_Eval(p->interp, p->zScript);
  69. if( rc!=TCL_OK ){
  70. Tcl_BackgroundError(p->interp);
  71. }
  72. UNUSED_PARAMETER(flags);
  73. return 1;
  74. }
  75. /*
  76. ** Register an EvalEvent to evaluate the script pScript in the
  77. ** parent interpreter/thread of SqlThread p.
  78. */
  79. static void postToParent(SqlThread *p, Tcl_Obj *pScript){
  80. EvalEvent *pEvent;
  81. char *zMsg;
  82. int nMsg;
  83. zMsg = Tcl_GetStringFromObj(pScript, &nMsg);
  84. pEvent = (EvalEvent *)ckalloc(sizeof(EvalEvent)+nMsg+1);
  85. pEvent->base.nextPtr = 0;
  86. pEvent->base.proc = tclScriptEvent;
  87. pEvent->zScript = (char *)&pEvent[1];
  88. memcpy(pEvent->zScript, zMsg, nMsg+1);
  89. pEvent->interp = p->interp;
  90. Tcl_ThreadQueueEvent(p->parent, (Tcl_Event *)pEvent, TCL_QUEUE_TAIL);
  91. Tcl_ThreadAlert(p->parent);
  92. }
  93. /*
  94. ** The main function for threads created with [sqlthread spawn].
  95. */
  96. static Tcl_ThreadCreateType tclScriptThread(ClientData pSqlThread){
  97. Tcl_Interp *interp;
  98. Tcl_Obj *pRes;
  99. Tcl_Obj *pList;
  100. int rc;
  101. SqlThread *p = (SqlThread *)pSqlThread;
  102. extern int Sqlitetest_mutex_Init(Tcl_Interp*);
  103. interp = Tcl_CreateInterp();
  104. Tcl_CreateObjCommand(interp, "clock_seconds", clock_seconds_proc, 0, 0);
  105. Tcl_CreateObjCommand(interp, "sqlthread", sqlthread_proc, pSqlThread, 0);
  106. #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
  107. Tcl_CreateObjCommand(interp, "sqlite3_blocking_step", blocking_step_proc,0,0);
  108. Tcl_CreateObjCommand(interp,
  109. "sqlite3_blocking_prepare_v2", blocking_prepare_v2_proc, (void *)1, 0);
  110. Tcl_CreateObjCommand(interp,
  111. "sqlite3_nonblocking_prepare_v2", blocking_prepare_v2_proc, 0, 0);
  112. #endif
  113. Sqlitetest1_Init(interp);
  114. Sqlitetest_mutex_Init(interp);
  115. Sqlite3_Init(interp);
  116. rc = Tcl_Eval(interp, p->zScript);
  117. pRes = Tcl_GetObjResult(interp);
  118. pList = Tcl_NewObj();
  119. Tcl_IncrRefCount(pList);
  120. Tcl_IncrRefCount(pRes);
  121. if( rc!=TCL_OK ){
  122. Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj("error", -1));
  123. Tcl_ListObjAppendElement(interp, pList, pRes);
  124. postToParent(p, pList);
  125. Tcl_DecrRefCount(pList);
  126. pList = Tcl_NewObj();
  127. }
  128. Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj("set", -1));
  129. Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(p->zVarname, -1));
  130. Tcl_ListObjAppendElement(interp, pList, pRes);
  131. postToParent(p, pList);
  132. ckfree((void *)p);
  133. Tcl_DecrRefCount(pList);
  134. Tcl_DecrRefCount(pRes);
  135. Tcl_DeleteInterp(interp);
  136. while( Tcl_DoOneEvent(TCL_ALL_EVENTS|TCL_DONT_WAIT) );
  137. Tcl_ExitThread(0);
  138. TCL_THREAD_CREATE_RETURN;
  139. }
  140. /*
  141. ** sqlthread spawn VARNAME SCRIPT
  142. **
  143. ** Spawn a new thread with its own Tcl interpreter and run the
  144. ** specified SCRIPT(s) in it. The thread terminates after running
  145. ** the script. The result of the script is stored in the variable
  146. ** VARNAME.
  147. **
  148. ** The caller can wait for the script to terminate using [vwait VARNAME].
  149. */
  150. static int sqlthread_spawn(
  151. ClientData clientData,
  152. Tcl_Interp *interp,
  153. int objc,
  154. Tcl_Obj *CONST objv[]
  155. ){
  156. Tcl_ThreadId x;
  157. SqlThread *pNew;
  158. int rc;
  159. int nVarname; char *zVarname;
  160. int nScript; char *zScript;
  161. /* Parameters for thread creation */
  162. const int nStack = TCL_THREAD_STACK_DEFAULT;
  163. const int flags = TCL_THREAD_NOFLAGS;
  164. assert(objc==4);
  165. UNUSED_PARAMETER(clientData);
  166. UNUSED_PARAMETER(objc);
  167. zVarname = Tcl_GetStringFromObj(objv[2], &nVarname);
  168. zScript = Tcl_GetStringFromObj(objv[3], &nScript);
  169. pNew = (SqlThread *)ckalloc(sizeof(SqlThread)+nVarname+nScript+2);
  170. pNew->zVarname = (char *)&pNew[1];
  171. pNew->zScript = (char *)&pNew->zVarname[nVarname+1];
  172. memcpy(pNew->zVarname, zVarname, nVarname+1);
  173. memcpy(pNew->zScript, zScript, nScript+1);
  174. pNew->parent = Tcl_GetCurrentThread();
  175. pNew->interp = interp;
  176. rc = Tcl_CreateThread(&x, tclScriptThread, (void *)pNew, nStack, flags);
  177. if( rc!=TCL_OK ){
  178. Tcl_AppendResult(interp, "Error in Tcl_CreateThread()", 0);
  179. ckfree((char *)pNew);
  180. return TCL_ERROR;
  181. }
  182. return TCL_OK;
  183. }
  184. /*
  185. ** sqlthread parent SCRIPT
  186. **
  187. ** This can be called by spawned threads only. It sends the specified
  188. ** script back to the parent thread for execution. The result of
  189. ** evaluating the SCRIPT is returned. The parent thread must enter
  190. ** the event loop for this to work - otherwise the caller will
  191. ** block indefinitely.
  192. **
  193. ** NOTE: At the moment, this doesn't work. FIXME.
  194. */
  195. static int sqlthread_parent(
  196. ClientData clientData,
  197. Tcl_Interp *interp,
  198. int objc,
  199. Tcl_Obj *CONST objv[]
  200. ){
  201. EvalEvent *pEvent;
  202. char *zMsg;
  203. int nMsg;
  204. SqlThread *p = (SqlThread *)clientData;
  205. assert(objc==3);
  206. UNUSED_PARAMETER(objc);
  207. if( p==0 ){
  208. Tcl_AppendResult(interp, "no parent thread", 0);
  209. return TCL_ERROR;
  210. }
  211. zMsg = Tcl_GetStringFromObj(objv[2], &nMsg);
  212. pEvent = (EvalEvent *)ckalloc(sizeof(EvalEvent)+nMsg+1);
  213. pEvent->base.nextPtr = 0;
  214. pEvent->base.proc = tclScriptEvent;
  215. pEvent->zScript = (char *)&pEvent[1];
  216. memcpy(pEvent->zScript, zMsg, nMsg+1);
  217. pEvent->interp = p->interp;
  218. Tcl_ThreadQueueEvent(p->parent, (Tcl_Event *)pEvent, TCL_QUEUE_TAIL);
  219. Tcl_ThreadAlert(p->parent);
  220. return TCL_OK;
  221. }
  222. static int xBusy(void *pArg, int nBusy){
  223. UNUSED_PARAMETER(pArg);
  224. UNUSED_PARAMETER(nBusy);
  225. sqlite3_sleep(50);
  226. return 1; /* Try again... */
  227. }
  228. /*
  229. ** sqlthread open
  230. **
  231. ** Open a database handle and return the string representation of
  232. ** the pointer value.
  233. */
  234. static int sqlthread_open(
  235. ClientData clientData,
  236. Tcl_Interp *interp,
  237. int objc,
  238. Tcl_Obj *CONST objv[]
  239. ){
  240. int sqlite3TestMakePointerStr(Tcl_Interp *interp, char *zPtr, void *p);
  241. const char *zFilename;
  242. sqlite3 *db;
  243. char zBuf[100];
  244. extern void Md5_Register(sqlite3*);
  245. UNUSED_PARAMETER(clientData);
  246. UNUSED_PARAMETER(objc);
  247. zFilename = Tcl_GetString(objv[2]);
  248. sqlite3_open(zFilename, &db);
  249. #ifdef SQLITE_HAS_CODEC
  250. if( db && objc>=4 ){
  251. const char *zKey;
  252. int nKey;
  253. int rc;
  254. zKey = Tcl_GetStringFromObj(objv[3], &nKey);
  255. rc = sqlite3_key(db, zKey, nKey);
  256. if( rc!=SQLITE_OK ){
  257. char *zErrMsg = sqlite3_mprintf("error %d: %s", rc, sqlite3_errmsg(db));
  258. sqlite3_close(db);
  259. Tcl_AppendResult(interp, zErrMsg, (char*)0);
  260. sqlite3_free(zErrMsg);
  261. return TCL_ERROR;
  262. }
  263. }
  264. #endif
  265. Md5_Register(db);
  266. sqlite3_busy_handler(db, xBusy, 0);
  267. if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
  268. Tcl_AppendResult(interp, zBuf, 0);
  269. return TCL_OK;
  270. }
  271. /*
  272. ** sqlthread open
  273. **
  274. ** Return the current thread-id (Tcl_GetCurrentThread()) cast to
  275. ** an integer.
  276. */
  277. static int sqlthread_id(
  278. ClientData clientData,
  279. Tcl_Interp *interp,
  280. int objc,
  281. Tcl_Obj *CONST objv[]
  282. ){
  283. Tcl_ThreadId id = Tcl_GetCurrentThread();
  284. Tcl_SetObjResult(interp, Tcl_NewIntObj(SQLITE_PTR_TO_INT(id)));
  285. UNUSED_PARAMETER(clientData);
  286. UNUSED_PARAMETER(objc);
  287. UNUSED_PARAMETER(objv);
  288. return TCL_OK;
  289. }
  290. /*
  291. ** Dispatch routine for the sub-commands of [sqlthread].
  292. */
  293. static int sqlthread_proc(
  294. ClientData clientData,
  295. Tcl_Interp *interp,
  296. int objc,
  297. Tcl_Obj *CONST objv[]
  298. ){
  299. struct SubCommand {
  300. char *zName;
  301. Tcl_ObjCmdProc *xProc;
  302. int nArg;
  303. char *zUsage;
  304. } aSub[] = {
  305. {"parent", sqlthread_parent, 1, "SCRIPT"},
  306. {"spawn", sqlthread_spawn, 2, "VARNAME SCRIPT"},
  307. {"open", sqlthread_open, 1, "DBNAME"},
  308. {"id", sqlthread_id, 0, ""},
  309. {0, 0, 0}
  310. };
  311. struct SubCommand *pSub;
  312. int rc;
  313. int iIndex;
  314. if( objc<2 ){
  315. Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND");
  316. return TCL_ERROR;
  317. }
  318. rc = Tcl_GetIndexFromObjStruct(
  319. interp, objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iIndex
  320. );
  321. if( rc!=TCL_OK ) return rc;
  322. pSub = &aSub[iIndex];
  323. if( objc<(pSub->nArg+2) ){
  324. Tcl_WrongNumArgs(interp, 2, objv, pSub->zUsage);
  325. return TCL_ERROR;
  326. }
  327. return pSub->xProc(clientData, interp, objc, objv);
  328. }
  329. /*
  330. ** The [clock_seconds] command. This is more or less the same as the
  331. ** regular tcl [clock seconds], except that it is available in testfixture
  332. ** when linked against both Tcl 8.4 and 8.5. Because [clock seconds] is
  333. ** implemented as a script in Tcl 8.5, it is not usually available to
  334. ** testfixture.
  335. */
  336. static int clock_seconds_proc(
  337. ClientData clientData,
  338. Tcl_Interp *interp,
  339. int objc,
  340. Tcl_Obj *CONST objv[]
  341. ){
  342. Tcl_Time now;
  343. Tcl_GetTime(&now);
  344. Tcl_SetObjResult(interp, Tcl_NewIntObj(now.sec));
  345. UNUSED_PARAMETER(clientData);
  346. UNUSED_PARAMETER(objc);
  347. UNUSED_PARAMETER(objv);
  348. return TCL_OK;
  349. }
  350. /*************************************************************************
  351. ** This block contains the implementation of the [sqlite3_blocking_step]
  352. ** command available to threads created by [sqlthread spawn] commands. It
  353. ** is only available on UNIX for now. This is because pthread condition
  354. ** variables are used.
  355. **
  356. ** The source code for the C functions sqlite3_blocking_step(),
  357. ** blocking_step_notify() and the structure UnlockNotification is
  358. ** automatically extracted from this file and used as part of the
  359. ** documentation for the sqlite3_unlock_notify() API function. This
  360. ** should be considered if these functions are to be extended (i.e. to
  361. ** support windows) in the future.
  362. */
  363. #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
  364. /* BEGIN_SQLITE_BLOCKING_STEP */
  365. /* This example uses the pthreads API */
  366. #include <pthread.h>
  367. /*
  368. ** A pointer to an instance of this structure is passed as the user-context
  369. ** pointer when registering for an unlock-notify callback.
  370. */
  371. typedef struct UnlockNotification UnlockNotification;
  372. struct UnlockNotification {
  373. int fired; /* True after unlock event has occurred */
  374. pthread_cond_t cond; /* Condition variable to wait on */
  375. pthread_mutex_t mutex; /* Mutex to protect structure */
  376. };
  377. /*
  378. ** This function is an unlock-notify callback registered with SQLite.
  379. */
  380. static void unlock_notify_cb(void **apArg, int nArg){
  381. int i;
  382. for(i=0; i<nArg; i++){
  383. UnlockNotification *p = (UnlockNotification *)apArg[i];
  384. pthread_mutex_lock(&p->mutex);
  385. p->fired = 1;
  386. pthread_cond_signal(&p->cond);
  387. pthread_mutex_unlock(&p->mutex);
  388. }
  389. }
  390. /*
  391. ** This function assumes that an SQLite API call (either sqlite3_prepare_v2()
  392. ** or sqlite3_step()) has just returned SQLITE_LOCKED. The argument is the
  393. ** associated database connection.
  394. **
  395. ** This function calls sqlite3_unlock_notify() to register for an
  396. ** unlock-notify callback, then blocks until that callback is delivered
  397. ** and returns SQLITE_OK. The caller should then retry the failed operation.
  398. **
  399. ** Or, if sqlite3_unlock_notify() indicates that to block would deadlock
  400. ** the system, then this function returns SQLITE_LOCKED immediately. In
  401. ** this case the caller should not retry the operation and should roll
  402. ** back the current transaction (if any).
  403. */
  404. static int wait_for_unlock_notify(sqlite3 *db){
  405. int rc;
  406. UnlockNotification un;
  407. /* Initialize the UnlockNotification structure. */
  408. un.fired = 0;
  409. pthread_mutex_init(&un.mutex, 0);
  410. pthread_cond_init(&un.cond, 0);
  411. /* Register for an unlock-notify callback. */
  412. rc = sqlite3_unlock_notify(db, unlock_notify_cb, (void *)&un);
  413. assert( rc==SQLITE_LOCKED || rc==SQLITE_OK );
  414. /* The call to sqlite3_unlock_notify() always returns either SQLITE_LOCKED
  415. ** or SQLITE_OK.
  416. **
  417. ** If SQLITE_LOCKED was returned, then the system is deadlocked. In this
  418. ** case this function needs to return SQLITE_LOCKED to the caller so
  419. ** that the current transaction can be rolled back. Otherwise, block
  420. ** until the unlock-notify callback is invoked, then return SQLITE_OK.
  421. */
  422. if( rc==SQLITE_OK ){
  423. pthread_mutex_lock(&un.mutex);
  424. if( !un.fired ){
  425. pthread_cond_wait(&un.cond, &un.mutex);
  426. }
  427. pthread_mutex_unlock(&un.mutex);
  428. }
  429. /* Destroy the mutex and condition variables. */
  430. pthread_cond_destroy(&un.cond);
  431. pthread_mutex_destroy(&un.mutex);
  432. return rc;
  433. }
  434. /*
  435. ** This function is a wrapper around the SQLite function sqlite3_step().
  436. ** It functions in the same way as step(), except that if a required
  437. ** shared-cache lock cannot be obtained, this function may block waiting for
  438. ** the lock to become available. In this scenario the normal API step()
  439. ** function always returns SQLITE_LOCKED.
  440. **
  441. ** If this function returns SQLITE_LOCKED, the caller should rollback
  442. ** the current transaction (if any) and try again later. Otherwise, the
  443. ** system may become deadlocked.
  444. */
  445. int sqlite3_blocking_step(sqlite3_stmt *pStmt){
  446. int rc;
  447. while( SQLITE_LOCKED==(rc = sqlite3_step(pStmt)) ){
  448. rc = wait_for_unlock_notify(sqlite3_db_handle(pStmt));
  449. if( rc!=SQLITE_OK ) break;
  450. sqlite3_reset(pStmt);
  451. }
  452. return rc;
  453. }
  454. /*
  455. ** This function is a wrapper around the SQLite function sqlite3_prepare_v2().
  456. ** It functions in the same way as prepare_v2(), except that if a required
  457. ** shared-cache lock cannot be obtained, this function may block waiting for
  458. ** the lock to become available. In this scenario the normal API prepare_v2()
  459. ** function always returns SQLITE_LOCKED.
  460. **
  461. ** If this function returns SQLITE_LOCKED, the caller should rollback
  462. ** the current transaction (if any) and try again later. Otherwise, the
  463. ** system may become deadlocked.
  464. */
  465. int sqlite3_blocking_prepare_v2(
  466. sqlite3 *db, /* Database handle. */
  467. const char *zSql, /* UTF-8 encoded SQL statement. */
  468. int nSql, /* Length of zSql in bytes. */
  469. sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
  470. const char **pz /* OUT: End of parsed string */
  471. ){
  472. int rc;
  473. while( SQLITE_LOCKED==(rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, pz)) ){
  474. rc = wait_for_unlock_notify(db);
  475. if( rc!=SQLITE_OK ) break;
  476. }
  477. return rc;
  478. }
  479. /* END_SQLITE_BLOCKING_STEP */
  480. /*
  481. ** Usage: sqlite3_blocking_step STMT
  482. **
  483. ** Advance the statement to the next row.
  484. */
  485. static int blocking_step_proc(
  486. void * clientData,
  487. Tcl_Interp *interp,
  488. int objc,
  489. Tcl_Obj *CONST objv[]
  490. ){
  491. sqlite3_stmt *pStmt;
  492. int rc;
  493. if( objc!=2 ){
  494. Tcl_WrongNumArgs(interp, 1, objv, "STMT");
  495. return TCL_ERROR;
  496. }
  497. pStmt = (sqlite3_stmt*)sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  498. rc = sqlite3_blocking_step(pStmt);
  499. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), 0);
  500. return TCL_OK;
  501. }
  502. /*
  503. ** Usage: sqlite3_blocking_prepare_v2 DB sql bytes ?tailvar?
  504. ** Usage: sqlite3_nonblocking_prepare_v2 DB sql bytes ?tailvar?
  505. */
  506. static int blocking_prepare_v2_proc(
  507. void * clientData,
  508. Tcl_Interp *interp,
  509. int objc,
  510. Tcl_Obj *CONST objv[]
  511. ){
  512. sqlite3 *db;
  513. const char *zSql;
  514. int bytes;
  515. const char *zTail = 0;
  516. sqlite3_stmt *pStmt = 0;
  517. char zBuf[50];
  518. int rc;
  519. int isBlocking = !(clientData==0);
  520. if( objc!=5 && objc!=4 ){
  521. Tcl_AppendResult(interp, "wrong # args: should be \"",
  522. Tcl_GetString(objv[0]), " DB sql bytes tailvar", 0);
  523. return TCL_ERROR;
  524. }
  525. if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  526. zSql = Tcl_GetString(objv[2]);
  527. if( Tcl_GetIntFromObj(interp, objv[3], &bytes) ) return TCL_ERROR;
  528. if( isBlocking ){
  529. rc = sqlite3_blocking_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
  530. }else{
  531. rc = sqlite3_prepare_v2(db, zSql, bytes, &pStmt, &zTail);
  532. }
  533. assert(rc==SQLITE_OK || pStmt==0);
  534. if( zTail && objc>=5 ){
  535. if( bytes>=0 ){
  536. bytes = bytes - (zTail-zSql);
  537. }
  538. Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
  539. }
  540. if( rc!=SQLITE_OK ){
  541. assert( pStmt==0 );
  542. sprintf(zBuf, "%s ", (char *)sqlite3ErrName(rc));
  543. Tcl_AppendResult(interp, zBuf, sqlite3_errmsg(db), 0);
  544. return TCL_ERROR;
  545. }
  546. if( pStmt ){
  547. if( sqlite3TestMakePointerStr(interp, zBuf, pStmt) ) return TCL_ERROR;
  548. Tcl_AppendResult(interp, zBuf, 0);
  549. }
  550. return TCL_OK;
  551. }
  552. #endif /* SQLITE_OS_UNIX && SQLITE_ENABLE_UNLOCK_NOTIFY */
  553. /*
  554. ** End of implementation of [sqlite3_blocking_step].
  555. ************************************************************************/
  556. /*
  557. ** Register commands with the TCL interpreter.
  558. */
  559. int SqlitetestThread_Init(Tcl_Interp *interp){
  560. Tcl_CreateObjCommand(interp, "sqlthread", sqlthread_proc, 0, 0);
  561. Tcl_CreateObjCommand(interp, "clock_seconds", clock_seconds_proc, 0, 0);
  562. #if SQLITE_OS_UNIX && defined(SQLITE_ENABLE_UNLOCK_NOTIFY)
  563. Tcl_CreateObjCommand(interp, "sqlite3_blocking_step", blocking_step_proc,0,0);
  564. Tcl_CreateObjCommand(interp,
  565. "sqlite3_blocking_prepare_v2", blocking_prepare_v2_proc, (void *)1, 0);
  566. Tcl_CreateObjCommand(interp,
  567. "sqlite3_nonblocking_prepare_v2", blocking_prepare_v2_proc, 0, 0);
  568. #endif
  569. return TCL_OK;
  570. }
  571. #else
  572. int SqlitetestThread_Init(Tcl_Interp *interp){
  573. return TCL_OK;
  574. }
  575. #endif