[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Implementing events using futex calls
From: |
Marin Ramesa |
Subject: |
Implementing events using futex calls |
Date: |
Sun, 22 Sep 2013 22:04:39 +0200 |
This is a first in the series of constructs that can be used to test
the new futex call (that is, when it's finished). It's based on Ulrich
Drepper's paper "Futexes Are Tricky" and assumes the futex.h from my
patch, with the exception that the corrected futex prototype is:
int futex(int *address, int value, int operation);
where 'value' in the case of FUTEX_WAKE operation denotes the number of
threads to be resumed.
Objects of type event_t can be used to synchronize a number of threads
inside a task, where threads which are to be synchronized use the same
event_t object.
The idea is that each thread calls event_wait() with the same object as
the argument. Then a thread outside of the synchronization circle calls
event_signal() to send a wakeup signal. event_signal() first modifies
the event's value, which closes the synch circle and then calls
FUTEX_WAKE with the INT_MAX argument (which means that all suspended
threads at the same address should be resumed).
Here's the code:
#include <limits.h>
#include <mach/futex.h>
struct event {
int value;
};
typedef struct event *event_t;
void event_init(event_t ev);
void event_signal(event_t ev);
void event_wait(event_t ev);
void event_init(event_t ev)
{
ev->value = 0;
}
void event_signal(event_t ev)
{
ev->value++;
futex(&(ev->value), INT_MAX, FUTEX_WAKE);
}
void event_wait(event_t ev)
{
futex(&(ev->value), ev->value, FUTEX_WAIT);
}
- Implementing events using futex calls,
Marin Ramesa <=