Пример #1
0
        public object Accept(int timeout, out S service)
        {
            lock (_lock)
            {
                var myTicket = new Ticket();
                if (_services.Count != 0)
                {
                    RemoveFirstRequestedToken(ref myTicket);
                    service = myTicket.token.service;
                    var token = myTicket.token;
                    return(token);
                }

                myTicket._condition = new Object();
                myTicket.processing = Status.Accepting;
                _services.AddLast(myTicket);
                int lastTime = (timeout != Timeout.Infinite) ? Environment.TickCount : 0;
                while (true)
                {
                    SyncUtils.Wait(_lock, myTicket._condition, timeout);
                    if (myTicket.processing == Status.InProcess)
                    {
                        service = myTicket.token.service;
                        return(myTicket.token);
                    }
                    if (SyncUtils.AdjustTimeout(ref lastTime, ref timeout) == 0)
                    {
                        _services.Remove(myTicket);
                        service = default(S);
                        return(null);
                    }
                }
            }
        }
Пример #2
0
        public bool Request(S service, int timeout, out R response)
        {
            lock (_lock)
            {
                Ticket myTicket;
                if (CheckForAcceptingTokens(out myTicket, service))
                {
                    SyncUtils.Notify(_lock, myTicket._condition);
                }
                else
                {
                    myTicket.token      = new Token(service);
                    myTicket._condition = new Object();

                    _services.AddLast(myTicket);
                }

                int lastTime = (timeout != Timeout.Infinite) ? Environment.TickCount : 0;
                while (true)
                {
                    try
                    {
                        SyncUtils.Wait(_lock, myTicket._condition, timeout);
                        if (myTicket.processing == Status.Concluded)
                        {
                            response = myTicket.token.response;
                            return(true);
                        }

                        if (SyncUtils.AdjustTimeout(ref lastTime, ref timeout) == 0 &&
                            myTicket.processing == Status.Open)
                        {
                            _services.Remove(myTicket);
                            response = default(R);
                            return(false);
                        }
                    }
                    catch (ThreadInterruptedException e)
                    {
                        if (myTicket.processing == Status.Open)
                        {
                            Thread.CurrentThread.Interrupt();
                            _services.Remove(myTicket);
                            response = default(R);
                            return(false);
                        }
                        if (myTicket.processing == Status.Concluded)
                        {
                            response = myTicket.token.response;
                            return(true);
                        }
                        timeout = Timeout.Infinite;
                    }
                }
            }
        }
Пример #3
0
 private void FindTokenInProcessingRequests(ref Token myToken, R response)
 {
     foreach (Ticket ticket in _processingRequests)
     {
         if (ticket.token.Equals(myToken))
         {
             myToken.response  = response;
             ticket.processing = Status.Concluded;
             _processingRequests.Remove(ticket);
             SyncUtils.Notify(_lock, ticket._condition);
             return;
         }
     }
 }
Пример #4
0
 public bool WaitForCompletion(int timeout)
 {
     lock (this)
     {
         if (_complete)
         {
             return(true);
         }
         if (_permits > 0)
         {
             _permits--;
             return(true);
         }
         int lastTime = (timeout != Timeout.Infinite) ? Environment.TickCount : 0;
         while (true)
         {
             try
             {
                 Monitor.Wait(this);
                 if (_complete)
                 {
                     return(true);
                 }
                 if (_permits > 0)
                 {
                     _permits--;
                     return(true);
                 }
                 if (SyncUtils.AdjustTimeout(ref lastTime, ref timeout) == 0)
                 {
                     return(false);
                 }
             }
             catch (Exception e)
             {
                 Thread.CurrentThread.Interrupt();
                 throw new ThreadInterruptedException();
             }
         }
     }
 }