Back to index

glibc  2.9
pthread_once.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004, 2007 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 
00023 unsigned long int __fork_generation attribute_hidden;
00024 
00025 static void
00026 clear_once_control (void *arg)
00027 {
00028   pthread_once_t *once_control = (pthread_once_t *) arg;
00029 
00030   *once_control = 0;
00031   lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
00032 }
00033 
00034 int
00035 __pthread_once (pthread_once_t *once_control, void (*init_routine) (void))
00036 {
00037   for (;;)
00038     {
00039       int oldval;
00040       int newval;
00041       int tmp;
00042 
00043       /* Pseudo code:
00044         newval = __fork_generation | 1;
00045         oldval = *once_control;
00046         if ((oldval & 2) == 0)
00047           *once_control = newval;
00048         Do this atomically.
00049       */
00050       newval = __fork_generation | 1;
00051       __asm __volatile (
00052               "1:    ldl_l  %0, %2\n"
00053               "      and    %0, 2, %1\n"
00054               "      bne    %1, 2f\n"
00055               "      mov    %3, %1\n"
00056               "      stl_c  %1, %2\n"
00057               "      beq    %1, 1b\n"
00058               "2:    mb"
00059               : "=&r" (oldval), "=&r" (tmp), "=m" (*once_control)
00060               : "r" (newval), "m" (*once_control));
00061 
00062       /* Check if the initializer has already been done.  */
00063       if ((oldval & 2) != 0)
00064        return 0;
00065 
00066       /* Check if another thread already runs the initializer. */
00067       if ((oldval & 1) == 0)
00068        break;
00069 
00070       /* Check whether the initializer execution was interrupted by a fork.  */
00071       if (oldval != newval)
00072        break;
00073 
00074       /* Same generation, some other thread was faster. Wait.  */
00075       lll_futex_wait (once_control, oldval, LLL_PRIVATE);
00076     }
00077 
00078   /* This thread is the first here.  Do the initialization.
00079      Register a cleanup handler so that in case the thread gets
00080      interrupted the initialization can be restarted.  */
00081   pthread_cleanup_push (clear_once_control, once_control);
00082 
00083   init_routine ();
00084 
00085   pthread_cleanup_pop (0);
00086 
00087   /* Add one to *once_control to take the bottom 2 bits from 01 to 10.  */
00088   atomic_increment (once_control);
00089 
00090   /* Wake up all other threads.  */
00091   lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
00092 
00093   return 0;
00094 }
00095 weak_alias (__pthread_once, pthread_once)
00096 strong_alias (__pthread_once, __pthread_once_internal)