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