From 9fda06276c79075b36da6056bacdb7c287857966 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 19 Mar 2022 16:44:40 +0100 Subject: [PATCH] Move m_once_t to the public interface --- README.md | 15 +++++++++++++++ m-mutex.h | 32 +++++++++++++++----------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ec667dd3..6a3ab963 100644 --- a/README.md +++ b/README.md @@ -7076,6 +7076,21 @@ If the initialization fails, the program aborts. Wait indefinitely for the thread 'thread' to exit. +#### m\_once\_t + +A type representing a helper structure for m_once_call. +An object of type m\_once\_t shall be initialized at declaration level with M\_ONCE\_INIT\_VALUE + +#### M\_ONCE\_INIT\_VALUE + +Initial value to set an object of type m\_once\_t when declaring it. + +#### void m\_once\_call(m\_once\_t obj, void (*func)(void)) + +Executes the function pointer func exactly once, +even if called concurrently, from several threads, +provided that they share the same object obj. + ### M-WORKER diff --git a/m-mutex.h b/m-mutex.h index df45568c..97ca3e8e 100644 --- a/m-mutex.h +++ b/m-mutex.h @@ -152,14 +152,14 @@ static inline bool m_thread_sleep(unsigned long long usec) return retval == 0; } -// Internal type, not exported. -typedef once_flag m_once_ct[1]; +// a helper structure for m_once_call +typedef once_flag m_once_t[1]; -// Initial value for m_once_ct -#define M_ONCEI_INIT_VALUE { ONCE_FLAG_INIT } +// Initial value for m_once_t +#define M_ONCE_INIT_VALUE { ONCE_FLAG_INIT } // Call the function exactly once -static inline void m_oncei_call(m_once_ct o, void (*func)(void)) +static inline void m_once_call(m_once_t o, void (*func)(void)) { call_once(o,func); } @@ -315,10 +315,9 @@ static inline bool m_thread_sleep(unsigned long long usec) } -// Internal type, not exported. -typedef INIT_ONCE m_once_ct[1]; -#define M_ONCEI_INIT_VALUE { INIT_ONCE_STATIC_INIT } -static inline BOOL CALLBACK m_oncei_callback( PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContext) +typedef INIT_ONCE m_once_t[1]; +#define M_ONCE_INIT_VALUE { INIT_ONCE_STATIC_INIT } +static inline BOOL CALLBACK m_once_callback( PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContext) { void (*func)(void); (void) InitOnce; @@ -327,9 +326,9 @@ static inline BOOL CALLBACK m_oncei_callback( PINIT_ONCE InitOnce, PVOID Paramet (*func)(); return TRUE; } -static inline void m_oncei_call(m_once_ct o, void (*func)(void)) +static inline void m_once_call(m_once_t o, void (*func)(void)) { - InitOnceExecuteOnce(o, m_oncei_callback, (void*)(intptr_t)func, NULL); + InitOnceExecuteOnce(o, m_once_callback, (void*)(intptr_t)func, NULL); } #if defined(_MSC_VER) @@ -474,10 +473,9 @@ static inline bool m_thread_sleep(unsigned long long usec) return retval == 0; } -// Internal type, not exported. -typedef pthread_once_t m_once_ct[1]; -#define M_ONCEI_INIT_VALUE { PTHREAD_ONCE_INIT } -static inline void m_oncei_call(m_once_ct o, void (*func)(void)) +typedef pthread_once_t m_once_t[1]; +#define M_ONCE_INIT_VALUE { PTHREAD_ONCE_INIT } +static inline void m_once_call(m_once_t o, void (*func)(void)) { pthread_once(o,func); } @@ -511,9 +509,9 @@ M_END_PROTECTED_CODE static void M_C(m_mutex_init_, name)(void) { \ m_mutex_init(name); \ } \ - m_once_ct M_C(m_once_, name) = M_ONCEI_INIT_VALUE + m_once_t M_C(m_once_, name) = M_ONCE_INIT_VALUE # define M_LOCKI_BY_ONCE(name) \ - (m_oncei_call(M_C(m_once_, name), M_C(m_mutex_init_, name)), \ + (m_once_call(M_C(m_once_, name), M_C(m_mutex_init_, name)), \ m_mutex_lock(name), (void) 0 ) # define M_LOCK(name) \ M_LOCKI_DO(name, M_C(local_cont_, __LINE__), M_LOCKI_BY_ONCE, m_mutex_unlock)