public void Run()
        {
            if (Interlocked.CompareExchange(ref _running, Running, Idle) == Idle)
            {
                _sequenceBarrier.ClearAlert();
                NotifyStart();

                try
                {
                    if (_running == Running)
                    {
                        ProcessEvents();
                    }
                }
                finally
                {
                    NotifyShutdown();
                    Interlocked.Exchange(ref _running, Idle);
                }
            }
            else
            {
                if (_running == Running)
                {
                    throw new IllegalStateException("Thread is already running.");
                }
                else
                {
                    EarlyExit();
                }
            }
        }
示例#2
0
        /// <summary>
        /// It is ok to have another thread rerun this method after a halt().
        /// </summary>
        public void Run()
        {
            if (Interlocked.Exchange(ref _running, 1) != 0)
            {
                throw new InvalidOperationException("Thread is already running");
            }
            _sequenceBarrier.ClearAlert();

            NotifyStart();

            T   evt          = null;
            var nextSequence = _sequence.Value + 1L;

            try
            {
                while (true)
                {
                    try
                    {
                        var availableSequence = _sequenceBarrier.WaitFor(nextSequence);

                        if (_batchStartAware != null)
                        {
                            _batchStartAware.OnBatchStart(availableSequence - nextSequence + 1);
                        }

                        while (nextSequence <= availableSequence)
                        {
                            evt = _dataProvider[nextSequence];
                            _eventHandler.OnEvent(evt, nextSequence, nextSequence == availableSequence);
                            nextSequence++;
                        }

                        _sequence.SetValue(availableSequence);
                    }
                    catch (TimeoutException)
                    {
                        NotifyTimeout(_sequence.Value);
                    }
                    catch (AlertException)
                    {
                        if (_running == 0)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        _exceptionHandler.HandleEventException(ex, nextSequence, evt);
                        _sequence.SetValue(nextSequence);
                        nextSequence++;
                    }
                }
            }
            finally
            {
                NotifyShutdown();
                _running = 0;
            }
        }
        /// <summary>
        /// It is ok to have another thread rerun this method after a halt().
        /// </summary>
        public void Run()
        {
            if (!running.AtomicCompareExchange(true, false))
            {
                throw new InvalidOperationException("Thread is already running");
            }
            sequenceBarrier.ClearAlert();

            NotifyStart();

            T   @event       = null;
            var nextSequence = sequence.Value + 1L;

            try
            {
                while (true)
                {
                    try
                    {
                        var availableSequence = sequenceBarrier.WaitFor(nextSequence);
                        ///availableSequence=-1 ??????????
                        while (nextSequence <= availableSequence)
                        {
                            @event = dataProvider.Get(nextSequence);
                            eventHandler.OnEvent(@event, nextSequence, nextSequence == availableSequence);
                            nextSequence++;
                        }
                        sequence.LazySet(availableSequence);
                        //sequence.Value = availableSequence;
                    }
                    catch (TimeoutException e)
                    {
                        NotifyTimeout(sequence.Value);
                    }
                    catch (AlertException ex)
                    {
                        if (!running.ReadFullFence())
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        exceptionHandler.HandleEventException(ex, nextSequence, @event);
                        sequence.LazySet(nextSequence);
                        //sequence.Value = nextSequence;
                        nextSequence++;
                    }
                }
            }
            finally
            {
                NotifyShutdown();
                running.WriteFullFence(false);
            }
        }
示例#4
0
        /// <summary>
        /// It is ok to have another thread re-run this method after a halt().
        /// </summary>
        public void Run()
        {
            if (!_running.AtomicCompareExchange(Running, Stopped))
            {
                throw new InvalidOperationException("Thread is already running");
            }
            _sequenceBarrier.ClearAlert();

            NotifyStart();

            var  processedSequence = true;
            long nextSequence      = _sequence.Value;
            T    eventRef          = null;

            while (true)
            {
                try
                {
                    if (processedSequence)
                    {
                        processedSequence = false;
                        nextSequence      = _workSequence.IncrementAndGet();
                        _sequence.Value   = nextSequence - 1L;
                    }

                    _sequenceBarrier.WaitFor(nextSequence);
                    eventRef = _ringBuffer[nextSequence];
                    _workHandler.OnEvent(eventRef);

                    processedSequence = true;
                }
                catch (AlertException)
                {
                    if (_running.ReadFullFence() == Stopped)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    _exceptionHandler.HandleEventException(ex, nextSequence, eventRef);
                    processedSequence = true;
                }
            }

            NotifyShutdown();

            _running.WriteFullFence(Stopped);
        }
示例#5
0
        /// <summary>
        /// It is ok to have another thread rerun this method after a halt().
        /// </summary>
        public void Run()
        {
            if (!_running.AtomicCompareExchange(Running, Stopped))
            {
                throw new InvalidOperationException("Thread is already running");
            }
            _sequenceBarrier.ClearAlert();

            NotifyStart();

            T    evt          = null;
            long nextSequence = _sequence.Value + 1L;

            while (true)
            {
                try
                {
                    long availableSequence = _sequenceBarrier.WaitFor(nextSequence);
                    while (nextSequence <= availableSequence)
                    {
                        evt = _ringBuffer[nextSequence];
                        _eventHandler.OnNext(evt, nextSequence, nextSequence == availableSequence);
                        nextSequence++;
                    }

                    _sequence.LazySet(nextSequence - 1L);
                }
                catch (AlertException)
                {
                    if (!_running.ReadFullFence())
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    _exceptionHandler.HandleEventException(ex, nextSequence, evt);
                    _sequence.LazySet(nextSequence);
                    nextSequence++;
                }
            }

            NotifyShutdown();

            _running.WriteFullFence(Stopped);
        }
示例#6
0
        public void Run()
        {
            if (Interlocked.Exchange(ref _running, 1) != 0)
            {
                throw new InvalidOperationException("Thread is already running");
            }

            _sequenceBarrier.ClearAlert();
            var nextSequence = _current.Value + 1L;

            while (true)
            {
                try
                {
                    var availableSequence = _sequenceBarrier.WaitFor(nextSequence);

                    while (nextSequence <= availableSequence)
                    {
                        this._batchMessageHandler.Handle(_messageBuffer[nextSequence].Value, nextSequence, nextSequence == availableSequence);
                        nextSequence++;
                    }

                    _current.SetValue(availableSequence);
                }
                catch (AlertException)
                {
                    if (_running == 0)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    _current.SetValue(nextSequence);
                    nextSequence++;
                }
            }
        }
示例#7
0
 public void ClearAlert()
 {
     _target.ClearAlert();
 }