Source/NFX/OS/ManyReadersOneWriterSynchronizer.cs
/// <summary>
/// Obtains a read lock returning true on success.
/// Many threads may obtain a read lock at the same time, however a single write lock excludes any read locks,
/// corollary any single read lock held excludes anyone obtaining a write lock.
/// False is returned if cancel func was supplied and returned true to cancel-out the waiting
/// </summary>
Obtains a read lock returning true on success.
Many threads may obtain a read lock at the same time, however a single write lock excludes any read locks,
corollary any single read lock held excludes anyone obtaining a write lock.
False is returned if cancel func was supplied and returned true to cancel-out the waiting
Obtains a read lock returning true on success . Many threads may obtain a read lock at the same time however a single write lock excludes any read locks corollary any single read lock held excludes anyone obtaining a write lock . False is returned if cancel func was supplied and returned true to cancel - out the waiting
public bool GetReadLock(Func<object, bool> cancel = null, object cancelContext = null)
      {
          int pauseCount = 0;
          while(true)
          {
            long current = Interlocked.Increment(ref m_Readers);
            if (current>0) return true;
            if (current==0)
             throw new NFXException(StringConsts.INVALID_OPERATION_ERROR+GetType().FullName+".GetReadLock() overflow failure");
            if (current < READ_TIGHT_THRESHOLD)
            {
                if (pauseCount<PAUSE_COUNT_PER_BATCH)
                {
                     var tightWait = MULTI_CPU && Thread.VolatileRead(ref m_WaitingWriters) < CPU_COUNT;
                     if (tightWait)
                       tightOnePause(pauseCount);
                     else
                       Thread.Sleep(1);
                     pauseCount++;
                     continue;
                }
                pauseCount = 0;
            }
            if (cancel!=null && cancel(cancelContext)==true) return false;
            Thread.Sleep(MIN_SLEEP_MS + (Thread.CurrentThread.GetHashCode() & 0xf));
          }
      }
public bool GetReadLock ( Func < object , bool > cancel = null , object cancelContext = null ) { int pauseCount = 0 ; while ( true ) { long current = Interlocked . Increment ( ref m_Readers ) ; if ( current > 0 ) return true ; if ( current == 0 ) throw new NFXException ( StringConsts . INVALID_OPERATION_ERROR + GetType ( ) . FullName + " " ) ; if ( current < READ_TIGHT_THRESHOLD ) { if ( pauseCount < PAUSE_COUNT_PER_BATCH ) { var tightWait = MULTI_CPU && Thread . VolatileRead ( ref m_WaitingWriters ) < CPU_COUNT ; if ( tightWait ) tightOnePause ( pauseCount ) ; else Thread . Sleep ( 1 ) ; pauseCount ++ ; continue ; } pauseCount = 0 ; } if ( cancel != null && cancel ( cancelContext ) == true ) return false ; Thread . Sleep ( MIN_SLEEP_MS + ( Thread . CurrentThread . GetHashCode ( ) & 0xf ) ) ; } }
Obtains a read lock returning true on success.
Obtains a read lock returning true on success .
//lock taken //Suppose there are 1000 real/physical threads all executing at the same time (which is not physically possible but is used as the worst case) //There are 200 5-ms intervals in a second (the worst case below MIN_SLEEP_MS=5), = 1000 threads * 200 intervals * 60 sec = 12,000,000 increments a minute //4B / 12M = 333 minutes = 5.5+ hrs //severe contention //lock failed //while
cancel
Func<object, bool>
cancelContext
object