public RemoteCredentialsDecorator(Uri brokerUri, ISecurityService securityService, IMainThread mainThread) {
     _securityService = securityService;
     _mainThread = mainThread;
     _authority = brokerUri.ToCredentialAuthority();
     _lock = new AsyncReaderWriterLock();
     _credentialsAreValid = true;
 }
 public void Unlocked_PermitsMultipleReaderLocks()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.ReaderLockAsync();
         await rwl.ReaderLockAsync();
     });
 }
        public GooglePublicKeyProviderFetchAndCache(IHttpClientFactory httpClientFactory, ILogger <GooglePublicKeyProviderFetchAndCache> logger)
        {
            this.httpClientFactory = httpClientFactory;
            this.logger            = logger;

            cacheLock                = new AsyncReaderWriterLock();
            cachedKeyString          = null;
            cacheExpirationTimeStamp = 0;
        }
Esempio n. 4
0
 public UniqueEntityIndex(AsyncReaderWriterLock repoLock, Func <TEntity, IEnumerable <TKey> > keySelector,
                          Func <TEntity, bool> filter = null)
 {
     _repoLock         = repoLock;
     _index            = new Dictionary <TKey, TEntity>();
     _keySelector      = keySelector;
     _filter           = filter;
     _keySubscriptions = new Dictionary <TKey, ISet <Subscription <TEntity> > >();
 }
 public LockingNonceStore(INonceStore decorated, ILockFactory lockFactory)
 {
     if (lockFactory == null)
     {
         throw new ArgumentNullException(nameof(lockFactory));
     }
     _decorated = decorated ?? throw new ArgumentNullException(nameof(decorated));
     _lock      = lockFactory.CreateLock();
 }
 public void ReadLocked_AllowsUpgradeableReaderLock()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.ReaderLockAsync();
         await rwl.UpgradeableReaderLockAsync();
     });
 }
Esempio n. 7
0
 public RemoteCredentialsDecorator(Uri brokerUri, ISecurityService securityService)
 {
     _securityService = securityService;
     _authority       = new UriBuilder {
         Scheme = brokerUri.Scheme, Host = brokerUri.Host, Port = brokerUri.Port
     }.ToString();
     _lock = new AsyncReaderWriterLock();
     _credentialsAreValid = true;
 }
Esempio n. 8
0
 private void SetValue(StatusMapTypes id, object value, AsyncReaderWriterLock locker = null)
 {
     if (!map.ContainsKey(id))
     {
         throw new InvalidOperationException();
     }
     map[id].Value       = value;
     map[id].ValueLocker = locker;
 }
        /// <summary>
        /// Initialises a new <see cref="ApiClient"/> using a user-set <see cref="ApiSerializer"/>
        /// </summary>
        public ApiClient(ApiSerializer serializer)
        {
            Headers    = new HeaderCollection(this);
            Serializer = new SerializerResolver(serializer);

            _lock = new AsyncReaderWriterLock();

            RequestClientReset(true);
        }
Esempio n. 10
0
 public RemoteCredentialsDecorator(string credentialAuthority, string workspaceName, ISecurityService securityService, IMainThread mainThread)
 {
     _securityService     = securityService;
     _mainThread          = mainThread;
     _authority           = credentialAuthority;
     _lock                = new AsyncReaderWriterLock();
     _credentialsAreValid = true;
     _workspaceName       = workspaceName;
 }
Esempio n. 11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="action"></param>
 /// <param name="condition"></param>
 public AsyncRelayCommand(Func <object, Task> action, Predicate <object> condition = null)
 {
     this.action     = new WeakDelegate <Func <object, Task> >(action);
     this.condition  = null != condition ? new WeakPredicate <object>(condition) : null;
     access          = new AsyncReaderWriterLock();
     executeChanged  = new WeakEventHandler();
     complete        = new WeakEvent <TypedEventHandler <IAsyncCommand, CommandCompleteEventArgs> >();
     propertyChanged = new WeakEvent <PropertyChangedEventHandler>();
 }
