private async Task RunAsync()
        {
            while (true)
            {
                await jobSignal.WaitAsync().ConfigureAwait(false);

                var jobResultBoxByKey = new SCG.Dictionary <K, AsyncBox <Entry <K, V> > >();
                var spinner           = new SpinWait();
                do
                {
                    Tuple <K, AsyncBox <Entry <K, V> > > job;
                    while (!jobQueue.TryDequeue(out job))
                    {
                        spinner.SpinOnce();
                    }
                    jobResultBoxByKey.Add(job.Item1, job.Item2);
                } while (jobSignal.TryTake());

//            Console.WriteLine("Batch Read: " + jobResultBoxByKey.Count);
                var results = await hitler.GetManyAsync(new HashSet <K>(jobResultBoxByKey.Keys)).ConfigureAwait(false);

                foreach (var kvp in results)
                {
                    jobResultBoxByKey[kvp.Key].SetResult(kvp.Value);
                }
            }
        }
示例#2
0
 public bool TryRead(out T message)
 {
     if (!readSemaphore.TryTake())
     {
         message = default(T);
         return(false);
     }
     if (!writeQueue.TryDequeue(out message))
     {
         throw new InvalidStateException();
     }
     return(true);
 }
        public async Task DisposeTryTakeZero()
        {               //****************************************
            var MyLock = new AsyncSemaphore();
            //****************************************

            var MyDispose = MyLock.DisposeAsync();

            Assert.False(MyLock.TryTake(out _, TimeSpan.Zero), "Nested lock taken");

            //****************************************

            await MyDispose;

            Assert.AreEqual(0, MyLock.WaitingCount, "Still waiting for Semaphore");
            Assert.AreEqual(MyLock.MaxCount, MyLock.CurrentCount, "Semaphore still held");
        }
        public void TryTakeTakeZero()
        {               //****************************************
            var MyLock = new AsyncSemaphore();

            //****************************************

            Assert.True(MyLock.TryTake(out var MyHandle1), "Lock not taken");


            Assert.False(MyLock.TryTake(out _, TimeSpan.Zero), "Nested lock taken");

            MyHandle1.Dispose();

            //****************************************

            Assert.AreEqual(0, MyLock.WaitingCount, "Still waiting for Semaphore");
            Assert.AreEqual(MyLock.MaxCount, MyLock.CurrentCount, "Semaphore still held");
        }
        private async Task RunAsync()
        {
            while (true)
            {
                Console.WriteLine("Begin BU");
                var sw = new Stopwatch();
                sw.Start();

                SCG.Dictionary <K, PendingUpdate <K, V> > mergedPendingUpdates = new SCG.Dictionary <K, PendingUpdate <K, V> >();
                var spinner = new SpinWait();
                while (pendingUpdatesSignal.TryTake())
                {
                    PendingUpdate <K, V> pendingUpdate;
                    while (!pendingUpdates.TryDequeue(out pendingUpdate))
                    {
                        spinner.SpinOnce();
                    }
                    PendingUpdate <K, V> existingPendingUpdate;
                    if (mergedPendingUpdates.TryGetValue(pendingUpdate.Base.Key, out existingPendingUpdate))
                    {
                        existingPendingUpdate.Updated = pendingUpdate.Updated;
                    }
                    else
                    {
                        mergedPendingUpdates[pendingUpdate.Base.Key] = pendingUpdate;
                    }
                }

                if (mergedPendingUpdates.Any())
                {
                    await hitler.BatchUpdateAsync(mergedPendingUpdates.Values.ToArray()).ConfigureAwait(false);
                }
                Console.WriteLine("END BU " + sw.ElapsedMilliseconds);
                await Task.Delay(batchUpdateIntervalMillis).ConfigureAwait(false);
            }
        }
        public void TryTakeCancel()
        {               //****************************************
            var MyLock = new AsyncSemaphore();

            //****************************************

            Assert.True(MyLock.TryTake(out var MyHandle1), "Lock not taken");

            using (var MyCancel = new CancellationTokenSource())
            {
                MyCancel.CancelAfter(50);

                var StartTime = DateTime.Now;

                try
                {
                    MyLock.TryTake(out _, MyCancel.Token);

                    Assert.Fail("Nested lock taken");
                }
                catch (OperationCanceledException)
                {
                }

                var EndTime = DateTime.Now;

                Assert.GreaterOrEqual((EndTime - StartTime).TotalMilliseconds, 50.0, "Finished too early");
            }

            MyHandle1.Dispose();

            //****************************************

            Assert.AreEqual(0, MyLock.WaitingCount, "Still waiting for Semaphore");
            Assert.AreEqual(MyLock.MaxCount, MyLock.CurrentCount, "Semaphore still held");
        }
        public void TryTakeMaxTime()
        {               //****************************************
            var MyLock = new AsyncSemaphore();

            //****************************************

            Assert.True(MyLock.TryTake(out var MyHandle1), "Lock not taken");

            var StartTime = DateTime.Now;


            Assert.False(MyLock.TryTake(out _, TimeSpan.FromMilliseconds(50)), "Nested lock taken");

            var EndTime = DateTime.Now;

            MyHandle1.Dispose();

            //****************************************

            Assert.GreaterOrEqual((EndTime - StartTime).TotalMilliseconds, 50.0, "Finished too early");

            Assert.AreEqual(0, MyLock.WaitingCount, "Still waiting for Semaphore");
            Assert.AreEqual(MyLock.MaxCount, MyLock.CurrentCount, "Semaphore still held");
        }