Q_UINT32 finalTicks;
UInt32 finalTicks;
int sqliteOsWriteLock(OsFile *id){#if OS_UNIX  int rc;  sqliteOsEnterMutex();  if( id->pLock->cnt==0 || (id->pLock->cnt==1 && id->locked==1) ){    struct flock lock;    int s;    lock.l_type = F_WRLCK;    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 res;    int cnt = 100;    while( cnt-->0 && (res = LockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0))==0 ){      Sleep(1);    }    if( res ){      if( id->locked>0 ){        if( isNT() ){          UnlockFile(id->h, FIRST_LOCKBYTE+1, 0, N_LOCKBYTE, 0);        }else{          res = UnlockFile(id->h, FIRST_LOCKBYTE + id->locked, 0, 1, 0);        }      }      if( res ){        res = LockFile(id->h, FIRST_LOCKBYTE+1, 0, N_LOCKBYTE, 0);      }else{        res = 0;      }      UnlockFile(id->h, FIRST_LOCKBYTE, 0, 1, 0);    }    if( res ){      id->locked = -1;      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{    OSErr res;    int cnt = 5;    ParamBlockRec params;    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 + id->locked;      params.ioParam.ioReqCount = 1;      if( id->locked==0             || PBUnlockRangeSync(&params)==noErr ){        params.ioParam.ioPosOffset = FIRST_LOCKBYTE+1;        params.ioParam.ioReqCount = N_LOCKBYTE;        res = PBLockRangeSync(&params);      }else{        res = afpRangeNotLocked;      }      params.ioParam.ioPosOffset = FIRST_LOCKBYTE;      params.ioParam.ioReqCount = 1;      PBUnlockRangeSync(&params);    }    if( res == noErr ){      id->locked = -1;      rc = SQLITE_OK;    }else{      rc = SQLITE_BUSY;    }  }  return rc;#endif}