Back to index

glibc  2.9
pthread_once.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include "pthreadP.h"
00020 #include <lowlevellock.h>
00021 
00022 unsigned long int __fork_generation attribute_hidden;
00023 
00024 static void
00025 clear_once_control (void *arg)
00026 {
00027   pthread_once_t *once_control = (pthread_once_t *) arg;
00028 
00029   *once_control = 0;
00030   lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
00031 }
00032 
00033 int
00034 __pthread_once (pthread_once_t *once_control, void (*init_routine) (void))
00035 {
00036   for (;;)
00037     {
00038       int oldval;
00039       int newval;
00040 
00041       /* Pseudo code:
00042         newval = __fork_generation | 1;
00043         oldval = *once_control;
00044         if ((oldval & 2) == 0)
00045           *once_control = newval;
00046         Do this atomically.
00047       */
00048       do
00049        {
00050          newval = __fork_generation | 1;
00051          oldval = *once_control;
00052          if (oldval & 2)
00053            break;
00054        } while (atomic_compare_and_exchange_val_acq (once_control, newval, oldval) != oldval);
00055 
00056       /* Check if the initializer has already been done.  */
00057       if ((oldval & 2) != 0)
00058        return 0;
00059 
00060       /* Check if another thread already runs the initializer. */
00061       if ((oldval & 1) == 0)
00062        break;
00063 
00064       /* Check whether the initializer execution was interrupted by a fork.  */
00065       if (oldval != newval)
00066        break;
00067 
00068       /* Same generation, some other thread was faster. Wait.  */
00069       lll_futex_wait (once_control, oldval, LLL_PRIVATE);
00070     }
00071 
00072   /* This thread is the first here.  Do the initialization.
00073      Register a cleanup handler so that in case the thread gets
00074      interrupted the initialization can be restarted.  */
00075   pthread_cleanup_push (clear_once_control, once_control);
00076 
00077   init_routine ();
00078 
00079   pthread_cleanup_pop (0);
00080 
00081   /* Say that the initialisation is done.  */
00082   *once_control = __fork_generation | 2;
00083 
00084   /* Wake up all other threads.  */
00085   lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
00086 
00087   return 0;
00088 }
00089 weak_alias (__pthread_once, pthread_once)
00090 strong_alias (__pthread_once, __pthread_once_internal)
00091 
00092 #if defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__PIC__)
00093 /* When statically linked, if pthread_create is used, this file
00094    will be brought in.  The exception handling code in GCC assumes
00095    that if pthread_create is available, so are these.  */
00096 const void *include_pthread_getspecific attribute_hidden = pthread_getspecific;
00097 const void *include_pthread_setspecific attribute_hidden = pthread_setspecific;
00098 const void *include_pthread_key_create attribute_hidden = pthread_key_create;
00099 #endif