Esempio n. 1
0
bool
cThreadLockRw::CheckLock()
{
  bool rv = TryWriteLock();
  
  if ( rv )
       WriteUnlock();

  return rv;
}
    /*!
     * Acquire the lock for exclusive "write" access.  A "writer" thread has
     * exclusive ownership of the lock, not shared by any other "reader" or
     * "writer" threads.
     */
    void WriteLock()
    {
        if (TryWriteLock())
            return;

        ATOMIC::EXPONENTIAL_BACKOFF<> backoff;
        STATE_PAIR statePair;
        bool isWaiting = FALSE; //TRUE, if this writer is on the waiting list
        while (TRUE) 
        {
            backoff.Delay();
            LoadCurrentState(&statePair);
            if (!isWaiting)
            {
                // Try to acquire the lock right away - without waiting list
                //
                if (!statePair.ProposeWriteState())
                {
                    // Lock is busy, try to put this writer on the waiting list
                    //
                    statePair.ProposeWaitState();
                    isWaiting = SetState(&statePair);
                    continue;
                }
            }
            else if (!statePair.ProposeWriteAfterWaitState())
            {
                // Lock is still busy - leave this writer on the waiting list and Yield
                //
                OS::Yield();
                backoff.Reset(); //Reset backoff delay after Yield
                continue;
            } 
            if (SetLockState(&statePair))
                return;
        }
    }