From d0aed2a7bfdad688de32a7c4d894590502e6bac4 Mon Sep 17 00:00:00 2001 From: bje Date: Sun, 4 Oct 1998 21:04:28 +0000 Subject: 1998-10-05 Ben Elliston * misc.c (pthread_once): Use the POSIX mutex primitives, not Win32. Remove irrelevant FIXME comment. * pthread.h (PTHREAD_ONCE_INIT): Define. * tests/once1.c: New file; test for pthread_once(). Passes. --- ChangeLog | 5 +++++ misc.c | 8 ++------ pthread.h | 1 + tests/once1.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 tests/once1.c diff --git a/ChangeLog b/ChangeLog index aa08366..91c1c68 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,8 @@ 1998-10-05 Ben Elliston * misc.c (pthread_equal): Correct inverted logic bug. + (pthread_once): Use the POSIX mutex primitives, not Win32. Remove + irrelevant FIXME comment. * global.c (PTHREAD_MUTEX_INITIALIZER): Move to pthread.h. @@ -10,6 +12,7 @@ we call pthread_mutex_create() to initialise the object. This fixes the problem of how to handle statically initialised objects that can't call InitializeCriticalSection() due to their context. + (PTHREAD_ONCE_INIT): Define. * mutex.c (pthread_mutex_init): Set valid flag. (pthread_mutex_destroy): Clear valid flag. @@ -25,6 +28,8 @@ * tests/equal.c: Poor test; remove. * tests/equal1.c New file; test pthread_equal(). Passes. + + * tests/once1.c: New file; test for pthread_once(). Passes. 1998-10-04 Ben Elliston diff --git a/misc.c b/misc.c index eebebae..d39af50 100644 --- a/misc.c +++ b/misc.c @@ -22,17 +22,13 @@ pthread_once(pthread_once_t *once_control, 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(&once_control->lock); + pthread_mutex_lock(&once_control->lock); if (once_control->flag == 0) { flag = once_control->flag = 1; } - LeaveCriticalSection(&once_control->lock); + pthread_mutex_unlock(&once_control->lock); if (flag) { diff --git a/pthread.h b/pthread.h index e9278c2..b678c68 100644 --- a/pthread.h +++ b/pthread.h @@ -75,6 +75,7 @@ struct timespec { #define PTHREAD_CANCELED ((void *) 1) #define PTHREAD_MUTEX_INITIALIZER {0 /* ignore internals */ } +#define PTHREAD_ONCE_INIT { 0, PTHREAD_MUTEX_INITIALIZER } typedef struct _pthread * pthread_t; typedef struct { diff --git a/tests/once1.c b/tests/once1.c new file mode 100644 index 0000000..f4164ac --- /dev/null +++ b/tests/once1.c @@ -0,0 +1,35 @@ +#include +#include + +pthread_once_t once = PTHREAD_ONCE_INIT; + +void +myfunc(void) +{ + printf("only see this once\n"); +} + +void * +mythread(void * arg) +{ + int rc = pthread_once(&once, myfunc); + printf("returned %d\n", rc); + + return 0; +} + +int +main() +{ + int rc; + pthread_t t1, t2; + + rc = pthread_create(&t1, NULL, mythread, NULL); + printf("pthread_create returned %d\n", rc); + + rc = pthread_create(&t2, NULL, mythread, NULL); + printf("pthread_create returned %d\n", rc); + + Sleep(2000); + return 0; +} -- cgit v1.2.3