+2024-05-28 Bruno Haible <bruno@clisp.org>
+
+ pthread-once: Work around Cygwin 3.5.3 bug.
+ * m4/pthread-once.m4 (gl_PTHREAD_ONCE): On Cygwin, set
+ REPLACE_PTHREAD_ONCE to 1.
+ * lib/pthread-once.c (pthread_once): Add an implementation for Cygwin.
+ * doc/posix-functions/pthread_once.texi: Mention the Cygwin bug.
+
2024-05-28 Paul Eggert <eggert@cs.ucla.edu>
attribute: const/pure defaults to unsequenced/reproducible
#elif HAVE_PTHREAD_H
/* Provide workarounds for POSIX threads. */
+# if defined __CYGWIN__
+
+# include <stdlib.h>
+
+int
+pthread_once (pthread_once_t *once_control, void (*initfunction) (void))
+{
+# if 0
+ /* This would be the code, for
+ typedef struct
+ {
+ pthread_mutex_t mutex;
+ _Atomic unsigned int num_threads;
+ _Atomic unsigned int done;
+ }
+ pthread_once_t;
+ */
+ if (once_control->done == 0)
+ {
+ once_control->num_threads += 1;
+ pthread_mutex_lock (&once_control->mutex);
+ if (once_control->done == 0)
+ {
+ (*initfunction) ();
+ once_control->done = 1;
+ }
+ pthread_mutex_unlock (&once_control->mutex);
+ if ((once_control->num_threads -= 1) == 0)
+ pthread_mutex_destroy (&once_control->mutex);
+ }
+# else
+ /* In this implementation, we reuse the type
+ typedef struct { pthread_mutex_t mutex; int state; } pthread_once_t;
+ #define PTHREAD_ONCE_INIT { PTHREAD_MUTEX_INITIALIZER, 0 }
+ while assigning the following meaning to the state:
+ state = 2 * <number of waiting threads> + <1 if done>
+ In other words:
+ state = { unsigned int num_threads : 31; unsigned int done : 1; }
+ */
+ /* Test the 'done' bit. */
+ if ((* (unsigned int volatile *) &once_control->state & 1) == 0)
+ {
+ /* The 'done' bit is still zero. Increment num_threads (atomically). */
+ for (;;)
+ {
+ unsigned int prev = * (unsigned int volatile *) &once_control->state;
+ if (__sync_bool_compare_and_swap ((unsigned int *) &once_control->state,
+ prev, prev + 2))
+ break;
+ }
+ /* We have incremented num_threads. Now take the lock. */
+ pthread_mutex_lock (&once_control->mutex);
+ /* Test the 'done' bit again. */
+ if ((* (unsigned int volatile *) &once_control->state & 1) == 0)
+ {
+ /* Execute the initfunction. */
+ (*initfunction) ();
+ /* Set the 'done' bit to 1 (atomically). */
+ for (;;)
+ {
+ unsigned int prev = * (unsigned int volatile *) &once_control->state;
+ if (__sync_bool_compare_and_swap ((unsigned int *) &once_control->state,
+ prev, prev | 1))
+ break;
+ }
+ }
+ /* Now the 'done' bit is 1. Release the lock. */
+ pthread_mutex_unlock (&once_control->mutex);
+ /* Decrement num_threads (atomically). */
+ for (;;)
+ {
+ unsigned int prev = * (unsigned int volatile *) &once_control->state;
+ if (prev < 2)
+ abort ();
+ if (__sync_bool_compare_and_swap ((unsigned int *) &once_control->state,
+ prev, prev - 2))
+ {
+ if (prev - 2 == 1)
+ /* num_threads is now zero, and done is 1.
+ No other thread will need to use the lock.
+ We can therefore destroy the lock, to free resources. */
+ pthread_mutex_destroy (&once_control->mutex);
+ break;
+ }
+ }
+ }
+# endif
+ return 0;
+}
+
+# endif
+
#else
/* Provide a dummy implementation for single-threaded applications. */
# pthread-once.m4
-# serial 2
+# serial 3
dnl Copyright (C) 2019-2024 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
else
if test $HAVE_PTHREAD_H = 0; then
HAVE_PTHREAD_ONCE=0
+ else
+ dnl Work around Cygwin 3.5.3 bug.
+ AC_CACHE_CHECK([whether pthread_once works],
+ [gl_cv_func_pthread_once_works],
+ [case "$host_os" in
+ cygwin*) gl_cv_func_pthread_once_works="guessing no" ;;
+ *) gl_cv_func_pthread_once_works="yes" ;;
+ esac
+ ])
+ case "$gl_cv_func_pthread_once_works" in
+ *yes) ;;
+ *) REPLACE_PTHREAD_ONCE=1 ;;
+ esac
fi
fi
])