System.Threading.ReaderWriterLockSlim.EnterReadLock()

Here are the examples of the csharp api System.Threading.ReaderWriterLockSlim.EnterReadLock() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

773 Examples 7

19 View Source File : NetManager.cs
License : MIT License
Project Creator : nievesj

public int GetPeersCount(ConnectionState peerState)
        {
            int count = 0;
            _peersLock.EnterReadLock();
            for (var netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer)
            {
                if ((netPeer.ConnectionState & peerState) != 0)
                    count++;
            }
            _peersLock.ExitReadLock();
            return count;
        }

19 View Source File : NetManager.cs
License : MIT License
Project Creator : nievesj

public void GetPeersNonAlloc(List<NetPeer> peers, ConnectionState peerState)
        {
            peers.Clear();
            _peersLock.EnterReadLock();
            for (var netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer)
            {
                if ((netPeer.ConnectionState & peerState) != 0)
                    peers.Add(netPeer);
            }
            _peersLock.ExitReadLock();
        }

19 View Source File : NetManager.cs
License : MIT License
Project Creator : nievesj

public void DisconnectAll(byte[] data, int start, int count)
        {
            //Send disconnect packets
            _peersLock.EnterReadLock();
            for (var netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer)
            {
                DisconnectPeer(
                    netPeer, 
                    DisconnectReason.DisconnectPeerCalled, 
                    0, 
                    false,
                    data, 
                    start, 
                    count,
                    null);
            }
            _peersLock.ExitReadLock();
        }

19 View Source File : ICURWLock.cs
License : Apache License 2.0
Project Creator : NightOwl888

public virtual void AcquireRead()
        {
            if (stats != null)
            {    // stats is null by default
                lock (syncLock)
                {
                    stats.ReadCount++;
                    if (rwl.CurrentReadCount > 0)
                    {
                        stats.MultipleReadCount++;
                    }
                    if (rwl.IsWriteLockHeld)
                    {
                        stats.WaitingReadCount++;
                    }
                }
            }
            rwl.EnterReadLock();
        }

19 View Source File : StatisticsCollector.cs
License : MIT License
Project Creator : notifo-io

public async Task AddAsync(Guid id, string channel, string configuration, ChannelSendInfo info)
        {
            readerWriterLock.EnterReadLock();
            try
            {
                updates[(id, channel, configuration)] = info;
            }
            finally
            {
                readerWriterLock.ExitReadLock();
            }

            if (updates.Count >= updatesCapacity)
            {
                await StoreAsync(default);
            }
        }

19 View Source File : CounterCollector.cs
License : MIT License
Project Creator : notifo-io

public ValueTask AddAsync(T group, CounterMap newCounters)
        {
            readerWriterLock.EnterReadLock();
            try
            {
                counters.AddOrUpdate(group, newCounters, (k, c) => c.IncrementWithLock(newCounters));
            }
            finally
            {
                readerWriterLock.ExitReadLock();
            }

            if (counters.Count >= countersCapacity)
            {
                timer.SkipCurrentDelay();
            }

            return default;
        }

19 View Source File : LogCollector.cs
License : MIT License
Project Creator : notifo-io

public async Task AddAsync(string appId, string message)
        {
            readerWriterLock.EnterReadLock();
            try
            {
                updates.AddOrUpdate((appId, message), 1, (_, value) => value + 1);
            }
            finally
            {
                readerWriterLock.ExitReadLock();
            }

            if (updates.Count >= updatesCapacity)
            {
                await StoreAsync(default);
            }
        }

19 View Source File : FirstChanceExceptionStatisticsTelemetryModule.cs
License : MIT License
Project Creator : novotnyllc

private static string GetDimCappedString(string dimensionValue, HashCache<string> hashCache, int cacheSize)
        {
            hashCache.RwLock.EnterReadLock();

            if (hashCache.ValueCache.Contains(dimensionValue) == true)
            {
                hashCache.RwLock.ExitReadLock();

                return dimensionValue;
            }

            if (hashCache.ValueCache.Count > cacheSize)
            {
                hashCache.RwLock.ExitReadLock();

                return null;
            }

            hashCache.RwLock.ExitReadLock();
            hashCache.RwLock.EnterWriteLock();

            hashCache.ValueCache.Add(dimensionValue);

            hashCache.RwLock.ExitWriteLock();

            return dimensionValue;
        }

