public void EventConsumer() { var lw = new ReadOnlyListRingWrapper(_ring); var nextSequence = _sequence.Value + 1L; while (true) { try { var availableSequence = _sequenceBarrier.WaitFor(nextSequence); var count = (int)(availableSequence - nextSequence + 1); if (count == 0) { continue; } lw.SetStartAndCount(nextSequence, count); nextSequence = availableSequence + 1; _writeStore.Store(null, lw); lw.RunContinuations(); _sequence.LazySet(nextSequence - 1L); } catch (AlertException) { break; } } }
public long WaitFor(long sequence) { var result = _suppress ? sequence - 1 : _target.WaitFor(sequence); _suppress = !_suppress; return(result); }
/// <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; } }
public void Should_Publish_And_Get() { Assert.Equal(Sequence.InitialValue, _ringBuffer.GetCursor()); var expectedEvent = new StubEvent(2701); _ringBuffer.PublishEvent(StubEvent.Translator, expectedEvent.Value, expectedEvent.TestString); var sequence = _barrier.WaitFor(0L); Assert.Equal(0L, sequence); var @event = _ringBuffer.Get(sequence); Assert.Equal(expectedEvent, @event); Assert.Equal(0L, _barrier.GetCursor()); }
/// <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); } }
public void Run() { if (Interlocked.Exchange(ref _running, 1) != 0) { throw new InvalidOperationException("Already running"); } _sequenceBarrier.WaitFor(0L); Sequence.SetValue(Sequence.Value + 1); }
public void ShouldClaimAndGet() { Assert.AreEqual(Sequence.InitialCursorValue, _ringBuffer.Cursor); var expectedEvent = new T { Value = 2701 }; var claimSequence = _ringBuffer.Next(); _ringBuffer[claimSequence] = expectedEvent; _ringBuffer.Publish(claimSequence); var sequence = _sequenceBarrier.WaitFor(0); Assert.AreEqual(0, sequence); var evt = _ringBuffer[sequence]; Assert.AreEqual(expectedEvent, evt); Assert.AreEqual(0L, _ringBuffer.Cursor); }
public List <StubEvent> Call() { _barrier.SignalAndWait(); _sequenceBarrier.WaitFor(_toWaitForSequence); var messages = new List <StubEvent>(); for (long l = _initialSequence; l <= _toWaitForSequence; l++) { messages.Add(_ringBuffer.Get(l)); } return(messages); }
public List <StubEvent> Call() { _barrier.SignalAndWait(); _sequenceBarrier.WaitFor(_toWaitForSequence); var events = new List <StubEvent>(); for (var l = _initialSequence; l <= _toWaitForSequence; l++) { events.Add(_ringBuffer[l]); } return(events); }
//this will block if too many events published private void AddEventToDisruptor(IDomainEventRecord message) { if (message.Sequence <= _ringBuffer.Cursor) { return; } while ((_ringBuffer.Cursor + _ringBuffer.RemainingCapacity()) < message.Sequence) { //this will block until message.Sequence will be available or timeout _barrier.WaitFor(message.Sequence, TimeSpan.FromMilliseconds(100)); } _ringBuffer[message.Sequence].Value = message; _ringBuffer.Publish(message.Sequence); }
/// <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); }
public void ShouldClaimBatchAndPublishBack() { const int batchSize = 5; var batchDescriptor = _ringBuffer.NewBatchDescriptor(batchSize); _ringBuffer.Next(batchDescriptor); Assert.AreEqual(0L, batchDescriptor.Start); Assert.AreEqual(4L, batchDescriptor.End); Assert.AreEqual(Sequencer.InitialCursorValue, _ringBuffer.Cursor); _ringBuffer.Publish(batchDescriptor); Assert.AreEqual(batchSize - 1L, _ringBuffer.Cursor); Assert.AreEqual(batchSize - 1L, _sequenceBarrier.WaitFor(0L)); }
/// <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); }
public virtual void Run() { CompareAndSwapRunningState(RunState.Running, RunState.Halted); while (runningState == (int)RunState.Running) { var avaliableUpstreamSequence = _upstreamBarrier.WaitFor(currentUpstreamSequence); while (currentUpstreamSequence <= avaliableUpstreamSequence) { OnNextAvaliable( _ringBuffer[currentUpstreamSequence], currentUpstreamSequence, currentUpstreamSequence < avaliableUpstreamSequence ); currentUpstreamSequence++; } } }
public void Run() { if (Interlocked.Exchange(ref _running, 1) == 1) { throw new IllegalStateException("Thread is already running"); } try { _barrier.WaitFor(0L); } catch (Exception ex) { throw new RuntimeException(ex); } _sequence.SetValue(0L); }
public void EventConsumer() { var l = new object[_ring.BufferSize]; var t = new TaskCompletionSource <bool> [_ring.BufferSize]; var lw = new ReadOnlyListArrayWrapper <object>(l); var nextSequence = _sequence.Value + 1L; while (true) { try { var availableSequence = _sequenceBarrier.WaitFor(nextSequence); var count = 0; while (nextSequence <= availableSequence) { var evt = _ring[nextSequence]; l[count] = evt.Event; t[count] = evt.TaskCompletionSource; count++; nextSequence++; } if (count == 0) { continue; } lw.Count = count; _writeStore.Store(null, lw); for (var i = 0; i < count; i++) { Task.Factory.StartNew(o => ((TaskCompletionSource <bool>)o).SetResult(true), t[i], CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default); t[i] = null; l[i] = null; } _sequence.LazySet(nextSequence - 1L); } catch (AlertException) { break; } } }
private void ProcessEvents() { TEvent @event = null; var nextSequence = _sequence.GetValue() + 1L; while (true) { try { var availableSequence = _sequenceBarrier.WaitFor(nextSequence); _batchStartAware?.OnBatchStart(availableSequence - nextSequence + 1); while (nextSequence <= availableSequence) { @event = _dataProvider.Get(nextSequence); _eventHandler.OnEvent(@event, nextSequence, nextSequence == availableSequence); nextSequence++; } _sequence.SetValue(availableSequence); } catch (TimeoutException) { NotifyTimeout(_sequence.GetValue()); } catch (AlertException) { if (_running != Running) { break; } } catch (Exception e) { _exceptionHandler?.HandleEventException(e, nextSequence, @event); _sequence.SetValue(nextSequence); nextSequence++; } } }
public void Run() { long expected = expectedCount; long processed = -1; try { do { processed = barrier.WaitFor(sequence.Value + 1); sequence.Value = processed; }while (processed < expected); latch.Signal(); sequence.Value = processed; } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public void Run() { var expected = _expectedCount; try { long processed; do { processed = _barrier.WaitFor(Sequence.Value + 1); Sequence.SetValue(processed); }while (processed < expected); _latch.Set(); Sequence.SetValueVolatile(processed); } catch (Exception ex) { Console.WriteLine(ex); } }
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++; } } }
public void Run() { _sequenceBarrier.WaitFor(0L); _sequence.Value += 1; }
public void Run() { IsRunning = true; _sequenceBarrier.WaitFor(0L); Sequence.SetValue(Sequence.Value + 1); }