SynchronizedCircularBuffer

This is primarily a helper for the event queues. It is public in the hope it might be useful, but subject to change without notice; I will treat breaking it the same as if it is private. (That said, it is a simple little utility that does its job, so it is unlikely to change much. The biggest change would probably be letting it grow and changing from inline to dynamic array.)

It is a fixed-size ring buffer that synchronizes on a given object you give it in the constructor.

After enqueuing something, you should probably set an event to notify the other threads. This is left as an exercise to you (or another wrapper).

Constructors

this
this(Object synchronizedOn)

The Object's monitor is used to synchronize the methods in here.

Members

Functions

dequeue
T dequeue()

If you are using a semaphore to signal, you can call this once for each count of it and you can do that separately from this call (though they should be paired).

dequeueSeveral
T[] dequeueSeveral(T[] buffer, void delegate() runInsideLockIfEmpty)

Note that if you use a semaphore to signal waiting threads, you should probably not call this.

enqueue
bool enqueue(T what)

If this returns true, you should signal listening threads (with an event or a semaphore, depending on how you dequeue it). If it returns false, the queue was full and your thing was NOT added. You might wait and retry later (you could set up another event to signal it has been read and wait for that, or maybe try on a timer), or just fail and throw an exception or to abandon the message.

isEmpty
bool isEmpty()

Note the potential race condition between calling this and actually dequeuing something. You might want to acquire the lock on the object before calling this (nested synchronized things are allowed as long as the same thread is the one doing it).

isFull
bool isFull()

Note the potential race condition between calling this and actually queuing something.

Meta