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 DoLock()
 {
     using (var monitor = SynchronizedBlock.Lock(this.SyncObject))
     {
         Debug.WriteLine("Re-entered lock from the same task {0}.", GetCurrentTaskId());
     }
 }
 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.");
         }
     }
 }
示例#8
0
        /// <summary>
        /// Releases the lock on an object and blocks the current thread until it reacquires the lock.
        /// If the specified time-out interval elapses, the thread enters the ready queue.
        /// </summary>
        public static bool Wait(object obj, TimeSpan timeout)
        {
            var runtime = CoyoteRuntime.Current;

            if (runtime.SchedulingPolicy is SchedulingPolicy.Systematic)
            {
                var block = SynchronizedBlock.Find(obj) ??
                            throw new SystemThreading.SynchronizationLockException();
                return(block.Wait(timeout));
            }

            return(SystemThreading.Monitor.Wait(obj, timeout));
        }
示例#9
0
        /// <summary>
        /// Determines whether the current thread holds the lock on the specified object.
        /// </summary>
        public static bool IsEntered(object obj)
        {
            var runtime = CoyoteRuntime.Current;

            if (runtime.SchedulingPolicy is SchedulingPolicy.Interleaving)
            {
                var block = SynchronizedBlock.Find(obj) ??
                            throw new SystemThreading.SynchronizationLockException();
                return(block.IsEntered());
            }

            return(SystemThreading.Monitor.IsEntered(obj));
        }
示例#10
0
        /// <summary>
        /// Releases the lock on an object and blocks the current thread until it reacquires the lock.
        /// If the specified time-out interval elapses, the thread enters the ready queue.
        /// </summary>
        public static bool Wait(object obj, int millisecondsTimeout)
        {
            var runtime = CoyoteRuntime.Current;

            if (runtime.SchedulingPolicy is SchedulingPolicy.Interleaving)
            {
                var block = SynchronizedBlock.Find(obj) ??
                            throw new SystemThreading.SynchronizationLockException();
                return(block.Wait(millisecondsTimeout));
            }

            return(SystemThreading.Monitor.Wait(obj, millisecondsTimeout));
        }
示例#11
0
        public void TestSynchronizedBlockWithInvalidPulseAllState()
        {
            this.TestWithException <SynchronizationLockException>(() =>
            {
                object syncObject = new object();
                SynchronizedBlock monitor;
                using (monitor = SynchronizedBlock.Lock(syncObject))
                {
                }

                monitor.PulseAll();
            },
                                                                  replay: true);
        }
示例#12
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));
        }
示例#13
0
        /// <summary>
        /// Releases the lock on an object and blocks the current thread until it reacquires the lock.
        /// If the specified time-out interval elapses, the thread enters the ready queue. Optionally
        /// exits the synchronization domain for the synchronized context before the wait and reacquires
        /// the domain afterward.
        /// </summary>
        public static bool Wait(object obj, TimeSpan timeout, bool exitContext)
        {
            var runtime = CoyoteRuntime.Current;

            if (runtime.SchedulingPolicy is SchedulingPolicy.Interleaving)
            {
                var block = SynchronizedBlock.Find(obj) ??
                            throw new SystemThreading.SynchronizationLockException();

                // TODO: implement exitContext.
                return(block.Wait(timeout));
            }

            return(SystemThreading.Monitor.Wait(obj, timeout, exitContext));
        }
示例#14
0
        /// <summary>
        /// Releases an exclusive lock on the specified object.
        /// </summary>
        public static void Exit(object obj)
        {
            var runtime = CoyoteRuntime.Current;

            if (runtime.SchedulingPolicy is SchedulingPolicy.Interleaving)
            {
                var block = SynchronizedBlock.Find(obj) ??
                            throw new SystemThreading.SynchronizationLockException();
                block.Exit();
            }
            else
            {
                SystemThreading.Monitor.Exit(obj);
            }
        }
示例#15
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));
        }
示例#16
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);
            }
        }
示例#17
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);
            }
        }
示例#18
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);
            }
        }
示例#19
0
        public override void Render()
        {
            /*EditorGUIUtility.labelWidth = Screen.width / 2;
            *  EditorGUIUtility.fieldWidth = Screen.width / 2;*/

            _horizontalAlign.enumValueIndex = (int)(HorizontalAlign)EditorGUILayout.EnumPopup(
                "Horizontal align",
                (HorizontalAlign)Enum.GetValues(typeof(HorizontalAlign)).GetValue(_horizontalAlign.enumValueIndex),
                GUILayout.ExpandWidth(false),
                GUILayout.Width(300)
                );

            _verticalAlign.enumValueIndex = (int)(VerticalAlign)EditorGUILayout.EnumPopup(
                "Vertical align",
                (VerticalAlign)Enum.GetValues(typeof(VerticalAlign)).GetValue(_verticalAlign.enumValueIndex),
                GUILayout.ExpandWidth(false),
                GUILayout.Width(300)
                );

            _gap.intValue = EditorGUILayout.IntField("Gap", _gap.intValue, GUILayout.ExpandWidth(false));

            GUILayout.Space(15);

            /*_paddingLeft.intValue = EditorGUILayout.IntField("Padding left", _paddingLeft.intValue);
             * _paddingRight.intValue = EditorGUILayout.IntField("Padding right", _paddingRight.intValue);
             * _paddingTop.intValue = EditorGUILayout.IntField("Padding top", _paddingTop.intValue);
             * _paddingBottom.intValue = EditorGUILayout.IntField("Padding bottom", _paddingBottom.intValue);*/

            _prevPaddingLeft   = _paddingLeft.intValue;
            _prevPaddingRight  = _paddingRight.intValue;
            _prevPaddingTop    = _paddingTop.intValue;
            _prevPaddingBottom = _paddingBottom.intValue;

            EditorSettings.PaddingExpanded = EditorGUILayout.Foldout(EditorSettings.PaddingExpanded, "Padding");
            if (EditorSettings.PaddingExpanded)
            {
                SynchronizedBlock.Render(_syncPadding, _paddingLeft, _paddingRight, _paddingTop, _paddingBottom,
                                         ref _prevPaddingLeft, ref _prevPaddingRight, ref _prevPaddingTop, ref _prevPaddingBottom);
            }
        }
示例#20
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();
                }
            }
        }
示例#21
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);
        }