move rt_once_call to once.c and remove rt_once_call_slow

This commit is contained in:
Chris Copeland 2023-10-31 22:51:40 -07:00
parent 7c691e5423
commit 2321d43661
Signed by: chrisnc
GPG Key ID: 14550DA72485DF30
2 changed files with 12 additions and 20 deletions

View File

@ -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

View File

@ -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);
}