xref: /dflybsd-src/lib/libc/gen/_pthread_stubs.c (revision ceeadccd358e346511796c7b057af4af0ae50f2d)
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. Redistributions in binary form must reproduce the above copyright
1117ea2221SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
1217ea2221SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
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  *
26ce0e08e2SPeter Avalos  * $FreeBSD: src/lib/libc/gen/_pthread_stubs.c,v 1.5 2001/06/11 23:18:22 iedowse Exp $
2717ea2221SMatthew Dillon  */
2817ea2221SMatthew Dillon 
2982205bfbSSimon Schubert #include <stdlib.h>
306692f0a1SAlex Hornung #include <pthread.h>
3119c7a913SDavid Xu 
3217ea2221SMatthew Dillon /*
3317ea2221SMatthew Dillon  * Weak symbols: All libc internal usage of these functions should
3417ea2221SMatthew Dillon  * use the weak symbol versions (_pthread_XXX).  If libpthread is
3517ea2221SMatthew Dillon  * linked, it will override these functions with (non-weak) routines.
3617ea2221SMatthew Dillon  * The _pthread_XXX functions are provided solely for internal libc
3717ea2221SMatthew Dillon  * usage to avoid unwanted cancellation points and to differentiate
3817ea2221SMatthew Dillon  * between application locks and libc locks (threads holding the
3917ea2221SMatthew Dillon  * latter can't be allowed to exit/terminate).
4017ea2221SMatthew Dillon  */
41ce0e08e2SPeter Avalos 
42*ceeadccdSzrj #define WRlc(f, n)			\
43*ceeadccdSzrj     __weak_reference(f, _ ## n);
4482205bfbSSimon Schubert #define WR(f, n)			\
4582205bfbSSimon Schubert     __weak_reference(f, _ ## n);	\
4682205bfbSSimon Schubert     __weak_reference(f, n)
4782205bfbSSimon Schubert 
4882657471SMarkus Pfeiffer WR(__atfork, pthread_atfork);
4982205bfbSSimon Schubert WR(stub_zero, pthread_attr_destroy);
5082205bfbSSimon Schubert WR(stub_zero, pthread_attr_get_np);
5182205bfbSSimon Schubert WR(stub_zero, pthread_attr_getdetachstate);
520490f058SHasso Tepper WR(stub_zero, pthread_attr_getguardsize);
5382205bfbSSimon Schubert WR(stub_zero, pthread_attr_getinheritsched);
5482205bfbSSimon Schubert WR(stub_zero, pthread_attr_getschedparam);
5582205bfbSSimon Schubert WR(stub_zero, pthread_attr_getschedpolicy);
5682205bfbSSimon Schubert WR(stub_zero, pthread_attr_getscope);
5782205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstack);
5882205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstackaddr);
5982205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstacksize);
6082205bfbSSimon Schubert WR(stub_zero, pthread_attr_init);
6182205bfbSSimon Schubert WR(stub_zero, pthread_attr_setcreatesuspend_np);
6282205bfbSSimon Schubert WR(stub_zero, pthread_attr_setdetachstate);
630490f058SHasso Tepper WR(stub_zero, pthread_attr_setguardsize);
6482205bfbSSimon Schubert WR(stub_zero, pthread_attr_setinheritsched);
6582205bfbSSimon Schubert WR(stub_zero, pthread_attr_setschedparam);
6682205bfbSSimon Schubert WR(stub_zero, pthread_attr_setschedpolicy);
6782205bfbSSimon Schubert WR(stub_zero, pthread_attr_setscope);
6882205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstack);
6982205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstackaddr);
7082205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstacksize);
710490f058SHasso Tepper WR(stub_zero, pthread_barrier_destroy);
720490f058SHasso Tepper WR(stub_zero, pthread_barrier_init);
730490f058SHasso Tepper WR(stub_zero, pthread_barrier_wait);
740490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_destroy);
750490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_getpshared);
760490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_init);
770490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_setpshared);
78*ceeadccdSzrj WRlc(stub_zero, pthread_cancel);
7982205bfbSSimon Schubert WR(stub_zero, pthread_cleanup_pop);
8082205bfbSSimon Schubert WR(stub_zero, pthread_cleanup_push);
8182205bfbSSimon Schubert WR(stub_zero, pthread_cond_broadcast);
8282205bfbSSimon Schubert WR(stub_zero, pthread_cond_destroy);
8382205bfbSSimon Schubert WR(stub_zero, pthread_cond_init);
8482205bfbSSimon Schubert WR(stub_zero, pthread_cond_signal);
8582205bfbSSimon Schubert WR(stub_zero, pthread_cond_timedwait);
8682205bfbSSimon Schubert WR(stub_zero, pthread_cond_wait);
8782205bfbSSimon Schubert WR(stub_zero, pthread_condattr_destroy);
880490f058SHasso Tepper WR(stub_zero, pthread_condattr_getclock);
890490f058SHasso Tepper WR(stub_zero, pthread_condattr_getpshared);
9082205bfbSSimon Schubert WR(stub_zero, pthread_condattr_init);
910490f058SHasso Tepper WR(stub_zero, pthread_condattr_setclock);
920490f058SHasso Tepper WR(stub_zero, pthread_condattr_setpshared);
9382205bfbSSimon Schubert WR(stub_zero, pthread_detach);
9482205bfbSSimon Schubert WR(stub_true, pthread_equal);
9582205bfbSSimon Schubert WR(stub_exit, pthread_exit);
9682205bfbSSimon Schubert WR(stub_zero, pthread_getconcurrency);
9782205bfbSSimon Schubert WR(stub_zero, pthread_getprio);
9882205bfbSSimon Schubert WR(stub_zero, pthread_getschedparam);
9982205bfbSSimon Schubert WR(stub_null, pthread_getspecific);
100a809fd39SMarkus Pfeiffer WR(stub_empty, pthread_init_early);
10182205bfbSSimon Schubert WR(stub_zero, pthread_join);
10282205bfbSSimon Schubert WR(stub_zero, pthread_key_create);
10382205bfbSSimon Schubert WR(stub_zero, pthread_key_delete);
10482205bfbSSimon Schubert WR(stub_zero, pthread_kill);
10582205bfbSSimon Schubert WR(stub_main, pthread_main_np);
10682205bfbSSimon Schubert WR(stub_zero, pthread_multi_np);
10782205bfbSSimon Schubert WR(stub_zero, pthread_mutex_destroy);
10882205bfbSSimon Schubert WR(stub_zero, pthread_mutex_getprioceiling);
10982205bfbSSimon Schubert WR(stub_zero, pthread_mutex_init);
11082205bfbSSimon Schubert WR(stub_zero, pthread_mutex_lock);
11182205bfbSSimon Schubert WR(stub_zero, pthread_mutex_setprioceiling);
1120490f058SHasso Tepper WR(stub_zero, pthread_mutex_timedlock);
11382205bfbSSimon Schubert WR(stub_zero, pthread_mutex_trylock);
11482205bfbSSimon Schubert WR(stub_zero, pthread_mutex_unlock);
11582205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_destroy);
11682205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getkind_np);
11782205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getprioceiling);
11882205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getprotocol);
11982205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getpshared);
12082205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_gettype);
12182205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_init);
12282205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setkind_np);
12382205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setprioceiling);
12482205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setprotocol);
12582205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setpshared);
12682205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_settype);
1276692f0a1SAlex Hornung WR(stub_once, pthread_once);
12882205bfbSSimon Schubert WR(stub_zero, pthread_resume_all_np);
12982205bfbSSimon Schubert WR(stub_zero, pthread_resume_np);
13082205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_destroy);
13182205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_init);
13282205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_rdlock);
1330490f058SHasso Tepper WR(stub_zero, pthread_rwlock_timedrdlock);
1340490f058SHasso Tepper WR(stub_zero, pthread_rwlock_timedwrlock);
13582205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_tryrdlock);
13682205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_trywrlock);
13782205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_unlock);
13882205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_wrlock);
13982205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_destroy);
14082205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_getpshared);
14182205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_init);
14282205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_setpshared);
14382205bfbSSimon Schubert WR(stub_self, pthread_self);
14482205bfbSSimon Schubert WR(stub_zero, pthread_set_name_np);
14582205bfbSSimon Schubert WR(stub_zero, pthread_setcancelstate);
14682205bfbSSimon Schubert WR(stub_zero, pthread_setcanceltype);
14782205bfbSSimon Schubert WR(stub_zero, pthread_setconcurrency);
14882205bfbSSimon Schubert WR(stub_zero, pthread_setprio);
14982205bfbSSimon Schubert WR(stub_zero, pthread_setschedparam);
15082205bfbSSimon Schubert WR(stub_zero, pthread_setspecific);
15182205bfbSSimon Schubert WR(stub_zero, pthread_sigmask);
15282205bfbSSimon Schubert WR(stub_zero, pthread_single_np);
1530490f058SHasso Tepper WR(stub_zero, pthread_spin_destroy);
1540490f058SHasso Tepper WR(stub_zero, pthread_spin_init);
1550490f058SHasso Tepper WR(stub_zero, pthread_spin_lock);
1560490f058SHasso Tepper WR(stub_zero, pthread_spin_trylock);
1570490f058SHasso Tepper WR(stub_zero, pthread_spin_unlock);
15882205bfbSSimon Schubert WR(stub_zero, pthread_suspend_all_np);
15982205bfbSSimon Schubert WR(stub_zero, pthread_suspend_np);
16082205bfbSSimon Schubert WR(stub_zero, pthread_switch_add_np);
16182205bfbSSimon Schubert WR(stub_zero, pthread_switch_delete_np);
16282205bfbSSimon Schubert WR(stub_zero, pthread_testcancel);
1630490f058SHasso Tepper WR(stub_zero, pthread_timedjoin_np);
16482205bfbSSimon Schubert WR(stub_zero, pthread_yield);
16582205bfbSSimon Schubert WR(stub_zero, sched_yield);
16682205bfbSSimon Schubert WR(stub_zero, sem_close);
16782205bfbSSimon Schubert WR(stub_zero, sem_destroy);
16882205bfbSSimon Schubert WR(stub_zero, sem_getvalue);
16982205bfbSSimon Schubert WR(stub_zero, sem_init);
17082205bfbSSimon Schubert WR(stub_zero, sem_open);
17182205bfbSSimon Schubert WR(stub_zero, sem_post);
17282205bfbSSimon Schubert WR(stub_zero, sem_trywait);
173775923a8SSascha Wildner WR(stub_zero, sem_timedwait);
17482205bfbSSimon Schubert WR(stub_zero, sem_unlink);
17582205bfbSSimon Schubert WR(stub_zero, sem_wait);
17682205bfbSSimon Schubert 
17782205bfbSSimon Schubert 
17882205bfbSSimon Schubert static int __used
17982205bfbSSimon Schubert stub_zero(void)
180ce0e08e2SPeter Avalos {
181ce0e08e2SPeter Avalos 	return (0);
182ce0e08e2SPeter Avalos }
183ce0e08e2SPeter Avalos 
1846692f0a1SAlex Hornung static int __used
1856692f0a1SAlex Hornung stub_once(pthread_once_t *o, void (*r)(void))
1866692f0a1SAlex Hornung {
1876692f0a1SAlex Hornung 	if (o->state != PTHREAD_DONE_INIT) {
1886692f0a1SAlex Hornung 		(*r)();
1896692f0a1SAlex Hornung 		o->state = PTHREAD_DONE_INIT;
1906692f0a1SAlex Hornung 	}
1916692f0a1SAlex Hornung 
1926692f0a1SAlex Hornung 	return (0);
1936692f0a1SAlex Hornung }
1946692f0a1SAlex Hornung 
19582205bfbSSimon Schubert static void * __used
19682205bfbSSimon Schubert stub_null(void)
19717ea2221SMatthew Dillon {
19817ea2221SMatthew Dillon 	return (NULL);
19917ea2221SMatthew Dillon }
20017ea2221SMatthew Dillon 
20182205bfbSSimon Schubert static void * __used
20282205bfbSSimon Schubert stub_self(void)
20317ea2221SMatthew Dillon {
20482205bfbSSimon Schubert 	static struct {} main_thread;
20582205bfbSSimon Schubert 
20682205bfbSSimon Schubert 	return (&main_thread);
20717ea2221SMatthew Dillon }
20817ea2221SMatthew Dillon 
20982205bfbSSimon Schubert static int __used
21082205bfbSSimon Schubert stub_main(void)
211ce0e08e2SPeter Avalos {
212ce0e08e2SPeter Avalos 	return (-1);
213ce0e08e2SPeter Avalos }
214ce0e08e2SPeter Avalos 
21582205bfbSSimon Schubert static int __used
21682205bfbSSimon Schubert stub_true(void)
21717ea2221SMatthew Dillon {
21882205bfbSSimon Schubert 	return (1);
21917ea2221SMatthew Dillon }
22017ea2221SMatthew Dillon 
22182205bfbSSimon Schubert static void __used
222a809fd39SMarkus Pfeiffer stub_empty(void)
223a809fd39SMarkus Pfeiffer {
224a809fd39SMarkus Pfeiffer }
225a809fd39SMarkus Pfeiffer 
226a809fd39SMarkus Pfeiffer static void __used
22782205bfbSSimon Schubert stub_exit(void)
22817ea2221SMatthew Dillon {
22982205bfbSSimon Schubert 	exit(0);
230ce0e08e2SPeter Avalos }
231a809fd39SMarkus Pfeiffer 
232a809fd39SMarkus Pfeiffer /*
233a809fd39SMarkus Pfeiffer  * If libpthread is loaded, make sure it is initialised before
234a809fd39SMarkus Pfeiffer  * other libraries call pthread functions
235a809fd39SMarkus Pfeiffer  */
236450f08dbSSascha Wildner void _pthread_init(void) __constructor(101);
237450f08dbSSascha Wildner void _pthread_init_early(void);
238a809fd39SMarkus Pfeiffer void
239a809fd39SMarkus Pfeiffer _pthread_init(void)
240a809fd39SMarkus Pfeiffer {
241a809fd39SMarkus Pfeiffer 	_pthread_init_early();
242a809fd39SMarkus Pfeiffer }
24382657471SMarkus Pfeiffer 
24482657471SMarkus Pfeiffer extern void (*cb_prepare)(void);
24582657471SMarkus Pfeiffer extern void (*cb_parent)(void);
24682657471SMarkus Pfeiffer extern void (*cb_child)(void);
24782657471SMarkus Pfeiffer extern int __isthreaded;
24882657471SMarkus Pfeiffer 
2490d67d370SSascha Wildner static int __used
25082657471SMarkus Pfeiffer __atfork(void (*prepare)(void), void (*parent)(void),
25182657471SMarkus Pfeiffer     void (*child)(void))
25282657471SMarkus Pfeiffer {
25382657471SMarkus Pfeiffer 	if (__isthreaded)
25482657471SMarkus Pfeiffer 		return (-1);
25582657471SMarkus Pfeiffer 	cb_prepare = prepare;
25682657471SMarkus Pfeiffer 	cb_parent = parent;
25782657471SMarkus Pfeiffer 	cb_child = child;
25882657471SMarkus Pfeiffer 	return (0);
25982657471SMarkus Pfeiffer }
260