xref: /dflybsd-src/lib/libc/gen/_once_stub.c (revision cf8046a92768d53e67d2533fb51b137d5506248d)
10d5acd74SJohn Marino /*-
20d5acd74SJohn Marino  * Copyright (c) 2009 Advanced Computing Technologies LLC
30d5acd74SJohn Marino  * Written by: John H. Baldwin <jhb@FreeBSD.org>
40d5acd74SJohn Marino  * All rights reserved.
50d5acd74SJohn Marino  *
60d5acd74SJohn Marino  * Redistribution and use in source and binary forms, with or without
70d5acd74SJohn Marino  * modification, are permitted provided that the following conditions
80d5acd74SJohn Marino  * are met:
90d5acd74SJohn Marino  * 1. Redistributions of source code must retain the above copyright
100d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer.
110d5acd74SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
120d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
130d5acd74SJohn Marino  *    documentation and/or other materials provided with the distribution.
140d5acd74SJohn Marino  *
150d5acd74SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
160d5acd74SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170d5acd74SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180d5acd74SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
190d5acd74SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
200d5acd74SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
210d5acd74SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
220d5acd74SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
230d5acd74SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240d5acd74SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250d5acd74SJohn Marino  * SUCH DAMAGE.
260d5acd74SJohn Marino  *
270d5acd74SJohn Marino  * $FreeBSD: head/lib/libc/gen/_once_stub.c 199614 2009-11-20 20:43:34Z jhb $
280d5acd74SJohn Marino  */
290d5acd74SJohn Marino 
300d5acd74SJohn Marino 
310d5acd74SJohn Marino #include "namespace.h"
320d5acd74SJohn Marino #include <pthread.h>
330d5acd74SJohn Marino #include "un-namespace.h"
340d5acd74SJohn Marino #include "libc_private.h"
350d5acd74SJohn Marino 
360d5acd74SJohn Marino /* This implements pthread_once() for the single-threaded case. */
370d5acd74SJohn Marino static int
_libc_once(pthread_once_t * once_control,void (* init_routine)(void))380d5acd74SJohn Marino _libc_once(pthread_once_t *once_control, void (*init_routine)(void))
390d5acd74SJohn Marino {
400d5acd74SJohn Marino 
41*cf8046a9Szrj 	if (once_control->__state == PTHREAD_DONE_INIT)
420d5acd74SJohn Marino 		return (0);
430d5acd74SJohn Marino 	init_routine();
44*cf8046a9Szrj 	once_control->__state = PTHREAD_DONE_INIT;
450d5acd74SJohn Marino 	return (0);
460d5acd74SJohn Marino }
470d5acd74SJohn Marino 
480d5acd74SJohn Marino /*
490d5acd74SJohn Marino  * This is the internal interface provided to libc.  It will use
500d5acd74SJohn Marino  * pthread_once() from the threading library in a multi-threaded
510d5acd74SJohn Marino  * process and _libc_once() for a single-threaded library.  Because
520d5acd74SJohn Marino  * _libc_once() uses the same ABI for the values in the pthread_once_t
530d5acd74SJohn Marino  * structure as the threading library, it is safe for a process to
540d5acd74SJohn Marino  * switch from _libc_once() to pthread_once() when threading is
550d5acd74SJohn Marino  * enabled.
560d5acd74SJohn Marino  */
570d5acd74SJohn Marino int
_once(pthread_once_t * once_control,void (* init_routine)(void))580d5acd74SJohn Marino _once(pthread_once_t *once_control, void (*init_routine)(void))
590d5acd74SJohn Marino {
600d5acd74SJohn Marino 
610d5acd74SJohn Marino 	if (__isthreaded)
620d5acd74SJohn Marino 		return (_pthread_once(once_control, init_routine));
630d5acd74SJohn Marino 	return (_libc_once(once_control, init_routine));
640d5acd74SJohn Marino }
65