978e96
Note: The __pthread_once definition in the new unified implementation in
978e96
this patch has been edited. The original version of the patch had an old
978e96
style declaration that was causing a -Werror=old-style-definition failure.
978e96
978e96
commit 36875b06e0ed7f137190b9228bef553adfc338ba
978e96
Author: Torvald Riegel <triegel@redhat.com>
978e96
Date:   Wed May 8 16:35:10 2013 +0200
978e96
978e96
    Fixed and unified pthread_once.
978e96
    
978e96
    [BZ #15215] This unifies various pthread_once architecture-specific
978e96
    implementations which were using the same algorithm with slightly different
978e96
    implementations.  It also adds missing memory barriers that are required for
978e96
    correctness.
978e96
978e96
diff --git a/nptl/sysdeps/unix/sysv/linux/pthread_once.c b/nptl/sysdeps/unix/sysv/linux/pthread_once.c
978e96
new file mode 100644
978e96
index 0000000000000000..2684b660958361d4
978e96
--- /dev/null
978e96
+++ b/nptl/sysdeps/unix/sysv/linux/pthread_once.c
978e96
@@ -0,0 +1,129 @@
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)
978e96
diff --git a/nptl/sysdeps/unix/sysv/linux/sparc/pthread_once.c b/nptl/sysdeps/unix/sysv/linux/sparc/pthread_once.c
978e96
deleted file mode 100644
978e96
index a2111756374174f2..0000000000000000
978e96
--- a/nptl/sysdeps/unix/sysv/linux/sparc/pthread_once.c
978e96
+++ /dev/null
978e96
@@ -1,93 +0,0 @@
978e96
-/* Copyright (C) 2003-2012 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
-
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
-  *once_control = 0;
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-}
978e96
-
978e96
-
978e96
-int
978e96
-__pthread_once (once_control, init_routine)
978e96
-     pthread_once_t *once_control;
978e96
-     void (*init_routine) (void);
978e96
-{
978e96
-  while (1)
978e96
-    {
978e96
-      int oldval, val, newval;
978e96
-
978e96
-      val = *once_control;
978e96
-      do
978e96
-	{
978e96
-	  /* Check if the initialized has already been done.  */
978e96
-	  if ((val & 2) != 0)
978e96
-	    return 0;
978e96
-
978e96
-	  oldval = val;
978e96
-	  newval = (oldval & 3) | __fork_generation | 1;
978e96
-	  val = atomic_compare_and_exchange_val_acq (once_control, newval,
978e96
-						     oldval);
978e96
-	}
978e96
-      while (__builtin_expect (val != oldval, 0));
978e96
-
978e96
-      /* Check if another thread already runs the initializer.	*/
978e96
-      if ((oldval & 1) != 0)
978e96
-	{
978e96
-	  /* Check whether the initializer execution was interrupted
978e96
-	     by a fork.	 */
978e96
-	  if (((oldval ^ newval) & -4) == 0)
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
-      /* Add one to *once_control.  */
978e96
-      atomic_increment (once_control);
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)
978e96
diff --git a/sysdeps/unix/sysv/linux/aarch64/nptl/pthread_once.c b/sysdeps/unix/sysv/linux/aarch64/nptl/pthread_once.c
978e96
deleted file mode 100644
978e96
index 0897e1e004ef3278..0000000000000000
978e96
--- a/sysdeps/unix/sysv/linux/aarch64/nptl/pthread_once.c
978e96
+++ /dev/null
978e96
@@ -1,90 +0,0 @@
978e96
-/* Copyright (C) 2004-2012 Free Software Foundation, Inc.
978e96
-
978e96
-   This file is part of the GNU C Library.
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 License as
978e96
-   published by the Free Software Foundation; either version 2.1 of the
978e96
-   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
-
978e96
-unsigned long int __fork_generation attribute_hidden;
978e96
-
978e96
-static void
978e96
-clear_once_control (void *arg)
978e96
-{
978e96
-  pthread_once_t *once_control = (pthread_once_t *) arg;
978e96
-
978e96
-  *once_control = 0;
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-}
978e96
-
978e96
-int
978e96
-__pthread_once (pthread_once_t *once_control, void (*init_routine) (void))
978e96
-{
978e96
-  for (;;)
978e96
-    {
978e96
-      int oldval;
978e96
-      int newval;
978e96
-
978e96
-      /* Pseudo code:
978e96
-	 newval = __fork_generation | 1;
978e96
-	 oldval = *once_control;
978e96
-	 if ((oldval & 2) == 0)
978e96
-	   *once_control = newval;
978e96
-	 Do this atomically.
978e96
-      */
978e96
-      do
978e96
-	{
978e96
-	  newval = __fork_generation | 1;
978e96
-	  oldval = *once_control;
978e96
-	  if (oldval & 2)
978e96
-	    break;
978e96
-	} while (atomic_compare_and_exchange_val_acq (once_control, newval, oldval) != oldval);
978e96
-
978e96
-      /* Check if the initializer has already been done.  */
978e96
-      if ((oldval & 2) != 0)
978e96
-	return 0;
978e96
-
978e96
-      /* Check if another thread already runs the initializer.	*/
978e96
-      if ((oldval & 1) == 0)
978e96
-	break;
978e96
-
978e96
-      /* Check whether the initializer execution was interrupted by a fork.  */
978e96
-      if (oldval != newval)
978e96
-	break;
978e96
-
978e96
-      /* Same generation, some other thread was faster. Wait.  */
978e96
-      lll_futex_wait (once_control, oldval, LLL_PRIVATE);
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
-  /* Say that the initialisation is done.  */
978e96
-  *once_control = __fork_generation | 2;
978e96
-
978e96
-  /* Wake up all other threads.  */
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-
978e96
-  return 0;
978e96
-}
978e96
-weak_alias (__pthread_once, pthread_once)
978e96
-hidden_def (__pthread_once)
978e96
diff --git a/sysdeps/unix/sysv/linux/arm/nptl/pthread_once.c b/sysdeps/unix/sysv/linux/arm/nptl/pthread_once.c
978e96
deleted file mode 100644
978e96
index 0c03f1c816a2fad5..0000000000000000
978e96
--- a/sysdeps/unix/sysv/linux/arm/nptl/pthread_once.c
978e96
+++ /dev/null
978e96
@@ -1,89 +0,0 @@
978e96
-/* Copyright (C) 2004-2012 Free Software Foundation, Inc.
978e96
-   This file is part of the GNU C Library.
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
-
978e96
-unsigned long int __fork_generation attribute_hidden;
978e96
-
978e96
-static void
978e96
-clear_once_control (void *arg)
978e96
-{
978e96
-  pthread_once_t *once_control = (pthread_once_t *) arg;
978e96
-
978e96
-  *once_control = 0;
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-}
978e96
-
978e96
-int
978e96
-__pthread_once (pthread_once_t *once_control, void (*init_routine) (void))
978e96
-{
978e96
-  for (;;)
978e96
-    {
978e96
-      int oldval;
978e96
-      int newval;
978e96
-
978e96
-      /* Pseudo code:
978e96
-	 newval = __fork_generation | 1;
978e96
-	 oldval = *once_control;
978e96
-	 if ((oldval & 2) == 0)
978e96
-	   *once_control = newval;
978e96
-	 Do this atomically.
978e96
-      */
978e96
-      do
978e96
-	{
978e96
-	  newval = __fork_generation | 1;
978e96
-	  oldval = *once_control;
978e96
-	  if (oldval & 2)
978e96
-	    break;
978e96
-	} while (atomic_compare_and_exchange_val_acq (once_control, newval, oldval) != oldval);
978e96
-
978e96
-      /* Check if the initializer has already been done.  */
978e96
-      if ((oldval & 2) != 0)
978e96
-	return 0;
978e96
-
978e96
-      /* Check if another thread already runs the initializer.	*/
978e96
-      if ((oldval & 1) == 0)
978e96
-	break;
978e96
-
978e96
-      /* Check whether the initializer execution was interrupted by a fork.  */
978e96
-      if (oldval != newval)
978e96
-	break;
978e96
-
978e96
-      /* Same generation, some other thread was faster. Wait.  */
978e96
-      lll_futex_wait (once_control, oldval, LLL_PRIVATE);
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
-  /* Say that the initialisation is done.  */
978e96
-  *once_control = __fork_generation | 2;
978e96
-
978e96
-  /* Wake up all other threads.  */
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-
978e96
-  return 0;
978e96
-}
978e96
-weak_alias (__pthread_once, pthread_once)
978e96
-hidden_def (__pthread_once)
978e96
diff --git a/sysdeps/unix/sysv/linux/ia64/nptl/pthread_once.c b/sysdeps/unix/sysv/linux/ia64/nptl/pthread_once.c
978e96
deleted file mode 100644
978e96
index 7730935dfec85ae6..0000000000000000
978e96
--- a/sysdeps/unix/sysv/linux/ia64/nptl/pthread_once.c
978e96
+++ /dev/null
978e96
@@ -1,93 +0,0 @@
978e96
-/* Copyright (C) 2003, 2004, 2007 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
-
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
-  *once_control = 0;
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-}
978e96
-
978e96
-
978e96
-int
978e96
-__pthread_once (once_control, init_routine)
978e96
-     pthread_once_t *once_control;
978e96
-     void (*init_routine) (void);
978e96
-{
978e96
-  while (1)
978e96
-    {
978e96
-      int oldval, val, newval;
978e96
-
978e96
-      val = *once_control;
978e96
-      do
978e96
-	{
978e96
-	  /* Check if the initialized has already been done.  */
978e96
-	  if ((val & 2) != 0)
978e96
-	    return 0;
978e96
-
978e96
-	  oldval = val;
978e96
-	  newval = (oldval & 3) | __fork_generation | 1;
978e96
-	  val = atomic_compare_and_exchange_val_acq (once_control, newval,
978e96
-						     oldval);
978e96
-	}
978e96
-      while (__builtin_expect (val != oldval, 0));
978e96
-
978e96
-      /* Check if another thread already runs the initializer.	*/
978e96
-      if ((oldval & 1) != 0)
978e96
-	{
978e96
-	  /* Check whether the initializer execution was interrupted
978e96
-	     by a fork.	 */
978e96
-	  if (((oldval ^ newval) & -4) == 0)
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
-      /* Add one to *once_control.  */
978e96
-      atomic_increment (once_control);
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)
978e96
diff --git a/sysdeps/unix/sysv/linux/m68k/nptl/pthread_once.c b/sysdeps/unix/sysv/linux/m68k/nptl/pthread_once.c
978e96
deleted file mode 100644
978e96
index 8d81db602eee601f..0000000000000000
978e96
--- a/sysdeps/unix/sysv/linux/m68k/nptl/pthread_once.c
978e96
+++ /dev/null
978e96
@@ -1,90 +0,0 @@
978e96
-/* Copyright (C) 2010-2012 Free Software Foundation, Inc.
978e96
-   This file is part of the GNU C Library.
978e96
-   Contributed by Maxim Kuvyrkov <maxim@codesourcery.com>, 2010.
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
-
978e96
-unsigned long int __fork_generation attribute_hidden;
978e96
-
978e96
-static void
978e96
-clear_once_control (void *arg)
978e96
-{
978e96
-  pthread_once_t *once_control = (pthread_once_t *) arg;
978e96
-
978e96
-  *once_control = 0;
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-}
978e96
-
978e96
-int
978e96
-__pthread_once (pthread_once_t *once_control, void (*init_routine) (void))
978e96
-{
978e96
-  for (;;)
978e96
-    {
978e96
-      int oldval;
978e96
-      int newval;
978e96
-
978e96
-      /* Pseudo code:
978e96
-	 newval = __fork_generation | 1;
978e96
-	 oldval = *once_control;
978e96
-	 if ((oldval & 2) == 0)
978e96
-	   *once_control = newval;
978e96
-	 Do this atomically.
978e96
-      */
978e96
-      do
978e96
-	{
978e96
-	  newval = __fork_generation | 1;
978e96
-	  oldval = *once_control;
978e96
-	  if (oldval & 2)
978e96
-	    break;
978e96
-	} while (atomic_compare_and_exchange_val_acq (once_control, newval, oldval) != oldval);
978e96
-
978e96
-      /* Check if the initializer has already been done.  */
978e96
-      if ((oldval & 2) != 0)
978e96
-	return 0;
978e96
-
978e96
-      /* Check if another thread already runs the initializer.	*/
978e96
-      if ((oldval & 1) == 0)
978e96
-	break;
978e96
-
978e96
-      /* Check whether the initializer execution was interrupted by a fork.  */
978e96
-      if (oldval != newval)
978e96
-	break;
978e96
-
978e96
-      /* Same generation, some other thread was faster. Wait.  */
978e96
-      lll_futex_wait (once_control, oldval, LLL_PRIVATE);
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
-  /* Say that the initialisation is done.  */
978e96
-  *once_control = __fork_generation | 2;
978e96
-
978e96
-  /* Wake up all other threads.  */
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-
978e96
-  return 0;
978e96
-}
978e96
-weak_alias (__pthread_once, pthread_once)
978e96
-hidden_def (__pthread_once)
978e96
diff --git a/sysdeps/unix/sysv/linux/mips/nptl/pthread_once.c b/sysdeps/unix/sysv/linux/mips/nptl/pthread_once.c
978e96
deleted file mode 100644
978e96
index 308da8bbce0c3800..0000000000000000
978e96
--- a/sysdeps/unix/sysv/linux/mips/nptl/pthread_once.c
978e96
+++ /dev/null
978e96
@@ -1,93 +0,0 @@
978e96
-/* Copyright (C) 2003-2012 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
-
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
-  *once_control = 0;
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-}
978e96
-
978e96
-
978e96
-int
978e96
-__pthread_once (once_control, init_routine)
978e96
-     pthread_once_t *once_control;
978e96
-     void (*init_routine) (void);
978e96
-{
978e96
-  while (1)
978e96
-    {
978e96
-      int oldval, val, newval;
978e96
-
978e96
-      val = *once_control;
978e96
-      do
978e96
-	{
978e96
-	  /* Check if the initialized has already been done.  */
978e96
-	  if ((val & 2) != 0)
978e96
-	    return 0;
978e96
-
978e96
-	  oldval = val;
978e96
-	  newval = (oldval & 3) | __fork_generation | 1;
978e96
-	  val = atomic_compare_and_exchange_val_acq (once_control, newval,
978e96
-						     oldval);
978e96
-	}
978e96
-      while (__builtin_expect (val != oldval, 0));
978e96
-
978e96
-      /* Check if another thread already runs the initializer.	*/
978e96
-      if ((oldval & 1) != 0)
978e96
-	{
978e96
-	  /* Check whether the initializer execution was interrupted
978e96
-	     by a fork.	 */
978e96
-	  if (((oldval ^ newval) & -4) == 0)
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
-      /* Add one to *once_control.  */
978e96
-      atomic_increment (once_control);
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)
978e96
diff --git a/sysdeps/unix/sysv/linux/tile/nptl/pthread_once.c b/sysdeps/unix/sysv/linux/tile/nptl/pthread_once.c
978e96
deleted file mode 100644
978e96
index 93ac29b24c440b2c..0000000000000000
978e96
--- a/sysdeps/unix/sysv/linux/tile/nptl/pthread_once.c
978e96
+++ /dev/null
978e96
@@ -1,94 +0,0 @@
978e96
-/* Copyright (C) 2011-2012 Free Software Foundation, Inc.
978e96
-   This file is part of the GNU C Library.
978e96
-   Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
978e96
-   Based on work 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 <nptl/pthreadP.h>
978e96
-#include <lowlevellock.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
-  *once_control = 0;
978e96
-  lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
978e96
-}
978e96
-
978e96
-
978e96
-int
978e96
-__pthread_once (once_control, init_routine)
978e96
-     pthread_once_t *once_control;
978e96
-     void (*init_routine) (void);
978e96
-{
978e96
-  while (1)
978e96
-    {
978e96
-      int oldval, val, newval;
978e96
-
978e96
-      val = *once_control;
978e96
-      do
978e96
-	{
978e96
-	  /* Check if the initialized has already been done.  */
978e96
-	  if ((val & 2) != 0)
978e96
-	    return 0;
978e96
-
978e96
-	  oldval = val;
978e96
-	  newval = (oldval & 3) | __fork_generation | 1;
978e96
-	  val = atomic_compare_and_exchange_val_acq (once_control, newval,
978e96
-						     oldval);
978e96
-	}
978e96
-      while (__builtin_expect (val != oldval, 0));
978e96
-
978e96
-      /* Check if another thread already runs the initializer.	*/
978e96
-      if ((oldval & 1) != 0)
978e96
-	{
978e96
-	  /* Check whether the initializer execution was interrupted
978e96
-	     by a fork.	 */
978e96
-	  if (((oldval ^ newval) & -4) == 0)
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
-      /* Add one to *once_control.  */
978e96
-      atomic_increment (once_control);
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)