public void TestSynchronizeTwoParallelTasks() { this.TestWithError(async() => { SharedEntry entry = new SharedEntry(); AsyncLock mutex = AsyncLock.Create(); async Task WriteAsync(int value) { using (await mutex.AcquireAsync()) { entry.Value = value; } } Task task1 = Task.Run(async() => { await WriteAsync(3); }); Task task2 = Task.Run(async() => { await WriteAsync(5); }); await Task.WhenAll(task1, task2); AssertSharedEntryValue(entry, 5); }, configuration: this.GetConfiguration().WithTestingIterations(200), expectedError: "Value is 3 instead of 5.", replay: true); }
public async Task TestRun() { Task[] ids = new Task[2]; this.mLock = AsyncLock.Create(); ids[0] = Task.Run(async() => { for (int k = 0; k < this.num; k++) { Task.ExploreContextSwitch(); using (await this.mLock.AcquireAsync()) { this.i = this.j + 1; } } }); ids[1] = Task.Run(async() => { for (int k = 0; k < this.num; k++) { Task.ExploreContextSwitch(); using (await this.mLock.AcquireAsync()) { this.j = this.i + 1; } } }); int temp_i, temp_j; Task.ExploreContextSwitch(); using (await this.mLock.AcquireAsync()) { temp_i = this.i; } bool condI = temp_i >= this.limit; Task.ExploreContextSwitch(); using (await this.mLock.AcquireAsync()) { temp_j = this.j; } bool condJ = temp_j >= this.limit; if (condI || condJ) { Specification.Assert(false, "<Triangular-2> Bug found!"); } await Task.WhenAll(ids); }
public CoyoteAsyncQueue(int capacity, InterestingEvents logger) { _logger = logger; _buffer = new Queue <T>(); _mutex = AsyncLock.Create(); _consumerSemaphore = Semaphore.Create(0, capacity); _producerSemaphore = Semaphore.Create(capacity, capacity); }
public async SystemTasks.Task TestAcquireRelease() { AsyncLock mutex = AsyncLock.Create(); Assert.True(!mutex.IsAcquired); var releaser = await mutex.AcquireAsync(); Assert.True(mutex.IsAcquired); releaser.Dispose(); Assert.True(!mutex.IsAcquired); }
public void TestAcquireRelease() { this.Test(async() => { AsyncLock mutex = AsyncLock.Create(); Specification.Assert(!mutex.IsAcquired, "Mutex is acquired."); var releaser = await mutex.AcquireAsync(); Specification.Assert(mutex.IsAcquired, "Mutex is not acquired."); releaser.Dispose(); Specification.Assert(!mutex.IsAcquired, "Mutex is acquired."); }); }
public WebSocketClient(string serverUri) : base() { this.serverUri = serverUri; this.socket = new ClientWebSocket(); this.sendLock = AsyncLock.Create("WebSocketClient"); // this.sendLock = new object(); this.receiveLock = new object(); this.ReadyFlag = this.socket.ConnectAsync(new Uri(this.serverUri), CancellationToken.None) .ContinueWith(prev => { Listen(); }); }
public void TestAcquireTwice() { this.TestWithError(async() => { AsyncLock mutex = AsyncLock.Create(); await mutex.AcquireAsync(); await Task.Run(async() => { await mutex.AcquireAsync(); }); }, expectedError: "Deadlock detected. Task() and Task() are waiting for a task to complete, but no other " + "controlled tasks are enabled. Task() is waiting to acquire a resource that is already " + "acquired, but no other controlled tasks are enabled.", replay: true); }
public async Task TestRun() { this.mlock = AsyncLock.Create(); Task[] ids = new Task[2]; ids[0] = Task.Run(async() => { for (int k = 0; k < this.num; k++) { Task.ExploreContextSwitch(); using (await this.mlock.AcquireAsync()) { this.i += this.j; } } }); ids[1] = Task.Run(async() => { for (int k = 0; k < this.num; k++) { Task.ExploreContextSwitch(); using (await this.mlock.AcquireAsync()) { this.j += this.i; } } }); await Task.WhenAll(ids); if (this.num == 11 && (this.i >= 46368 || this.j >= 46368)) { Specification.Assert(false, "<Fib_Bench_Larger> Bug found!"); } if (this.num == 5 && (this.i >= 144 || this.j >= 144)) { Specification.Assert(false, "<Fib_Bench> Bug found!"); } }
public Stack(int pushCount) { this.Array = new SafeStackItem[pushCount]; this.Head = 0; this.Count = pushCount; for (int i = 0; i < pushCount - 1; i++) { this.Array[i].Next = i + 1; } this.Array[pushCount - 1].Next = -1; this.ArrayLock = AsyncLock.Create(); this.HeadLock = AsyncLock.Create(); this.CountLock = AsyncLock.Create(); Runtime.Monitor <StateMonitor>(new StateMonitor.UpdateStateEvent(this.Array)); }
public async SystemTasks.Task TestSynchronizeTwoAsynchronousTasks() { SharedEntry entry = new SharedEntry(); AsyncLock mutex = AsyncLock.Create(); async Task WriteAsync(int value) { using (await mutex.AcquireAsync()) { entry.Value = value; } } Task task1 = WriteAsync(3); Task task2 = WriteAsync(5); await Task.WhenAll(task1, task2); Assert.True(task1.IsCompleted && task2.IsCompleted && !mutex.IsAcquired); Assert.True(entry.Value == 5, $"Value is '{entry.Value}' instead of 5."); }
public void TestSynchronizeTwoAsynchronousTasks() { this.Test(async() => { SharedEntry entry = new SharedEntry(); AsyncLock mutex = AsyncLock.Create(); async Task WriteAsync(int value) { using (await mutex.AcquireAsync()) { entry.Value = value; } } Task task1 = WriteAsync(3); Task task2 = WriteAsync(5); await Task.WhenAll(task1, task2); AssertSharedEntryValue(entry, 5); }, configuration: this.GetConfiguration().WithTestingIterations(200)); }
public void TestAcquireTwice() { if (!this.SystematicTest) { // .NET cannot detect these deadlocks. return; } this.TestWithError(async() => { AsyncLock mutex = AsyncLock.Create(); await mutex.AcquireAsync(); await Task.Run(async() => { await mutex.AcquireAsync(); }); }, expectedError: "Deadlock detected. Task() is waiting for a task to complete, but no other " + "controlled tasks are enabled. Task() is waiting to acquire a resource that is already " + "acquired, but no other controlled tasks are enabled.", replay: true); }
public void TestSynchronizeTwoAsynchronousTasksWithYield() { this.Test(async() => { AsyncLock mutex = AsyncLock.Create(); SharedEntry entry = new SharedEntry(); async Task WriteAsync(int value) { using (await mutex.AcquireAsync()) { entry.Value = value; await Task.Yield(); Specification.Assert(entry.Value == value, "Value is '{0}' instead of '{1}'.", entry.Value, value); } } Task task1 = WriteAsync(3); Task task2 = WriteAsync(5); await Task.WhenAll(task1, task2); }, configuration: this.GetConfiguration().WithTestingIterations(200)); }
public CustomAsyncLock() { this.asyncLock = AsyncLock.Create(); }
public WebSocketClientHandle(WebSocketServer server, WebSocket socket) { this.id = Helpers.RandomString(16); this.socket = socket; this.sendLock = AsyncLock.Create("WebSocketClientHandle-" + this.id); this.onClose = () => { }; var socketDestroyer = new CancellationTokenSource(); Console.WriteLine(" --> New WebSocketClient {0}", this.id); byte[] buffer = new byte[65536]; // 8 KB buffer Helpers.AsyncTaskLoop(() => { if (socket.State == WebSocketState.Open) { return(socket.ReceiveAsync(new ArraySegment <byte>(buffer), socketDestroyer.Token) .ContinueWith(prev => { try { var received = prev.Result; // Console.WriteLine("WebSocket {0}: {1} {2} {3}", this.id, received.Count, received.MessageType, received.EndOfMessage); if (prev.Result.MessageType == WebSocketMessageType.Close) { Console.WriteLine(" !!! WebSocket Client {0} dropped connection", this.id); Console.WriteLine(" !!! Closing WebSocket {0}", this.id); socketDestroyer.Cancel(); // return Task.CompletedTask; return socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Close OK", CancellationToken.None) .ContinueWith(_ => { this.onClose(); this.socket.Dispose(); }); } else { string message = Encoding.UTF8.GetString(buffer, 0, prev.Result.Count); // Console.WriteLine(" !!! Message from WebSocket {0}: {1}", this.id, message); // Spawning a new task to make the message handler "non-blocking" // TODO: Errors thrown inside here will become silent, so that needs to be handled // Also, now that the single execution flow is broken, the requests are under race conditions return Task.Run(() => this.onMessage(message)); } } catch (AggregateException ae) { Console.WriteLine(" !!! Exception during async communication with Client {0}", this.id); Console.WriteLine(" ... Connection closed... if this was not expected, inspect the AggregateException here"); foreach (var ie in ae.Flatten().InnerExceptions) { Console.WriteLine("Exception -------------------"); Console.WriteLine(ie.Message); Console.WriteLine(ie.InnerException.Message); Console.WriteLine(ie.InnerException.StackTrace); Console.WriteLine("-----------------------------\n"); } socketDestroyer.Cancel(); this.onClose(); this.socket.Dispose(); return Task.CompletedTask; } }).Unwrap()); } else { Console.WriteLine(" !!! WebSocket {0} Connection Dropped!", this.id); socketDestroyer.Cancel(); this.onClose(); this.socket.Dispose(); return(Task.CompletedTask); } }, socketDestroyer.Token); }