public Task ShutdownAsync() { if (_timer != null) { // .NET-specific ExecutorService.Cancel(_timer); } var tcsShutdown = new TaskCompletionSource <object>(); lock (_lock) { _shutdown = true; int max = _runningTasks.Count; if (max == 0) { tcsShutdown.SetResult(null); return(tcsShutdown.Task); } var counter = new VolatileInteger(0); foreach (var task in _runningTasks.Keys) { task.ContinueWith(t => { if (counter.IncrementAndGet() == max) { tcsShutdown.SetResult(null); // complete } }); } } return(tcsShutdown.Task); }
internal VolatileLoop(long iters, VolatileInteger obj, CyclicBarrier b) { this.iters = iters; this.obj = obj; this.barrier = b; obj.set_Renamed(CASLoops.rng.next()); }
/// <summary> /// Shuts down all the channel creators. /// </summary> /// <returns></returns> public Task ShutdownAsync() { _readWriteLock.EnterWriteLock(); try { if (_shutdown) { _tcsReservationDone.SetException(new TaskFailedException("Already shutting down.")); return(_tcsReservationDone.Task); } _shutdown = true; } finally { _readWriteLock.ExitWriteLock(); } // Fast shutdown for those that are in the queue is not required. // Let the executor finish since the shutdown-flag is set and the // future will be set as well to "shutting down". // .NET-specific: queued tasks cannot be aborted, but the flag indicates // the shutdown and the result will be set to failed // the channel creator doesn't change anymore from here on int size = _channelCreators.Count; if (size == 0) { _tcsReservationDone.SetResult(null); } else { var completeCounter = new VolatileInteger(0); foreach (var channelCreator in _channelCreators) { // It's important to set the listener before calling shutdown. channelCreator.ShutdownTask.ContinueWith(delegate { if (completeCounter.IncrementAndGet() == size) { // we can block here _semaphoreUdp.Acquire(_maxPermitsUdp); _semaphoreTcp.Acquire(_maxPermitsTcp); _semaphorePermanentTcp.Acquire(_maxPermitsPermanentTcp); _tcsReservationDone.SetResult(null); } }); channelCreator.ShutdownAsync(); } } return(_tcsReservationDone.Task); }
internal static long runVolatile(int n, long iters) { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier b = new CyclicBarrier(n + 1, timer); VolatileInteger a = new VolatileInteger(); for (int j = 0; j < n; ++j) { new SupportClass.ThreadClass(new System.Threading.ThreadStart(new VolatileLoop(iters, a, b).Run)).Start(); } b.Await(); b.Await(); if (sum.Value == 0) { System.Console.Out.Write(" "); } return(timer.Time); }
/// <summary> /// Adds a listener to the response tasks and releases all acquired channels in the channel creator. /// </summary> /// <param name="channelCreator">The channel creator that will be shutdown and all connections will be closed.</param> /// <param name="tasks">The tasks to listen to. If all the tasks finished, then the channel creator is shut down. /// If null is provided, then the channel crator is shut down immediately.</param> /// <returns>The task that completes when all listeners have completed.</returns> public static Task AddReleaseListener(ChannelCreator channelCreator, params Task[] tasks) { if (tasks == null) { return(channelCreator.ShutdownAsync()); } var continuationTasks = new List <Task>(tasks.Length); int count = tasks.Count(); var finished = new VolatileInteger(0); foreach (var task in tasks) { var contTask = task.ContinueWith(delegate { if (finished.IncrementAndGet() == count) { channelCreator.ShutdownAsync(); } }); continuationTasks.Add(contTask); } return(Task.WhenAll(continuationTasks)); }
internal VolatileLoop(long iters, VolatileInteger obj, CyclicBarrier b) { this.iters = iters; this.obj = obj; this.barrier = b; obj.set_Renamed(CASLoops.rng.next()); }
internal static long runVolatile(int n, long iters) { LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); CyclicBarrier b = new CyclicBarrier(n + 1, timer); VolatileInteger a = new VolatileInteger(); for (int j = 0; j < n; ++j) new SupportClass.ThreadClass(new System.Threading.ThreadStart(new VolatileLoop(iters, a, b).Run)).Start(); b.Await(); b.Await(); if (sum.Value == 0) System.Console.Out.Write(" "); return timer.Time; }