private void CheckNotStarted() { if (started.ReadFullFence()) { throw new InvalidOperationException("All event handlers must be added before calling starts."); } }
public void Run() { if (!isRunning.AtomicCompareExchange(true, false)) { throw new Exception("Already running"); } foreach (ISequenceBarrier barrier in barriers) { barrier.ClearAlert(); } int barrierLength = barriers.Length; //????????????????????? //long[] lastConsumed = new long[barrierLength]; //fill(lastConsumed, -1L); //Array.CreateInstance(typeof(long),barrierLength); while (true) { try { for (int i = 0; i < barrierLength; i++) { long available = barriers[i].WaitFor(-1); Sequence sequence = sequences[i]; long previous = sequence.Value; for (long l = previous + 1; l <= available; l++) { handler.OnEvent(providers[i].Get(l), l, previous == available); } sequence.Value = available; count += (available - previous); } Thread.Yield(); } catch (AlertException e) { if (!isRunning.ReadFullFence()) { break; } } catch (Disruptor.TimeoutException e) { Console.WriteLine(e.ToString()); } catch (Exception e) { Console.WriteLine(e.ToString()); break; } } }
/// <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 Execute(Action command) { executionCount.AtomicIncrementAndGet(); if (!ignoreExecution.ReadFullFence()) { var th = new Thread(() => command()); th.Name = command.ToString(); th.IsBackground = true; threads.Add(th); th.Start(); } }
/// <summary> /// /// </summary> /// <returns></returns> public bool IsRunning() { return(started.ReadFullFence()); }
public void ReadFullFenceReturnsInitialValue() { Assert.AreEqual(InitialValue, _volatile.ReadFullFence()); }