Beispiel #1
0
        public override void Run()
        {
            Debug.Assert(_backgroundThread == null, "A thread is already running " + _backgroundThread);
            _backgroundThread = Thread.CurrentThread;
            _startupLatch.release();

            try
            {
                do
                {
                    AsyncEvent events = _stackUpdater.getAndSet(this, _endSentinel);
                    Process(events);
                    if (_stack == _endSentinel && !_shutdown)
                    {
                        LockSupport.park(this);
                    }
                } while (!_shutdown);

                AsyncEvent events = _stackUpdater.getAndSet(this, _shutdownSentinel);
                Process(events);
            }
            finally
            {
                _backgroundThread = null;
                _shutdownLatch.release();
            }
        }
Beispiel #2
0
 /// <summary>
 /// Construct a new {@code AsyncEvents} instance, that will use the given consumer to process the events.
 /// </summary>
 /// <param name="eventConsumer"> The <seealso cref="Consumer"/> used for processing the events that are sent in. </param>
 public AsyncEvents(System.Action <T> eventConsumer, Monitor monitor)
 {
     this._eventConsumer = eventConsumer;
     this._monitor       = monitor;
     this._startupLatch  = new BinaryLatch();
     this._shutdownLatch = new BinaryLatch();
     this._stack         = _endSentinel;
 }
Beispiel #3
0
        private void Process(AsyncEvent events)
        {
            events = ReverseAndStripEndMark(events);

            while (events != null)
            {
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") T event = (T) events;
                T @event = ( T )events;
                _eventConsumer.accept(@event);
                events = events.Next;
            }
        }
Beispiel #4
0
        public override void Send(T @event)
        {
            AsyncEvent prev = _stackUpdater.getAndSet(this, @event);

            Debug.Assert(prev != null);
            @event.Next = prev;
            if (prev == _endSentinel)
            {
                LockSupport.unpark(_backgroundThread);
            }
            else if (prev == _shutdownSentinel)
            {
                AsyncEvent events = _stackUpdater.getAndSet(this, _shutdownSentinel);
                Process(events);
            }
        }
Beispiel #5
0
        private AsyncEvent ReverseAndStripEndMark(AsyncEvent events)
        {
            AsyncEvent result = null;
            long       count  = 0;

            while (events != _endSentinel && events != _shutdownSentinel)
            {
                AsyncEvent next;
                do
                {
                    next = events.Next;
                } while (next == null);
                events.Next = result;
                result      = events;
                events      = next;
                count++;
            }
            if (count > 0)
            {
                _monitor.eventCount(count);
            }
            return(result);
        }