github / brubeck

A Statsd-compatible metrics aggregator
MIT License
1.19k stars 94 forks source link

apply changes from #4 to utils.h #12

Closed haneefmubarak closed 9 years ago

haneefmubarak commented 9 years ago

and I even signed the CLA this time!

haneefmubarak commented 9 years ago

Good point. I think I'll have the thread yield in case of contention then. Also, while we're at it, you should use -pthread instead of -lpthread for compiler options.

haneefmubarak commented 9 years ago

Okay so on any platform where explicit bus yielding support is unavailable, we call pthread_yield(), which yields the remainder of the thread's timeslice to the scheduler, who may then switch to another thread or resume the calling thread. Either way, this should hopefully be long enough for contention to clear (and it's non-deterministic).

The only minus to this is that in cases of contention this is effectively going to be at least two context-switches long, if not longer.


One possible solution might be to just switch to using pthread locks entirely, especially considering that on normal, reasonably updated linux systems, glibc (the default libc) is already pretty ingenious with locks:

Simplified Pseudocode:

some_lock () {

    ...

    int x = 0;
    while (CANNOT_LOCK) {
        if (x < a_few_times) {
            bus_yield ();
            spin ();
        } else {
            yield_to_kernel ();
        }
    }

    set (lock);
    return;
}

This makes it decently fast for most purposes and allows the kernel to time switch some threads if a lock is held for a while so that other threads can make progress.

haneefmubarak commented 9 years ago

@vmg anyways, that's more of a long term thing that needs benchmarking and such; for right now, I think it's ready to merge.

vmg commented 9 years ago

Thanks for the PR. :)