/ Check-in [084f3fff]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a comment on the implementation of sqlite3OsLock(). No code changes. (CVS 1688)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:084f3fffbdeb1e2b375817ae1111b867c9d32559
User & Date: danielk1977 2004-06-25 07:21:28
Context
2004-06-25
08:32
A few more comment changes. No code alterations. (CVS 1689) check-in: b68b4a42 user: danielk1977 tags: trunk
07:21
Add a comment on the implementation of sqlite3OsLock(). No code changes. (CVS 1688) check-in: 084f3fff user: danielk1977 tags: trunk
06:23
Activate test cases for new journal format. (CVS 1687) check-in: 197d00d6 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

   692    692   **    RESERVED -> (PENDING) -> EXCLUSIVE
   693    693   **    PENDING -> EXCLUSIVE
   694    694   **
   695    695   ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   696    696   ** routine to lower a locking level.
   697    697   */
   698    698   int sqlite3OsLock(OsFile *id, int locktype){
          699  +  /* The following describes the implementation of the various locks and
          700  +  ** lock transitions in terms of the POSIX advisory shared and exclusive
          701  +  ** lock primitives (called read-locks and write-locks below, to avoid
          702  +  ** confusion with SQLite lock names). The algorithms are complicated
          703  +  ** slightly in order to be compatible with windows systems simultaneously
          704  +  ** accessing the same database file, in case that is ever required.
          705  +  **
          706  +  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
          707  +  ** byte', each single bytes at well known offsets, and the 'shared byte
          708  +  ** range', a range of 510 bytes at a well known offset.
          709  +  **
          710  +  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
          711  +  ** byte'.  If this is successful, a random byte from the 'shared byte
          712  +  ** range' is read-locked and the lock on the 'pending byte' released.
          713  +  **
          714  +  ** A process may only obtain a RESERVED lock after it has a SHARED lock
          715  +  ** (the sqlite3OsLock() routine will try to obtain this lock
          716  +  ** automatically if it is not already held). A RESERVED lock is
          717  +  ** implemented by grabbing a write-lock on the 'reserved byte'. 
          718  +  **
          719  +  ** A process may only obtain a PENDING lock after it has obtained a
          720  +  ** SHARED lock (done automatically by sqlite3OsLock()). A PENDING lock is
          721  +  ** implemented by obtaining a write-lock on the 'pending byte'. This
          722  +  ** ensures that no new SHARED locks can be obtained, but existing SHARED
          723  +  ** locks are allowed to persist. A process does not have to obtain a
          724  +  ** RESERVED lock on the way to a PENDING lock. This property is used by
          725  +  ** the algorithm for rolling back a journal file after a crash.
          726  +  **
          727  +  ** An EXCLUSIVE lock is implemented by obtaining a write-lock on the
          728  +  ** entire 'shared byte range'. Since all other locks require a read-lock
          729  +  ** on one of the bytes within this range, this ensures that no other
          730  +  ** locks are held on the database. 
          731  +  **
          732  +  ** The reason a single byte cannot be used instead of the 'shared byte
          733  +  ** range' is that some versions of windows do not support read-locks. By
          734  +  ** locking a random byte from a range, concurrent SHARED locks may exist
          735  +  ** even if the locking primitive used is always a write-lock.
          736  +  */
   699    737     int rc = SQLITE_OK;
   700    738     struct lockInfo *pLock = id->pLock;
   701    739     struct flock lock;
   702    740     int s;
   703    741   
   704    742     assert( id->isOpen );
   705    743     TRACE6("LOCK %d %d was %d(%d,%d)\n",

Changes to src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.139 2004/06/25 06:23:23 danielk1977 Exp $
           21  +** @(#) $Id: pager.c,v 1.140 2004/06/25 07:21:28 danielk1977 Exp $
    22     22   */
    23     23   #include "os.h"         /* Must be first to enable large file support */
    24     24   #include "sqliteInt.h"
    25     25   #include "pager.h"
    26     26   #include <assert.h>
    27     27   #include <string.h>
    28     28   
................................................................................
   471    471   ** The format for the journal header is as follows:
   472    472   ** - 8 bytes: Magic identifying journal format.
   473    473   ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
   474    474   ** - 4 bytes: Random number used for page hash.
   475    475   ** - 4 bytes: Initial database page count.
   476    476   ** - 4 bytes: Sector size used by the process that wrote this journal.
   477    477   ** 
   478         -** Followed by (JOURNAL_HDR_SZ - 20) bytes of unused space.
          478  +** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
   479    479   */
   480    480   static int writeJournalHdr(Pager *pPager){
   481    481   
   482    482     int rc = seekJournalHdr(pPager);
   483    483     if( rc ) return rc;
   484    484   
   485    485     pPager->journalHdr = pPager->journalOff;