From f79b5dbeba8340efaa801cb26be12c2c5a1fab05 Mon Sep 17 00:00:00 2001 From: bje Date: Sun, 19 Jul 1998 22:26:42 +0000 Subject: 1998-07-20 Ben Elliston * misc.c (pthread_once): Implement. --- misc.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'misc.c') diff --git a/misc.c b/misc.c index eb5b5cf..f58005b 100644 --- a/misc.c +++ b/misc.c @@ -7,6 +7,40 @@ #include "pthread.h" +int +pthread_once(pthread_once_t *once_control, + void (*init_routine)(void)) +{ + /* A flag, allocated per invocation, that indicates if the amotic + test-and-set occured. */ + unsigned short flag = 0; + + if (once_control == NULL || init_routine == NULL) + { + return EINVAL; + } + + /* FIXME: we are assuming that the `cs' object is initialised at DLL + load time. Likewise, the object should be destroyed when (if) + the DLL is unloaded. */ + + /* An atomic test-and-set of the "once" flag. */ + EnterCriticalSection(&_pthread_once_lock); + if (_pthread_once_flag == 0) + { + flag = _pthread_once_flag = 1; + } + LeaveCriticalSection(&_pthread_once_lock); + + if (flag) + { + /* Run the init routine. */ + init_routine(); + } + + return 0; +} + pthread_t pthread_self(void) { -- cgit v1.2.3