19 View Source File : FirstChanceExceptionStatisticsTelemetryModule.cs
License : MIT License
Project Creator : novotnyllc

internal bool Contains(T value)
            {
                bool rc;

                this.RwLock.EnterReadLock();
                rc = this.ValueCache.Contains(value);
                this.RwLock.ExitReadLock();

                return rc;
            }

19 View Source File : ConfigHandler.cs
License : GNU General Public License v3.0
Project Creator : Nsiso

public void Read()
        {
            mainconfigLock.EnterReadLock();
            try
            {
                MainConfig = JsonConvert.DeserializeObject<MainConfig>(File.ReadAllText(MainConfigPath));
#if !DEBUG
                    if (string.IsNullOrWhiteSpace(MainConfig.ConfigVersion) || 
                        (!string.Equals(replacedembly.GetExecutingreplacedembly().GetName().Version.ToString(), MainConfig.ConfigVersion)))
                    {
                        MessageBox.Show("启动器配置文件版本不符。\n" +
                            "这可能是因为配置文件为旧版本启动器生成而导致的,继续使用可能导致bug出现,请重新生成(删除)原配置文件以保证平稳运行",
                            "启动器配置文件版本不符", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
#endif
            }
            catch (UnauthorizedAccessException e)
            {
                NoAccessWarning(e);
            }
            catch (System.Security.SecurityException e)
            {
                NoAccessWarning(e);
            }
            finally
            {
                mainconfigLock.ExitReadLock();
            }
        }

19 View Source File : InMemoryPartition.cs
License : MIT License
Project Creator : nstoredev

public async Task ReadForward(
            long fromLowerIndexInclusive,
            ISubscription subscription,
            long toUpperIndexInclusive,
            int limit,
            CancellationToken cancellationToken)
        {
            _lockSlim.EnterReadLock();

            var result = Chunks
                .Where(x => x.Index >= fromLowerIndexInclusive && x.Index <= toUpperIndexInclusive)
                .Take(limit)
                .ToArray();

            _lockSlim.ExitReadLock();
            await PushToSubscriber(fromLowerIndexInclusive, subscription, result, cancellationToken).ConfigureAwait(false);
        }

19 View Source File : InMemoryPartition.cs
License : MIT License
Project Creator : nstoredev

public Task ReadBackward(
            long fromUpperIndexInclusive,
            ISubscription subscription,
            long toLowerIndexInclusive,
            int limit,
            CancellationToken cancellationToken
        )
        {
            _lockSlim.EnterReadLock();

            var result = Chunks.Reverse()
                .Where(x => x.Index <= fromUpperIndexInclusive && x.Index >= toLowerIndexInclusive)
                .Take(limit)
                .ToArray();

            _lockSlim.ExitReadLock();

            return PushToSubscriber(fromUpperIndexInclusive, subscription, result, cancellationToken);
        }

19 View Source File : InMemoryPartition.cs
License : MIT License
Project Creator : nstoredev

public Task<IChunk> Peek(long maxValue, CancellationToken cancellationToken)
        {
            _lockSlim.EnterReadLock();

            var chunk = Chunks.Reverse()
                .Where(x => x.Index <= maxValue)
                .Take(1)
                .SingleOrDefault();

            _lockSlim.ExitReadLock();

            return Task.FromResult((IChunk)Clone(chunk));
        }

19 View Source File : InMemoryPartition.cs
License : MIT License
Project Creator : nstoredev

public MemoryChunk[] Delete(long fromIndex, long toIndex)
        {
            _lockSlim.EnterReadLock();
            var toDelete = Chunks.Where(x => x.Index >= fromIndex && x.Index <= toIndex).ToArray();
            _lockSlim.ExitReadLock();

            _lockSlim.EnterWriteLock();
            foreach (var chunk in toDelete)
            {
                this._sortedChunks.Remove(chunk.Index);
                this._operations.Remove(chunk.OperationId);
            }
            _lockSlim.ExitWriteLock();

            return toDelete;
        }

19 View Source File : InMemoryPartition.cs
License : MIT License
Project Creator : nstoredev

public Task<IChunk> GetByOperationId(string operationId)
        {
            _lockSlim.EnterReadLock();
            _operations.TryGetValue(operationId, out MemoryChunk chunk);
            _lockSlim.ExitReadLock();

            return Task.FromResult((IChunk)Clone(chunk));
        }

19 View Source File : InMemoryPersistence.cs
License : MIT License
Project Creator : nstoredev

public async Task ReadAllAsync(long fromPositionInclusive, ISubscription subscription, int limit, CancellationToken cancellationToken)
        {
            await subscription.OnStartAsync(fromPositionInclusive).ConfigureAwait(false);

            int start = (int)Math.Max(fromPositionInclusive - 1, 0);

            _lockSlim.EnterReadLock();
            int lastWritten = _lastWrittenPosition;
            _lockSlim.ExitReadLock();

            if (start > lastWritten)
            {
                await subscription.StoppedAsync(fromPositionInclusive).ConfigureAwait(false);
                return;
            }

            var toRead = Math.Min(limit, lastWritten - start + 1);
            if (toRead <= 0)
            {
                await subscription.StoppedAsync(fromPositionInclusive).ConfigureAwait(false);
                return;
            }

            IEnumerable<MemoryChunk> list = new ArraySegment<MemoryChunk>(_chunks, start, toRead);

            long position = 0;

            try
            {
                foreach (var chunk in list)
                {
                    if (chunk.Deleted)
                    {
                        continue;
                    }

                    position = chunk.Position;

                    await _networkSimulator.Wait().ConfigureAwait(false);
                    cancellationToken.ThrowIfCancellationRequested();

                    if (!await subscription.OnNextAsync(Clone(chunk)).ConfigureAwait(false))
                    {
                        await subscription.StoppedAsync(position).ConfigureAwait(false);
                        return;
                    }
                }

                if (position == 0)
                {
                    await subscription.StoppedAsync(fromPositionInclusive).ConfigureAwait(false);
                }
                else
                {
                    await subscription.CompletedAsync(position).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                await subscription.OnErrorAsync(position, e).ConfigureAwait(false);
            }
        }

19 View Source File : InMemoryPersistence.cs
License : MIT License
Project Creator : nstoredev

public Task<long> ReadLastPositionAsync(CancellationToken cancellationToken)
        {
            try
            {
                _lockSlim.EnterReadLock();
                if (_lastWrittenPosition == -1)
                    return Task.FromResult(0L);

                return Task.FromResult(_chunks[_lastWrittenPosition].Position);
            }
            finally
            {
                _lockSlim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public bool TryGet(int index, out T result)
        {
            if (index >= 0)
            {
                slim.EnterReadLock();
                try
                {
                    if (index < list.Count)
                    {
                        result = list[index];
                        return true;
                    }
                }
                catch (ArgumentException)
                {
                }
                catch (InvalidOperationException)
                {
                }
                finally
                {
                    slim.ExitReadLock();
                }
            }

            result = default;
            return false;
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public bool Contains(T item)
        {
            slim.EnterReadLock();
            try
            {
                return list.Contains(item);
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public void CopyTo(T[] array, int arrayIndex)
        {
            slim.EnterReadLock();
            try
            {
                list.CopyTo(array, arrayIndex);
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public IEnumerator<T> GetEnumerator()
        {
            List<T> col;
            slim.EnterReadLock();
            try
            {
                col = new List<T>(list);
            }
            finally
            {
                slim.ExitReadLock();
            }

            return col.GetEnumerator();
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public int IndexOf(T item)
        {
            slim.EnterReadLock();
            try
            {
                return list.IndexOf(item);
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public int IndexOf(T item, int index, int? count = null)
        {
            if (index < 0) index = 0;
            try
            {
                if (!count.HasValue)
                {
                    slim.EnterReadLock();
                    try
                    {
                        return list.IndexOf(item, index);
                    }
                    finally
                    {
                        slim.ExitReadLock();
                    }
                }

                slim.EnterReadLock();
                try
                {
                    return list.IndexOf(item, index, count.Value);
                }
                finally
                {
                    slim.ExitReadLock();
                }
            }
            catch (ArgumentException)
            {
                return -1;
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public int LastIndexOf(T item)
        {
            slim.EnterReadLock();
            try
            {
                return list.LastIndexOf(item);
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public int LastIndexOf(T item, int index)
        {
            slim.EnterReadLock();
            try
            {
                return list.LastIndexOf(item, index);
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public int LastIndexOf(T item, int index, int count)
        {
            slim.EnterReadLock();
            try
            {
                return list.LastIndexOf(item, index, count);
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public T[] ToArray()
        {
            slim.EnterReadLock();
            try
            {
                return list.ToArray();
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public T[] ToArray(int start, int? length = null)
        {
            slim.EnterReadLock();
            try
            {
                var col = list.Skip(start);
                if (length.HasValue) col = col.Take(length.Value);
                return col.ToArray();
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public SynchronizedList<T> Clone()
        {
            slim.EnterReadLock();
            try
            {
                return new SynchronizedList<T>(list);
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public SynchronizedList<T> Clone(int index, int? count = null)
        {
            slim.EnterReadLock();
            try
            {
                var col = list.Skip(index);
                if (count.HasValue) col = col.Take(count.Value);
                return new SynchronizedList<T>(col);
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : SynchronizedList.cs
License : MIT License
Project Creator : nuscien

public SynchronizedList<T> Clone(IEnumerable<int> indexes)
        {
            var col = new SynchronizedList<T>();
            slim.EnterReadLock();
            try
            {
                col.AddRange(indexes.Select(ele => list[ele]));
            }
            finally
            {
                slim.ExitReadLock();
            }

            return col;
        }

19 View Source File : FactorySet.cs
License : MIT License
Project Creator : nuscien

public Func<T> GetFactory(string key = null, bool disableBackup = false)
        {
            slim.EnterReadLock();
            try
            {
                if (factories.TryGetValue(key ?? string.Empty, out var h) && h is Func<T> r)
                    return r;
            }
            finally
            {
                slim.ExitReadLock();
            }

            if (!disableBackup) return null;
            var b = backup1;
            if (b != null) return () => b(key);
            return backup2;
        }

19 View Source File : FactorySet.cs
License : MIT License
Project Creator : nuscien

public IList<Type> GetAllTypes()
        {
            slim.EnterReadLock();
            try
            {
                return factories.Keys.ToList();
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : FactorySet.cs
License : MIT License
Project Creator : nuscien

public Func<T> GetFactory<T>()
        {
            slim.EnterReadLock();
            try
            {
                if (factories.TryGetValue(typeof(T), out var h) && h is Func<T> r)
                    return r;
            }
            finally
            {
                slim.ExitReadLock();
            }

            return null;
        }

19 View Source File : FactorySet.cs
License : MIT License
Project Creator : nuscien

public IList<string> GetKeys()
        {
            slim.EnterReadLock();
            try
            {
                return factories.Keys.ToList();
            }
            finally
            {
                slim.ExitReadLock();
            }
        }

19 View Source File : QueueDatagramTransport.cs
License : Apache License 2.0
Project Creator : NZSmartie

public int Receive(byte[] buf, int off, int len, int waitMillis)
        {
            try
            {
                CloseLock.EnterReadLock();
                if (IsClosed)
                    throw new DtlsConnectionClosedException();

                var success = ReceiveQueue.TryTake(out var data, waitMillis, _cts.Token);
                if (!success)
                    return -1; // DO NOT return 0. This will disable the wait timeout effectively for the caller and any abort logic will by bypreplaceded!
                var readLen = Math.Min(len, data.Length);
                Array.Copy(data, 0, buf, off, readLen);
                return readLen;
            }
            catch (OperationCanceledException)
            {
                return -1; // DO NOT return 0. This will disable the wait timeout effectively for the caller and any abort logic will by bypreplaceded!
            }
            finally
            {
                CloseLock.ExitReadLock();
            }
        }

19 View Source File : QueueDatagramTransport.cs
License : Apache License 2.0
Project Creator : NZSmartie

public void Send(byte[] buf, int off, int len)
        {
            try
            {
                CloseLock.EnterReadLock();
                if (IsClosed)
                    throw new DtlsConnectionClosedException(); // throw is important here, so DtlsServer.Accept() throws when the connection is closed and the client doesn't answer
                _sendCallback(new ArraySegment<byte>(buf, off, len).ToArray());
            }
            finally
            {
                CloseLock.ExitReadLock();
            }
        }

19 View Source File : SecurityManager.cs
License : GNU Affero General Public License v3.0
Project Creator : OpenIIoT

private void PurgeExpiredSessions()
        {
            logger.EnterMethod();

            IList<ISession> sessions = new List<ISession>();

            sessionLock.EnterReadLock();

            try
            {
                sessions = SessionList.ToList();
            }
            finally
            {
                sessionLock.ExitReadLock();
            }

            foreach (Session session in sessions)
            {
                if (session.IsExpired)
                {
                    EndSession(session);
                }
            }

            logger.ExitMethod();
        }

19 View Source File : ThreadSafeList.cs
License : GNU Lesser General Public License v3.0
Project Creator : OpenMiNET

public bool Contains(T item)
        {
            _lock.EnterReadLock();
 
            bool result;
            try
            {
                result = _list.Contains(item);
            }
            finally
            {
                _lock.ExitReadLock();
            }
 
            return result;
        }

19 View Source File : ThreadSafeList.cs
License : GNU Lesser General Public License v3.0
Project Creator : OpenMiNET

public IEnumerator<T> GetEnumerator()
        {
            _lock.EnterReadLock();
 
            try
            {
                foreach (T value in _list)
                {
                    yield return value;
                }
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }

19 View Source File : ThreadSafeList.cs
License : GNU Lesser General Public License v3.0
Project Creator : OpenMiNET

public int IndexOf(T item)
        {
            _lock.EnterReadLock();
 
            int result;
            try
            {
                result = _list.IndexOf(item);
            }
            finally
            {
                _lock.ExitReadLock();
            }
 
            return result;
        }

19 View Source File : GrainReferenceCache.cs
License : MIT License
Project Creator : OrleansContrib

public TValue Get(TKey key)
        {
            TimestampedValue result;
            bool readerLockUpgraded = false;

            try
            {
                rwLock.EnterReadLock();

                if (cache.TryGetValue(key, out result))
                {
                    result.Generation = Interlocked.Increment(ref nextGeneration);
                    TimeSpan age = result.WhenLoaded.Subtract(DateTime.UtcNow);
                    if (age > requiredFreshness)
                    {
                        try
                        {
                            rwLock.ExitReadLock();
                            readerLockUpgraded = true;
                            rwLock.EnterWriteLock();
                            cache.Remove(key);
                        }
                        finally
                        {
                            rwLock.ExitWriteLock();
                        }
                        result = null;
                    }
                }

                if (result != null)
                    return result.Value;
            }
            finally
            {
                if (!readerLockUpgraded)
                    rwLock.ExitReadLock();
            }

            try
            {
                rwLock.EnterWriteLock();

                if (cache.TryGetValue(key, out result))
                {
                    result.Generation = Interlocked.Increment(ref nextGeneration);
                    return result.Value;
                }

                while (cache.Count >= maximumCount)
                {
                    long generationToDelete = Interlocked.Increment(ref generationToFree);
                    KeyValuePair<TKey, TimestampedValue> entryToFree =
                        cache.FirstOrDefault(kvp => kvp.Value.Generation == generationToDelete);

                    if (entryToFree.Key != null)
                    {
                        cache.Remove(entryToFree.Key);
                    }
                }

                result = new TimestampedValue { Generation = Interlocked.Increment(ref nextGeneration) };
                try
                {
                    var r = fetcher(key);
                    result.Value = r;
                    result.WhenLoaded = DateTime.UtcNow;
                    cache.Add(key, result);
                }
                catch (Exception)
                {
                    if (cache.ContainsKey(key))
                        cache.Remove(key);
                    throw;
                }
            }
            finally
            {
                rwLock.ExitWriteLock();
            }
            return result.Value;
        }

19 View Source File : InMemoryTimeoutRepository.cs
License : MIT License
Project Creator : osstotalsoft

public Task<TimeoutRecord> Peek(Guid timeoutId)
        {
            try
            {
                _readerWriterLock.EnterReadLock();
                return Task.FromResult(_storage.SingleOrDefault(t => t.Id == timeoutId));
            }
            finally
            {
                _readerWriterLock.ExitReadLock();
            }
        }

19 View Source File : InMemoryTimeoutRepository.cs
License : MIT License
Project Creator : osstotalsoft

public Task<TimeoutBatch> GetNextBatch(DateTime startSlice)
        {
            var now = _currentTimeProvider();
            var nextTimeToRunQuery = DateTime.MaxValue;
            var dueTimeouts = new List<TimeoutRecord>();

            try
            {
                _readerWriterLock.EnterReadLock();

                foreach (var data in _storage)
                {
                    if (data.DueDate > now && data.DueDate < nextTimeToRunQuery)
                    {
                        nextTimeToRunQuery = data.DueDate;
                    }
                    if (data.DueDate > startSlice && data.DueDate <= now)
                    {
                        dueTimeouts.Add(data);
                    }
                }
            }
            finally
            {
                _readerWriterLock.ExitReadLock();
            }

            if (nextTimeToRunQuery == DateTime.MaxValue)
            {
                nextTimeToRunQuery = now.Add(EmptyResultsNextTimeToRunQuerySpan);
            }

            return Task.FromResult(new TimeoutBatch(dueTimeouts.ToArray(), nextTimeToRunQuery));
        }

19 View Source File : Concurrent.cs
License : MIT License
Project Creator : outerminds

[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public ReadDisposable Read(bool upgradeable = false)
        {
            if (upgradeable)
            {
                _lock.EnterUpgradeableReadLock();
                return new ReadDisposable(this, concurrent => concurrent._lock.ExitUpgradeableReadLock());
            }
            else
            {
                _lock.EnterReadLock();
                return new ReadDisposable(this, concurrent => concurrent._lock.ExitReadLock());
            }
        }

19 View Source File : LockTests.cs
License : MIT License
Project Creator : outerminds

public static void Test()
        {
            var locker = new ReaderWriterLockSlim();
            var counter = 0;
            var _8x8 = Arrays(8, 8);
            var _16x8 = Arrays(16, 8);
            var _32x8 = Arrays(32, 8);

            var _8x16 = Arrays(8, 16);
            var _16x16 = Arrays(16, 16);
            var _32x16 = Arrays(32, 16);

            var _8x32 = Arrays(8, 32);
            var _16x32 = Arrays(16, 32);
            var _32x32 = Arrays(32, 32);

            var _8x64 = Arrays(8, 64);
            var _16x64 = Arrays(16, 64);
            var _32x64 = Arrays(32, 64);
            var _64x64 = Arrays(64, 64);

            var _8x128 = Arrays(8, 128);
            var _16x128 = Arrays(16, 128);
            var _32x128 = Arrays(32, 128);
            var _64x128 = Arrays(64, 128);
            var _128x128 = Arrays(128, 128);
            var indices = Enumerable.Range(0, 128).ToArray();
            var slots = Enumerable.Range(0, 128).Select(_ => new Slot()).ToArray();

            int[][] Arrays(int x, int y) => Enumerable.Range(0, x)
                .Select(i => Enumerable.Range(0, y).Select(j => i * j).ToArray())
                .ToArray();

            void Simple(int[][] arrays)
            {
                foreach (var array in arrays) for (int i = 0; i < array.Length; i++) array[i]++;
            }

            void Indexed(int[][] arrays)
            {
                foreach (var array in arrays) for (int i = 0; i < array.Length; i++) array[indices[i]]++;
            }

            void Locked(int[][] arrays)
            {
                foreach (var array in arrays) lock (array) for (int i = 0; i < array.Length; i++) array[i]++;
            }

            void Reads(int[][] arrays)
            {
                foreach (var array in arrays)
                {
                    locker.EnterReadLock();
                    for (int i = 0; i < array.Length; i++) array[i]++;
                    locker.ExitReadLock();
                }
            }

            void Writes(int[][] arrays)
            {
                foreach (var array in arrays)
                {
                    locker.EnterWriteLock();
                    for (int i = 0; i < array.Length; i++) array[i]++;
                    locker.ExitWriteLock();
                }
            }

            void Inter(int[][] arrays)
            {
                foreach (var array in arrays)
                {
                    if (Interlocked.Increment(ref counter) == 1) for (int i = 0; i < array.Length; i++) array[i]++;
                    Interlocked.Decrement(ref counter);
                }
            }

            void Simple8x8() => Simple(_8x8);
            void Simple16x8() => Simple(_16x8);
            void Simple32x8() => Simple(_32x8);
            void Simple8x16() => Simple(_8x16);
            void Simple16x16() => Simple(_16x16);
            void Simple32x16() => Simple(_32x16);
            void Simple8x32() => Simple(_8x32);
            void Simple16x32() => Simple(_16x32);
            void Simple32x32() => Simple(_32x32);
            void Simple8x64() => Simple(_8x64);
            void Simple16x64() => Simple(_16x64);
            void Simple32x64() => Simple(_32x64);
            void Simple64x64() => Simple(_64x64);
            void Simple8x128() => Simple(_8x128);
            void Simple16x128() => Simple(_16x128);
            void Simple32x128() => Simple(_32x128);
            void Simple64x128() => Simple(_64x128);
            void Simple128x128() => Simple(_128x128);

            void Indexed8x8() => Indexed(_8x8);
            void Indexed16x8() => Indexed(_16x8);
            void Indexed32x8() => Indexed(_32x8);
            void Indexed8x16() => Indexed(_8x16);
            void Indexed16x16() => Indexed(_16x16);
            void Indexed32x16() => Indexed(_32x16);
            void Indexed8x32() => Indexed(_8x32);
            void Indexed16x32() => Indexed(_16x32);
            void Indexed32x32() => Indexed(_32x32);
            void Indexed8x64() => Indexed(_8x64);
            void Indexed16x64() => Indexed(_16x64);
            void Indexed32x64() => Indexed(_32x64);
            void Indexed64x64() => Indexed(_64x64);
            void Indexed8x128() => Indexed(_8x128);
            void Indexed16x128() => Indexed(_16x128);
            void Indexed32x128() => Indexed(_32x128);
            void Indexed64x128() => Indexed(_64x128);
            void Indexed128x128() => Indexed(_128x128);

            void Locked8x8() => Locked(_8x8);
            void Locked16x8() => Locked(_16x8);
            void Locked32x8() => Locked(_32x8);
            void Locked8x16() => Locked(_8x16);
            void Locked16x16() => Locked(_16x16);
            void Locked32x16() => Locked(_32x16);
            void Locked8x32() => Locked(_8x32);
            void Locked16x32() => Locked(_16x32);
            void Locked32x32() => Locked(_32x32);
            void Locked8x64() => Locked(_8x64);
            void Locked16x64() => Locked(_16x64);
            void Locked32x64() => Locked(_32x64);
            void Locked64x64() => Locked(_64x64);
            void Locked8x128() => Locked(_8x128);
            void Locked16x128() => Locked(_16x128);
            void Locked32x128() => Locked(_32x128);
            void Locked64x128() => Locked(_64x128);
            void Locked128x128() => Locked(_128x128);

            void Reads8x8() => Reads(_8x8);
            void Reads16x8() => Reads(_16x8);
            void Reads32x8() => Reads(_32x8);
            void Reads8x16() => Reads(_8x16);
            void Reads16x16() => Reads(_16x16);
            void Reads32x16() => Reads(_32x16);
            void Reads8x32() => Reads(_8x32);
            void Reads16x32() => Reads(_16x32);
            void Reads32x32() => Reads(_32x32);
            void Reads8x64() => Reads(_8x64);
            void Reads16x64() => Reads(_16x64);
            void Reads32x64() => Reads(_32x64);
            void Reads64x64() => Reads(_64x64);
            void Reads8x128() => Reads(_8x128);
            void Reads16x128() => Reads(_16x128);
            void Reads32x128() => Reads(_32x128);
            void Reads64x128() => Reads(_64x128);
            void Reads128x128() => Reads(_128x128);

            void Writes8x8() => Writes(_8x8);
            void Writes16x8() => Writes(_16x8);
            void Writes32x8() => Writes(_32x8);
            void Writes8x16() => Writes(_8x16);
            void Writes16x16() => Writes(_16x16);
            void Writes32x16() => Writes(_32x16);
            void Writes8x32() => Writes(_8x32);
            void Writes16x32() => Writes(_16x32);
            void Writes32x32() => Writes(_32x32);
            void Writes8x64() => Writes(_8x64);
            void Writes16x64() => Writes(_16x64);
            void Writes32x64() => Writes(_32x64);
            void Writes64x64() => Writes(_64x64);
            void Writes8x128() => Writes(_8x128);
            void Writes16x128() => Writes(_16x128);
            void Writes32x128() => Writes(_32x128);
            void Writes64x128() => Writes(_64x128);
            void Writes128x128() => Writes(_128x128);

            void Inter8x8() => Inter(_8x8);
            void Inter16x8() => Inter(_16x8);
            void Inter32x8() => Inter(_32x8);
            void Inter8x16() => Inter(_8x16);
            void Inter16x16() => Inter(_16x16);
            void Inter32x16() => Inter(_32x16);
            void Inter8x32() => Inter(_8x32);
            void Inter16x32() => Inter(_16x32);
            void Inter32x32() => Inter(_32x32);
            void Inter8x64() => Inter(_8x64);
            void Inter16x64() => Inter(_16x64);
            void Inter32x64() => Inter(_32x64);
            void Inter64x64() => Inter(_64x64);
            void Inter8x128() => Inter(_8x128);
            void Inter16x128() => Inter(_16x128);
            void Inter32x128() => Inter(_32x128);
            void Inter64x128() => Inter(_64x128);
            void Inter128x128() => Inter(_128x128);

            for (int i = 0; i < 10; i++)
            {
                Experiment.Test.Measure(Simple8x8, new Action[]{
                    Simple8x8,
                    Simple16x8,
                    Simple32x8,
                    Simple8x16,
                    Simple16x16,
                    Simple32x16,
                    Simple8x32,
                    Simple16x32,
                    Simple32x32,
                    Simple8x64,
                    Simple16x64,
                    Simple32x64,
                    Simple64x64,
                    Simple8x128,
                    Simple16x128,
                    Simple32x128,
                    Simple64x128,
                    Simple128x128,

                    Indexed8x8,
                    Indexed16x8,
                    Indexed32x8,
                    Indexed8x16,
                    Indexed16x16,
                    Indexed32x16,
                    Indexed8x32,
                    Indexed16x32,
                    Indexed32x32,
                    Indexed8x64,
                    Indexed16x64,
                    Indexed32x64,
                    Indexed64x64,
                    Indexed8x128,
                    Indexed16x128,
                    Indexed32x128,
                    Indexed64x128,
                    Indexed128x128,

                    Locked8x8,
                    Locked16x8,
                    Locked32x8,
                    Locked8x16,
                    Locked16x16,
                    Locked32x16,
                    Locked8x32,
                    Locked16x32,
                    Locked32x32,
                    Locked8x64,
                    Locked16x64,
                    Locked32x64,
                    Locked64x64,
                    Locked8x128,
                    Locked16x128,
                    Locked32x128,
                    Locked64x128,
                    Locked128x128,

                    Reads8x8,
                    Reads16x8,
                    Reads32x8,
                    Reads8x16,
                    Reads16x16,
                    Reads32x16,
                    Reads8x32,
                    Reads16x32,
                    Reads32x32,
                    Reads8x64,
                    Reads16x64,
                    Reads32x64,
                    Reads64x64,
                    Reads8x128,
                    Reads16x128,
                    Reads32x128,
                    Reads64x128,
                    Reads128x128,

                    Writes8x8,
                    Writes16x8,
                    Writes32x8,
                    Writes8x16,
                    Writes16x16,
                    Writes32x16,
                    Writes8x32,
                    Writes16x32,
                    Writes32x32,
                    Writes8x64,
                    Writes16x64,
                    Writes32x64,
                    Writes64x64,
                    Writes8x128,
                    Writes16x128,
                    Writes32x128,
                    Writes64x128,
                    Writes128x128,

                    Inter8x8,
                    Inter16x8,
                    Inter32x8,
                    Inter8x16,
                    Inter16x16,
                    Inter32x16,
                    Inter8x32,
                    Inter16x32,
                    Inter32x32,
                    Inter8x64,
                    Inter16x64,
                    Inter32x64,
                    Inter64x64,
                    Inter8x128,
                    Inter16x128,
                    Inter32x128,
                    Inter64x128,
                    Inter128x128,
                }, 100_000);
            }
        }

19 View Source File : NaiveSessionCache.cs
License : MIT License
Project Creator : PacktPublishing

public void Load()
        {
            SessionLock.EnterReadLock();
            this.Deserialize((byte[])HttpContext.Current.Session[CacheId]);
            SessionLock.ExitReadLock();
        }

19 View Source File : 5SlimLocks.cs
License : MIT License
Project Creator : PacktPublishing

static void ReaderTask()
        {
            for (int i = 0; i < 2; i++)
            {
                try
                {
                    _readerWriterLockSlim.EnterReadLock();
                Console.WriteLine($"Entered ReadLock on Task {Task.CurrentId}");

                Console.WriteLine($"Items: { _list.Select(j => j.ToString()).Aggregate((a, b) => a + "," + b)} on Task {Task.CurrentId}");
                Console.WriteLine($"Exiting ReadLock on Task {Task.CurrentId}");
                }
                finally
                {
                    _readerWriterLockSlim.ExitWriteLock();
                }
                Thread.Sleep(1000);
            }
        }

19 View Source File : ReaderAttribute.cs
License : MIT License
Project Creator : Panallox

public void OnEnter(MethodInterceptionArgs e) => GetSynchronizationLock().EnterReadLock();

19 View Source File : ReaderAttribute.cs
License : MIT License
Project Creator : Panallox

public void OnGet(PropertyInterceptionArgs e) => GetSynchronizationLock().EnterReadLock();

See More Examples