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

📄 lock.cpp

📁 一个通讯管理机的源代码。比较好用。推荐
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   {      if(LRecNo) /* record lock */      {         if(LockType != F_UNLCK)         {            CurLockedRecNo = LRecNo;            CurRecLockType = LockType;            CurRecLockCount++;         }         else if(!CurRecLockCount)         {            CurLockedRecNo = 0;            CurRecLockType = -1;         }      }      else      {         if(LockType != F_UNLCK)         {            CurLockType = LockType;            CurLockCount++;         }         else if (!CurLockCount)            CurLockType = -1;      }      return XB_NO_ERROR;   }#endif} /************************************************************************///! Short Description/*!  \param WaitOption  \param LockType*/#ifdef XB_INDEX_ANYxbShort xbIndex::LockIndex( const xbShort WaitOption,             const xbShort LockType ){   /*  This method locks the first 512 bytes of the index file,       effectively locking the file from other processes that are       using the locking protocols      if WaitOption = F_SETLK  - return immediately                       F_SETLKW - wait until lock function executes      if LockType   = F_RDLCK - do a read / shared lock                      F_WRLCK - do a write / exclusive lock                      F_UNLCK - do an unlock      if locking routines fail, look in errno or do perror() for explanation   */   //fprintf(stderr, "LockIndex\n");   /*   **  Support nested index locking.   */   if(CurLockCount)   {     if(LockType != F_UNLCK)     {        /*        **  Allow changing a read lock into a write lock, but        **  if a write lock is already held or the current lock        **  type is the same as the new lock type just say success.        */        if(CurLockType == F_WRLCK || CurLockType == LockType)        {           CurLockCount++;           return XB_NO_ERROR;        }     }     else     {        /*        **  This is an unlock        */        CurLockCount--;                /*        **  If there are still outstanding locks, just indicate success.        */        if(CurLockCount)           return XB_NO_ERROR;     }   }        #ifdef HAVE_FCNTL   struct flock fl;   fl.l_type = LockType;   fl.l_whence = SEEK_SET;   fl.l_start = 0L;//   fl.l_len = XB_NDX_NODE_SIZE;   fl.l_len = 1;   if( fcntl( fileno( indexfp ), WaitOption, &fl ) == -1 )       xb_error(XB_LOCK_FAILED)   else   {      if(LockType != F_UNLCK)      {         CurLockType = LockType;         CurLockCount++;      }      else if(!CurLockCount)         CurLockType = 0;      return XB_NO_ERROR;   }#else   if( fseek( indexfp, 0L, SEEK_SET ) != 0 )     return XB_SEEK_ERROR;   if( locking( fileno( indexfp ),    dbf->UnixToDosLockCommand( WaitOption, LockType ),XB_NDX_NODE_SIZE ) != 0 )       xb_error(XB_LOCK_FAILED)   else   {      if(LockType != F_UNLCK)      {         CurLockType = LockType;         CurLockCount++;      }      else if(!CurLockCount)         CurLockType = 0;      return XB_NO_ERROR;   }#endif}#endif   /* XB_INDEX_ANY  *//************************************************************************///! Short description/*!  \param WaitOption  \param LockType*/#ifdef XB_MEMO_FIELDS xbShort xbDbf::LockMemoFile( const xbShort WaitOption, const xbShort LockType ){   /*  This method locks the first 4 bytes of the memo file,       effectively locking the file from other processes that are       using the locking protocols       The first four bytes point to the free block chain      if WaitOption = F_SETLK  - return immediately                       F_SETLKW - wait until lock function executes      if LockType   = F_RDLCK - do a read / shared lock                      F_WRLCK - do a write / exclusive lock                      F_UNLCK - do an unlock      if locking routines fail, look in errno or do perror() for explanation   */   /*   **  Support nested index locking.   */   if(CurMemoLockCount)   {     if(LockType != F_UNLCK)     {        /*        **  Allow changing a read lock into a write lock, but        **  if a write lock is already held or the current lock        **  type is the same as the new lock type just say success.        */        if(CurMemoLockType == F_WRLCK || CurMemoLockType == LockType)        {           CurMemoLockCount++;           return XB_NO_ERROR;        }     }     else     {        /*        **  This is an unlock        */        CurMemoLockCount--;                /*        **  If there are still outstanding locks, just indicate success.        */        if(CurMemoLockCount)           return XB_NO_ERROR;     }   }        #ifdef HAVE_FCNTL   struct flock fl;   fl.l_type   = LockType;   fl.l_whence = SEEK_SET;   fl.l_start  = 0L;   fl.l_len    = 4L;   if( fcntl( fileno( mfp ), WaitOption, &fl ) == -1 )       xb_error(XB_LOCK_FAILED)   else   {      if(LockType != F_UNLCK)      {         CurMemoLockType = LockType;         CurMemoLockCount++;      }      else if(!CurMemoLockCount)         CurMemoLockType = 0;      return XB_NO_ERROR;   }#else   if( fseek( mfp , 0L, SEEK_SET ) != 0 )       xb_error(XB_SEEK_ERROR);   if( locking( fileno( mfp ),    UnixToDosLockCommand( WaitOption, LockType ), 4L ) != 0 )       xb_error(XB_LOCK_FAILED)   else   {      if(LockType != F_UNLCK)      {         CurMemoLockType = LockType;         CurMemoLockCount++;      }      else if(!CurMemoLockCount)         CurMemoLockType = 0;      return XB_NO_ERROR;   }#endif}#endif   /* XB_MEMO_FIELDS  *//***********************************************************************///! Short description/*!  \param LockWaitOption*/xbShort xbDbf::ExclusiveLock( const xbShort LockWaitOption ){   /* this routine locks all files and indexes for a database file */   /* if it fails, no locks are left on (theoretically)            */   xbIxList *i;   xbShort rc;   AutoLockOff();   if(( rc = LockDatabase( LockWaitOption, F_WRLCK, 0 )) != XB_NO_ERROR )      return rc;#ifdef XB_MEMO_FIELDS   if( MemoFieldsPresent())      if(( rc = LockMemoFile( LockWaitOption, F_WRLCK )) != XB_NO_ERROR )        return rc;#endif#ifdef XB_INDEX_ANY   i = NdxList;   while( i )    {#ifdef HAVE_EXCEPTIONS       try {#endif      if(( rc = i->index->LockIndex( LockWaitOption, F_WRLCK )) != XB_NO_ERROR )      {         ExclusiveUnlock();#ifndef HAVE_EXCEPTIONS             return rc;#endif      }#ifdef HAVE_EXCEPTIONS       } catch (xbException &x) {          ExclusiveUnlock();          xb_error(XB_LOCK_FAILED);       }#endif      i = i->NextIx;   }#endif   return XB_NO_ERROR;}  /***********************************************************************///! Short description/*!*/xbShort xbDbf::ExclusiveUnlock( void ){   /* this routine unlocks all files and indexes for a database file */   xbIxList *i;   LockDatabase( F_SETLK, F_UNLCK, 0 );#ifdef XB_MEMO_FIELDS   if( MemoFieldsPresent())      LockMemoFile( F_SETLK, F_UNLCK );#endif#ifdef XB_INDEX_ANY   i = NdxList;   while( i )    {      i->index->LockIndex( F_SETLK, F_UNLCK );      i = i->NextIx;   }#endif   AutoLockOn();   return XB_NO_ERROR;}  /***********************************************************************/#endif  /* XB_LOCKING_ON */

⌨️ 快捷键说明

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