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