Exemplo n.º 1
0
        // Writers block both readers and writers - wait for all other readers and writers to finish
        public QuietWriterLock(RWLockInfo info, bool?active = null)
        {
            _info = info;

            if (active.GetValueOrDefault(Globals.UseReaderWriterLocks))
            {
                try
                {
                    Thread.BeginCriticalRegion();

                    if (_info.Lock.TryEnterWriteLock(0))
                    {
                        Interlocked.Increment(ref _active);

#if LOCK_TRACE
                        _info.LastWriter = Environment.CurrentManagedThreadId;
#endif
                    }
                }
                finally
                {
                    Thread.EndCriticalRegion();
                }
            }
        }
Exemplo n.º 2
0
        public ReaderLock(RWLockInfo info, bool?active = null)
        {
            _info = info;

            if (active.GetValueOrDefault(Globals.UseReaderWriterLocks && (!Globals.AllowDirtyReads || !info.AllowDirtyReads)))
            {
                try
                {
#if LOCK_TRACE
                    RWLockInfo.Waits++;
                    long start = DateTime.Now.Ticks;
#endif
                    Thread.BeginCriticalRegion();

                    if (!_info.Lock.TryEnterReadLock(_info.Timeout))
                    {
#if LOCK_TRACE
                        RWLockInfo.WaitDuration += DateTime.Now.Ticks - start;
#endif
                        throw new TimeoutException("Failed to obtain a read lock in timeout interval.");
                    }

                    Interlocked.Increment(ref _active);

#if LOCK_TRACE
                    _info.LastReader         = Environment.CurrentManagedThreadId;
                    RWLockInfo.WaitDuration += DateTime.Now.Ticks - start;
#endif
                }
                finally
                {
                    Thread.EndCriticalRegion();
                }
            }
        }
Exemplo n.º 3
0
 // Writers block both readers and writers - wait for all other readers and writers to finish
 public WriterLock(RWLockInfo info, bool?active = null)
 {
     if (info != null)
     {
         _info = info;
         Reacquire(active);
     }
 }