[gamin] GQueue not signal safe, atomic operations?



As the comment in gam_dnotify.c indicates, GQueue is not signal safe.
When there is little load this shouldn't be a problem.  In my stress
tests, however, I have noticed failures (see my email from yesterday
with the subject gam-poll.c rewrite).

I suggest that we use atomic operations.  Unfortunately, there is no
single API.  Glib has a very nice abstraction.  As it is easily
extracted from the code base for all of the architectures which glibc
supports, we can just source copy it into gamin.

I've attached a small (untested code snippet) as an example.
Comments?

Thanks,
Neal


#include "atomic.h"

struct event {
    struct event *next;
    int fd;
};

struct event *head;

void
enqueue(int fd)
{
    struct event *e = xmalloc(sizeof(struct event));
    e->fd = fd;

    do
	/* We don't need to do an atomic read here: if we get an
	   inconsistent result, the while predicate will fail.  */
	e->next = head;
    while (atomic_compare_and_exchange_bool_rel(&head, e, e->next));
}

struct e *
dequeue(void)
{
    struct event *e;

    do
	e = head;
    while (atomic_compare_and_exchange_bool_rel(&head, e->next, e));

    return e;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]