Ejemplo n.º 1
0
        /// <summary>
        /// Acquires the reader
        ///     If there is no convention the readers will just continue reading for as
        ///     long as they need to
        /// </summary>
        public void AcquireReader()
        {
            // ---------------------- #1 TS -------------------------------------------------------
            // Should be signed if there there is something waiting and should be released when the last
            //  thread has finished with the LS
            readerTS.Acquire();
            readerTS.Release();

            lock (lockObject) {
                ++readersWaiting;                       // Used to figure out when the last reader has finished with the LS
            }
            // -------------------------------------------------------------------------------------


            LS.Acquire();                                               // << ADDS READER TO LS

            lock (lockObject) {
                --readersWaiting;

                // If readers waiting then pulse
                if (readersWaiting != 0)
                {
                    Monitor.Pulse(lockObject);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 绘制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MonitorUserControl_Paint(object sender, PaintEventArgs e)
        {
            if (_objRef == null)
            {
                return;
            }

            if (SM.TryEnter(_objRef))
            {
                try
                {
                    if (_bitmap == null)
                    {
                        return;
                    }

                    Point point = new Point(AutoScrollPosition.X, AutoScrollPosition.Y);
                    e.Graphics.DrawImage(_bitmap, point);
                }
                finally
                {
                    SM.Exit(_objRef);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 屏幕
        /// </summary>
        public void UpdateDisplay()
        {
            if (_objRef == null)
            {
                return;
            }

            if (SM.TryEnter(_objRef))
            {
                try
                {
                    byte[] bitmapBytes = _objRef.GetDesktopBitmapBytes();
                    if (bitmapBytes.IsNullOrEmpty())
                    {
                        return;
                    }

                    var stream = new MemoryStream(bitmapBytes, false);
                    _bitmap = (Bitmap)Image.FromStream(stream);
                    Point point = new Point(AutoScrollPosition.X, AutoScrollPosition.Y);
                    CreateGraphics().DrawImage(_bitmap, point);
                }
                finally
                {
                    SM.Exit(_objRef);
                }
            }
        }
Ejemplo n.º 4
0
 private void PutTask()
 {
     lock (this.SyncObject1)
     {
         this.Value = "put";
         Monitor.Pulse(this.SyncObject1);
     }
 }
Ejemplo n.º 5
0
        private void TakeTask()
        {
            lock (this.SyncObject1)
            {
                if (this.Value != "put")
                {
                    Monitor.Wait(this.SyncObject1);
                }

                this.Value = "taken";
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Releases the writer, another writer my continue or all the readers may start reading
        /// </summary>
        public void ReleaseWriter()
        {
            writePermission.Release();             // >> REMOVES WRITER FROM WRITE QUEUE

            lock (lockObject) {
                if (readersWaiting > 0)
                {
                    Monitor.Wait(lockObject);
                }
            }

            writerTS.Release();
        }
Ejemplo n.º 7
0
 public Random GetNamedRandom(string name, long seed)
 {
     try {
         ThreadMonitor.Enter(LockNamedDict);
         if (!NamedPRNGs.ContainsKey(name))
         {
             NamedPRNGs.Add(name, GetNewRandom(seed));
         }
     }
     finally {
         ThreadMonitor.Exit(LockNamedDict);
     }
     return(NamedPRNGs[name]);
 }
Ejemplo n.º 8
0
        public void TestMonitorWithLockTaken()
        {
            this.Test(() =>
            {
                object obj     = new object();
                bool lockTaken = false;
                Monitor.TryEnter(obj, ref lockTaken);
                if (lockTaken)
                {
                    Monitor.Exit(obj);
                }

                Specification.Assert(lockTaken, "lockTaken is false");
            },
                      GetConfiguration());
        }
Ejemplo n.º 9
0
            private bool RemoveCore(WaitNode node)
            {
                Debug.Assert(Monitor.IsEntered(this));

                var inList = false;

                if (ReferenceEquals(first, node))
                {
                    first  = node.Next;
                    inList = true;
                }

                if (ReferenceEquals(last, node))
                {
                    last   = node.Previous;
                    inList = true;
                }

                inList |= node.IsNotRoot;
                node.Detach();
                return(inList);
            }
Ejemplo n.º 10
0
 public void RequestExitAndWait()
 {
     // don't call this from GLThread thread or it is a guaranteed
     // deadlock!
     //synchronized (this) {
     lock (locker)
     {
         this.mDone = true;
         //this.notify();
         Monitor.Pulse(locker);
     }
     try
     {
         //this.join();
         //Thread.CurrentThread().Join();
         System.Threading.Monitor.Enter(locker);
     }
     catch (InterruptedException ex)
     {
         Java.Lang.Thread.CurrentThread().Interrupt();
     }
     // TODO: Added this call ... check it
     finally { System.Threading.Monitor.Exit(locker); }
 }
 public static void Exit(object obj)
 {
     SMonitor.Exit(obj);
 }
 public static bool TryEnter(object obj)
 {
     return(SMonitor.TryEnter(obj));
 }
 public static bool Wait(object obj, int timeout)
 {
     return(SMonitor.Wait(obj, timeout));
 }
 public static void Wait(object obj)
 {
     SMonitor.Wait(obj);
 }
 public static void PulseAll(object obj)
 {
     SMonitor.PulseAll(obj);
 }
 public static void Enter(object obj)
 {
     SMonitor.Enter(obj);
 }
 public static bool TryEnter(object obj, TimeSpan timeout)
 {
     return(SMonitor.TryEnter(obj, timeout));
 }