Esempio n. 12
0
        internal ConcurrentEnumerator(ICollection <T> collection, AsyncReaderWriterLock rwLock)
        {
            if ((collection == null) || (rwLock == null))
            {
                throw new ArgumentNullException(nameof(collection) + " || " + nameof(rwLock));
            }

            Lock       = rwLock.ReaderLock();
            Enumerator = collection.GetEnumerator();
        }
 public void ReadLocked_PreventsWriterLock()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.ReaderLockAsync();
         var task = rwl.WriterLockAsync();
         await AssertEx.NeverCompletesAsync(task);
     });
 }
 public void WriteLocked_PreventsUpgradeableReaderLock()
 {
     Test.Async(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.WriterLockAsync();
         var task = rwl.UpgradeableReaderLockAsync().AsTask();
         await AssertEx.NeverCompletesAsync(task);
     });
 }
 public void UpgradeableKey_MultiUpgrade_ThrowsException()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         var key = await rwl.UpgradeableReaderLockAsync();
         await key.UpgradeAsync();
         await AssertEx.ThrowsExceptionAsync <InvalidOperationException>(async() => { await key.UpgradeAsync(); });
     });
 }
        public void TestBlockingAsync()
        {
            var l = new AsyncReaderWriterLock();

            for (var i = 0; i < 2; i++)
            {
                var readReleaserTask = l.ReadLockAsync();
                AssertEqualValue(() => l.CurrentReaders, 1, readReleaserTask);
                AssertTaskCompleted(readReleaserTask);

                var readReleaserTask2 = l.ReadLockAsync();
                AssertEqualValue(() => l.CurrentReaders, 2, readReleaserTask2);
                AssertTaskCompleted(readReleaserTask2);

                var writeReleaserTask = l.WriteLockAsync();
                AssertEqualValue(() => l.AcquiredWriteLock, true, writeReleaserTask);
                AssertEqualValue(() => l.Writing, false, writeReleaserTask);
                Assert.That(writeReleaserTask.IsCompleted, Is.False);

                readReleaserTask.Result.Dispose();
                Assert.That(l.CurrentReaders, Is.EqualTo(1));
                Assert.That(writeReleaserTask.IsCompleted, Is.False);

                readReleaserTask2.Result.Dispose();
                Assert.That(l.CurrentReaders, Is.EqualTo(0));
                AssertEqualValue(() => l.Writing, true, writeReleaserTask);
                AssertTaskCompleted(writeReleaserTask);

                readReleaserTask = l.ReadLockAsync();
                AssertEqualValue(() => l.ReadersWaiting, 1, readReleaserTask);
                Assert.That(readReleaserTask.IsCompleted, Is.False);

                var writeReleaserTask2 = l.WriteLockAsync();
                AssertEqualValue(() => l.WritersWaiting, 1, writeReleaserTask2);
                Assert.That(writeReleaserTask2.IsCompleted, Is.False);

                writeReleaserTask.Result.Dispose();
                AssertEqualValue(() => l.WritersWaiting, 0, writeReleaserTask2);
                AssertEqualValue(() => l.Writing, true, writeReleaserTask2);
                Assert.That(readReleaserTask.IsCompleted, Is.False);
                AssertTaskCompleted(writeReleaserTask2);

                writeReleaserTask2.Result.Dispose();
                AssertEqualValue(() => l.Writing, false, writeReleaserTask2);
                AssertEqualValue(() => l.ReadersWaiting, 0, readReleaserTask);
                AssertEqualValue(() => l.CurrentReaders, 1, readReleaserTask);
                AssertTaskCompleted(readReleaserTask);

                readReleaserTask.Result.Dispose();
                Assert.That(l.ReadersWaiting, Is.EqualTo(0));
                Assert.That(l.WritersWaiting, Is.EqualTo(0));
                Assert.That(l.CurrentReaders, Is.EqualTo(0));
                Assert.That(l.Writing, Is.False);
            }
        }
 public void UpgradeableReadLocked_Downgrade_AllowsReaderLock()
 {
     AsyncContext.Run(async() =>
     {
         var rwl        = new AsyncReaderWriterLock();
         var key        = await rwl.UpgradeableReaderLockAsync();
         var upgradeKey = await key.UpgradeAsync();
         upgradeKey.Dispose();
         await rwl.ReaderLockAsync();
     });
 }
        public void UpgradeableReaderLock_PreCancelled_LockAvailable_SynchronouslyTakesLock()
        {
            var rwl   = new AsyncReaderWriterLock();
            var token = new CancellationToken(true);

            var task = rwl.UpgradeableReaderLockAsync(token);

            Assert.IsTrue(task.IsCompleted);
            Assert.IsFalse(task.IsCanceled);
            Assert.IsFalse(task.IsFaulted);
        }
 public void UpgradeableKey_MultiUpgradeWhenFirstUpgradeIsIncomplete_ThrowsSynchronousException()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         await rwl.ReaderLockAsync();
         var key = await rwl.UpgradeableReaderLockAsync();
         var _   = key.UpgradeAsync();
         AssertEx.ThrowsException <InvalidOperationException>(() => key.UpgradeAsync());
     });
 }
 public void UpgradeableReadLocked_AllowsMultipleReaderLocks()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         var key = await rwl.UpgradeableReaderLockAsync();
         Assert.IsFalse(key.Upgraded);
         await rwl.ReaderLockAsync();
         await rwl.ReaderLockAsync();
     });
 }
        public async Task TestInvaildExitReadLockUsageAsync()
        {
            var l             = new AsyncReaderWriterLock();
            var readReleaser  = await(l.ReadLockAsync());
            var readReleaser2 = await(l.ReadLockAsync());

            readReleaser.Dispose();
            readReleaser2.Dispose();
            Assert.Throws <InvalidOperationException>(() => readReleaser.Dispose());
            Assert.Throws <InvalidOperationException>(() => readReleaser2.Dispose());
        }
 public void UpgradableKey_MultiDispose_DoesNothing()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         var key = await rwl.UpgradeableReaderLockAsync();
         key.Dispose();
         key.Dispose();
         await rwl.UpgradeableReaderLockAsync();
     });
 }
        public async Task WaitForReadAsync_TheSecondWriterWouldBlock()
        {
            var locker = new AsyncReaderWriterLock();

            using (await locker.WaitForReadAsync())
            {
                Assert.IsFalse(locker.TryEnterWrite(out _));
                using var tokenSrc = new CancellationTokenSource(1000);
                Assert.CatchAsync <OperationCanceledException>(() => locker.WaitForWriteAsync(tokenSrc.Token));
            }
        }
 public void UpgradeableReadLocked_PreventsWriterLock()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         var key = await rwl.UpgradeableReaderLockAsync();
         Assert.IsFalse(key.Upgraded);
         var task = rwl.WriterLockAsync();
         await AssertEx.NeverCompletesAsync(task);
     });
 }
        public FakeStatusCyclocityRepository(
            ICyclocityRepository innerRepository,
            IFakeGenerator fakeStatusGenerator,
            ITaskulerScheduleHandle updateFakeDataScheduleHandle)
        {
            _InnerRepository     = innerRepository;
            _FakeStatusGenerator = fakeStatusGenerator;
            _StationsListLock    = new AsyncReaderWriterLock();

            updateFakeDataScheduleHandle.AddTask(async() => await _UpdateFakeDataAsync());
        }
 public OrderedCache(string key, int cacheSize, ITradesLogRepository tradesLogRepository)
 {
     _tradesLogRepository   = tradesLogRepository;
     _initLock              = new SemaphoreSlim(1, 1);
     _asyncReaderWriterLock = new AsyncReaderWriterLock();
     _eventsComparer        = new EventsComparer <Trade>();
     _data      = new List <Trade>();
     _cacheSize = cacheSize;
     _dbLoaded  = false;
     _key       = key;
 }
        public void WriterLock_PreCancelled_LockAvailable_SynchronouslyTakesLock()
        {
            AsyncReaderWriterLock rwl   = new AsyncReaderWriterLock();
            CancellationToken     token = new CancellationToken(true);

            Task <IDisposable> task = rwl.WriterLockAsync(token).AsTask();

            Assert.True(task.IsCompleted);
            Assert.False(task.IsCanceled);
            Assert.False(task.IsFaulted);
        }
        public async Task WaitForReadAsync_MultipleCall_OK(int callTimes)
        {
            var locker  = new AsyncReaderWriterLock();
            var readers = new List <IDisposable>();

            for (int i = 0; i < callTimes; i++)
            {
                readers.Add(await locker.WaitForReadAsync());
                Assert.IsTrue(locker.TryEnterRead(out var reader));
                readers.Add(reader);
            }
        }
        public async Task Reader_Writer_Reader_LastReaderBlock()
        {
            var locker  = new AsyncReaderWriterLock();
            var reader1 = await locker.WaitForReadAsync();

            var writer = locker.WaitForWriteAsync();
            await Task.Delay(100);

            Assert.IsFalse(locker.TryEnterRead(out _));
            using var tokenSrc = new CancellationTokenSource(1000);
            Assert.CatchAsync <OperationCanceledException>(() => locker.WaitForReadAsync(tokenSrc.Token));
        }
