move rt_once_call to once.c and remove rt_once_call_slow
This commit is contained in:
parent
7c691e5423
commit
2321d43661
|
@ -12,7 +12,7 @@ struct rt_once;
|
|||
/* Call fn exactly once among all callers using the same struct rt_once.
|
||||
* Regardless of which caller actually executes fn, rt_once_call only returns
|
||||
* for any caller after fn has returned. */
|
||||
static inline void rt_once_call(struct rt_once *once, void (*fn)(void));
|
||||
void rt_once_call(struct rt_once *once, void (*fn)(void));
|
||||
|
||||
struct rt_once
|
||||
{
|
||||
|
@ -27,17 +27,6 @@ struct rt_once
|
|||
|
||||
#define RT_ONCE(name) struct rt_once name = RT_ONCE_INIT(name)
|
||||
|
||||
// The slow path for rt_once. Do not call this directly.
|
||||
void rt_once_call_slow(struct rt_once *once, void (*fn)(void));
|
||||
|
||||
static inline void rt_once_call(struct rt_once *once, void (*fn)(void))
|
||||
{
|
||||
if (rt_atomic_load(&once->done, RT_ATOMIC_ACQUIRE) == 0)
|
||||
{
|
||||
rt_once_call_slow(once, fn);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
19
src/once.c
19
src/once.c
|
@ -1,14 +1,17 @@
|
|||
#include <rt/once.h>
|
||||
|
||||
void rt_once_call_slow(struct rt_once *once, void (*fn)(void))
|
||||
void rt_once_call(struct rt_once *once, void (*fn)(void))
|
||||
{
|
||||
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)
|
||||
if (rt_atomic_load(&once->done, RT_ATOMIC_ACQUIRE) == 0)
|
||||
{
|
||||
fn();
|
||||
// This release pairs with the acquire in the fast path.
|
||||
rt_atomic_store(&once->done, 1, RT_ATOMIC_RELEASE);
|
||||
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);
|
||||
}
|
||||
rt_mutex_unlock(&once->mutex);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue