diff options
Diffstat (limited to 'mysys/my_winthread.c')
-rw-r--r-- | mysys/my_winthread.c | 47 |
1 files changed, 12 insertions, 35 deletions
diff --git a/mysys/my_winthread.c b/mysys/my_winthread.c index 81fd0e7277c..31385fad360 100644 --- a/mysys/my_winthread.c +++ b/mysys/my_winthread.c @@ -149,45 +149,22 @@ int pthread_cancel(pthread_t thread) return -1; } + + /* - One time initialization. For simplicity, we assume initializer thread - does not exit within init_routine(). + One time initialization. */ -int my_pthread_once(my_pthread_once_t *once_control, - void (*init_routine)(void)) -{ - LONG state; - - /* - Do "dirty" read to find out if initialization is already done, to - save an interlocked operation in common case. Memory barriers are ensured by - Visual C++ volatile implementation. - */ - if (*once_control == MY_PTHREAD_ONCE_DONE) - return 0; - state= InterlockedCompareExchange(once_control, MY_PTHREAD_ONCE_INPROGRESS, - MY_PTHREAD_ONCE_INIT); +static BOOL CALLBACK init_once_callback(my_pthread_once_t *once_control, PVOID param, PVOID *context) +{ + typedef void(*void_f)(void); + ((void_f)param)(); + return TRUE; +} - switch(state) - { - case MY_PTHREAD_ONCE_INIT: - /* This is initializer thread */ - (*init_routine)(); - *once_control= MY_PTHREAD_ONCE_DONE; - break; - - case MY_PTHREAD_ONCE_INPROGRESS: - /* init_routine in progress. Wait for its completion */ - while(*once_control == MY_PTHREAD_ONCE_INPROGRESS) - { - Sleep(1); - } - break; - case MY_PTHREAD_ONCE_DONE: - /* Nothing to do */ - break; - } +int my_pthread_once(my_pthread_once_t *once_control, void (*func)(void)) +{ + InitOnceExecuteOnce(once_control, init_once_callback, func, NULL); return 0; } #endif |