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