xref: /dflybsd-src/lib/libc/gen/_thread_init.c (revision 721505dec240e78696660384d988da78813a33bd)
117ea2221SMatthew Dillon /*
217ea2221SMatthew Dillon  * Copyright (c) 2001 Daniel Eischen <deischen@FreeBSD.org>
317ea2221SMatthew Dillon  * All rights reserved.
417ea2221SMatthew Dillon  *
517ea2221SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
617ea2221SMatthew Dillon  * modification, are permitted provided that the following conditions
717ea2221SMatthew Dillon  * are met:
817ea2221SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
917ea2221SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
1017ea2221SMatthew Dillon  * 2. Neither the name of the author nor the names of any co-contributors
1117ea2221SMatthew Dillon  *    may be used to endorse or promote products derived from this software
1217ea2221SMatthew Dillon  *    without specific prior written permission.
1317ea2221SMatthew Dillon  *
1417ea2221SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS''
1517ea2221SMatthew Dillon  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1617ea2221SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1717ea2221SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1817ea2221SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1917ea2221SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2017ea2221SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2117ea2221SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2217ea2221SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2317ea2221SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2417ea2221SMatthew Dillon  * SUCH DAMAGE.
2517ea2221SMatthew Dillon  *
2617ea2221SMatthew Dillon  */
2717ea2221SMatthew Dillon 
28f8406b33Szrj #include <sys/cdefs.h>
2917ea2221SMatthew Dillon #include <sys/types.h>
30*721505deSMatthew Dillon #include "libc_private.h"
3117ea2221SMatthew Dillon 
32434dae9bSJoerg Sonnenberger void	_thread_init_stub(void);
33*721505deSMatthew Dillon void	_nmalloc_thr_init(void);
34*721505deSMatthew Dillon void	_upmap_thr_init(void);
35434dae9bSJoerg Sonnenberger 
3617ea2221SMatthew Dillon int	_thread_autoinit_dummy_decl_stub = 0;
3717ea2221SMatthew Dillon 
38*721505deSMatthew Dillon /*
39*721505deSMatthew Dillon  * This stub is overridden when libpthreads is linked in.  However,
40*721505deSMatthew Dillon  * we can apply the constructor to the weak reference.  Instead the
41*721505deSMatthew Dillon  * constructor is applied to the stub and we do a run-time check to
42*721505deSMatthew Dillon  * see if the stub has been overridden by pthreads.
43*721505deSMatthew Dillon  */
44*721505deSMatthew Dillon void _thread_init_stub(void) __attribute__ ((constructor));
45*721505deSMatthew Dillon __weak_reference(_thread_init_stub, _thread_init);
46*721505deSMatthew Dillon 
4717ea2221SMatthew Dillon void
_thread_init_stub(void)4817ea2221SMatthew Dillon _thread_init_stub(void)
4917ea2221SMatthew Dillon {
50*721505deSMatthew Dillon 	/*
51*721505deSMatthew Dillon 	 * Only run libc related pthreads initialization from here
52*721505deSMatthew Dillon 	 * if pthreads did not override the weak reference.  Otherwise
53*721505deSMatthew Dillon 	 * pthreads will do it after setting up a real thread context.
54*721505deSMatthew Dillon 	 */
55*721505deSMatthew Dillon 	if (_thread_init == _thread_init_stub) {
56*721505deSMatthew Dillon 		_libc_thr_init();
57*721505deSMatthew Dillon 	}
5817ea2221SMatthew Dillon }
59f8406b33Szrj 
60*721505deSMatthew Dillon /*
61*721505deSMatthew Dillon  * Pre-initialization prior to thread startup to avoid certain
62*721505deSMatthew Dillon  * chicken-and-egg issues with low-level allocations.
63*721505deSMatthew Dillon  */
64*721505deSMatthew Dillon void
_libc_thr_init(void)65*721505deSMatthew Dillon _libc_thr_init(void)
66*721505deSMatthew Dillon {
67*721505deSMatthew Dillon 	_nmalloc_thr_init();
68*721505deSMatthew Dillon 	_upmap_thr_init();
69*721505deSMatthew Dillon }
70*721505deSMatthew Dillon 
71f8406b33Szrj __weak_reference(_thread_autoinit_dummy_decl_stub, _thread_autoinit_dummy_decl);
72