On Mon, 28 Dec 2009, Jérémy Zurcher wrote:
> here are the latest results of my efforts to provide H2 with a faster
> logging system.
> any comments welcomed.
I've looked this over a couple times. I wanted to play with
it in more detail... but stomach bugs has hit our house!
It looks fine, except that your ring buffer is locked with a
mutex while writing to the buffer. This results in a
priority inversion. Consider:
1. Low-priority thread calls log(). Mutex gets
lost while string is processed.
2. Right after this, a high-priority audio thread
calls log(). It will wait until the mutex is
unlocked before continuing.
In the US, we call this "a dollar waiting on a dime." It
effectively lowers the priority of the audio thread. This,
again, becomes a real-time violation.
Ideas to overcome this:
* Create a lock-free ringbuffer for each thread
using thread-local storage.
* Create a pool of lock-free ringbuffers (e.g. 5...
since Hydrogen typically has 4-5 threads). Use
atomic integer operations to assign the buffer
to use in log(). If all of them are being used,
drop the messages.
* Change the semantics of the lock-free ringbuffer
to have separate steps for reserving space and
then declaring that all writing is complete.
char msg = "Some string....";
int len = strnlen(msg, 140);
char *pos = rb.reserve(len); // Reserve 'len' chars,
// located at pos
strncpy(pos, msg, len); // Write data directly
rb.written(pos, len); // Notify that data is
I'm pretty sure that these 'reserve' and 'written'
operations can be done with some simple atomic
Hope this helps!