Q_UINT32 finalTicks;
UInt32 finalTicks;
int sqliteOsReadLock(OsFile *id){#if OS_UNIX  int rc;  sqliteOsEnterMutex();  if( id->pLock->cnt>0 ){    if( !id->locked ){      id->pLock->cnt++;      id->locked = 1;      id->pOpen->nLock++;    }    rc = SQLITE_OK;  }else if( id->locked || id->pLock->cnt==0 ){    struct flock lock;    int s;    lock.l_type = F_RDLCK;    lock.l_whence = SEEK_SET;    lock.l_start = lock.l_len = 0L;    s = fcntl(id->fd, F_SETLK, &lock);    if( s!=0 ){      rc = (errno==EINVAL) ? SQLITE_NOLFS : SQLITE_BUSY;    }else{      rc = SQLITE_OK;      if( !id->locked ){        id->pOpen->nLock++;        id->locked = 1;      }      id->pLock->cnt = 1;    }  }else{    rc = SQLITE_BUSY;  }  sqliteOsLeaveMutex();  return rc;#endif#if OS_WIN  int rc;  if( id->locked>0 ){    rc = SQLITE_OK;  }else{    int lk;    int res;    int cnt = 100;    sqliteRandomness(sizeof(lk), &lk);    lk = (lk & 0x7fffffff)%N_LOCKBYTE + 1;    while( cnt-->0 && (res = LockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0))==0 ){      Sleep(1);    }    if( res ){      UnlockFile(id->h, FIRST_LOCKBYTE+1, 0, N_LOCKBYTE, 0);      if( isNT() ){        OVERLAPPED ovlp;        ovlp.Offset = FIRST_LOCKBYTE+1;        ovlp.OffsetHigh = 0;        ovlp.hEvent = 0;        res = LockFileEx(id->h, LOCKFILE_FAIL_IMMEDIATELY,                           0, N_LOCKBYTE, 0, &ovlp);      }else{        res = LockFile(id->h, FIRST_LOCKBYTE+lk, 0, 1, 0);      }      UnlockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0);    }    if( res ){      id->locked = lk;      rc = SQLITE_OK;    }else{      rc = SQLITE_BUSY;    }  }  return rc;#endif#if OS_MAC  int rc;  if( id->locked>0 || id->refNumRF == -1 ){    rc = SQLITE_OK;  }else{    int lk;    OSErr res;    int cnt = 5;    ParamBlockRec params;    sqliteRandomness(sizeof(lk), &lk);    lk = (lk & 0x7fffffff)%N_LOCKBYTE + 1;    memset(&params, 0, sizeof(params));    params.ioParam.ioRefNum = id->refNumRF;    params.ioParam.ioPosMode = fsFromStart;    params.ioParam.ioPosOffset = FIRST_LOCKBYTE;    params.ioParam.ioReqCount = 1;    while( cnt-->0 && (res = PBLockRangeSync(&params))!=noErr ){      Q_UINT32 finalTicks;      Delay(1, &finalTicks); /* 1/60 sec */    }    if( res == noErr ){      params.ioParam.ioPosOffset = FIRST_LOCKBYTE+1;      params.ioParam.ioReqCount = N_LOCKBYTE;      PBUnlockRangeSync(&params);      params.ioParam.ioPosOffset = FIRST_LOCKBYTE+lk;      params.ioParam.ioReqCount = 1;      res = PBLockRangeSync(&params);      params.ioParam.ioPosOffset = FIRST_LOCKBYTE;      params.ioParam.ioReqCount = 1;      PBUnlockRangeSync(&params);    }    if( res == noErr ){      id->locked = lk;      rc = SQLITE_OK;    }else{      rc = SQLITE_BUSY;    }  }  return rc;#endif}