internal void DoLock() { using (var monitor = SynchronizedBlock.Lock(this.SyncObject)) { Debug.WriteLine("Re-entered lock from the same task {0}.", GetCurrentTaskId()); } }
public object Take() { object result = null; using (var monitor = SynchronizedBlock.Lock(this.SyncObject)) { while (this.Occupied == 0) { monitor.Wait(); } --this.Occupied; this.TakeAt %= this.Buffer.Length; result = this.Buffer[this.TakeAt++]; if (PulseAll) { monitor.PulseAll(); } else { monitor.Pulse(); } } return(result); }
internal void Signal() { using (var monitor = SynchronizedBlock.Lock(this.SyncObject)) { this.Signalled = true; monitor.Pulse(); } }
internal void ReentrantWait() { Debug.WriteLine("Entering lock on task {0}.", GetCurrentTaskId()); using (var monitor = SynchronizedBlock.Lock(this.SyncObject)) { Debug.WriteLine("Entered lock on task {0}.", GetCurrentTaskId()); this.DoWait(); } }
public void TestSynchronizedBlockWithInvalidSyncObject() { this.TestWithException <ArgumentNullException>(() => { using (var monitor = SynchronizedBlock.Lock(null)) { } }, replay: true); }
internal void DoWait() { using (var monitor = SynchronizedBlock.Lock(this.SyncObject)) { Debug.WriteLine("Re-entered lock from the same task {0}.", GetCurrentTaskId()); Debug.WriteLine("Task {0} is now waiting...", GetCurrentTaskId()); this.Wait(); Debug.WriteLine("Task {0} received the signal.", GetCurrentTaskId()); } }
internal void Wait() { using (var monitor = SynchronizedBlock.Lock(this.SyncObject)) { while (!this.Signalled) { bool result = monitor.Wait(); Assert.True(result, "Wait returned false."); } } }
public void TestSynchronizedBlockWithInvalidPulseAllState() { this.TestWithException <SynchronizationLockException>(() => { object syncObject = new object(); SynchronizedBlock monitor; using (monitor = SynchronizedBlock.Lock(syncObject)) { } monitor.PulseAll(); }, replay: true); }
/// <summary> /// Attempts to acquire an exclusive lock on the specified object. /// </summary> public static bool TryEnter(object obj) { var runtime = CoyoteRuntime.Current; if (runtime.SchedulingPolicy is SchedulingPolicy.Interleaving) { return(SynchronizedBlock.Lock(obj).IsLockTaken); } else if (runtime.SchedulingPolicy is SchedulingPolicy.Fuzzing) { runtime.DelayOperation(); } return(SystemThreading.Monitor.TryEnter(obj)); }
/// <summary> /// Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object, /// and atomically sets a value that indicates whether the lock was taken. /// </summary> public static bool TryEnter(object obj, TimeSpan timeout) { var runtime = CoyoteRuntime.Current; if (runtime.SchedulingPolicy is SchedulingPolicy.Interleaving) { // TODO: how to implement this timeout? return(SynchronizedBlock.Lock(obj).IsLockTaken); } else if (runtime.SchedulingPolicy is SchedulingPolicy.Fuzzing) { runtime.DelayOperation(); } return(SystemThreading.Monitor.TryEnter(obj, timeout)); }
/// <summary> /// Acquires an exclusive lock on the specified object. /// </summary> public static void Enter(object obj, ref bool lockTaken) { var runtime = CoyoteRuntime.Current; if (runtime.SchedulingPolicy is SchedulingPolicy.Interleaving) { lockTaken = SynchronizedBlock.Lock(obj).IsLockTaken; } else { if (runtime.SchedulingPolicy is SchedulingPolicy.Fuzzing) { runtime.DelayOperation(); } SystemThreading.Monitor.Enter(obj, ref lockTaken); } }
/// <summary> /// Acquires an exclusive lock on the specified object. /// </summary> public static void Enter(object obj) { var runtime = CoyoteRuntime.Current; if (runtime.SchedulingPolicy is SchedulingPolicy.Systematic) { SynchronizedBlock.Lock(obj); } else { if (runtime.SchedulingPolicy is SchedulingPolicy.Fuzzing) { runtime.DelayOperation(); } SystemThreading.Monitor.Enter(obj); } }
/// <summary> /// Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified object, /// and atomically sets a value that indicates whether the lock was taken. /// </summary> public static void TryEnter(object obj, int millisecondsTimeout, ref bool lockTaken) { var runtime = CoyoteRuntime.Current; if (runtime.SchedulingPolicy is SchedulingPolicy.Interleaving) { // TODO: how to implement this timeout? lockTaken = SynchronizedBlock.Lock(obj).IsLockTaken; } else { if (runtime.SchedulingPolicy is SchedulingPolicy.Fuzzing) { runtime.DelayOperation(); } SystemThreading.Monitor.TryEnter(obj, millisecondsTimeout, ref lockTaken); } }
public void Put(object x) { using (var monitor = SynchronizedBlock.Lock(this.SyncObject)) { while (this.Occupied == this.Buffer.Length) { monitor.Wait(); } ++this.Occupied; this.PutAt %= this.Buffer.Length; this.Buffer[this.PutAt++] = x; if (PulseAll) { monitor.PulseAll(); } else { monitor.Pulse(); } } }
public void TestSynchronizedBlockWithInvalidUsage() { if (!this.SystematicTest) { // bugbug: don't know why but the build machines are hanging on this test, but we cannot // reproduce this hang locally. return; } this.TestWithError(async() => { try { object syncObject = new object(); using (var monitor = SynchronizedBlock.Lock(syncObject)) { var t1 = Task.Run(monitor.Wait); var t2 = Task.Run(monitor.Pulse); var t3 = Task.Run(monitor.PulseAll); await Task.WhenAll(t1, t2, t3); } } catch (Exception ex) { while (ex.InnerException != null) { ex = ex.InnerException; } if (ex is SynchronizationLockException) { Specification.Assert(false, "Expected exception thrown."); } } }, expectedError: "Expected exception thrown.", replay: true); }