Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add new thread-testing code and fix locking under Linux threads. Ticket #530. (CVS 1137) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
b36a4bb61094d539273c21a9e4042384 |
User & Date: | drh 2003-12-19 02:52:06.000 |
Context
2003-12-19
| ||
08:40 | A better fix for ticket #530 - one that is likely to work on unix implementations in addition to linux. Also more tests for multi-thread locking added. (CVS 1138) (check-in: 7dddbeb586 user: drh tags: trunk) | |
02:52 | Add new thread-testing code and fix locking under Linux threads. Ticket #530. (CVS 1137) (check-in: b36a4bb610 user: drh tags: trunk) | |
2003-12-18
| ||
14:19 | Typo on the "datatypes.html" document. (CVS 1136) (check-in: 80b1e27712 user: drh tags: trunk) | |
Changes
Changes to main.mk.
︙ | ︙ | |||
108 109 110 111 112 113 114 115 116 117 118 119 120 121 | $(TOP)/src/btree.c \ $(TOP)/src/func.c \ $(TOP)/src/os.c \ $(TOP)/src/pager.c \ $(TOP)/src/test1.c \ $(TOP)/src/test2.c \ $(TOP)/src/test3.c \ $(TOP)/src/md5.c # Header files used by all library source files. # HDR = \ sqlite.h \ $(TOP)/src/btree.h \ | > | 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | $(TOP)/src/btree.c \ $(TOP)/src/func.c \ $(TOP)/src/os.c \ $(TOP)/src/pager.c \ $(TOP)/src/test1.c \ $(TOP)/src/test2.c \ $(TOP)/src/test3.c \ $(TOP)/src/test4.c \ $(TOP)/src/md5.c # Header files used by all library source files. # HDR = \ sqlite.h \ $(TOP)/src/btree.h \ |
︙ | ︙ | |||
308 309 310 311 312 313 314 | tclsqlite: $(TOP)/src/tclsqlite.c libsqlite.a $(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite \ $(TOP)/src/tclsqlite.c libsqlite.a $(LIBTCL) testfixture$(EXE): $(TOP)/src/tclsqlite.c libsqlite.a $(TESTSRC) $(TCCX) $(TCL_FLAGS) -DTCLSH=1 -DSQLITE_TEST=1 -o testfixture$(EXE) \ $(TESTSRC) $(TOP)/src/tclsqlite.c \ | | | 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | tclsqlite: $(TOP)/src/tclsqlite.c libsqlite.a $(TCCX) $(TCL_FLAGS) -DTCLSH=1 -o tclsqlite \ $(TOP)/src/tclsqlite.c libsqlite.a $(LIBTCL) testfixture$(EXE): $(TOP)/src/tclsqlite.c libsqlite.a $(TESTSRC) $(TCCX) $(TCL_FLAGS) -DTCLSH=1 -DSQLITE_TEST=1 -o testfixture$(EXE) \ $(TESTSRC) $(TOP)/src/tclsqlite.c \ libsqlite.a $(LIBTCL) $(THREADLIB) fulltest: testfixture$(EXE) sqlite$(EXE) ./testfixture$(EXE) $(TOP)/test/all.test test: testfixture$(EXE) sqlite$(EXE) ./testfixture$(EXE) $(TOP)/test/quick.test |
︙ | ︙ |
Changes to src/os.c.
︙ | ︙ | |||
57 58 59 60 61 62 63 64 65 66 67 68 69 70 | ** that always succeeds. This means that locking does not occur under ** DJGPP. But its DOS - what did you expect? */ #ifdef __DJGPP__ # define fcntl(A,B,C) 0 #endif /* ** Macros for performance tracing. Normally turned off */ #if 0 static int last_page = 0; __inline__ unsigned long long int hwtime(void){ unsigned long long int x; | > > > > > > > > > > > > > > > > > > | 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | ** that always succeeds. This means that locking does not occur under ** DJGPP. But its DOS - what did you expect? */ #ifdef __DJGPP__ # define fcntl(A,B,C) 0 #endif /* ** Macros used to determine whether or not to use threads. The ** SQLITE_UNIX_THREADS macro is defined if we are synchronizing for ** Posix threads and SQLITE_W32_THREADS is defined if we are ** synchronizing using Win32 threads. */ #if OS_UNIX && defined(THREADSAFE) && THREADSAFE # include <pthread.h> # define SQLITE_UNIX_THREADS 1 #endif #if OS_WIN && defined(THREADSAFE) && THREADSAFE # define SQLITE_W32_THREADS 1 #endif #if OS_MAC && defined(THREADSAFE) && THREADSAFE # include <Multiprocessing.h> # define SQLITE_MACOS_MULTITASKING 1 #endif /* ** Macros for performance tracing. Normally turned off */ #if 0 static int last_page = 0; __inline__ unsigned long long int hwtime(void){ unsigned long long int x; |
︙ | ︙ | |||
151 152 153 154 155 156 157 158 159 160 161 162 163 164 | /* ** An instance of the following structure serves as the key used ** to locate a particular lockInfo structure given its inode. */ struct inodeKey { dev_t dev; /* Device number */ ino_t ino; /* Inode number */ }; /* ** An instance of the following structure is allocated for each inode. ** A single inode can have multiple file descriptors, so each OsFile ** structure contains a pointer to an instance of this object and this ** object keeps a count of the number of OsFiles pointing to it. | > > > | 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | /* ** An instance of the following structure serves as the key used ** to locate a particular lockInfo structure given its inode. */ struct inodeKey { dev_t dev; /* Device number */ ino_t ino; /* Inode number */ #ifdef SQLITE_UNIX_THREADS pthread_t thread_id; /* Which thread are we */ #endif }; /* ** An instance of the following structure is allocated for each inode. ** A single inode can have multiple file descriptors, so each OsFile ** structure contains a pointer to an instance of this object and this ** object keeps a count of the number of OsFiles pointing to it. |
︙ | ︙ | |||
186 187 188 189 190 191 192 193 194 195 196 197 198 199 | struct stat statbuf; struct lockInfo *pInfo; rc = fstat(fd, &statbuf); if( rc!=0 ) return 0; memset(&key, 0, sizeof(key)); key.dev = statbuf.st_dev; key.ino = statbuf.st_ino; pInfo = (struct lockInfo*)sqliteHashFind(&lockHash, &key, sizeof(key)); if( pInfo==0 ){ struct lockInfo *pOld; pInfo = sqliteMalloc( sizeof(*pInfo) ); if( pInfo==0 ) return 0; pInfo->key = key; pInfo->nRef = 1; | > > > | 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | struct stat statbuf; struct lockInfo *pInfo; rc = fstat(fd, &statbuf); if( rc!=0 ) return 0; memset(&key, 0, sizeof(key)); key.dev = statbuf.st_dev; key.ino = statbuf.st_ino; #ifdef SQLITE_UNIX_THREADS key.thread_id = pthread_self(); #endif pInfo = (struct lockInfo*)sqliteHashFind(&lockHash, &key, sizeof(key)); if( pInfo==0 ){ struct lockInfo *pOld; pInfo = sqliteMalloc( sizeof(*pInfo) ); if( pInfo==0 ) return 0; pInfo->key = key; pInfo->nRef = 1; |
︙ | ︙ | |||
1463 1464 1465 1466 1467 1468 1469 | UInt32 finalTicks; UInt32 ticks = (((UInt32)ms+16)*3)/50; /* 1/60 sec per tick */ Delay(ticks, &finalTicks); return (int)((ticks*50)/3); #endif } | < < < < < < < < < < < < < < < < < < | 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 | UInt32 finalTicks; UInt32 ticks = (((UInt32)ms+16)*3)/50; /* 1/60 sec per tick */ Delay(ticks, &finalTicks); return (int)((ticks*50)/3); #endif } /* ** Static variables used for thread synchronization */ static int inMutex = 0; #ifdef SQLITE_UNIX_THREADS static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; #endif |
︙ | ︙ |
Changes to src/tclsqlite.c.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** A TCL Interface to SQLite ** | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** A TCL Interface to SQLite ** ** $Id: tclsqlite.c,v 1.52 2003/12/19 02:52:09 drh Exp $ */ #ifndef NO_TCL /* Omit this whole file if TCL is unavailable */ #include "sqliteInt.h" #include "tcl.h" #include <stdlib.h> #include <string.h> |
︙ | ︙ | |||
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 | interp = Tcl_CreateInterp(); Sqlite_Init(interp); #ifdef SQLITE_TEST { extern int Sqlitetest1_Init(Tcl_Interp*); extern int Sqlitetest2_Init(Tcl_Interp*); extern int Sqlitetest3_Init(Tcl_Interp*); extern int Md5_Init(Tcl_Interp*); Sqlitetest1_Init(interp); Sqlitetest2_Init(interp); Sqlitetest3_Init(interp); Md5_Init(interp); } #endif if( argc>=2 ){ int i; Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); | > > | 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 | interp = Tcl_CreateInterp(); Sqlite_Init(interp); #ifdef SQLITE_TEST { extern int Sqlitetest1_Init(Tcl_Interp*); extern int Sqlitetest2_Init(Tcl_Interp*); extern int Sqlitetest3_Init(Tcl_Interp*); extern int Sqlitetest4_Init(Tcl_Interp*); extern int Md5_Init(Tcl_Interp*); Sqlitetest1_Init(interp); Sqlitetest2_Init(interp); Sqlitetest3_Init(interp); Sqlitetest4_Init(interp); Md5_Init(interp); } #endif if( argc>=2 ){ int i; Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY); Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY); |
︙ | ︙ |
Added src/test4.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | /* ** 2003 December 18 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Code for testing the the SQLite library in a multithreaded environment. ** ** $Id: test4.c,v 1.1 2003/12/19 02:52:09 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" #if defined(OS_UNIX) && defined(THREADSAFE) && THREADSAFE==1 #include <stdlib.h> #include <string.h> #include <pthread.h> #include <sched.h> #include <ctype.h> /* ** Each thread is controlled by an instance of the following ** structure. */ typedef struct Thread Thread; struct Thread { /* The first group of fields are writable by the master and read-only ** to the thread. */ char *zFilename; /* Name of database file */ void (*xOp)(Thread*); /* next operation to do */ char *zArg; /* argument usable by xOp */ int opnum; /* Operation number */ int busy; /* True if this thread is in use */ /* The next group of fields are writable by the thread but read-only to the ** master. */ int completed; /* Number of operations completed */ sqlite *db; /* Open database */ sqlite_vm *vm; /* Pending operation */ char *zErr; /* operation error */ char *zStaticErr; /* Static error message */ int rc; /* operation return code */ int argc; /* number of columns in result */ const char **argv; /* result columns */ const char **colv; /* result column names */ }; /* ** There can be as many as 26 threads running at once. Each is named ** by a capital letter: A, B, C, ..., Y, Z. */ #define N_THREAD 26 static Thread threadset[N_THREAD]; /* ** The main loop for a thread. Threads use busy waiting. */ static void *thread_main(void *pArg){ Thread *p = (Thread*)pArg; if( p->db ){ sqlite_close(p->db); } p->db = sqlite_open(p->zFilename, 0, &p->zErr); p->vm = 0; p->completed = 1; while( p->opnum<=p->completed ) sched_yield(); while( p->xOp ){ if( p->zErr && p->zErr!=p->zStaticErr ){ sqlite_freemem(p->zErr); p->zErr = 0; } (*p->xOp)(p); p->completed++; while( p->opnum<=p->completed ) sched_yield(); } if( p->vm ){ sqlite_finalize(p->vm, 0); p->vm = 0; } if( p->db ){ sqlite_close(p->db); p->db = 0; } if( p->zErr && p->zErr!=p->zStaticErr ){ sqlite_freemem(p->zErr); p->zErr = 0; } p->completed++; return 0; } /* ** Get a thread ID which is an upper case letter. Return the index. ** If the argument is not a valid thread ID put an error message in ** the interpreter and return -1. */ static int parse_thread_id(Tcl_Interp *interp, const char *zArg){ if( zArg==0 || zArg[0]==0 || zArg[1]!=0 || !isupper(zArg[0]) ){ Tcl_AppendResult(interp, "thread ID must be an upper case letter", 0); return -1; } return zArg[0] - 'A'; } /* ** Usage: thread_create NAME FILENAME ** ** NAME should be an upper case letter. Start the thread running with ** an open connection to the given database. */ static int tcl_thread_create( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; pthread_t x; int rc; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID FILENAME", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( threadset[i].busy ){ Tcl_AppendResult(interp, "thread ", argv[1], " is already running", 0); return TCL_ERROR; } threadset[i].busy = 1; sqliteFree(threadset[i].zFilename); threadset[i].zFilename = sqliteStrDup(argv[2]); threadset[i].opnum = 1; rc = pthread_create(&x, 0, thread_main, &threadset[i]); if( rc ){ Tcl_AppendResult(interp, "failed to create the thread", 0); sqliteFree(threadset[i].zFilename); threadset[i].busy = 0; return TCL_ERROR; } pthread_detach(x); return TCL_OK; } /* ** Wait for a thread to reach its idle state. */ static void thread_wait(Thread *p){ while( p->opnum>p->completed ) sched_yield(); } /* ** Usage: thread_wait ID ** ** Wait on thread ID to reach its idle state. */ static int tcl_thread_wait( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); return TCL_OK; } /* ** Stop a thread. */ static void stop_thread(Thread *p){ thread_wait(p); p->xOp = 0; p->opnum++; thread_wait(p); sqliteFree(p->zArg); p->zArg = 0; sqliteFree(p->zFilename); p->zFilename = 0; p->busy = 0; } /* ** Usage: thread_halt ID ** ** Cause a thread to shut itself down. Wait for the shutdown to be ** completed. If ID is "*" then stop all threads. */ static int tcl_thread_halt( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } if( argv[1][0]=='*' && argv[1][1]==0 ){ for(i=0; i<N_THREAD; i++){ if( threadset[i].busy ) stop_thread(&threadset[i]); } }else{ i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } stop_thread(&threadset[i]); } return TCL_OK; } /* ** Usage: thread_argc ID ** ** Wait on the most recent thread_step to complete, then return the ** number of columns in the result set. */ static int tcl_thread_argc( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; char zBuf[100]; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); sprintf(zBuf, "%d", threadset[i].argc); Tcl_AppendResult(interp, zBuf, 0); return TCL_OK; } /* ** Usage: thread_argv ID N ** ** Wait on the most recent thread_step to complete, then return the ** value of the N-th columns in the result set. */ static int tcl_thread_argv( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; int n; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID N", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR; thread_wait(&threadset[i]); if( n<0 || n>=threadset[i].argc ){ Tcl_AppendResult(interp, "column number out of range", 0); return TCL_ERROR; } Tcl_AppendResult(interp, threadset[i].argv[n], 0); return TCL_OK; } /* ** Usage: thread_colname ID N ** ** Wait on the most recent thread_step to complete, then return the ** name of the N-th columns in the result set. */ static int tcl_thread_colname( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; int n; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID N", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[2], &n) ) return TCL_ERROR; thread_wait(&threadset[i]); if( n<0 || n>=threadset[i].argc ){ Tcl_AppendResult(interp, "column number out of range", 0); return TCL_ERROR; } Tcl_AppendResult(interp, threadset[i].colv[n], 0); return TCL_OK; } /* ** Usage: thread_result ID ** ** Wait on the most recent operation to complete, then return the ** result code from that operation. */ static int tcl_thread_result( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; const char *zName; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); switch( threadset[i].rc ){ case SQLITE_OK: zName = "SQLITE_OK"; break; case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; case SQLITE_PERM: zName = "SQLITE_PERM"; break; case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break; case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break; case SQLITE_FULL: zName = "SQLITE_FULL"; break; case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break; case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; case SQLITE_ROW: zName = "SQLITE_ROW"; break; case SQLITE_DONE: zName = "SQLITE_DONE"; break; default: zName = "SQLITE_Unknown"; break; } Tcl_AppendResult(interp, zName, 0); return TCL_OK; } /* ** Usage: thread_error ID ** ** Wait on the most recent operation to complete, then return the ** error string. */ static int tcl_thread_error( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); Tcl_AppendResult(interp, threadset[i].zErr, 0); return TCL_OK; } /* ** This procedure runs in the thread to compile an SQL statement. */ static void do_compile(Thread *p){ if( p->db==0 ){ p->zErr = p->zStaticErr = "no database is open"; p->rc = SQLITE_ERROR; return; } if( p->vm ){ sqlite_finalize(p->vm, 0); p->vm = 0; } p->rc = sqlite_compile(p->db, p->zArg, 0, &p->vm, &p->zErr); } /* ** Usage: thread_compile ID SQL ** ** Compile a new virtual machine. */ static int tcl_thread_compile( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; if( argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " ID SQL", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); threadset[i].xOp = do_compile; sqliteFree(threadset[i].zArg); threadset[i].zArg = sqliteStrDup(argv[2]); threadset[i].opnum++; return TCL_OK; } /* ** This procedure runs in the thread to step the virtual machine. */ static void do_step(Thread *p){ if( p->vm==0 ){ p->zErr = p->zStaticErr = "no virtual machine available"; p->rc = SQLITE_ERROR; return; } p->rc = sqlite_step(p->vm, &p->argc, &p->argv, &p->colv); } /* ** Usage: thread_step ID ** ** Advance the virtual machine by one step */ static int tcl_thread_step( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " IDL", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); threadset[i].xOp = do_step; threadset[i].opnum++; return TCL_OK; } /* ** This procedure runs in the thread to finalize a virtual machine. */ static void do_finalize(Thread *p){ if( p->vm==0 ){ p->zErr = p->zStaticErr = "no virtual machine available"; p->rc = SQLITE_ERROR; return; } p->rc = sqlite_finalize(p->vm, &p->zErr); p->vm = 0; } /* ** Usage: thread_finalize ID ** ** Finalize the virtual machine. */ static int tcl_thread_finalize( void *NotUsed, Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ int i; if( argc!=2 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " IDL", 0); return TCL_ERROR; } i = parse_thread_id(interp, argv[1]); if( i<0 ) return TCL_ERROR; if( !threadset[i].busy ){ Tcl_AppendResult(interp, "no such thread", 0); return TCL_ERROR; } thread_wait(&threadset[i]); threadset[i].xOp = do_finalize; sqliteFree(threadset[i].zArg); threadset[i].zArg = 0; threadset[i].opnum++; return TCL_OK; } /* ** Register commands with the TCL interpreter. */ int Sqlitetest4_Init(Tcl_Interp *interp){ static struct { char *zName; Tcl_CmdProc *xProc; } aCmd[] = { { "thread_create", (Tcl_CmdProc*)tcl_thread_create }, { "thread_wait", (Tcl_CmdProc*)tcl_thread_wait }, { "thread_halt", (Tcl_CmdProc*)tcl_thread_halt }, { "thread_argc", (Tcl_CmdProc*)tcl_thread_argc }, { "thread_argv", (Tcl_CmdProc*)tcl_thread_argv }, { "thread_colname", (Tcl_CmdProc*)tcl_thread_colname }, { "thread_result", (Tcl_CmdProc*)tcl_thread_result }, { "thread_error", (Tcl_CmdProc*)tcl_thread_error }, { "thread_compile", (Tcl_CmdProc*)tcl_thread_compile }, { "thread_step", (Tcl_CmdProc*)tcl_thread_step }, { "thread_finalize", (Tcl_CmdProc*)tcl_thread_finalize }, }; int i; for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); } return TCL_OK; } #else int Sqlitetest4_Init(Tcl_Interp *interp){ return TCL_OK; } #endif /* OS_UNIX */ |
Added test/thread1.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # 2003 December 18 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this script is multithreading behavior # # $Id: thread1.test,v 1.1 2003/12/19 02:52:09 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Skip this whole file if the thread testing code is not enabled # if {[llength [info command thread_step]]==0} { finish_test return } # Create some data to work with # do_test thread1-1.1 { execsql { CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(1,'abcdefgh'); INSERT INTO t1 SELECT a+1, b||b FROM t1; INSERT INTO t1 SELECT a+2, b||b FROM t1; INSERT INTO t1 SELECT a+4, b||b FROM t1; SELECT count(*), max(length(b)) FROM t1; } } {8 64} # Interleave two threads on read access. Then make sure a third # thread can write the database. # do_test thread1-1.2 { thread_create A test.db thread_create B test.db thread_create C test.db thread_compile A {SELECT a FROM t1} thread_step A thread_result A } SQLITE_ROW do_test thread1-1.3 { thread_argc A } 1 do_test thread1-1.4 { thread_argv A 0 } 1 do_test thread1-1.5 { thread_compile B {SELECT b FROM t1} thread_step B thread_result B } SQLITE_ROW do_test thread1-1.6 { thread_argc B } 1 do_test thread1-1.7 { thread_argv B 0 } abcdefgh do_test thread1-1.8 { thread_finalize A thread_result A } SQLITE_OK do_test thread1-1.9 { thread_finalize B thread_result B } SQLITE_OK do_test thread1-1.10 { thread_compile C {CREATE TABLE t2(x,y)} thread_step C thread_result C } SQLITE_DONE do_test thread1-1.11 { thread_finalize C thread_result C } SQLITE_OK thread_halt * finish_test |