|
|
147e83 |
This patch is based on the below upstream commit.
|
|
|
147e83 |
It only includes relevant pthread_once bits.
|
|
|
147e83 |
|
|
|
147e83 |
commit 08192659bbeae149e7cb1f4c43547257f7099bb0
|
|
|
147e83 |
Author: Roland McGrath <roland@hack.frob.com>
|
|
|
147e83 |
Date: Mon Jul 7 09:28:38 2014 -0700
|
|
|
147e83 |
|
|
|
147e83 |
Get rid of nptl/sysdeps/ entirely!
|
|
|
147e83 |
|
|
|
147e83 |
diff --git a/nptl/pthread_once.c b/nptl/pthread_once.c
|
|
|
147e83 |
index ed1ea3498c397e5c..10c01d6023508e3c 100644
|
|
|
147e83 |
--- a/nptl/pthread_once.c
|
|
|
147e83 |
+++ b/nptl/pthread_once.c
|
|
|
147e83 |
@@ -1,6 +1,6 @@
|
|
|
147e83 |
-/* Copyright (C) 2002-2012 Free Software Foundation, Inc.
|
|
|
147e83 |
+/* Copyright (C) 2003-2014 Free Software Foundation, Inc.
|
|
|
147e83 |
This file is part of the GNU C Library.
|
|
|
147e83 |
- Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
|
|
|
147e83 |
+ Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
|
|
|
147e83 |
|
|
|
147e83 |
The GNU C Library is free software; you can redistribute it and/or
|
|
|
147e83 |
modify it under the terms of the GNU Lesser General Public
|
|
|
147e83 |
@@ -9,7 +9,7 @@
|
|
|
147e83 |
|
|
|
147e83 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
147e83 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
147e83 |
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
Lesser General Public License for more details.
|
|
|
147e83 |
|
|
|
147e83 |
You should have received a copy of the GNU Lesser General Public
|
|
|
147e83 |
@@ -18,37 +18,114 @@
|
|
|
147e83 |
|
|
|
147e83 |
#include "pthreadP.h"
|
|
|
147e83 |
#include <lowlevellock.h>
|
|
|
147e83 |
+#include <atomic.h>
|
|
|
147e83 |
|
|
|
147e83 |
|
|
|
147e83 |
+unsigned long int __fork_generation attribute_hidden;
|
|
|
147e83 |
|
|
|
147e83 |
-static int once_lock = LLL_LOCK_INITIALIZER;
|
|
|
147e83 |
|
|
|
147e83 |
+static void
|
|
|
147e83 |
+clear_once_control (void *arg)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ pthread_once_t *once_control = (pthread_once_t *) arg;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Reset to the uninitialized state here. We don't need a stronger memory
|
|
|
147e83 |
+ order because we do not need to make any other of our writes visible to
|
|
|
147e83 |
+ other threads that see this value: This function will be called if we
|
|
|
147e83 |
+ get interrupted (see __pthread_once), so all we need to relay to other
|
|
|
147e83 |
+ threads is the state being reset again. */
|
|
|
147e83 |
+ *once_control = 0;
|
|
|
147e83 |
+ lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
|
|
|
147e83 |
+}
|
|
|
147e83 |
|
|
|
147e83 |
+
|
|
|
147e83 |
+/* This is similar to a lock implementation, but we distinguish between three
|
|
|
147e83 |
+ states: not yet initialized (0), initialization finished (2), and
|
|
|
147e83 |
+ initialization in progress (__fork_generation | 1). If in the first state,
|
|
|
147e83 |
+ threads will try to run the initialization by moving to the second state;
|
|
|
147e83 |
+ the first thread to do so via a CAS on once_control runs init_routine,
|
|
|
147e83 |
+ other threads block.
|
|
|
147e83 |
+ When forking the process, some threads can be interrupted during the second
|
|
|
147e83 |
+ state; they won't be present in the forked child, so we need to restart
|
|
|
147e83 |
+ initialization in the child. To distinguish an in-progress initialization
|
|
|
147e83 |
+ from an interrupted initialization (in which case we need to reclaim the
|
|
|
147e83 |
+ lock), we look at the fork generation that's part of the second state: We
|
|
|
147e83 |
+ can reclaim iff it differs from the current fork generation.
|
|
|
147e83 |
+ XXX: This algorithm has an ABA issue on the fork generation: If an
|
|
|
147e83 |
+ initialization is interrupted, we then fork 2^30 times (30 bits of
|
|
|
147e83 |
+ once_control are used for the fork generation), and try to initialize
|
|
|
147e83 |
+ again, we can deadlock because we can't distinguish the in-progress and
|
|
|
147e83 |
+ interrupted cases anymore. */
|
|
|
147e83 |
int
|
|
|
147e83 |
__pthread_once (once_control, init_routine)
|
|
|
147e83 |
pthread_once_t *once_control;
|
|
|
147e83 |
void (*init_routine) (void);
|
|
|
147e83 |
{
|
|
|
147e83 |
- /* XXX Depending on whether the LOCK_IN_ONCE_T is defined use a
|
|
|
147e83 |
- global lock variable or one which is part of the pthread_once_t
|
|
|
147e83 |
- object. */
|
|
|
147e83 |
- if (*once_control == PTHREAD_ONCE_INIT)
|
|
|
147e83 |
+ while (1)
|
|
|
147e83 |
{
|
|
|
147e83 |
- lll_lock (once_lock, LLL_PRIVATE);
|
|
|
147e83 |
+ int oldval, val, newval;
|
|
|
147e83 |
|
|
|
147e83 |
- /* XXX This implementation is not complete. It doesn't take
|
|
|
147e83 |
- cancelation and fork into account. */
|
|
|
147e83 |
- if (*once_control == PTHREAD_ONCE_INIT)
|
|
|
147e83 |
+ /* We need acquire memory order for this load because if the value
|
|
|
147e83 |
+ signals that initialization has finished, we need to be see any
|
|
|
147e83 |
+ data modifications done during initialization. */
|
|
|
147e83 |
+ val = *once_control;
|
|
|
147e83 |
+ atomic_read_barrier();
|
|
|
147e83 |
+ do
|
|
|
147e83 |
{
|
|
|
147e83 |
- init_routine ();
|
|
|
147e83 |
+ /* Check if the initialization has already been done. */
|
|
|
147e83 |
+ if (__glibc_likely ((val & 2) != 0))
|
|
|
147e83 |
+ return 0;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ oldval = val;
|
|
|
147e83 |
+ /* We try to set the state to in-progress and having the current
|
|
|
147e83 |
+ fork generation. We don't need atomic accesses for the fork
|
|
|
147e83 |
+ generation because it's immutable in a particular process, and
|
|
|
147e83 |
+ forked child processes start with a single thread that modified
|
|
|
147e83 |
+ the generation. */
|
|
|
147e83 |
+ newval = __fork_generation | 1;
|
|
|
147e83 |
+ /* We need acquire memory order here for the same reason as for the
|
|
|
147e83 |
+ load from once_control above. */
|
|
|
147e83 |
+ val = atomic_compare_and_exchange_val_acq (once_control, newval,
|
|
|
147e83 |
+ oldval);
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ while (__glibc_unlikely (val != oldval));
|
|
|
147e83 |
|
|
|
147e83 |
- *once_control = !PTHREAD_ONCE_INIT;
|
|
|
147e83 |
+ /* Check if another thread already runs the initializer. */
|
|
|
147e83 |
+ if ((oldval & 1) != 0)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Check whether the initializer execution was interrupted by a
|
|
|
147e83 |
+ fork. We know that for both values, bit 0 is set and bit 1 is
|
|
|
147e83 |
+ not. */
|
|
|
147e83 |
+ if (oldval == newval)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Same generation, some other thread was faster. Wait. */
|
|
|
147e83 |
+ lll_futex_wait (once_control, newval, LLL_PRIVATE);
|
|
|
147e83 |
+ continue;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
}
|
|
|
147e83 |
|
|
|
147e83 |
- lll_unlock (once_lock, LLL_PRIVATE);
|
|
|
147e83 |
+ /* This thread is the first here. Do the initialization.
|
|
|
147e83 |
+ Register a cleanup handler so that in case the thread gets
|
|
|
147e83 |
+ interrupted the initialization can be restarted. */
|
|
|
147e83 |
+ pthread_cleanup_push (clear_once_control, once_control);
|
|
|
147e83 |
+
|
|
|
147e83 |
+ init_routine ();
|
|
|
147e83 |
+
|
|
|
147e83 |
+ pthread_cleanup_pop (0);
|
|
|
147e83 |
+
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Mark *once_control as having finished the initialization. We need
|
|
|
147e83 |
+ release memory order here because we need to synchronize with other
|
|
|
147e83 |
+ threads that want to use the initialized data. */
|
|
|
147e83 |
+ atomic_write_barrier();
|
|
|
147e83 |
+ *once_control = 2;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Wake up all other threads. */
|
|
|
147e83 |
+ lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
|
|
|
147e83 |
+ break;
|
|
|
147e83 |
}
|
|
|
147e83 |
|
|
|
147e83 |
return 0;
|
|
|
147e83 |
}
|
|
|
147e83 |
-strong_alias (__pthread_once, pthread_once)
|
|
|
147e83 |
+weak_alias (__pthread_once, pthread_once)
|
|
|
147e83 |
hidden_def (__pthread_once)
|
|
|
147e83 |
diff --git a/nptl/sysdeps/unix/sysv/linux/pthread_once.c b/nptl/sysdeps/unix/sysv/linux/pthread_once.c
|
|
|
147e83 |
deleted file mode 100644
|
|
|
147e83 |
index 2684b660958361d4..0000000000000000
|
|
|
147e83 |
--- a/nptl/sysdeps/unix/sysv/linux/pthread_once.c
|
|
|
147e83 |
+++ /dev/null
|
|
|
147e83 |
@@ -1,129 +0,0 @@
|
|
|
147e83 |
-/* Copyright (C) 2003-2014 Free Software Foundation, Inc.
|
|
|
147e83 |
- This file is part of the GNU C Library.
|
|
|
147e83 |
- Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
|
|
|
147e83 |
-
|
|
|
147e83 |
- The GNU C Library is free software; you can redistribute it and/or
|
|
|
147e83 |
- modify it under the terms of the GNU Lesser General Public
|
|
|
147e83 |
- License as published by the Free Software Foundation; either
|
|
|
147e83 |
- version 2.1 of the License, or (at your option) any later version.
|
|
|
147e83 |
-
|
|
|
147e83 |
- The GNU C Library is distributed in the hope that it will be useful,
|
|
|
147e83 |
- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
147e83 |
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
- Lesser General Public License for more details.
|
|
|
147e83 |
-
|
|
|
147e83 |
- You should have received a copy of the GNU Lesser General Public
|
|
|
147e83 |
- License along with the GNU C Library; if not, see
|
|
|
147e83 |
- <http://www.gnu.org/licenses/>. */
|
|
|
147e83 |
-
|
|
|
147e83 |
-#include "pthreadP.h"
|
|
|
147e83 |
-#include <lowlevellock.h>
|
|
|
147e83 |
-#include <atomic.h>
|
|
|
147e83 |
-
|
|
|
147e83 |
-
|
|
|
147e83 |
-unsigned long int __fork_generation attribute_hidden;
|
|
|
147e83 |
-
|
|
|
147e83 |
-
|
|
|
147e83 |
-static void
|
|
|
147e83 |
-clear_once_control (void *arg)
|
|
|
147e83 |
-{
|
|
|
147e83 |
- pthread_once_t *once_control = (pthread_once_t *) arg;
|
|
|
147e83 |
-
|
|
|
147e83 |
- /* Reset to the uninitialized state here. We don't need a stronger memory
|
|
|
147e83 |
- order because we do not need to make any other of our writes visible to
|
|
|
147e83 |
- other threads that see this value: This function will be called if we
|
|
|
147e83 |
- get interrupted (see __pthread_once), so all we need to relay to other
|
|
|
147e83 |
- threads is the state being reset again. */
|
|
|
147e83 |
- *once_control = 0;
|
|
|
147e83 |
- lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
|
|
|
147e83 |
-}
|
|
|
147e83 |
-
|
|
|
147e83 |
-
|
|
|
147e83 |
-/* This is similar to a lock implementation, but we distinguish between three
|
|
|
147e83 |
- states: not yet initialized (0), initialization finished (2), and
|
|
|
147e83 |
- initialization in progress (__fork_generation | 1). If in the first state,
|
|
|
147e83 |
- threads will try to run the initialization by moving to the second state;
|
|
|
147e83 |
- the first thread to do so via a CAS on once_control runs init_routine,
|
|
|
147e83 |
- other threads block.
|
|
|
147e83 |
- When forking the process, some threads can be interrupted during the second
|
|
|
147e83 |
- state; they won't be present in the forked child, so we need to restart
|
|
|
147e83 |
- initialization in the child. To distinguish an in-progress initialization
|
|
|
147e83 |
- from an interrupted initialization (in which case we need to reclaim the
|
|
|
147e83 |
- lock), we look at the fork generation that's part of the second state: We
|
|
|
147e83 |
- can reclaim iff it differs from the current fork generation.
|
|
|
147e83 |
- XXX: This algorithm has an ABA issue on the fork generation: If an
|
|
|
147e83 |
- initialization is interrupted, we then fork 2^30 times (30 bits of
|
|
|
147e83 |
- once_control are used for the fork generation), and try to initialize
|
|
|
147e83 |
- again, we can deadlock because we can't distinguish the in-progress and
|
|
|
147e83 |
- interrupted cases anymore. */
|
|
|
147e83 |
-int
|
|
|
147e83 |
-__pthread_once (pthread_once_t *once_control, void (*init_routine) (void))
|
|
|
147e83 |
-{
|
|
|
147e83 |
- while (1)
|
|
|
147e83 |
- {
|
|
|
147e83 |
- int oldval, val, newval;
|
|
|
147e83 |
-
|
|
|
147e83 |
- /* We need acquire memory order for this load because if the value
|
|
|
147e83 |
- signals that initialization has finished, we need to be see any
|
|
|
147e83 |
- data modifications done during initialization. */
|
|
|
147e83 |
- val = *once_control;
|
|
|
147e83 |
- atomic_read_barrier();
|
|
|
147e83 |
- do
|
|
|
147e83 |
- {
|
|
|
147e83 |
- /* Check if the initialization has already been done. */
|
|
|
147e83 |
- if (__glibc_likely ((val & 2) != 0))
|
|
|
147e83 |
- return 0;
|
|
|
147e83 |
-
|
|
|
147e83 |
- oldval = val;
|
|
|
147e83 |
- /* We try to set the state to in-progress and having the current
|
|
|
147e83 |
- fork generation. We don't need atomic accesses for the fork
|
|
|
147e83 |
- generation because it's immutable in a particular process, and
|
|
|
147e83 |
- forked child processes start with a single thread that modified
|
|
|
147e83 |
- the generation. */
|
|
|
147e83 |
- newval = __fork_generation | 1;
|
|
|
147e83 |
- /* We need acquire memory order here for the same reason as for the
|
|
|
147e83 |
- load from once_control above. */
|
|
|
147e83 |
- val = atomic_compare_and_exchange_val_acq (once_control, newval,
|
|
|
147e83 |
- oldval);
|
|
|
147e83 |
- }
|
|
|
147e83 |
- while (__glibc_unlikely (val != oldval));
|
|
|
147e83 |
-
|
|
|
147e83 |
- /* Check if another thread already runs the initializer. */
|
|
|
147e83 |
- if ((oldval & 1) != 0)
|
|
|
147e83 |
- {
|
|
|
147e83 |
- /* Check whether the initializer execution was interrupted by a
|
|
|
147e83 |
- fork. We know that for both values, bit 0 is set and bit 1 is
|
|
|
147e83 |
- not. */
|
|
|
147e83 |
- if (oldval == newval)
|
|
|
147e83 |
- {
|
|
|
147e83 |
- /* Same generation, some other thread was faster. Wait. */
|
|
|
147e83 |
- lll_futex_wait (once_control, newval, LLL_PRIVATE);
|
|
|
147e83 |
- continue;
|
|
|
147e83 |
- }
|
|
|
147e83 |
- }
|
|
|
147e83 |
-
|
|
|
147e83 |
- /* This thread is the first here. Do the initialization.
|
|
|
147e83 |
- Register a cleanup handler so that in case the thread gets
|
|
|
147e83 |
- interrupted the initialization can be restarted. */
|
|
|
147e83 |
- pthread_cleanup_push (clear_once_control, once_control);
|
|
|
147e83 |
-
|
|
|
147e83 |
- init_routine ();
|
|
|
147e83 |
-
|
|
|
147e83 |
- pthread_cleanup_pop (0);
|
|
|
147e83 |
-
|
|
|
147e83 |
-
|
|
|
147e83 |
- /* Mark *once_control as having finished the initialization. We need
|
|
|
147e83 |
- release memory order here because we need to synchronize with other
|
|
|
147e83 |
- threads that want to use the initialized data. */
|
|
|
147e83 |
- atomic_write_barrier();
|
|
|
147e83 |
- *once_control = 2;
|
|
|
147e83 |
-
|
|
|
147e83 |
- /* Wake up all other threads. */
|
|
|
147e83 |
- lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
|
|
|
147e83 |
- break;
|
|
|
147e83 |
- }
|
|
|
147e83 |
-
|
|
|
147e83 |
- return 0;
|
|
|
147e83 |
-}
|
|
|
147e83 |
-weak_alias (__pthread_once, pthread_once)
|
|
|
147e83 |
-hidden_def (__pthread_once)
|