Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add in-process file locking to test_async.c. The unix implementation of sqlite3OsFullPathname() now attempts to remove /./ and /../ elements from the path. (CVS 3090) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
42379c623073eb541d053c2dff9f4908 |
User & Date: | drh 2006-02-13 17:03:48.000 |
Context
2006-02-13
| ||
18:35 | Fix a bug in the handling of sqlite3OsTruncate by the test_async.c demo. Added trans.test to the suite of tests for asychronous I/O. (CVS 3091) (check-in: 5b16c43542 user: drh tags: trunk) | |
17:03 | Add in-process file locking to test_async.c. The unix implementation of sqlite3OsFullPathname() now attempts to remove /./ and /../ elements from the path. (CVS 3090) (check-in: 42379c6230 user: drh tags: trunk) | |
15:29 | Fix a deadlock problem on the sqlite3async_wait test interface. Improvements to tracing in test_async.c. (CVS 3089) (check-in: 58c6d50138 user: drh tags: trunk) | |
Changes
Changes to src/os_unix.c.
︙ | ︙ | |||
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 | ** Turn a relative pathname into a full pathname. Return a pointer ** to the full pathname stored in space obtained from sqliteMalloc(). ** The calling function is responsible for freeing this space once it ** is no longer needed. */ char *sqlite3UnixFullPathname(const char *zRelative){ char *zFull = 0; if( zRelative[0]=='/' ){ sqlite3SetString(&zFull, zRelative, (char*)0); }else{ char *zBuf = sqliteMalloc(5000); if( zBuf==0 ){ return 0; } zBuf[0] = 0; sqlite3SetString(&zFull, getcwd(zBuf, 5000), "/", zRelative, (char*)0); sqliteFree(zBuf); } return zFull; } /* ** Change the value of the fullsync flag in the given file descriptor. */ static void unixSetFullSync(OsFile *id, int v){ | > > > > > > > > > > > > > > > > > > > > > > > | 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 | ** Turn a relative pathname into a full pathname. Return a pointer ** to the full pathname stored in space obtained from sqliteMalloc(). ** The calling function is responsible for freeing this space once it ** is no longer needed. */ char *sqlite3UnixFullPathname(const char *zRelative){ char *zFull = 0; int i, j; if( zRelative[0]=='/' ){ sqlite3SetString(&zFull, zRelative, (char*)0); }else{ char *zBuf = sqliteMalloc(5000); if( zBuf==0 ){ return 0; } zBuf[0] = 0; sqlite3SetString(&zFull, getcwd(zBuf, 5000), "/", zRelative, (char*)0); sqliteFree(zBuf); } /* ** Remove "/./" path elements and convert "/A/./" path elements ** to just "/". */ if( zFull ){ for(i=j=0; zFull[i]; i++){ if( zFull[i]=='/' ){ if( zFull[i+1]=='/' ) continue; if( zFull[i+1]=='.' && zFull[i+2]=='/' ){ i += 1; continue; } if( zFull[i+1]=='.' && zFull[i+2]=='.' && zFull[i+3]=='/' ){ while( j>0 && zFull[j-1]!='/' ){ j--; } i += 3; continue; } } zFull[j++] = zFull[i]; } zFull[j] = 0; } return zFull; } /* ** Change the value of the fullsync flag in the given file descriptor. */ static void unixSetFullSync(OsFile *id, int v){ |
︙ | ︙ |
Changes to src/test_async.c.
︙ | ︙ | |||
157 158 159 160 161 162 163 | ** write-op queue. So async.queueMutex is held for the duration ** of these operations to prevent other threads from changing the ** queue in mid operation. ** ** ** asyncLock, asyncUnlock, asyncLockState, asyncCheckReservedLock ** | | > > | | 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | ** write-op queue. So async.queueMutex is held for the duration ** of these operations to prevent other threads from changing the ** queue in mid operation. ** ** ** asyncLock, asyncUnlock, asyncLockState, asyncCheckReservedLock ** ** These primitives implement in-process locking using a hash table ** on the file name. Files are locked correctly for connections coming ** from the same process. But other processes cannot see these locks ** and will therefore not honor them. ** ** ** asyncFileHandle. ** ** The sqlite3OsFileHandle() function is currently only used when ** debugging the pager module. Unless sqlite3OsClose() is called on the ** file (shouldn't be possible for other reasons), the underlying |
︙ | ︙ | |||
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 | /* ** State information is held in the static variable "async" defined ** as follows: */ static struct TestAsyncStaticData { pthread_mutex_t queueMutex; /* Mutex for access to write operation queue */ pthread_mutex_t writerMutex; /* Prevents multiple writer threads */ pthread_cond_t queueSignal; /* For waking up sleeping writer thread */ pthread_cond_t emptySignal; /* Notify when the write queue is empty */ AsyncWrite *pQueueFirst; /* Next write operation to be processed */ AsyncWrite *pQueueLast; /* Last write operation on the list */ volatile int ioDelay; /* Extra delay between write operations */ volatile int writerHaltWhenIdle; /* Writer thread halts when queue empty */ volatile int writerHaltNow; /* Writer thread halts after next op */ } async = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, }; /* Possible values of AsyncWrite.op */ #define ASYNC_NOOP 0 #define ASYNC_WRITE 1 #define ASYNC_SYNC 2 #define ASYNC_TRUNCATE 3 #define ASYNC_CLOSE 4 #define ASYNC_OPENDIRECTORY 5 #define ASYNC_SETFULLSYNC 6 | > > > < | | 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 | /* ** State information is held in the static variable "async" defined ** as follows: */ static struct TestAsyncStaticData { pthread_mutex_t queueMutex; /* Mutex for access to write operation queue */ pthread_mutex_t writerMutex; /* Prevents multiple writer threads */ pthread_mutex_t lockMutex; /* For access to aLock hash table */ pthread_cond_t queueSignal; /* For waking up sleeping writer thread */ pthread_cond_t emptySignal; /* Notify when the write queue is empty */ AsyncWrite *pQueueFirst; /* Next write operation to be processed */ AsyncWrite *pQueueLast; /* Last write operation on the list */ Hash aLock; /* Files locked */ volatile int ioDelay; /* Extra delay between write operations */ volatile int writerHaltWhenIdle; /* Writer thread halts when queue empty */ volatile int writerHaltNow; /* Writer thread halts after next op */ } async = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, }; /* Possible values of AsyncWrite.op */ #define ASYNC_NOOP 0 #define ASYNC_WRITE 1 #define ASYNC_SYNC 2 #define ASYNC_TRUNCATE 3 #define ASYNC_CLOSE 4 #define ASYNC_OPENDIRECTORY 5 #define ASYNC_SETFULLSYNC 6 #define ASYNC_DELETE 7 #define ASYNC_OPENEXCLUSIVE 8 #define ASYNC_SYNCDIRECTORY 9 /* Names of opcodes. Used for debugging only. ** Make sure these stay in sync with the macros above! */ static const char *azOpcodeName[] = { "NOOP", "WRITE", "SYNC", "TRUNCATE", "CLOSE", "OPENDIR", "SETFULLSYNC", "DELETE", "OPENEX", "SYNCDIR", }; /* ** Entries on the write-op queue are instances of the AsyncWrite ** structure, defined here. ** ** The interpretation of the iOffset and nByte variables varies depending |
︙ | ︙ | |||
303 304 305 306 307 308 309 310 311 312 313 314 315 316 | ** ASYNC_DELETE: ** iOffset -> Unused. ** nByte -> Number of bytes of zBuf points to (file name). ** ** ASYNC_OPENEXCLUSIVE: ** iOffset -> Value of "delflag". ** nByte -> Number of bytes of zBuf points to (file name). ** ** For an ASYNC_WRITE operation, zBuf points to the data to write to the file. ** This space is sqliteMalloc()d along with the AsyncWrite structure in a ** single blob, so is deleted when sqliteFree() is called on the parent ** structure. */ struct AsyncWrite { | > | 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | ** ASYNC_DELETE: ** iOffset -> Unused. ** nByte -> Number of bytes of zBuf points to (file name). ** ** ASYNC_OPENEXCLUSIVE: ** iOffset -> Value of "delflag". ** nByte -> Number of bytes of zBuf points to (file name). ** ** ** For an ASYNC_WRITE operation, zBuf points to the data to write to the file. ** This space is sqliteMalloc()d along with the AsyncWrite structure in a ** single blob, so is deleted when sqliteFree() is called on the parent ** structure. */ struct AsyncWrite { |
︙ | ︙ | |||
325 326 327 328 329 330 331 332 333 334 335 336 337 338 | /* ** The AsyncFile structure is a subclass of OsFile used for asynchronous IO. */ struct AsyncFile { IoMethod *pMethod; /* Must be first */ int ioError; /* Value of any asychronous error we have seen */ i64 iOffset; /* Current seek() offset in file */ OsFile *pBaseRead; /* Read handle to the underlying Os file */ OsFile *pBaseWrite; /* Write handle to the underlying Os file */ }; /* ** Add an entry to the end of the global write-op list. pWrite should point ** to an AsyncWrite structure allocated using sqlite3OsMalloc(). The writer | > > | 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | /* ** The AsyncFile structure is a subclass of OsFile used for asynchronous IO. */ struct AsyncFile { IoMethod *pMethod; /* Must be first */ int ioError; /* Value of any asychronous error we have seen */ i64 iOffset; /* Current seek() offset in file */ char *zName; /* Underlying OS filename - used for debugging */ int nName; /* Number of characters in zName */ OsFile *pBaseRead; /* Read handle to the underlying Os file */ OsFile *pBaseWrite; /* Write handle to the underlying Os file */ }; /* ** Add an entry to the end of the global write-op list. pWrite should point ** to an AsyncWrite structure allocated using sqlite3OsMalloc(). The writer |
︙ | ︙ | |||
352 353 354 355 356 357 358 | if( async.pQueueLast ){ assert( async.pQueueFirst ); async.pQueueLast->pNext = pWrite; }else{ async.pQueueFirst = pWrite; } async.pQueueLast = pWrite; | | > | 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | if( async.pQueueLast ){ assert( async.pQueueFirst ); async.pQueueLast->pNext = pWrite; }else{ async.pQueueFirst = pWrite; } async.pQueueLast = pWrite; TRACE(("PUSH %p (%s %s)\n", pWrite, azOpcodeName[pWrite->op], pWrite->pFile ? pWrite->pFile->zName : "-")); /* Drop the queue mutex */ pthread_mutex_unlock(&async.queueMutex); /* The writer thread might have been idle because there was nothing ** on the write-op queue for it to do. So wake it up. */ pthread_cond_signal(&async.queueSignal); |
︙ | ︙ | |||
581 582 583 584 585 586 587 | ** at the moment anyway. */ static int asyncFileHandle(OsFile *id){ return sqlite3OsFileHandle(((AsyncFile *)id)->pBaseRead); } /* | > | > > > | > > > > > | > > > > > > | | 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | ** at the moment anyway. */ static int asyncFileHandle(OsFile *id){ return sqlite3OsFileHandle(((AsyncFile *)id)->pBaseRead); } /* ** No disk locking is performed. We keep track of locks locally in ** the async.aLock hash table. Locking should appear to work the same ** as with standard (unmodified) SQLite as long as all connections ** come from this one process. Connections from external processes ** cannot see our internal hash table (obviously) and will thus not ** honor our locks. */ static int asyncLock(OsFile *id, int lockType){ AsyncFile *pFile = (AsyncFile*)id; TRACE(("LOCK %d (%s)\n", lockType, pFile->zName)); pthread_mutex_lock(&async.lockMutex); sqlite3HashInsert(&async.aLock, pFile->zName, pFile->nName, (void*)lockType); pthread_mutex_unlock(&async.lockMutex); return SQLITE_OK; } static int asyncUnlock(OsFile *id, int lockType){ return asyncLock(id, lockType); } /* ** This function is called when the pager layer first opens a database file ** and is checking for a hot-journal. */ static int asyncCheckReservedLock(OsFile *id){ AsyncFile *pFile = (AsyncFile*)id; int rc; pthread_mutex_lock(&async.lockMutex); rc = (int)sqlite3HashFind(&async.aLock, pFile->zName, pFile->nName); pthread_mutex_unlock(&async.lockMutex); TRACE(("CHECK-LOCK %d (%s)\n", rc, pFile->zName)); return rc; } /* ** This is broken. But sqlite3OsLockState() is only used for testing anyway. */ static int asyncLockState(OsFile *id){ return SQLITE_OK; |
︙ | ︙ | |||
628 629 630 631 632 633 634 | */ static int asyncOpenFile( const char *zName, /* The name of the file to be opened */ OsFile **pFile, /* Put the OsFile structure here */ OsFile *pBaseRead, /* The real OsFile from the real I/O routine */ int openForWriting /* Open a second file handle for writing if true */ ){ | | | 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | */ static int asyncOpenFile( const char *zName, /* The name of the file to be opened */ OsFile **pFile, /* Put the OsFile structure here */ OsFile *pBaseRead, /* The real OsFile from the real I/O routine */ int openForWriting /* Open a second file handle for writing if true */ ){ int rc, i, n; AsyncFile *p; OsFile *pBaseWrite = 0; static IoMethod iomethod = { asyncClose, asyncOpenDirectory, asyncRead, |
︙ | ︙ | |||
657 658 659 660 661 662 663 | int dummy; rc = xOrigOpenReadWrite(zName, &pBaseWrite, &dummy); if( rc!=SQLITE_OK ){ goto error_out; } } | > > | | > > | 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | int dummy; rc = xOrigOpenReadWrite(zName, &pBaseWrite, &dummy); if( rc!=SQLITE_OK ){ goto error_out; } } n = strlen(zName); for(i=n-1; i>=0 && zName[i]!='/'; i--){} p = (AsyncFile *)sqlite3OsMalloc(sizeof(AsyncFile) + n - i); if( !p ){ rc = SQLITE_NOMEM; goto error_out; } memset(p, 0, sizeof(AsyncFile)); p->zName = (char*)&p[1]; strcpy(p->zName, &zName[i+1]); p->nName = n - i; p->pMethod = &iomethod; p->pBaseRead = pBaseRead; p->pBaseWrite = pBaseWrite; p->ioError = SQLITE_OK; *pFile = (OsFile *)p; return SQLITE_OK; |
︙ | ︙ | |||
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 | if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, z) ){ ret = 0; }else if( p->op==ASYNC_OPENEXCLUSIVE && 0==strcmp(p->zBuf, z) ){ ret = 1; } } pthread_mutex_unlock(&async.queueMutex); return ret; } /* ** Call this routine to enable or disable the ** asynchronous IO features implemented in this file. ** ** This routine is not even remotely threadsafe. Do not call ** this routine while any SQLite database connections are open. */ static void asyncEnable(int enable){ if( enable && xOrigOpenReadWrite==0 ){ xOrigOpenReadWrite = sqlite3Os.xOpenReadWrite; xOrigOpenReadOnly = sqlite3Os.xOpenReadOnly; xOrigOpenExclusive = sqlite3Os.xOpenExclusive; xOrigDelete = sqlite3Os.xDelete; xOrigFileExists = sqlite3Os.xFileExists; xOrigSyncDirectory = sqlite3Os.xSyncDirectory; sqlite3Os.xOpenReadWrite = asyncOpenReadWrite; sqlite3Os.xOpenReadOnly = asyncOpenReadOnly; sqlite3Os.xOpenExclusive = asyncOpenExclusive; sqlite3Os.xDelete = asyncDelete; sqlite3Os.xFileExists = asyncFileExists; sqlite3Os.xSyncDirectory = asyncSyncDirectory; } if( !enable && xOrigOpenReadWrite!=0 ){ sqlite3Os.xOpenReadWrite = xOrigOpenReadWrite; sqlite3Os.xOpenReadOnly = xOrigOpenReadOnly; sqlite3Os.xOpenExclusive = xOrigOpenExclusive; sqlite3Os.xDelete = xOrigDelete; sqlite3Os.xFileExists = xOrigFileExists; sqlite3Os.xSyncDirectory = xOrigSyncDirectory; | > > > > > | 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 | if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, z) ){ ret = 0; }else if( p->op==ASYNC_OPENEXCLUSIVE && 0==strcmp(p->zBuf, z) ){ ret = 1; } } TRACE(("EXISTS: %s = %d\n", z, ret)); pthread_mutex_unlock(&async.queueMutex); return ret; } /* ** Call this routine to enable or disable the ** asynchronous IO features implemented in this file. ** ** This routine is not even remotely threadsafe. Do not call ** this routine while any SQLite database connections are open. */ static void asyncEnable(int enable){ if( enable && xOrigOpenReadWrite==0 ){ sqlite3HashInit(&async.aLock, SQLITE_HASH_BINARY, 1); xOrigOpenReadWrite = sqlite3Os.xOpenReadWrite; xOrigOpenReadOnly = sqlite3Os.xOpenReadOnly; xOrigOpenExclusive = sqlite3Os.xOpenExclusive; xOrigDelete = sqlite3Os.xDelete; xOrigFileExists = sqlite3Os.xFileExists; xOrigSyncDirectory = sqlite3Os.xSyncDirectory; sqlite3Os.xOpenReadWrite = asyncOpenReadWrite; sqlite3Os.xOpenReadOnly = asyncOpenReadOnly; sqlite3Os.xOpenExclusive = asyncOpenExclusive; sqlite3Os.xDelete = asyncDelete; sqlite3Os.xFileExists = asyncFileExists; sqlite3Os.xSyncDirectory = asyncSyncDirectory; } if( !enable && xOrigOpenReadWrite!=0 ){ sqlite3HashClear(&async.aLock); sqlite3Os.xOpenReadWrite = xOrigOpenReadWrite; sqlite3Os.xOpenReadOnly = xOrigOpenReadOnly; sqlite3Os.xOpenExclusive = xOrigOpenExclusive; sqlite3Os.xDelete = xOrigDelete; sqlite3Os.xFileExists = xOrigFileExists; sqlite3Os.xSyncDirectory = xOrigSyncDirectory; |
︙ | ︙ | |||
843 844 845 846 847 848 849 | }else{ TRACE(("IDLE\n")); pthread_cond_wait(&async.queueSignal, &async.queueMutex); TRACE(("WAKEUP\n")); } } if( p==0 ) break; | | > | 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | }else{ TRACE(("IDLE\n")); pthread_cond_wait(&async.queueSignal, &async.queueMutex); TRACE(("WAKEUP\n")); } } if( p==0 ) break; TRACE(("PROCESSING %p (%s %s)\n", p, azOpcodeName[p->op], p->pFile ? p->pFile->zName : "-")); /* Right now this thread is holding the mutex on the write-op queue. ** Variable 'p' points to the first entry in the write-op queue. In ** the general case, we hold on to the mutex for the entire body of ** the loop. ** ** However in the cases enumerated below, we relinquish the mutex, |
︙ | ︙ | |||
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 | */ static int testAsyncWait( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ if( async.writerHaltNow==0 && async.writerHaltWhenIdle==0 ){ Tcl_AppendResult(interp, "would block forever", (char*)0); return TCL_ERROR; } | > > > > > > | | | | | | > > > | 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 | */ static int testAsyncWait( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ int cnt = 10; if( async.writerHaltNow==0 && async.writerHaltWhenIdle==0 ){ Tcl_AppendResult(interp, "would block forever", (char*)0); return TCL_ERROR; } while( cnt-- && !pthread_mutex_trylock(&async.writerMutex) ){ pthread_mutex_unlock(&async.writerMutex); sched_yield(); } if( cnt>=0 ){ TRACE(("WAIT\n")); pthread_mutex_lock(&async.queueMutex); pthread_cond_broadcast(&async.queueSignal); pthread_mutex_unlock(&async.queueMutex); pthread_mutex_lock(&async.writerMutex); pthread_mutex_unlock(&async.writerMutex); }else{ TRACE(("NOTHING TO WAIT ON\n")); } return TCL_OK; } #endif /* OS_UNIX and THREADSAFE and defined(SQLITE_ENABLE_REDEF_IO) */ /* |
︙ | ︙ |