Disruptor

A single-producer, multiple consumer disruptor implementation

  • T = The type of the slots
  • Size = size of the ring buffer in slots. Rounded up to the next power of 2.
  • Consumers = number of ConsumerToken the disruptor can issue

Constructors

this
this(Disruptor another)
Undocumented in source.

Postblit

this(this)
this(this)
Undocumented in source.

Members

Functions

consume
bool consume(ConsumerToken token, void delegate(T[] slice, ulong firstIndex) del)

Consume from the Disruptor. Calls del with the slice of produced but not consumed elements. The argument firstIndex is the index of the first element in slice.

createConsumerToken
ConsumerToken createConsumerToken()

Generate a new consumer token.

initialize
void initialize()
Undocumented in source. Be warned that the author may not have intended to support it.
produce
bool produce(void delegate(ref T slot, ulong index) del)

Iff there is more room in the Disruptor, calls del with a reference to the free slot. The second argument index is the index of the slot.

Examples

import std.functional : toDelegate;
alias D = Disruptor!int;

int testInt = ubyte.max;
auto doNothing = (int[] values, ulong idx) {
    if (!values.empty)
        testInt = values[0];
};

shared D d;
ConsumerToken consumer1 = d.createConsumerToken();
ConsumerToken consumer2 = d.createConsumerToken();

assert (!d.consume(consumer1, doNothing));
assert (!d.consume(consumer2, doNothing));

d.produce((ref int v, ulong _) {
    v = 1;
}.toDelegate());

assert (d.consume(consumer1, doNothing));
assert (testInt == 1);
assert (!d.consume(consumer1, doNothing));
testInt = 2;
assert (d.consume(consumer2, doNothing));
assert (testInt == 1);
assert (!d.consume(consumer2, doNothing));
import std.functional : toDelegate;
alias D = Disruptor!int;

int testInt = ubyte.max;
auto doNothing = (int[] values, ulong idx) {
    if (!values.empty)
        testInt = values[0];
};

shared D d;
ConsumerToken consumer1 = d.createConsumerToken();
ConsumerToken consumer2 = d.createConsumerToken();
consumer2.waitFor(consumer1);

assert (!d.consume(consumer1, doNothing));
assert (!d.consume(consumer2, doNothing));

d.produce((ref int v, ulong _) {
    v = 1;
}.toDelegate());

testInt = ubyte.max;
assert (!d.consume(consumer2, doNothing));
assert (testInt == ubyte.max);
assert (d.consume(consumer1, doNothing));
assert (testInt == 1);
assert (!d.consume(consumer1, doNothing));
assert (d.consume(consumer2, doNothing));

Meta