コード例 #1
0
 public T Get(int timeout)
 {
     lock (monitor) {
         if (value != null)
         {
             return(value);
         }
         if (timeout == 0)
         {
             return(null);
         }
         do
         {
             int refTime = Environment.TickCount;
             Monitor.Wait(monitor, timeout);
             if (value != null)
             {
                 return(value);
             }
             timeout = SynchUtils.RemainingTimeout(refTime, timeout);
             if (timeout == 0)
             {
                 return(null);
             }
         }while (true);
     }
 }
コード例 #2
0
ファイル: TransferQueue.cs プロジェクト: RubenSantos/RubenPC
 public bool Take(int timeout, out T rmsg)
 {
     lock (monitor)
     {
         LinkedListNode <T> linkedListNode;
         if (queue.Count > 0)
         {
             linkedListNode = queue.First;
             rmsg           = linkedListNode.Value;
             queue.RemoveFirst();
             monitor.Signal(linkedListNode);
             return(true);
         }
         do
         {
             if (!monitor.Await(queue, timeout))
             {
                 rmsg = default(T);
                 return(false);
             }
             timeout = SynchUtils.RemainingTimeout(Environment.TickCount, timeout);
         } while (queue.Count == 0);
         linkedListNode = queue.First;
         rmsg           = linkedListNode.Value;
         queue.RemoveFirst();
         monitor.Signal(linkedListNode);
         return(true);
     }
 }
コード例 #3
0
ファイル: TransferQueue.cs プロジェクト: RubenSantos/RubenPC
 public bool Transfer(T msg, int timeout)
 {
     lock (monitor)
     {
         LinkedListNode <T> listNode = queue.AddLast(msg);
         monitor.Signal(queue);
         try
         {
             do
             {
                 if (!monitor.Await(listNode, timeout) && queue.Contains(msg))
                 {
                     queue.Remove(msg);
                     return(false);
                 }
                 timeout = SynchUtils.RemainingTimeout(Environment.TickCount, timeout);
             } while (queue.Contains(msg));
         }
         catch (ThreadInterruptedException)
         {
             if (queue.Contains(msg))
             {
                 queue.Remove(msg);
                 throw;
             }
             Thread.CurrentThread.Interrupt();
             return(true);
         }
         return(true);
     }
 }
コード例 #4
0
        public bool  Wait(int timeout)   //  throws InterruptedException
        {
            lock (monitor) {
                // fast path

                // note that is signaled the list must be empty
                if (signaled)
                {
                    signaled = false;
                    return(true);
                }
                if (timeout == 0)
                {
                    return(false);
                }

                var wNode = waiters.AddLast(false);
                do
                {
                    try {
                        int refTime = Environment.TickCount;
                        Monitor.Wait(monitor, timeout);
                        if (wNode.Value == true)
                        {
                            return(true);
                        }
                        timeout = SynchUtils.RemainingTimeout(refTime, timeout);
                        if (timeout == 0)
                        {
                            // abort wait
                            waiters.Remove(wNode);
                            return(false);
                        }
                    }
                    catch (ThreadInterruptedException e) {
                        if (wNode.Value == true)
                        {
                            // delay interruption and return success
                            Thread.CurrentThread.Interrupt();
                            return(true);
                        }
                        // abort wait
                        waiters.Remove(wNode);
                        throw e;
                    }
                } while (true);
            }
        }
