/ Check-in [9622dd46]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Do not hold the shared-memory mutex in os_unix.c if returning NULL.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | wal-incr-ckpt
Files: files | file ages | folders
SHA1: 9622dd468c8914262e9d8bd8dbca9e22ec6ee75c
User & Date: dan 2010-06-01 11:08:57
Context
2010-06-01
12:58
Fix an off-by-one boundary-value issue in walCleanupHash(). check-in: f039552e user: drh tags: wal-incr-ckpt
11:08
Do not hold the shared-memory mutex in os_unix.c if returning NULL. check-in: 9622dd46 user: dan tags: wal-incr-ckpt
10:44
If the checkpoint fails to obtain an exclusive lock on one of the read-lock bytes, do not consider this an error. check-in: 9e95e357 user: dan tags: wal-incr-ckpt
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

  3402   3402     /* Remove connection p from the set of connections associated
  3403   3403     ** with pShmNode */
  3404   3404     sqlite3_mutex_enter(pShmNode->mutex);
  3405   3405     for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
  3406   3406     *pp = p->pNext;
  3407   3407   
  3408   3408     /* Free the connection p */
         3409  +  assert( p->hasMutexBuf==0 );
  3409   3410     sqlite3_free(p);
  3410   3411     pDbFd->pShm = 0;
  3411   3412     sqlite3_mutex_leave(pShmNode->mutex);
  3412   3413   
  3413   3414     /* If pShmNode->nRef has reached 0, then close the underlying
  3414   3415     ** shared-memory file, too */
  3415   3416     unixEnterMutex();
................................................................................
  3460   3461       }
  3461   3462       rc = ftruncate(pShmNode->h, reqSize);
  3462   3463       reqSize = -1;
  3463   3464     }
  3464   3465     return rc;
  3465   3466   }
  3466   3467   
         3468  +/*
         3469  +** Release the lock held on the shared memory segment to that other
         3470  +** threads are free to resize it if necessary.
         3471  +**
         3472  +** If the lock is not currently held, this routine is a harmless no-op.
         3473  +**
         3474  +** If the shared-memory object is in lock state RECOVER, then we do not
         3475  +** really want to release the lock, so in that case too, this routine
         3476  +** is a no-op.
         3477  +*/
         3478  +static int unixShmRelease(sqlite3_file *fd){
         3479  +  unixFile *pDbFd = (unixFile*)fd;
         3480  +  unixShm *p = pDbFd->pShm;
         3481  +
         3482  +  if( p->hasMutexBuf ){
         3483  +    assert( sqlite3_mutex_notheld(p->pShmNode->mutex) );
         3484  +    sqlite3_mutex_leave(p->pShmNode->mutexBuf);
         3485  +    p->hasMutexBuf = 0;
         3486  +  }
         3487  +  return SQLITE_OK;
         3488  +}
  3467   3489   
  3468   3490   /*
  3469   3491   ** Map the shared storage into memory. 
  3470   3492   **
  3471   3493   ** If reqMapSize is positive, then an attempt is made to make the
  3472   3494   ** mapping at least reqMapSize bytes in size.  However, the mapping
  3473   3495   ** will never be larger than the size of the underlying shared memory
................................................................................
  3533   3555         pShmNode->pMMapBuf = 0;
  3534   3556         pShmNode->szMap = 0;
  3535   3557       }
  3536   3558     }
  3537   3559     *pNewMapSize = pShmNode->szMap;
  3538   3560     *ppBuf = pShmNode->pMMapBuf;
  3539   3561     sqlite3_mutex_leave(pShmNode->mutex);
         3562  +  if( *ppBuf==0 ){
         3563  +    /* Do not hold the mutex if a NULL pointer is being returned. */
         3564  +    unixShmRelease(fd);
         3565  +  }
  3540   3566     return rc;
  3541   3567   }
  3542   3568   
  3543         -/*
  3544         -** Release the lock held on the shared memory segment to that other
  3545         -** threads are free to resize it if necessary.
  3546         -**
  3547         -** If the lock is not currently held, this routine is a harmless no-op.
  3548         -**
  3549         -** If the shared-memory object is in lock state RECOVER, then we do not
  3550         -** really want to release the lock, so in that case too, this routine
  3551         -** is a no-op.
  3552         -*/
  3553         -static int unixShmRelease(sqlite3_file *fd){
  3554         -  unixFile *pDbFd = (unixFile*)fd;
  3555         -  unixShm *p = pDbFd->pShm;
  3556         -
  3557         -  if( p->hasMutexBuf ){
  3558         -    assert( sqlite3_mutex_notheld(p->pShmNode->mutex) );
  3559         -    sqlite3_mutex_leave(p->pShmNode->mutexBuf);
  3560         -    p->hasMutexBuf = 0;
  3561         -  }
  3562         -  return SQLITE_OK;
  3563         -}
  3564         -
  3565   3569   
  3566   3570   /*
  3567   3571   ** Change the lock state for a shared-memory segment.
  3568   3572   **
  3569   3573   ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
  3570   3574   ** different here than in posix.  In xShmLock(), one can go from unlocked
  3571   3575   ** to shared and back or from unlocked to exclusive and back.  But one may