⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_unix.c

📁 最新的sqlite3.6.2源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
**    PENDING -> EXCLUSIVE**** This routine will only increase a lock.  Use the sqlite3OsUnlock()** routine to lower a locking level.*/static int unixLock(sqlite3_file *id, int locktype){  /* The following describes the implementation of the various locks and  ** lock transitions in terms of the POSIX advisory shared and exclusive  ** lock primitives (called read-locks and write-locks below, to avoid  ** confusion with SQLite lock names). The algorithms are complicated  ** slightly in order to be compatible with windows systems simultaneously  ** accessing the same database file, in case that is ever required.  **  ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved  ** byte', each single bytes at well known offsets, and the 'shared byte  ** range', a range of 510 bytes at a well known offset.  **  ** To obtain a SHARED lock, a read-lock is obtained on the 'pending  ** byte'.  If this is successful, a random byte from the 'shared byte  ** range' is read-locked and the lock on the 'pending byte' released.  **  ** A process may only obtain a RESERVED lock after it has a SHARED lock.  ** A RESERVED lock is implemented by grabbing a write-lock on the  ** 'reserved byte'.   **  ** A process may only obtain a PENDING lock after it has obtained a  ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock  ** on the 'pending byte'. This ensures that no new SHARED locks can be  ** obtained, but existing SHARED locks are allowed to persist. A process  ** does not have to obtain a RESERVED lock on the way to a PENDING lock.  ** This property is used by the algorithm for rolling back a journal file  ** after a crash.  **  ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is  ** implemented by obtaining a write-lock on the entire 'shared byte  ** range'. Since all other locks require a read-lock on one of the bytes  ** within this range, this ensures that no other locks are held on the  ** database.   **  ** The reason a single byte cannot be used instead of the 'shared byte  ** range' is that some versions of windows do not support read-locks. By  ** locking a random byte from a range, concurrent SHARED locks may exist  ** even if the locking primitive used is always a write-lock.  */  int rc = SQLITE_OK;  unixFile *pFile = (unixFile*)id;  struct lockInfo *pLock = pFile->pLock;  struct flock lock;  int s;  assert( pFile );  OSTRACE7("LOCK    %d %s was %s(%s,%d) pid=%d\n", pFile->h,      locktypeName(locktype), locktypeName(pFile->locktype),      locktypeName(pLock->locktype), pLock->cnt , getpid());  /* If there is already a lock of this type or more restrictive on the  ** unixFile, do nothing. Don't use the end_lock: exit path, as  ** enterMutex() hasn't been called yet.  */  if( pFile->locktype>=locktype ){    OSTRACE3("LOCK    %d %s ok (already held)\n", pFile->h,            locktypeName(locktype));    return SQLITE_OK;  }  /* Make sure the locking sequence is correct  */  assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );  assert( locktype!=PENDING_LOCK );  assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );  /* This mutex is needed because pFile->pLock is shared across threads  */  enterMutex();  /* Make sure the current thread owns the pFile.  */  rc = transferOwnership(pFile);  if( rc!=SQLITE_OK ){    leaveMutex();    return rc;  }  pLock = pFile->pLock;  /* If some thread using this PID has a lock via a different unixFile*  ** handle that precludes the requested lock, return BUSY.  */  if( (pFile->locktype!=pLock->locktype &&           (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))  ){    rc = SQLITE_BUSY;    goto end_lock;  }  /* If a SHARED lock is requested, and some thread using this PID already  ** has a SHARED or RESERVED lock, then increment reference counts and  ** return SQLITE_OK.  */  if( locktype==SHARED_LOCK &&       (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){    assert( locktype==SHARED_LOCK );    assert( pFile->locktype==0 );    assert( pLock->cnt>0 );    pFile->locktype = SHARED_LOCK;    pLock->cnt++;    pFile->pOpen->nLock++;    goto end_lock;  }  lock.l_len = 1L;  lock.l_whence = SEEK_SET;  /* A PENDING lock is needed before acquiring a SHARED lock and before  ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will  ** be released.  */  if( locktype==SHARED_LOCK       || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)  ){    lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);    lock.l_start = PENDING_BYTE;    s = fcntl(pFile->h, F_SETLK, &lock);    if( s==(-1) ){      int tErrno = errno;      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);      if( IS_LOCK_ERROR(rc) ){        pFile->lastErrno = tErrno;      }      goto end_lock;    }  }  /* If control gets to this point, then actually go ahead and make  ** operating system calls for the specified lock.  */  if( locktype==SHARED_LOCK ){    int tErrno = 0;    assert( pLock->cnt==0 );    assert( pLock->locktype==0 );    /* Now get the read-lock */    lock.l_start = SHARED_FIRST;    lock.l_len = SHARED_SIZE;    if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){      tErrno = errno;    }    /* Drop the temporary PENDING lock */    lock.l_start = PENDING_BYTE;    lock.l_len = 1L;    lock.l_type = F_UNLCK;    if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){      if( s != -1 ){        /* This could happen with a network mount */        tErrno = errno;         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);         if( IS_LOCK_ERROR(rc) ){          pFile->lastErrno = tErrno;        }        goto end_lock;      }    }    if( s==(-1) ){      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);      if( IS_LOCK_ERROR(rc) ){        pFile->lastErrno = tErrno;      }    }else{      pFile->locktype = SHARED_LOCK;      pFile->pOpen->nLock++;      pLock->cnt = 1;    }  }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){    /* We are trying for an exclusive lock but another thread in this    ** same process is still holding a shared lock. */    rc = SQLITE_BUSY;  }else{    /* The request was for a RESERVED or EXCLUSIVE lock.  It is    ** assumed that there is a SHARED or greater lock on the file    ** already.    */    assert( 0!=pFile->locktype );    lock.l_type = F_WRLCK;    switch( locktype ){      case RESERVED_LOCK:        lock.l_start = RESERVED_BYTE;        break;      case EXCLUSIVE_LOCK:        lock.l_start = SHARED_FIRST;        lock.l_len = SHARED_SIZE;        break;      default:        assert(0);    }    s = fcntl(pFile->h, F_SETLK, &lock);    if( s==(-1) ){      int tErrno = errno;      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);      if( IS_LOCK_ERROR(rc) ){        pFile->lastErrno = tErrno;      }    }  }    if( rc==SQLITE_OK ){    pFile->locktype = locktype;    pLock->locktype = locktype;  }else if( locktype==EXCLUSIVE_LOCK ){    pFile->locktype = PENDING_LOCK;    pLock->locktype = PENDING_LOCK;  }end_lock:  leaveMutex();  OSTRACE4("LOCK    %d %s %s\n", pFile->h, locktypeName(locktype),       rc==SQLITE_OK ? "ok" : "failed");  return rc;}/*** Lower the locking level on file descriptor pFile to locktype.  locktype** must be either NO_LOCK or SHARED_LOCK.**** If the locking level of the file descriptor is already at or below** the requested locking level, this routine is a no-op.*/static int unixUnlock(sqlite3_file *id, int locktype){  struct lockInfo *pLock;  struct flock lock;  int rc = SQLITE_OK;  unixFile *pFile = (unixFile*)id;  int h;  assert( pFile );  OSTRACE7("UNLOCK  %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,      pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());  assert( locktype<=SHARED_LOCK );  if( pFile->locktype<=locktype ){    return SQLITE_OK;  }  if( CHECK_THREADID(pFile) ){    return SQLITE_MISUSE;  }  enterMutex();  h = pFile->h;  pLock = pFile->pLock;  assert( pLock->cnt!=0 );  if( pFile->locktype>SHARED_LOCK ){    assert( pLock->locktype==pFile->locktype );    SimulateIOErrorBenign(1);    SimulateIOError( h=(-1) )    SimulateIOErrorBenign(0);    if( locktype==SHARED_LOCK ){      lock.l_type = F_RDLCK;      lock.l_whence = SEEK_SET;      lock.l_start = SHARED_FIRST;      lock.l_len = SHARED_SIZE;      if( fcntl(h, F_SETLK, &lock)==(-1) ){        int tErrno = errno;        rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);        if( IS_LOCK_ERROR(rc) ){          pFile->lastErrno = tErrno;        }				goto end_unlock;      }    }    lock.l_type = F_UNLCK;    lock.l_whence = SEEK_SET;    lock.l_start = PENDING_BYTE;    lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );    if( fcntl(h, F_SETLK, &lock)!=(-1) ){      pLock->locktype = SHARED_LOCK;    }else{      int tErrno = errno;      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);      if( IS_LOCK_ERROR(rc) ){        pFile->lastErrno = tErrno;      }			goto end_unlock;    }  }  if( locktype==NO_LOCK ){    struct openCnt *pOpen;    /* Decrement the shared lock counter.  Release the lock using an    ** OS call only when all threads in this same process have released    ** the lock.    */    pLock->cnt--;    if( pLock->cnt==0 ){      lock.l_type = F_UNLCK;      lock.l_whence = SEEK_SET;      lock.l_start = lock.l_len = 0L;      SimulateIOErrorBenign(1);      SimulateIOError( h=(-1) )      SimulateIOErrorBenign(0);      if( fcntl(h, F_SETLK, &lock)!=(-1) ){        pLock->locktype = NO_LOCK;      }else{        int tErrno = errno;				rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);        if( IS_LOCK_ERROR(rc) ){          pFile->lastErrno = tErrno;        }        pLock->cnt = 1;				goto end_unlock;      }    }    /* Decrement the count of locks against this same file.  When the    ** count reaches zero, close any other file descriptors whose close    ** was deferred because of outstanding locks.    */    if( rc==SQLITE_OK ){      pOpen = pFile->pOpen;      pOpen->nLock--;      assert( pOpen->nLock>=0 );      if( pOpen->nLock==0 && pOpen->nPending>0 ){        int i;        for(i=0; i<pOpen->nPending; i++){          close(pOpen->aPending[i]);        }        sqlite3_free(pOpen->aPending);        pOpen->nPending = 0;        pOpen->aPending = 0;      }    }  }	end_unlock:  leaveMutex();  if( rc==SQLITE_OK ) pFile->locktype = locktype;  return rc;}/*** This function performs the parts of the "close file" operation ** common to all locking schemes. It closes the directory and file** handles, if they are valid, and sets all fields of the unixFile** structure to 0.*/static int closeUnixFile(sqlite3_file *id){  unixFile *pFile = (unixFile*)id;  if( pFile ){    if( pFile->dirfd>=0 ){      close(pFile->dirfd);    }    if( pFile->h>=0 ){      close(pFile->h);    }    OSTRACE2("CLOSE   %-3d\n", pFile->h);    OpenCounter(-1);    memset(pFile, 0, sizeof(unixFile));  }  return SQLITE_OK;}/*** Close a file.*/static int unixClose(sqlite3_file *id){  if( id ){    unixFile *pFile = (unixFile *)id;    unixUnlock(id, NO_LOCK);    enterMutex();    if( pFile->pOpen && pFile->pOpen->nLock ){      /* If there are outstanding locks, do not actually close the file just      ** yet because that would clear those locks.  Instead, add the file      ** descriptor to pOpen->aPending.  It will be automatically closed when      ** the last lock is cleared.      */      int *aNew;      struct openCnt *pOpen = pFile->pOpen;      aNew = sqlite3_realloc(pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );      if( aNew==0 ){        /* If a malloc fails, just leak the file descriptor */      }else{        pOpen->aPending = aNew;        pOpen->aPending[pOpen->nPending] = pFile->h;        pOpen->nPending++;        pFile->h = -1;      }    }    releaseLockInfo(pFile->pLock);    releaseOpenCnt(pFile->pOpen);    closeUnixFile(id);    leaveMutex();  }  return SQLITE_OK;}#ifdef SQLITE_ENABLE_LOCKING_STYLE#pragma mark AFP Support/* ** The afpLockingContext structure contains all afp lock specific state */typedef struct afpLockingContext afpLockingContext;struct afpLockingContext {  unsigned long long sharedLockByte;  const char *filePath;};struct ByteRangeLockPB2{  unsigned long long offset;        /* offset to first byte to lock */  unsigned long long length;        /* nbr of bytes to lock */  unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */  unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */  unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */  int fd;                           /* file desc to assoc this lock with */};#define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)/*  ** Return SQLITE_OK on success, SQLITE_BUSY on failure. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -