/// <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);
            }
        }
Пример #2
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);
        }
        ///<summary>
        /// Serialise publishers in sequence and set cursor to latest available sequence.
        ///</summary>
        ///<param name="sequence">sequence to be applied</param>
        ///<param name="cursor">cursor to serialise against.</param>
        ///<param name="batchSize">batchSize of the sequence.</param>
        public override void SerialisePublishing(long sequence, Sequence cursor, long batchSize)
        {
            long expectedSequence = sequence - batchSize;
            while (expectedSequence != cursor.Value)
            {
                // busy spin
            }

            cursor.LazySet(sequence);
        }
        ///<summary>
        /// Serialise publishers in sequence and set cursor to latest available sequence.
        ///</summary>
        ///<param name="sequence">sequence to be applied</param>
        ///<param name="cursor">cursor to serialise against.</param>
        ///<param name="batchSize">batchSize of the sequence.</param>
        public override void SerialisePublishing(long sequence, Sequence cursor, long batchSize)
        {
            long expectedSequence = sequence - batchSize;

            while (expectedSequence != cursor.Value)
            {
                // busy spin
            }

            cursor.LazySet(sequence);
        }
 ///<summary>
 /// Serialise publishers in sequence and set cursor to latest available sequence.
 ///</summary>
 ///<param name="sequence">sequence to be applied</param>
 ///<param name="cursor">cursor to serialise against.</param>
 ///<param name="batchSize">batchSize of the sequence.</param>
 public void SerialisePublishing(long sequence, Sequence cursor, long batchSize)
 {
     cursor.LazySet(sequence);
 }
Пример #6
0
 ///<summary>
 /// Serialise publishers in sequence and set cursor to latest available sequence.
 ///</summary>
 ///<param name="sequence">sequence to be applied</param>
 ///<param name="cursor">cursor to serialise against.</param>
 ///<param name="batchSize">batchSize of the sequence.</param>
 public void SerialisePublishing(long sequence, Sequence cursor, long batchSize)
 {
     cursor.LazySet(sequence);
 }
Пример #7
0
 /// <summary>
 /// Force the publication of a cursor sequence.
 ///
 /// Only use this method when forcing a sequence and you are sure only one publisher exists.
 /// This will cause the cursor to advance to this sequence.
 /// </summary>
 /// <param name="sequence">sequence which is to be forced for publication.</param>
 public void ForcePublish(long sequence)
 {
     _cursor.LazySet(sequence);
     _waitStrategy.SignalAllWhenBlocking();
 }