1*0d5acd74SJohn Marino /*- 2*0d5acd74SJohn Marino * Copyright (c) 2009 Advanced Computing Technologies LLC 3*0d5acd74SJohn Marino * Written by: John H. Baldwin <jhb@FreeBSD.org> 4*0d5acd74SJohn Marino * All rights reserved. 5*0d5acd74SJohn Marino * 6*0d5acd74SJohn Marino * Redistribution and use in source and binary forms, with or without 7*0d5acd74SJohn Marino * modification, are permitted provided that the following conditions 8*0d5acd74SJohn Marino * are met: 9*0d5acd74SJohn Marino * 1. Redistributions of source code must retain the above copyright 10*0d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer. 11*0d5acd74SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright 12*0d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer in the 13*0d5acd74SJohn Marino * documentation and/or other materials provided with the distribution. 14*0d5acd74SJohn Marino * 15*0d5acd74SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16*0d5acd74SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17*0d5acd74SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18*0d5acd74SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19*0d5acd74SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20*0d5acd74SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21*0d5acd74SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22*0d5acd74SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23*0d5acd74SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24*0d5acd74SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25*0d5acd74SJohn Marino * SUCH DAMAGE. 26*0d5acd74SJohn Marino * 27*0d5acd74SJohn Marino * $FreeBSD: head/lib/libc/gen/_once_stub.c 199614 2009-11-20 20:43:34Z jhb $ 28*0d5acd74SJohn Marino */ 29*0d5acd74SJohn Marino 30*0d5acd74SJohn Marino 31*0d5acd74SJohn Marino #include "namespace.h" 32*0d5acd74SJohn Marino #include <pthread.h> 33*0d5acd74SJohn Marino #include "un-namespace.h" 34*0d5acd74SJohn Marino #include "libc_private.h" 35*0d5acd74SJohn Marino 36*0d5acd74SJohn Marino /* This implements pthread_once() for the single-threaded case. */ 37*0d5acd74SJohn Marino static int 38*0d5acd74SJohn Marino _libc_once(pthread_once_t *once_control, void (*init_routine)(void)) 39*0d5acd74SJohn Marino { 40*0d5acd74SJohn Marino 41*0d5acd74SJohn Marino if (once_control->state == PTHREAD_DONE_INIT) 42*0d5acd74SJohn Marino return (0); 43*0d5acd74SJohn Marino init_routine(); 44*0d5acd74SJohn Marino once_control->state = PTHREAD_DONE_INIT; 45*0d5acd74SJohn Marino return (0); 46*0d5acd74SJohn Marino } 47*0d5acd74SJohn Marino 48*0d5acd74SJohn Marino /* 49*0d5acd74SJohn Marino * This is the internal interface provided to libc. It will use 50*0d5acd74SJohn Marino * pthread_once() from the threading library in a multi-threaded 51*0d5acd74SJohn Marino * process and _libc_once() for a single-threaded library. Because 52*0d5acd74SJohn Marino * _libc_once() uses the same ABI for the values in the pthread_once_t 53*0d5acd74SJohn Marino * structure as the threading library, it is safe for a process to 54*0d5acd74SJohn Marino * switch from _libc_once() to pthread_once() when threading is 55*0d5acd74SJohn Marino * enabled. 56*0d5acd74SJohn Marino */ 57*0d5acd74SJohn Marino int 58*0d5acd74SJohn Marino _once(pthread_once_t *once_control, void (*init_routine)(void)) 59*0d5acd74SJohn Marino { 60*0d5acd74SJohn Marino 61*0d5acd74SJohn Marino if (__isthreaded) 62*0d5acd74SJohn Marino return (_pthread_once(once_control, init_routine)); 63*0d5acd74SJohn Marino return (_libc_once(once_control, init_routine)); 64*0d5acd74SJohn Marino } 65