Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Make the test_async backend work again. (CVS 4293) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
04167483aad42d319393e8caf470d8de |
User & Date: | danielk1977 2007-08-25 12:29:30.000 |
Context
2007-08-25
| ||
12:39 | Update the async2.test test script to use the new malloc() failure simulation interface. (CVS 4294) (check-in: 686fcfeda4 user: danielk1977 tags: trunk) | |
12:29 | Make the test_async backend work again. (CVS 4293) (check-in: 04167483aa user: danielk1977 tags: trunk) | |
03:59 | Bug fix in the implementation of recursive mutexes using non-recursive pthreads mutexes. Ticket #2588. (CVS 4292) (check-in: 7d24c3a5a7 user: drh tags: trunk) | |
Changes
Changes to src/test6.c.
︙ | ︙ | |||
513 514 515 516 517 518 519 | ** Application data for the crash VFS */ struct crashAppData { sqlite3_vfs *pOrig; /* Wrapped vfs structure */ }; /* | | < | 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | ** Application data for the crash VFS */ struct crashAppData { sqlite3_vfs *pOrig; /* Wrapped vfs structure */ }; /* ** Open a crash-file file handle. ** ** The caller will have allocated pVfs->szOsFile bytes of space ** at pFile. This file uses this space for the CrashFile structure ** and allocates space for the "real" file structure using ** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is ** equal or greater than sizeof(CrashFile). */ |
︙ | ︙ | |||
580 581 582 583 584 585 586 587 588 589 590 591 592 593 | static int cfFullPathname(sqlite3_vfs *pCfVfs, const char *zPath, char *zPathOut){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xFullPathname(pVfs, zPath, zPathOut); } static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xDlOpen(pVfs, zPath); } static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xRandomness(pVfs, nByte, zBufOut); } static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; | > > > > > > > > > > > > | 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | static int cfFullPathname(sqlite3_vfs *pCfVfs, const char *zPath, char *zPathOut){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xFullPathname(pVfs, zPath, zPathOut); } static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xDlOpen(pVfs, zPath); } static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; pVfs->xDlError(pVfs, nByte, zErrMsg); } static void *cfDlSym(sqlite3_vfs *pCfVfs, void *pHandle, const char *zSymbol){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xDlSym(pVfs, pHandle, zSymbol); } static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; pVfs->xDlClose(pVfs, pHandle); } static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; return pVfs->xRandomness(pVfs, nByte, zBufOut); } static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
︙ | ︙ | |||
728 729 730 731 732 733 734 | cfOpen, /* xOpen */ cfDelete, /* xDelete */ cfAccess, /* xAccess */ cfGetTempName, /* xGetTempName */ cfFullPathname, /* xFullPathname */ cfDlOpen, /* xDlOpen */ | | | | < < < | 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 | cfOpen, /* xOpen */ cfDelete, /* xDelete */ cfAccess, /* xAccess */ cfGetTempName, /* xGetTempName */ cfFullPathname, /* xFullPathname */ cfDlOpen, /* xDlOpen */ cfDlError, /* xDlError */ cfDlSym, /* xDlSym */ cfDlClose, /* xDlClose */ cfRandomness, /* xRandomness */ cfSleep, /* xSleep */ cfCurrentTime /* xCurrentTime */ }; if( crashVfs.pAppData==0 ){ sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0); crashVfs.mxPathname = pOriginalVfs->mxPathname; crashVfs.pAppData = (void *)pOriginalVfs; crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile; /* sqlite3_vfs_unregister(pOriginalVfs); */ sqlite3_vfs_register(&crashVfs, 1); } |
︙ | ︙ |
Changes to src/test_async.c.
︙ | ︙ | |||
71 72 73 74 75 76 77 | */ #include "sqliteInt.h" #include <tcl.h> /* ** This test uses pthreads and hence only works on unix and with | | < < < | > | 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 | */ #include "sqliteInt.h" #include <tcl.h> /* ** This test uses pthreads and hence only works on unix and with ** a threadsafe build of SQLite. */ #if OS_UNIX && SQLITE_THREADSAFE /* ** This demo uses pthreads. If you do not have a pthreads implementation ** for your operating system, you will need to recode the threading ** logic. */ #include <pthread.h> #include <sched.h> /* Useful macros used in several places */ #define MIN(x,y) ((x)<(y)?(x):(y)) #define MAX(x,y) ((x)>(y)?(x):(y)) /* Forward references */ typedef struct AsyncWrite AsyncWrite; typedef struct AsyncFile AsyncFile; typedef struct AsyncFileData AsyncFileData; /* Enable for debugging */ static int sqlite3async_trace = 0; # define ASYNC_TRACE(X) if( sqlite3async_trace ) asyncTrace X static void asyncTrace(const char *zFormat, ...){ char *z; va_list ap; |
︙ | ︙ | |||
276 277 278 279 280 281 282 | ** on the value of AsyncWrite.op: ** ** ASYNC_WRITE: ** iOffset -> Offset in file to write to. ** nByte -> Number of bytes of data to write (pointed to by zBuf). ** ** ASYNC_SYNC: | < | | | | | > > > > > > > > | < > > > | | | | | 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 | ** on the value of AsyncWrite.op: ** ** ASYNC_WRITE: ** iOffset -> Offset in file to write to. ** nByte -> Number of bytes of data to write (pointed to by zBuf). ** ** ASYNC_SYNC: ** nByte -> flags to pass to sqlite3OsSync(). ** ** ASYNC_TRUNCATE: ** iOffset -> Size to truncate file to. ** nByte -> Unused. ** ** ASYNC_CLOSE: ** iOffset -> Unused. ** nByte -> Unused. ** ** ASYNC_OPENDIRECTORY: ** iOffset -> Unused. ** nByte -> Number of bytes of zBuf points to (directory name). ** ** ASYNC_SETFULLSYNC: ** iOffset -> Unused. ** nByte -> New value for the full-sync flag. ** ** ** ASYNC_DELETE: ** iOffset -> Contains the "syncDir" flag. ** 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 sqlite3_malloc()d along with the AsyncWrite structure in a ** single blob, so is deleted when sqlite3_free() is called on the parent ** structure. */ struct AsyncWrite { AsyncFileData *pFileData; /* File to write data to or sync */ int op; /* One of ASYNC_xxx etc. */ i64 iOffset; /* See above */ int nByte; /* See above */ char *zBuf; /* Data to write to file (or NULL if op!=ASYNC_WRITE) */ AsyncWrite *pNext; /* Next write operation (to any file) */ }; /* ** The AsyncFile structure is a subclass of sqlite3_file used for ** asynchronous IO. ** ** All of the actual data for the structure is stored in the structure ** pointed to by AsyncFile.pData, which is allocated as part of the ** sqlite3OsOpen() using sqlite3_malloc(). The reason for this is that the ** lifetime of the AsyncFile structure is ended by the caller after OsClose() ** is called, but the data in AsyncFileData may be required by the ** writer thread after that point. */ struct AsyncFile { sqlite3_io_methods *pMethod; AsyncFileData *pData; }; struct AsyncFileData { char *zName; /* Underlying OS filename - used for debugging */ int nName; /* Number of characters in zName */ sqlite3_file *pBaseRead; /* Read handle to the underlying Os file */ sqlite3_file *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 sqlite3_malloc(). The writer ** thread will call sqlite3_free() to free the structure after the specified ** operation has been completed. |
︙ | ︙ | |||
355 356 357 358 359 360 361 | assert( async.pQueueFirst ); async.pQueueLast->pNext = pWrite; }else{ async.pQueueFirst = pWrite; } async.pQueueLast = pWrite; ASYNC_TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op], | | | 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | assert( async.pQueueFirst ); async.pQueueLast->pNext = pWrite; }else{ async.pQueueFirst = pWrite; } async.pQueueLast = pWrite; ASYNC_TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op], pWrite->pFileData ? pWrite->pFileData->zName : "-", pWrite->iOffset)); if( pWrite->op==ASYNC_CLOSE ){ async.nFile--; if( async.nFile==0 ){ async.ioError = SQLITE_OK; } } |
︙ | ︙ | |||
390 391 392 393 394 395 396 | } /* ** This is a utility function to allocate and populate a new AsyncWrite ** structure and insert it (via addAsyncWrite() ) into the global list. */ static int addNewAsyncWrite( | | | | > | | < < < < < | < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < | > < | | | | < < < < | | | | < | | | | | < < | > > > > > > | > | > | | < | | > | | | | | | | | | | < < < < < < < < | | | | | | | | | | < > | < | > | | | | < | > | | < > | | < | > < | | > | | | | < | < | < | < | | < | | < < | | | < | > > | < | < < < < < | < < < < < < < < | > | | | | < | < | | > | | < | | < < > | | | < < < < < < < < | | | > > < < < < | > > > | > | | | < | < < < < < | > > | < < < | | < < > < < | | | < < < < < < < < | < < | | > > > > > > | < < | | | | | > > | | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > | | < < < < < < | < < < < < < < | | | > | < < < < < < | < < < < < < < | 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 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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 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 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 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | } /* ** This is a utility function to allocate and populate a new AsyncWrite ** structure and insert it (via addAsyncWrite() ) into the global list. */ static int addNewAsyncWrite( AsyncFileData *pFileData, int op, i64 iOffset, int nByte, const char *zByte ){ AsyncWrite *p; if( op!=ASYNC_CLOSE && async.ioError ){ return async.ioError; } p = sqlite3_malloc(sizeof(AsyncWrite) + (zByte?nByte:0)); if( !p ){ return SQLITE_NOMEM; } p->op = op; p->iOffset = iOffset; p->nByte = nByte; p->pFileData = pFileData; p->pNext = 0; if( zByte ){ p->zBuf = (char *)&p[1]; memcpy(p->zBuf, zByte, nByte); }else{ p->zBuf = 0; } addAsyncWrite(p); return SQLITE_OK; } /* ** Close the file. This just adds an entry to the write-op list, the file is ** not actually closed. */ static int asyncClose(sqlite3_file *pFile){ AsyncFileData *p = ((AsyncFile *)pFile)->pData; return addNewAsyncWrite(p, ASYNC_CLOSE, 0, 0, 0); } /* ** Implementation of sqlite3OsWrite() for asynchronous files. Instead of ** writing to the underlying file, this function adds an entry to the end of ** the global AsyncWrite list. Either SQLITE_OK or SQLITE_NOMEM may be ** returned. */ static int asyncWrite(sqlite3_file *pFile, const void *pBuf, int amt, i64 iOff){ AsyncFileData *p = ((AsyncFile *)pFile)->pData; return addNewAsyncWrite(p, ASYNC_WRITE, iOff, amt, pBuf); } /* ** Read data from the file. First we read from the filesystem, then adjust ** the contents of the buffer based on ASYNC_WRITE operations in the ** write-op queue. ** ** This method holds the mutex from start to finish. */ static int asyncRead(sqlite3_file *pFile, void *zOut, int iAmt, i64 iOffset){ AsyncFileData *p = ((AsyncFile *)pFile)->pData; int rc = SQLITE_OK; i64 filesize; int nRead; sqlite3_file *pBase = p->pBaseRead; /* If an I/O error has previously occurred in this virtual file ** system, then all subsequent operations fail. */ if( async.ioError!=SQLITE_OK ){ return async.ioError; } /* Grab the write queue mutex for the duration of the call */ pthread_mutex_lock(&async.queueMutex); if( pBase->pMethods ){ rc = sqlite3OsFileSize(pBase, &filesize); if( rc!=SQLITE_OK ){ goto asyncread_out; } nRead = MIN(filesize - iOffset, iAmt); if( nRead>0 ){ rc = sqlite3OsRead(pBase, zOut, nRead, iOffset); ASYNC_TRACE(("READ %s %d bytes at %d\n", p->zName, nRead, iOffset)); } } if( rc==SQLITE_OK ){ AsyncWrite *pWrite; for(pWrite=async.pQueueFirst; pWrite; pWrite = pWrite->pNext){ if( pWrite->pFileData==p && pWrite->op==ASYNC_WRITE ){ int iBeginOut = (pWrite->iOffset-iOffset); int iBeginIn = -iBeginOut; int nCopy; if( iBeginIn<0 ) iBeginIn = 0; if( iBeginOut<0 ) iBeginOut = 0; nCopy = MIN(pWrite->nByte-iBeginIn, iAmt-iBeginOut); if( nCopy>0 ){ memcpy(&((char *)zOut)[iBeginOut], &pWrite->zBuf[iBeginIn], nCopy); ASYNC_TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset)); } } } } asyncread_out: pthread_mutex_unlock(&async.queueMutex); return rc; } /* ** Truncate the file to nByte bytes in length. This just adds an entry to ** the write-op list, no IO actually takes place. */ static int asyncTruncate(sqlite3_file *pFile, i64 nByte){ AsyncFileData *p = ((AsyncFile *)pFile)->pData; return addNewAsyncWrite(p, ASYNC_TRUNCATE, nByte, 0, 0); } /* ** Sync the file. This just adds an entry to the write-op list, the ** sync() is done later by sqlite3_async_flush(). */ static int asyncSync(sqlite3_file *pFile, int flags){ AsyncFileData *p = ((AsyncFile *)pFile)->pData; return addNewAsyncWrite(p, ASYNC_SYNC, 0, flags, 0); } /* ** Read the size of the file. First we read the size of the file system ** entry, then adjust for any ASYNC_WRITE or ASYNC_TRUNCATE operations ** currently in the write-op list. ** ** This method holds the mutex from start to finish. */ int asyncFileSize(sqlite3_file *pFile, i64 *piSize){ AsyncFileData *p = ((AsyncFile *)pFile)->pData; int rc = SQLITE_OK; i64 s = 0; sqlite3_file *pBase; pthread_mutex_lock(&async.queueMutex); /* Read the filesystem size from the base file. If pBaseRead is NULL, this ** means the file hasn't been opened yet. In this case all relevant data ** must be in the write-op queue anyway, so we can omit reading from the ** file-system. */ pBase = p->pBaseRead; if( pBase->pMethods ){ rc = sqlite3OsFileSize(pBase, &s); } if( rc==SQLITE_OK ){ AsyncWrite *pWrite; for(pWrite=async.pQueueFirst; pWrite; pWrite = pWrite->pNext){ if( pWrite->pFileData==p ){ switch( pWrite->op ){ case ASYNC_WRITE: s = MAX(pWrite->iOffset + (i64)(pWrite->nByte), s); break; case ASYNC_TRUNCATE: s = MIN(s, pWrite->iOffset); break; } } } *piSize = s; } pthread_mutex_unlock(&async.queueMutex); return rc; } /* ** 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(sqlite3_file *pFile, int lockType){ AsyncFileData *p = ((AsyncFile *)pFile)->pData; ASYNC_TRACE(("LOCK %d (%s)\n", lockType, p->zName)); pthread_mutex_lock(&async.lockMutex); sqlite3HashInsert(&async.aLock, p->zName, p->nName, (void*)lockType); pthread_mutex_unlock(&async.lockMutex); return SQLITE_OK; } static int asyncUnlock(sqlite3_file *pFile, int lockType){ return asyncLock(pFile, lockType); } /* ** This function is called when the pager layer first opens a database file ** and is checking for a hot-journal. */ static int asyncCheckReservedLock(sqlite3_file *pFile){ AsyncFileData *p = ((AsyncFile *)pFile)->pData; int rc; pthread_mutex_lock(&async.lockMutex); rc = (int)sqlite3HashFind(&async.aLock, p->zName, p->nName); pthread_mutex_unlock(&async.lockMutex); ASYNC_TRACE(("CHECK-LOCK %d (%s)\n", rc, p->zName)); return rc>SHARED_LOCK; } /* ** This is a no-op, as the asynchronous backend does not support locking. */ static int asyncBreakLock(sqlite3_file *id){ return SQLITE_OK; } /* ** This is broken. But sqlite3OsLockState() is only used for testing anyway. */ static int asyncLockState(sqlite3_file *id){ return SQLITE_OK; } /* ** Return the device characteristics and sector-size of the device. It ** is not tricky to implement these correctly, as this backend might ** not have an open file handle at this point. */ static int asyncSectorSize(sqlite3_file *pFile){ return 512; } static int asyncDeviceCharacteristics(sqlite3_file *pFile){ return 0; } /* ** Open a file. */ static int asyncOpen( sqlite3_vfs *pAsyncVfs, const char *zName, sqlite3_file *pFile, int flags, int *pOutFlags ){ static sqlite3_io_methods async_methods = { 1, /* iVersion */ asyncClose, /* xClose */ asyncRead, /* xRead */ asyncWrite, /* xWrite */ asyncTruncate, /* xTruncate */ asyncSync, /* xSync */ asyncFileSize, /* xFileSize */ asyncLock, /* xLock */ asyncUnlock, /* xUnlock */ asyncCheckReservedLock, /* xCheckReservedLock */ asyncBreakLock, /* xBreakLock */ asyncLockState, /* xLockState */ asyncSectorSize, /* xSectorSize */ asyncDeviceCharacteristics /* xDeviceCharacteristics */ }; sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; AsyncFile *p = (AsyncFile *)pFile; int nName = strlen(zName);; int rc; int nByte; AsyncFileData *pData; nByte = ( sizeof(AsyncFileData) + /* AsyncFileData structure */ 2 * pVfs->szOsFile + /* AsyncFileData.zName */ nName + 1 /* AsyncFileData.pBaseRead and pBaseWrite */ ); pData = sqlite3_malloc(nByte); if( !pData ){ return SQLITE_NOMEM; } memset(pData, 0, nByte); pData->zName = (char *)&pData[1]; pData->nName = nName; pData->pBaseRead = (sqlite3_file *)&pData->zName[nName+1]; pData->pBaseWrite = (sqlite3_file *)&pData->zName[nName+1+pVfs->szOsFile]; memcpy(pData->zName, zName, nName+1); if( flags&SQLITE_OPEN_EXCLUSIVE ){ rc = addNewAsyncWrite(pData, ASYNC_OPENEXCLUSIVE, (i64)flags, 0, 0); if( pOutFlags ) *pOutFlags = flags; }else{ rc = sqlite3OsOpen(pVfs, zName, pData->pBaseRead, flags, pOutFlags); if( rc==SQLITE_OK && ((*pOutFlags)&SQLITE_OPEN_READWRITE) ){ rc = sqlite3OsOpen(pVfs, zName, pData->pBaseWrite, flags, 0); } } if( rc==SQLITE_OK ){ p->pMethod = &async_methods; p->pData = pData; incrOpenFileCount(); }else{ sqlite3OsClose(pData->pBaseRead); sqlite3OsClose(pData->pBaseWrite); sqlite3_free(pData); } return rc; } /* ** Implementation of sqlite3OsDelete. Add an entry to the end of the ** write-op queue to perform the delete. */ static int asyncDelete(sqlite3_vfs *pAsyncVfs, const char *z, int syncDir){ return addNewAsyncWrite(0, ASYNC_DELETE, syncDir, strlen(z)+1, z); } /* ** Implementation of sqlite3OsAccess. This method holds the mutex from ** start to finish. */ static int asyncAccess(sqlite3_vfs *pAsyncVfs, const char *zName, int flags){ int ret; AsyncWrite *p; sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; assert(flags==SQLITE_ACCESS_READWRITE || flags==SQLITE_ACCESS_READONLY || flags==SQLITE_ACCESS_EXISTS ); pthread_mutex_lock(&async.queueMutex); ret = sqlite3OsAccess(pVfs, zName, flags); if( flags==SQLITE_ACCESS_EXISTS ){ for(p=async.pQueueFirst; p; p = p->pNext){ if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, zName) ){ ret = 0; }else if( p->op==ASYNC_OPENEXCLUSIVE && 0==strcmp(p->pFileData->zName, zName) ){ ret = 1; } } } ASYNC_TRACE(("ACCESS(%s): %s = %d\n", flags==SQLITE_ACCESS_READWRITE?"read-write": flags==SQLITE_ACCESS_READONLY?"read-only":"exists" , zName, ret) ); pthread_mutex_unlock(&async.queueMutex); return ret; } static int asyncGetTempName(sqlite3_vfs *pAsyncVfs, char *zBufOut){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; return pVfs->xGetTempName(pVfs, zBufOut); } static int asyncFullPathname( sqlite3_vfs *pAsyncVfs, const char *zPath, char *zPathOut ){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; return sqlite3OsFullPathname(pVfs, zPath, zPathOut); } static void *asyncDlOpen(sqlite3_vfs *pAsyncVfs, const char *zPath){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; return pVfs->xDlOpen(pVfs, zPath); } static void asyncDlError(sqlite3_vfs *pAsyncVfs, int nByte, char *zErrMsg){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; pVfs->xDlError(pVfs, nByte, zErrMsg); } static void *asyncDlSym( sqlite3_vfs *pAsyncVfs, void *pHandle, const char *zSymbol ){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; return pVfs->xDlSym(pVfs, pHandle, zSymbol); } static void asyncDlClose(sqlite3_vfs *pAsyncVfs, void *pHandle){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; pVfs->xDlClose(pVfs, pHandle); } static int asyncRandomness(sqlite3_vfs *pAsyncVfs, int nByte, char *zBufOut){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; return pVfs->xRandomness(pVfs, nByte, zBufOut); } static int asyncSleep(sqlite3_vfs *pAsyncVfs, int nMicro){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; return pVfs->xSleep(pVfs, nMicro); } static int asyncCurrentTime(sqlite3_vfs *pAsyncVfs, double *pTimeOut){ sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData; return pVfs->xCurrentTime(pVfs, pTimeOut); } static sqlite3_vfs async_vfs = { 1, /* iVersion */ sizeof(AsyncFile), /* szOsFile */ 0, /* mxPathname */ 0, /* pNext */ "async", /* zName */ 0, /* pAppData */ asyncOpen, /* xOpen */ asyncDelete, /* xDelete */ asyncAccess, /* xAccess */ asyncGetTempName, /* xGetTempName */ asyncFullPathname, /* xFullPathname */ asyncDlOpen, /* xDlOpen */ asyncDlError, /* xDlError */ asyncDlSym, /* xDlSym */ asyncDlClose, /* xDlClose */ asyncRandomness, /* xDlError */ asyncSleep, /* xDlSym */ asyncCurrentTime /* xDlClose */ }; /* ** 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 ){ if( !async_vfs.pAppData ){ async_vfs.pAppData = (void *)sqlite3_vfs_find(0); async_vfs.mxPathname = ((sqlite3_vfs *)async_vfs.pAppData)->mxPathname; sqlite3_vfs_register(&async_vfs, 1); sqlite3HashInit(&async.aLock, SQLITE_HASH_BINARY, 1); } }else{ if( async_vfs.pAppData ){ sqlite3_vfs_unregister(&async_vfs); async_vfs.pAppData = 0; sqlite3HashClear(&async.aLock); } } } /* ** This procedure runs in a separate thread, reading messages off of the ** write queue and processing them one by one. ** |
︙ | ︙ | |||
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 | ** ** An artifical delay of async.ioDelay milliseconds is inserted before ** each write operation in order to simulate the effect of a slow disk. ** ** Only one instance of this procedure may be running at a time. */ static void *asyncWriterThread(void *NotUsed){ AsyncWrite *p = 0; int rc = SQLITE_OK; int holdingMutex = 0; if( pthread_mutex_trylock(&async.writerMutex) ){ return 0; } while( async.writerHaltNow==0 ){ | > | | 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 | ** ** An artifical delay of async.ioDelay milliseconds is inserted before ** each write operation in order to simulate the effect of a slow disk. ** ** Only one instance of this procedure may be running at a time. */ static void *asyncWriterThread(void *NotUsed){ sqlite3_vfs *pVfs = (sqlite3_vfs *)(async_vfs.pAppData); AsyncWrite *p = 0; int rc = SQLITE_OK; int holdingMutex = 0; if( pthread_mutex_trylock(&async.writerMutex) ){ return 0; } while( async.writerHaltNow==0 ){ sqlite3_file *pBase = 0; if( !holdingMutex ){ pthread_mutex_lock(&async.queueMutex); } while( (p = async.pQueueFirst)==0 ){ pthread_cond_broadcast(&async.emptySignal); if( async.writerHaltWhenIdle ){ |
︙ | ︙ | |||
936 937 938 939 940 941 942 | ** * ASYNC_SYNC and ASYNC_WRITE operations, if ** SQLITE_ASYNC_TWO_FILEHANDLES was set at compile time and two ** file-handles are open for the particular file being "synced". */ if( async.ioError!=SQLITE_OK && p->op!=ASYNC_CLOSE ){ p->op = ASYNC_NOOP; } | | | | | | | < < | < | | > | | | | < < < < < < < < < < < < < < | < < < < | | | > | < < < < | 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 | ** * ASYNC_SYNC and ASYNC_WRITE operations, if ** SQLITE_ASYNC_TWO_FILEHANDLES was set at compile time and two ** file-handles are open for the particular file being "synced". */ if( async.ioError!=SQLITE_OK && p->op!=ASYNC_CLOSE ){ p->op = ASYNC_NOOP; } if( p->pFileData ){ pBase = p->pFileData->pBaseWrite; if( p->op==ASYNC_CLOSE || p->op==ASYNC_OPENEXCLUSIVE || (pBase->pMethods && (p->op==ASYNC_SYNC || p->op==ASYNC_WRITE) ) ){ pthread_mutex_unlock(&async.queueMutex); holdingMutex = 0; } if( !pBase->pMethods ){ pBase = p->pFileData->pBaseRead; } } switch( p->op ){ case ASYNC_NOOP: break; case ASYNC_WRITE: assert( pBase ); ASYNC_TRACE(("WRITE %s %d bytes at %d\n", p->pFileData->zName, p->nByte, p->iOffset)); rc = sqlite3OsWrite(pBase, (void *)(p->zBuf), p->nByte, p->iOffset); break; case ASYNC_SYNC: assert( pBase ); ASYNC_TRACE(("SYNC %s\n", p->pFileData->zName)); rc = sqlite3OsSync(pBase, p->nByte); break; case ASYNC_TRUNCATE: assert( pBase ); ASYNC_TRACE(("TRUNCATE %s to %d bytes\n", p->pFileData->zName, p->iOffset)); rc = sqlite3OsTruncate(pBase, p->iOffset); break; case ASYNC_CLOSE: ASYNC_TRACE(("CLOSE %s\n", p->pFileData->zName)); sqlite3OsClose(p->pFileData->pBaseWrite); sqlite3OsClose(p->pFileData->pBaseRead); sqlite3_free(p->pFileData); break; case ASYNC_DELETE: ASYNC_TRACE(("DELETE %s\n", p->zBuf)); rc = sqlite3OsDelete(pVfs, p->zBuf, (int)p->iOffset); break; case ASYNC_OPENEXCLUSIVE: { int flags = (int)p->iOffset; AsyncFileData *pData = p->pFileData; ASYNC_TRACE(("OPEN %s flags=%d\n", p->zBuf, (int)p->iOffset)); assert(pData->pBaseRead->pMethods==0 && pData->pBaseWrite->pMethods==0); rc = sqlite3OsOpen(pVfs, pData->zName, pData->pBaseRead, flags, 0); assert( holdingMutex==0 ); pthread_mutex_lock(&async.queueMutex); holdingMutex = 1; break; } default: assert(!"Illegal value for AsyncWrite.op"); } /* If we didn't hang on to the mutex during the IO op, obtain it now |
︙ | ︙ | |||
1071 1072 1073 1074 1075 1076 1077 | ** in order to give other threads a chance to work with the write queue. */ if( !async.pQueueFirst || !async.ioError ){ sqlite3ApiExit(0, 0); pthread_mutex_unlock(&async.queueMutex); holdingMutex = 0; if( async.ioDelay>0 ){ | | | 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 | ** in order to give other threads a chance to work with the write queue. */ if( !async.pQueueFirst || !async.ioError ){ sqlite3ApiExit(0, 0); pthread_mutex_unlock(&async.queueMutex); holdingMutex = 0; if( async.ioDelay>0 ){ sqlite3OsSleep(pVfs, async.ioDelay); }else{ sched_yield(); } } } pthread_mutex_unlock(&async.writerMutex); |
︙ | ︙ | |||
1108 1109 1110 1111 1112 1113 1114 | Tcl_Obj *CONST objv[] ){ if( objc!=1 && objc!=2 ){ Tcl_WrongNumArgs(interp, 1, objv, "?YES/NO?"); return TCL_ERROR; } if( objc==1 ){ | | | 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 | Tcl_Obj *CONST objv[] ){ if( objc!=1 && objc!=2 ){ Tcl_WrongNumArgs(interp, 1, objv, "?YES/NO?"); return TCL_ERROR; } if( objc==1 ){ Tcl_SetObjResult(interp, Tcl_NewBooleanObj(async_vfs.pAppData!=0)); }else{ int en; if( Tcl_GetBooleanFromObj(interp, objv[1], &en) ) return TCL_ERROR; asyncEnable(en); } return TCL_OK; } |
︙ | ︙ |
Changes to test/async2.test.
1 2 3 4 5 6 7 | # # 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. # #*********************************************************************** # | | > > > > | > > > > < < < | 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 | # # 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. # #*********************************************************************** # # $Id: async2.test,v 1.4 2007/08/25 12:29:30 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl if { [info commands sqlite3async_enable]=="" || [info command sqlite_malloc_stat]=="" } { # The async logic is not built into this system puts "Skipping async2 tests: not compiled with required features" finish_test return } # Enable asynchronous IO. set setup_script { CREATE TABLE counter(c); INSERT INTO counter(c) VALUES (1); } |
︙ | ︙ |