Esempio n. 30
0
 public static string HexiClientsToString(object addresses, AsyncReaderWriterLock locker)
 {
     if (addresses == null)
     {
         return("");
     }
     using (locker.ReaderLock())
     {
         var addr = (List <HexiUdpClient>)addresses;
         return(String.Join(", ", addr));
     }
 }
Esempio n. 31
0
 public FileIndexer(IEnumerable <IFileIndexSource> sources, IJarvisLog log)
 {
     _log       = new LogDecorator("FileIndexer", log);
     _sources   = new List <IFileIndexSource>(sources ?? Array.Empty <IFileIndexSource>());
     _lock      = new AsyncReaderWriterLock();
     _stopWords = new HashSet <string>(StringComparer.OrdinalIgnoreCase)
     {
         "to", "the"
     };
     _comparer      = new ScoreComparer();
     _entryComparer = new IndexedEntryComparer();
 }
Esempio n. 32
0
 public AsyncReaderWriterLockTest() {
     _arwl = new AsyncReaderWriterLock();
 }
 public LockSource(AsyncReaderWriterLock host, bool isWriter) {
     _host = host;
     _tcs = new TaskCompletionSource<IAsyncReaderWriterLockToken>();
     _reentrancyCount = 1;
     IsWriter = isWriter;
 }
 public ExclusiveReaderLockSource(AsyncReaderWriterLock host, ExclusiveReaderLock erLock) : base(host, false) {
     ExclusiveReaderLock = erLock;
 }
 public ExclusiveReaderLock(AsyncReaderWriterLock host) {
     _host = host;
 }