rt/include/rt/barrier.h

40 lines
1.1 KiB
C

#pragma once
#include <rt/cond.h>
#include <rt/mutex.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
struct rt_barrier;
void rt_barrier_init(struct rt_barrier *barrier, unsigned int count);
/* Block until count threads have called rt_barrier_wait. The function will
* then return true to one of the threads in the group and reset to its initial
* state, waiting for another count threads. */
bool rt_barrier_wait(struct rt_barrier *barrier);
struct rt_barrier
{
struct rt_mutex mutex;
struct rt_cond cond;
unsigned int level, threshold, generation;
};
#define RT_BARRIER_INIT(name, count) \
{ \
.mutex = RT_MUTEX_INIT(name.mutex), .cond = RT_COND_INIT(name.cond), \
.level = 0, .threshold = (count), .generation = 0, \
}
#define RT_BARRIER(name, count) \
struct rt_barrier name = RT_BARRIER_INIT(name, count)
#ifdef __cplusplus
}
#endif