Ejemplo n.º 1
0
 internal void DoLock()
 {
     using (var monitor = SynchronizedBlock.Lock(this.SyncObject))
     {
         Debug.WriteLine("Re-entered lock from the same task {0}.", GetCurrentTaskId());
     }
 }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 internal void Signal()
 {
     using (var monitor = SynchronizedBlock.Lock(this.SyncObject))
     {
         this.Signalled = true;
         monitor.Pulse();
     }
 }
Ejemplo n.º 4
0
 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();
     }
 }
Ejemplo n.º 5
0
 public void TestSynchronizedBlockWithInvalidSyncObject()
 {
     this.TestWithException <ArgumentNullException>(() =>
     {
         using (var monitor = SynchronizedBlock.Lock(null))
         {
         }
     },
                                                    replay: true);
 }
Ejemplo n.º 6
0
 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());
     }
 }
Ejemplo n.º 7
0
 internal void Wait()
 {
     using (var monitor = SynchronizedBlock.Lock(this.SyncObject))
     {
         while (!this.Signalled)
         {
             bool result = monitor.Wait();
             Assert.True(result, "Wait returned false.");
         }
     }
 }
Ejemplo n.º 8
0
        public void TestSynchronizedBlockWithInvalidPulseAllState()
        {
            this.TestWithException <SynchronizationLockException>(() =>
            {
                object syncObject = new object();
                SynchronizedBlock monitor;
                using (monitor = SynchronizedBlock.Lock(syncObject))
                {
                }

                monitor.PulseAll();
            },
                                                                  replay: true);
        }
Ejemplo n.º 9
0
        /// <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));
        }
Ejemplo n.º 10
0
        /// <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));
        }
Ejemplo n.º 11
0
        /// <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);
            }
        }
Ejemplo n.º 12
0
        /// <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);
            }
        }
Ejemplo n.º 13
0
        /// <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);
            }
        }
Ejemplo n.º 14
0
        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();
                }
            }
        }
Ejemplo n.º 15
0
        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);
        }