_once_stub.c revision 199606
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 199606 2009-11-20 19:19:51Z jhb $"); 30199606Sjhb 31199606Sjhb#include "namespace.h" 32199606Sjhb#include <pthread.h> 33199606Sjhb#include "un-namespace.h" 34199606Sjhb#include "libc_private.h" 35199606Sjhb 36199606Sjhb/* 37199606Sjhb * This implements pthread_once() for the single-threaded case. It is 38199606Sjhb * non-static so that it can be used by _pthread_stubs.c. 39199606Sjhb */ 40199606Sjhbint 41199606Sjhb_libc_once(pthread_once_t *once_control, void (*init_routine)(void)) 42199606Sjhb{ 43199606Sjhb 44199606Sjhb if (once_control->state == PTHREAD_DONE_INIT) 45199606Sjhb return (0); 46199606Sjhb init_routine(); 47199606Sjhb once_control->state = PTHREAD_DONE_INIT; 48199606Sjhb return (0); 49199606Sjhb} 50199606Sjhb 51199606Sjhb/* 52199606Sjhb * This is the internal interface provided to libc. It will use 53199606Sjhb * pthread_once() from the threading library in a multi-threaded 54199606Sjhb * process and _libc_once() for a single-threaded library. Because 55199606Sjhb * _libc_once() uses the same ABI for the values in the pthread_once_t 56199606Sjhb * structure as the threading library, it is safe for a process to 57199606Sjhb * switch from _libc_once() to pthread_once() when threading is 58199606Sjhb * enabled. 59199606Sjhb */ 60199606Sjhbint 61199606Sjhb_once(pthread_once_t *once_control, void (*init_routine)(void)) 62199606Sjhb{ 63199606Sjhb 64199606Sjhb if (__isthreaded) 65199606Sjhb return (_pthread_once(once_control, init_routine)); 66199606Sjhb return (_libc_once(once_control, init_routine)); 67199606Sjhb} 68