_once_stub.c revision 199614
1199606Sjhb/*- 2199606Sjhb * Copyright (c) 2009 Advanced Computing Technologies LLC 3199606Sjhb * Written by: John H. Baldwin <jhb@FreeBSD.org> 4199606Sjhb * All rights reserved. 5199606Sjhb * 6199606Sjhb * Redistribution and use in source and binary forms, with or without 7199606Sjhb * modification, are permitted provided that the following conditions 8199606Sjhb * are met: 9199606Sjhb * 1. Redistributions of source code must retain the above copyright 10199606Sjhb * notice, this list of conditions and the following disclaimer. 11199606Sjhb * 2. Redistributions in binary form must reproduce the above copyright 12199606Sjhb * notice, this list of conditions and the following disclaimer in the 13199606Sjhb * documentation and/or other materials provided with the distribution. 14199606Sjhb * 15199606Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16199606Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17199606Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18199606Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19199606Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20199606Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21199606Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22199606Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23199606Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24199606Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25199606Sjhb * SUCH DAMAGE. 26199606Sjhb */ 27199606Sjhb 28199606Sjhb#include <sys/cdefs.h> 29199606Sjhb__FBSDID("$FreeBSD: head/lib/libc/gen/_once_stub.c 199614 2009-11-20 20:43:34Z jhb $"); 30199606Sjhb 31199606Sjhb#include "namespace.h" 32199606Sjhb#include <pthread.h> 33199606Sjhb#include "un-namespace.h" 34199606Sjhb#include "libc_private.h" 35199606Sjhb 36199614Sjhb/* This implements pthread_once() for the single-threaded case. */ 37199614Sjhbstatic int 38199606Sjhb_libc_once(pthread_once_t *once_control, void (*init_routine)(void)) 39199606Sjhb{ 40199606Sjhb 41199606Sjhb if (once_control->state == PTHREAD_DONE_INIT) 42199606Sjhb return (0); 43199606Sjhb init_routine(); 44199606Sjhb once_control->state = PTHREAD_DONE_INIT; 45199606Sjhb return (0); 46199606Sjhb} 47199606Sjhb 48199606Sjhb/* 49199606Sjhb * This is the internal interface provided to libc. It will use 50199606Sjhb * pthread_once() from the threading library in a multi-threaded 51199606Sjhb * process and _libc_once() for a single-threaded library. Because 52199606Sjhb * _libc_once() uses the same ABI for the values in the pthread_once_t 53199606Sjhb * structure as the threading library, it is safe for a process to 54199606Sjhb * switch from _libc_once() to pthread_once() when threading is 55199606Sjhb * enabled. 56199606Sjhb */ 57199606Sjhbint 58199606Sjhb_once(pthread_once_t *once_control, void (*init_routine)(void)) 59199606Sjhb{ 60199606Sjhb 61199606Sjhb if (__isthreaded) 62199606Sjhb return (_pthread_once(once_control, init_routine)); 63199606Sjhb return (_libc_once(once_control, init_routine)); 64199606Sjhb} 65