rt/src/once.c

18 lines
548 B
C

#include <rt/once.h>
void rt_once_call(struct rt_once *once, void (*fn)(void))
{
if (rt_atomic_load(&once->done, RT_ATOMIC_ACQUIRE) == 0)
{
rt_mutex_lock(&once->mutex);
// A mutex has acquire-release semantics, so we can load relaxed here.
if (rt_atomic_load(&once->done, RT_ATOMIC_RELAXED) == 0)
{
fn();
// This release pairs with the acquire in the fast path.
rt_atomic_store(&once->done, 1, RT_ATOMIC_RELEASE);
}
rt_mutex_unlock(&once->mutex);
}
}