本文整理汇总了C#中Disruptor.Sequence类的典型用法代码示例。如果您正苦于以下问题:C# Sequence类的具体用法?C# Sequence怎么用?C# Sequence使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Sequence类属于Disruptor命名空间,在下文中一共展示了Sequence类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerialisePublishing
///<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)
{
var spinWait = default(SpinWait);
while (sequence - cursor.Value > _pendingPublication.Length)
{
spinWait.SpinOnce();
}
long expectedSequence = sequence - batchSize;
for (long pendingSequence = expectedSequence + 1; pendingSequence <= sequence; pendingSequence++)
{
_pendingPublication.WriteCompilerOnlyFence((int)pendingSequence & _pendingMask, pendingSequence);
}
_pendingPublication.WriteFullFence((int)sequence & _pendingMask, sequence);
long cursorSequence = cursor.Value;
if (cursorSequence >= sequence)
{
return;
}
expectedSequence = Math.Max(expectedSequence, cursorSequence);
long nextSequence = expectedSequence + 1;
while (cursor.CompareAndSet(expectedSequence, nextSequence))
{
expectedSequence = nextSequence;
nextSequence++;
if (_pendingPublication.ReadFullFence((int)nextSequence & _pendingMask) != nextSequence)
{
break;
}
}
}
示例2: WaitFor
/// <summary>
/// <see cref="IWaitStrategy.WaitFor"/>
/// </summary>
public long WaitFor(long sequence, Sequence cursor, ISequence dependentSequence, ISequenceBarrier barrier)
{
long startTime = 0;
int counter = _spinTries;
do
{
long availableSequence;
if ((availableSequence = dependentSequence.Value) >= sequence)
return availableSequence;
if (0 == --counter)
{
if (0 == startTime)
{
startTime = GetSystemTimeTicks();
}
else
{
var timeDelta = GetSystemTimeTicks() - startTime;
if (timeDelta > _yieldTimeoutTicks)
{
return _fallbackStrategy.WaitFor(sequence, cursor, dependentSequence, barrier);
}
if (timeDelta > _spinTimeoutTicks)
{
Thread.Yield();
}
}
counter = _spinTries;
}
}
while (true);
}
示例3: WaitFor
/// <summary>
/// <see cref="IWaitStrategy.WaitFor"/>
/// </summary>
public long WaitFor(long sequence, Sequence cursor, ISequence dependentSequence, ISequenceBarrier barrier)
{
var timeSpan = _timeout;
if (cursor.Value < sequence)
{
lock (_gate)
{
while (cursor.Value < sequence)
{
barrier.CheckAlert();
if (!Monitor.Wait(_gate, timeSpan))
{
throw TimeoutException.Instance;
}
}
}
}
long availableSequence;
while ((availableSequence = dependentSequence.Value) < sequence)
{
barrier.CheckAlert();
}
return availableSequence;
}
示例4: WaitFor
/// <summary>
/// <see cref="IWaitStrategy.WaitFor"/>.
/// </summary>
public long WaitFor(long sequence, Sequence cursor, ISequence dependentSequence, ISequenceBarrier barrier)
{
var milliseconds = _timeoutInMilliseconds;
long availableSequence;
if (cursor.Value < sequence)
{
lock (_lock)
{
while (cursor.Value < sequence)
{
Interlocked.Exchange(ref _signalNeeded, 1);
barrier.CheckAlert();
if (!Monitor.Wait(_lock, milliseconds))
{
throw TimeoutException.Instance;
}
}
}
}
while ((availableSequence = dependentSequence.Value) < sequence)
{
barrier.CheckAlert();
}
return availableSequence;
}
示例5: WaitFor
/// <summary>
/// Wait for the given sequence to be available with a timeout specified.
/// </summary>
/// <param name="sequence">sequence to be waited on.</param>
/// <param name="cursor">cursor on which to wait.</param>
/// <param name="dependents">dependents further back the chain that must advance first</param>
/// <param name="barrier">barrier the processor is waiting on.</param>
/// <param name="timeout">timeout value to abort after.</param>
/// <returns>the sequence that is available which may be greater than the requested sequence.</returns>
/// <exception cref="AlertException">AlertException if the status of the Disruptor has changed.</exception>
public long WaitFor(long sequence, Sequence cursor, Sequence[] dependents, ISequenceBarrier barrier,
TimeSpan timeout)
{
long availableSequence;
if ((availableSequence = cursor.Value) < sequence)
{
Monitor.Enter(_gate);
try
{
while ((availableSequence = cursor.Value) < sequence)
{
barrier.CheckAlert();
if (!Monitor.Wait(_gate, timeout))
{
break;
}
}
}
finally
{
Monitor.Exit(_gate);
}
}
if (dependents.Length != 0)
{
while ((availableSequence = Util.GetMinimumSequence(dependents)) < sequence)
{
barrier.CheckAlert();
}
}
return availableSequence;
}
示例6: WaitFor
/// <summary>
/// Wait for the given sequence to be available
/// </summary>
/// <param name="sequence">sequence to be waited on.</param>
/// <param name="cursor">Ring buffer cursor on which to wait.</param>
/// <param name="dependents">dependents further back the chain that must advance first</param>
/// <param name="barrier">barrier the <see cref="IEventProcessor"/> is waiting on.</param>
/// <returns>the sequence that is available which may be greater than the requested sequence.</returns>
public long WaitFor(long sequence, Sequence cursor, Sequence[] dependents, ISequenceBarrier barrier)
{
long availableSequence;
var spinWait = default(SpinWait);
if (dependents.Length == 0)
{
while ((availableSequence = cursor.Value) < sequence) // volatile read
{
barrier.CheckAlert();
spinWait.SpinOnce();
if (spinWait.Count > 5000)
break;
}
}
else
{
while ((availableSequence = Util.GetMinimumSequence(dependents)) < sequence)
{
barrier.CheckAlert();
spinWait.SpinOnce();
if (spinWait.Count > 5000)
break;
}
}
return availableSequence;
}
示例7: WaitFor
/// <summary>
/// Wait for the given sequence to be available with a timeout specified.
/// </summary>
/// <param name="sequence">sequence to be waited on.</param>
/// <param name="cursor">cursor on which to wait.</param>
/// <param name="dependents">dependents further back the chain that must advance first</param>
/// <param name="barrier">barrier the processor is waiting on.</param>
/// <param name="timeout">timeout value to abort after.</param>
/// <returns>the sequence that is available which may be greater than the requested sequence.</returns>
/// <exception cref="AlertException">AlertException if the status of the Disruptor has changed.</exception>
public long WaitFor(long sequence, Sequence cursor, Sequence[] dependents, ISequenceBarrier barrier, TimeSpan timeout)
{
long availableSequence;
var spinWait = default(SpinWait);
var sw = Stopwatch.StartNew();
if (dependents.Length == 0)
{
while ((availableSequence = cursor.Value) < sequence) // volatile read
{
barrier.CheckAlert();
spinWait.SpinOnce();
if (sw.Elapsed > timeout)
{
break;
}
}
}
else
{
while ((availableSequence = Util.GetMinimumSequence(dependents)) < sequence)
{
barrier.CheckAlert();
spinWait.SpinOnce();
if (sw.Elapsed > timeout)
{
break;
}
}
}
return availableSequence;
}
示例8: Remove
/// <summary>
/// Remove the first occurrence of the <see cref="Sequence"/> from this aggregate.
/// </summary>
/// <param name="sequence">sequence to be removed from this aggregate.</param>
/// <returns>true if the sequence was removed otherwise false.</returns>
public bool Remove(Sequence sequence)
{
var found = false;
Sequence[] oldSequences;
Sequence[] newSequences;
do
{
oldSequences = _sequencesRef.ReadFullFence();
int oldSize = oldSequences.Length;
newSequences = new Sequence[oldSize - 1];
int pos = 0;
for (int i = 0; i < oldSize; i++)
{
var testSequence = oldSequences[i];
if (sequence == testSequence && !found)
{
found = true;
}
else
{
newSequences[pos++] = testSequence;
}
}
if (!found)
{
break;
}
}
while (!_sequencesRef.AtomicCompareExchange(newSequences, oldSequences));
return found;
}
示例9: WaitFor
/// <summary>
/// Wait for the given sequence to be available with a timeout specified.
/// </summary>
/// <param name="sequence">sequence to be waited on.</param>
/// <param name="cursor">cursor on which to wait.</param>
/// <param name="dependents">dependents further back the chain that must advance first</param>
/// <param name="barrier">barrier the processor is waiting on.</param>
/// <param name="timeout">timeout value to abort after.</param>
/// <returns>the sequence that is available which may be greater than the requested sequence.</returns>
/// <exception cref="AlertException">AlertException if the status of the Disruptor has changed.</exception>
public long WaitFor(long sequence, Sequence cursor, Sequence[] dependents, ISequenceBarrier barrier, TimeSpan timeout)
{
long availableSequence;
var counter = 0;
var sw = Stopwatch.StartNew();
if (dependents.Length == 0)
{
while ((availableSequence = cursor.Value) < sequence) // volatile read
{
counter = ApplyWaitMethod(barrier, counter);
if (sw.Elapsed > timeout)
{
break;
}
}
}
else
{
while ((availableSequence = Util.GetMinimumSequence(dependents)) < sequence)
{
counter = ApplyWaitMethod(barrier, counter);
if (sw.Elapsed > timeout)
{
break;
}
}
}
return availableSequence;
}
示例10: ShouldWaitForWorkCompleteWhereCompleteWorkThresholdIsAhead
public void ShouldWaitForWorkCompleteWhereCompleteWorkThresholdIsAhead()
{
const int expectedNumberEvents = 10;
const int expectedWorkSequence = 9;
FillRingBuffer(expectedNumberEvents);
var sequence1 = new Sequence(expectedNumberEvents);
var sequence2 = new Sequence(expectedWorkSequence);
var sequence3 = new Sequence(expectedNumberEvents);
_eventProcessorMock1.SetupGet(c => c.Sequence).Returns(sequence1);
_eventProcessorMock2.SetupGet(c => c.Sequence).Returns(sequence2);
_eventProcessorMock3.SetupGet(c => c.Sequence).Returns(sequence3);
var dependencyBarrier = _ringBuffer.NewBarrier(_eventProcessorMock1.Object.Sequence,
_eventProcessorMock2.Object.Sequence,
_eventProcessorMock3.Object.Sequence);
var completedWorkSequence = dependencyBarrier.WaitFor(expectedWorkSequence);
Assert.IsTrue(completedWorkSequence >= expectedWorkSequence);
_eventProcessorMock1.Verify();
_eventProcessorMock2.Verify();
_eventProcessorMock3.Verify();
}
示例11: ShouldAddOneSequenceToGroup
public void ShouldAddOneSequenceToGroup()
{
var sequence = new Sequence(7L);
var sequenceGroup = new SequenceGroup();
sequenceGroup.Add(sequence);
Assert.AreEqual(sequence.Value, sequenceGroup.Value);
}
示例12: IncrementAndGet
/// <summary>
/// Claim the next sequence in the <see cref="Sequencer"/>
/// The caller should be held up until the claimed sequence is available by tracking the dependentSequences.
/// </summary>
/// <param name="dependentSequences">dependentSequences to be checked for range.</param>
/// <returns>the index to be used for the publishing.</returns>
public long IncrementAndGet(Sequence[] dependentSequences)
{
long nextSequence = _claimSequence.Value + 1L;
_claimSequence.Value = nextSequence;
WaitForFreeSlotAt(nextSequence, dependentSequences);
return nextSequence;
}
示例13: ProcessingSequenceBarrier
public ProcessingSequenceBarrier(IWaitStrategy waitStrategy,
Sequence cursorSequence,
Sequence[] dependentSequences)
{
_waitStrategy = waitStrategy;
_cursorSequence = cursorSequence;
_dependentSequences = dependentSequences;
}
示例14: IncrementAndGet
/// <summary>
/// Claim the next sequence in the <see cref="Sequencer"/>
/// The caller should be held up until the claimed sequence is available by tracking the dependentSequences.
/// </summary>
/// <param name="dependentSequences">dependentSequences to be checked for range.</param>
/// <returns>the index to be used for the publishing.</returns>
public long IncrementAndGet(Sequence[] dependentSequences)
{
long nextSequence = _claimSequence.ReadUnfenced() + 1L;
_claimSequence.WriteUnfenced(nextSequence);
WaitForFreeSlotAt(nextSequence, dependentSequences);
return nextSequence;
}
示例15: CheckAndIncrement
public long CheckAndIncrement(int availableCapacity, int delta, Sequence[] dependentSequences)
{
if (!HasAvailableCapacity(availableCapacity, dependentSequences))
{
throw InsufficientCapacityException.Instance;
}
return IncrementAndGet(delta, dependentSequences);
}