コード例 #5
0
 public T Exchange(T mine, int timeout)   // throws TimeoutException, InterruptedException
 {
     lock (monitor) {
         T             other;
         ExchangePoint current = exchanger;
         if ((other = current.pairing(mine)) != null)
         {
             exchanger = new ExchangePoint();
             Monitor.PulseAll(monitor);
             return(other);
         }
         if (timeout == 0)
         {
             current.restart();
             throw new TimeoutException();
         }
         do
         {
             try {
                 int refTime = Environment.TickCount;
                 Monitor.Wait(monitor);
                 if (current.state == State.DONE)
                 {
                     return(current.second);
                 }
                 timeout = SynchUtils.RemainingTimeout(refTime, timeout);
                 if (timeout == 0)
                 {
                     current.state = State.TIMEOUT;
                     throw new TimeoutException();
                 }
             }
             catch (ThreadInterruptedException e) {
                 if (current.state == State.DONE)
                 {
                     Thread.CurrentThread.Interrupt();
                     return(current.second);
                 }
                 current.state = State.INTERRUPTED;
                 throw e;
             }
         }while (true);
     }
 }
コード例 #6
0
 public Pair Take(int timeout) // throws ThreadInterruptedException
 {
     lock (monitor) {
         // fast path
         if (waiters.Count > 0 /* avoid barging */ && PairCompleted())
         {
             var pair = pairs.First.Value;
             pairs.RemoveFirst();
             return(pair);
         }
         if (timeout == 0)
         {
             return(null);
         }
         // prepare wait
         var req = waiters.AddLast((object)null);
         do
         {
             try {
                 int refTime = Environment.TickCount;
                 Monitor.Wait(monitor, timeout);
                 if (waiters.First == req && PairCompleted())
                 {
                     var pair = pairs.First.Value;
                     pairs.RemoveFirst();
                     return(pair);
                 }
                 timeout = SynchUtils.RemainingTimeout(refTime, timeout);
                 if (timeout == 0)
                 {
                     // abort wait
                     waiters.Remove(req);
                     return(null);
                 }
             }
             catch (ThreadInterruptedException) {
                 // abort wait
                 waiters.Remove(req);
                 throw;
             }
         }while (true);
     }
 }
コード例 #7
0
ファイル: Pairing.cs プロジェクト: RubenSantos/RubenPC
 public Tuple <T, U> Provide(U value, int timeout)
 {
     lock (monitor)
     {
         MyTupleWrapper myTupleWrapper = list.Find(mtw => mtw.tValue != null);
         if (myTupleWrapper != null)
         {
             list.Remove(myTupleWrapper);
             monitor.Signal(myTupleWrapper);
             myTupleWrapper.tuple = new Tuple <T, U>(myTupleWrapper.tValue, value);
             return(myTupleWrapper.tuple);
         }
         myTupleWrapper        = new MyTupleWrapper();
         myTupleWrapper.uValue = value;
         list.Add(myTupleWrapper);
         do
         {
             timeout = SynchUtils.RemainingTimeout(Environment.TickCount, timeout);
             try
             {
                 if (!monitor.Await(myTupleWrapper))
                 {
                     if (myTupleWrapper.tuple == null)
                     {
                         throw new TimeoutException();
                     }
                 }
             }
             catch
             {
                 if (myTupleWrapper.tuple != null)
                 {
                     Thread.CurrentThread.Interrupt();
                     return(myTupleWrapper.tuple);
                 }
                 list.Remove(myTupleWrapper);
                 throw;
             }
         } while (myTupleWrapper == null);
         return(myTupleWrapper.tuple);
     }
 }
コード例 #8
0
 public bool Wait(int timeout)
 {
     lock (monitor)
     {
         if (signaled)
         {
             signaled = false;
             return(true);
         }
         var wNode = waiters.AddLast(false);
         while (true)
         {
             try
             {
                 int refTime = Environment.TickCount;
                 Monitor.Wait(monitor, timeout);
                 if (wNode.Value)
                 {
                     return(true);
                 }
                 timeout = SynchUtils.RemainingTimeout(refTime, timeout);
                 if (timeout == 0)
                 {
                     waiters.Remove(wNode);
                     return(false);
                 }
             }
             catch (ThreadInterruptedException e)
             {
                 if (wNode.Value == true)
                 {
                     Thread.CurrentThread.Interrupt();
                     return(true);
                 }
                 waiters.Remove(wNode);
                 throw e;
             }
         }
     }
 }