Exemplo n.º 1
0
        public bool Take(int timeout, out T rmsg)
        {
            TimeoutInstant timeoutInstant = new TimeoutInstant(timeout);

            Monitor.Enter(_monitor);
            //when there is a message and is first in takeQueue -- condition
            object condition             = new object();
            LinkedListNode <object> node = _takeQueue.AddLast(condition);

            try
            {
                //if first and message queue not empty -> can get message right away
                if (_takeQueue.First == node && _queue.Count != 0)
                {
                    ReceiveMessageSucessfuly(out rmsg);
                    return(true);
                }

                if (timeoutInstant.IsTimeout)
                {
                    rmsg = default(T);
                    return(false);
                }

                global::SyncUtils.SyncUtils.Wait(_monitor, condition, timeoutInstant.Remaining);


                ReceiveMessageSucessfuly(out rmsg);
                return(true);
            }
            catch (ThreadInterruptedException e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                _takeQueue.Remove(node);

                Monitor.Exit(_monitor);
            }


            // throws ThreadInterruptedException

            //receives message

            //return true if message received

            //return false if timeout

            //throws exception if Interrupted thread
        }
Exemplo n.º 2
0
        public bool Transfer(T msg, int timeout)
        {
            // throws ThreadInterruptedException

            //delivers message

            //waits for receit

            //return true if another thread receives it

            //return false if timeout

            //throws exception if Interrupted thread

            //if !sucess message should not stay on list
            TimeoutInstant timeoutInstant = new TimeoutInstant(timeout);

            Monitor.Enter(_monitor);

            LinkedListNode <T> node = _queue.AddLast(msg);


            try
            {
                //if takeQueue is not empty notify the first elem
                if (_takeQueue.Count != 0 && node == _queue.First)
                {
                    object takeQueueCondition = _takeQueue.First.Value;
                    global::SyncUtils.SyncUtils.Pulse(_monitor, takeQueueCondition);
                }


                if (timeoutInstant.IsTimeout)
                {
                    return(false);
                }
                global::SyncUtils.SyncUtils.Wait(_monitor, node, timeoutInstant.Remaining);

                return(true);
            }
            catch (ThreadInterruptedException)
            {
                _queue.Remove(node);
                Console.WriteLine("EXCEPTION TRANSFER");
                throw;
            }
            finally
            {
                Monitor.Exit(_monitor);
            }
        }
Exemplo n.º 3
0
        // throws ThreadInterruptedException, TimeoutException
        public Tuple <T, U> Provide(T value, int timeout)
        {
            Monitor.Enter(_monitor);

            try
            {
                TimeoutInstant timeoutInstant = new TimeoutInstant(timeout);
                var            last           = _tList.AddLast(value);
                T            curr             = last.Value;
                Tuple <T, U> tuple;
                if (_uList.Any())
                {
                    var uListFirst = _uList.First;
                    global::SyncUtils.SyncUtils.Pulse(_monitor, uListFirst);
                    _uList.RemoveFirst();
                    _tList.Remove(last);
                    tuple = new Tuple <T, U>(curr, uListFirst.Value);
                    _tuples.AddLast(tuple);

                    if (timeoutInstant.IsTimeout)
                    {
                        return(null);
                    }

                    global::SyncUtils.SyncUtils.Wait(_monitor, tuple, timeoutInstant.Remaining);
                    return(tuple);
                }
                if (timeoutInstant.IsTimeout)
                {
                    return(null);
                }

                global::SyncUtils.SyncUtils.Wait(_monitor, last, timeoutInstant.Remaining);


                tuple = _tuples.First(t => t.Item1.Equals(curr));
                global::SyncUtils.SyncUtils.Pulse(_monitor, tuple);
                return(tuple);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.WriteLine("Exceçao");
                return(null);
            }
            finally
            {
                Monitor.Exit(_monitor);
            }
        }