Example #1
0
 public static void lonelyThreadWrite(ConcurrencyUtils.Semaphore semaphore)
 {
     while (true)
     {
         semaphore.Acquire();
         Console.WriteLine("I'm a lonely thread");
         Thread.Sleep(100);
         semaphore.Release();
     }
 }
Example #2
0
 public static void groupThreadWrite(ConcurrencyUtils.LightSwitch LS)
 {
     while (true)
     {
         LS.Acquire();
         Console.WriteLine("Group thread #" + Thread.CurrentThread.Name);
         Thread.Sleep(3600);
         LS.Release();
     }
 }
Example #3
0
        public async Task RunAsync(PatchInfo[] toUpdate, PatchCache patchCache, CancellationToken ct = default)
        {
            Progress.Progress        = 0;
            Progress.IsIndeterminate = true;
            Progress.CompletedCount  = 0;
            Progress.TotalCount      = 0;

            if (toUpdate.Length == 0)
            {
                return;
            }

            var state = new ProcessState
            {
                AtomicIndex          = 0,
                AtomicCompletedCount = 0,
                AtomicProcessCount   = 0,
                UpdateBuckets        = new ConcurrentQueue <List <PatchCacheEntry> >()
            };

            var processCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(ct);

            Progress.TotalCount = toUpdate.Length;

            App.Logger.Info(nameof(VerifyFilesPhase), "Starting processing threads");
            Progress.IsIndeterminate = false;
            // TODO: processor affinity?
            var threadTasks = Enumerable.Range(0, Environment.ProcessorCount)
                              .Select(i => ConcurrencyUtils.RunOnDedicatedThreadAsync(() =>
            {
                Interlocked.Increment(ref state.AtomicProcessCount);
                try
                {
                    App.Logger.Info(nameof(VerifyFilesPhase), $"Processing thread {i} started");

                    _Process(state, toUpdate, processCancellationTokenSource.Token);
                }
                catch (OperationCanceledException)
                {
                    App.Logger.Error(nameof(VerifyFilesPhase), $"Processing thread {i} canceled");
                    processCancellationTokenSource.Cancel();
                    throw;
                }
                catch (Exception ex)
                {
                    App.Logger.Error(nameof(VerifyFilesPhase), $"Exception in processing thread {i}", ex);
                    processCancellationTokenSource.Cancel();
                    throw;
                }
                finally
                {
                    Interlocked.Decrement(ref state.AtomicProcessCount);
                    App.Logger.Info(nameof(VerifyFilesPhase), $"Processing thread {i} ended");
                }
            }, $"{nameof(VerifyFilesPhase)}({i})")).ToArray();

            await Task.Run(async() =>
            {
                while (state.AtomicProcessCount > 0 || state.UpdateBuckets.Count != 0)
                {
                    await Task.Delay(250, ct);

                    Progress.Progress       = state.AtomicCompletedCount / (double)toUpdate.Length;
                    Progress.CompletedCount = state.AtomicCompletedCount;

                    if (state.UpdateBuckets.Count == 0)
                    {
                        continue;
                    }

                    while (state.UpdateBuckets.TryDequeue(out var list))
                    {
                        if (list.Count > 0)
                        {
                            await patchCache.InsertUnderTransactionAsync(list);
                        }
                    }
                }
            });

            Progress.IsIndeterminate = true;

            App.Logger.Info(nameof(VerifyFilesPhase), "Joining processing threads");
            try
            {
                await Task.WhenAll(threadTasks);
            }
            catch (Exception ex)
            {
                App.Logger.Error(nameof(VerifyFilesPhase), "Error verifying files", ex);
                throw;
            }
        }
Example #4
0
 public DiscordManager()
 {
     _DisposedTokenSource = new CancellationTokenSource();
     App.Logger.Info(nameof(DiscordManager), "Discord manager created");
     App.Current.Dispatcher.Invoke(async() => await ConcurrencyUtils.RunOnDedicatedThreadAsync(_BackgroundLoop));
 }
Example #5
0
 public static void MutexTestReleaser(ConcurrencyUtils.Mutex mutex)
 {
     while (true)
     {
         Console.WriteLine("\t\t\t" + Thread.CurrentThread.Name + ": Releasing mutex token");
         mutex.Release();
         Thread.Sleep(6000);
     }
 }
Example #6
0
 public static void latchAcquire(ConcurrencyUtils.Latch latch)
 {
     Console.WriteLine(Thread.CurrentThread.Name + "trying to acquire");
     latch.Acquire();
     Console.WriteLine(Thread.CurrentThread.Name + "got through the latch");
 }
Example #7
0
 public static void writeLots(ConcurrencyUtils.ReaderWriterLock RWLock)
 {
     while (true)
     {
         Console.WriteLine(Thread.CurrentThread.Name + " waiting to write");
         Thread.Sleep(500);
         RWLock.WriterAcquire();
         Console.WriteLine(Thread.CurrentThread.Name + " write successful");
         RWLock.WriterRelease();
         Thread.Sleep(1000);
     }
 }
Example #8
0
        public static void SemaphoreTestReleaser(ConcurrencyUtils.Semaphore testSemaphore)
        {
            while (true)
            {
                Console.WriteLine("\t\t\t" + Thread.CurrentThread.Name + ": Releasing 3 tokens");
                testSemaphore.Release(3);
                // Console.ForegroundColor = ConsoleColor.White;

                Thread.Sleep(6000);
            }
        }
Example #9
0
 public static void SemaphoreTest(ConcurrencyUtils.Semaphore testSemaphore)
 {
     while (true)
     {
         // Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine(Thread.CurrentThread.Name + ": Trying to Acquire...");
         testSemaphore.Acquire();
         // Console.ForegroundColor = ConsoleColor.Green;
         Console.WriteLine(Thread.CurrentThread.Name + ": acquired token!");
         Thread.Sleep(DELAY_SECONDS * 1000);
     }
 }