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 29f8406b33Szrj #include <sys/cdefs.h> 3082205bfbSSimon Schubert #include <stdlib.h> 316692f0a1SAlex Hornung #include <pthread.h> 3219c7a913SDavid Xu 3317ea2221SMatthew Dillon /* 3417ea2221SMatthew Dillon * Weak symbols: All libc internal usage of these functions should 3517ea2221SMatthew Dillon * use the weak symbol versions (_pthread_XXX). If libpthread is 3617ea2221SMatthew Dillon * linked, it will override these functions with (non-weak) routines. 3717ea2221SMatthew Dillon * The _pthread_XXX functions are provided solely for internal libc 3817ea2221SMatthew Dillon * usage to avoid unwanted cancellation points and to differentiate 3917ea2221SMatthew Dillon * between application locks and libc locks (threads holding the 4017ea2221SMatthew Dillon * latter can't be allowed to exit/terminate). 4117ea2221SMatthew Dillon */ 42ce0e08e2SPeter Avalos 43ceeadccdSzrj #define WRlc(f, n) \ 44f8406b33Szrj __weak_reference_asm(f, _ ## n); 4582205bfbSSimon Schubert #define WR(f, n) \ 46f8406b33Szrj __weak_reference_asm(f, _ ## n); \ 47f8406b33Szrj __weak_reference_asm(f, n) 4882205bfbSSimon Schubert 49f8406b33Szrj /* XXX this needs something more clever, should not some of these return errors? */ 5082657471SMarkus Pfeiffer WR(__atfork, pthread_atfork); 5182205bfbSSimon Schubert WR(stub_zero, pthread_attr_destroy); 5282205bfbSSimon Schubert WR(stub_zero, pthread_attr_get_np); 533ae362f2SSepherosa Ziehau WR(stub_zero, pthread_attr_getaffinity_np); 5482205bfbSSimon Schubert WR(stub_zero, pthread_attr_getdetachstate); 550490f058SHasso Tepper WR(stub_zero, pthread_attr_getguardsize); 5682205bfbSSimon Schubert WR(stub_zero, pthread_attr_getinheritsched); 5782205bfbSSimon Schubert WR(stub_zero, pthread_attr_getschedparam); 5882205bfbSSimon Schubert WR(stub_zero, pthread_attr_getschedpolicy); 5982205bfbSSimon Schubert WR(stub_zero, pthread_attr_getscope); 6082205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstack); 6182205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstackaddr); 6282205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstacksize); 6382205bfbSSimon Schubert WR(stub_zero, pthread_attr_init); 643ae362f2SSepherosa Ziehau WR(stub_zero, pthread_attr_setaffinity_np); 6582205bfbSSimon Schubert WR(stub_zero, pthread_attr_setcreatesuspend_np); 6682205bfbSSimon Schubert WR(stub_zero, pthread_attr_setdetachstate); 670490f058SHasso Tepper WR(stub_zero, pthread_attr_setguardsize); 6882205bfbSSimon Schubert WR(stub_zero, pthread_attr_setinheritsched); 6982205bfbSSimon Schubert WR(stub_zero, pthread_attr_setschedparam); 7082205bfbSSimon Schubert WR(stub_zero, pthread_attr_setschedpolicy); 7182205bfbSSimon Schubert WR(stub_zero, pthread_attr_setscope); 7282205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstack); 7382205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstackaddr); 7482205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstacksize); 750490f058SHasso Tepper WR(stub_zero, pthread_barrier_destroy); 760490f058SHasso Tepper WR(stub_zero, pthread_barrier_init); 770490f058SHasso Tepper WR(stub_zero, pthread_barrier_wait); 780490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_destroy); 790490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_getpshared); 800490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_init); 810490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_setpshared); 82ceeadccdSzrj WRlc(stub_zero, pthread_cancel); 8382205bfbSSimon Schubert WR(stub_zero, pthread_cleanup_pop); 8482205bfbSSimon Schubert WR(stub_zero, pthread_cleanup_push); 8582205bfbSSimon Schubert WR(stub_zero, pthread_cond_broadcast); 8682205bfbSSimon Schubert WR(stub_zero, pthread_cond_destroy); 8782205bfbSSimon Schubert WR(stub_zero, pthread_cond_init); 8882205bfbSSimon Schubert WR(stub_zero, pthread_cond_signal); 8982205bfbSSimon Schubert WR(stub_zero, pthread_cond_timedwait); 9082205bfbSSimon Schubert WR(stub_zero, pthread_cond_wait); 9182205bfbSSimon Schubert WR(stub_zero, pthread_condattr_destroy); 920490f058SHasso Tepper WR(stub_zero, pthread_condattr_getclock); 930490f058SHasso Tepper WR(stub_zero, pthread_condattr_getpshared); 9482205bfbSSimon Schubert WR(stub_zero, pthread_condattr_init); 950490f058SHasso Tepper WR(stub_zero, pthread_condattr_setclock); 960490f058SHasso Tepper WR(stub_zero, pthread_condattr_setpshared); 9782205bfbSSimon Schubert WR(stub_zero, pthread_detach); 9882205bfbSSimon Schubert WR(stub_true, pthread_equal); 9982205bfbSSimon Schubert WR(stub_exit, pthread_exit); 100b2920380SSepherosa Ziehau WR(stub_zero, pthread_getaffinity_np); 10182205bfbSSimon Schubert WR(stub_zero, pthread_getconcurrency); 10282205bfbSSimon Schubert WR(stub_zero, pthread_getprio); 10382205bfbSSimon Schubert WR(stub_zero, pthread_getschedparam); 10482205bfbSSimon Schubert WR(stub_null, pthread_getspecific); 105*c0f25c26SImre Vadász WR(stub_zero, pthread_getthreadid_np); 106a809fd39SMarkus Pfeiffer WR(stub_empty, pthread_init_early); 10782205bfbSSimon Schubert WR(stub_zero, pthread_join); 10882205bfbSSimon Schubert WR(stub_zero, pthread_key_create); 10982205bfbSSimon Schubert WR(stub_zero, pthread_key_delete); 11082205bfbSSimon Schubert WR(stub_zero, pthread_kill); 11182205bfbSSimon Schubert WR(stub_main, pthread_main_np); 11282205bfbSSimon Schubert WR(stub_zero, pthread_multi_np); 11382205bfbSSimon Schubert WR(stub_zero, pthread_mutex_destroy); 11482205bfbSSimon Schubert WR(stub_zero, pthread_mutex_getprioceiling); 11582205bfbSSimon Schubert WR(stub_zero, pthread_mutex_init); 11682205bfbSSimon Schubert WR(stub_zero, pthread_mutex_lock); 11782205bfbSSimon Schubert WR(stub_zero, pthread_mutex_setprioceiling); 1180490f058SHasso Tepper WR(stub_zero, pthread_mutex_timedlock); 11982205bfbSSimon Schubert WR(stub_zero, pthread_mutex_trylock); 12082205bfbSSimon Schubert WR(stub_zero, pthread_mutex_unlock); 12182205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_destroy); 12282205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getkind_np); 12382205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getprioceiling); 12482205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getprotocol); 12582205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getpshared); 12682205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_gettype); 12782205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_init); 12882205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setkind_np); 12982205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setprioceiling); 13082205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setprotocol); 13182205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setpshared); 13282205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_settype); 1336692f0a1SAlex Hornung WR(stub_once, pthread_once); 13482205bfbSSimon Schubert WR(stub_zero, pthread_resume_all_np); 13582205bfbSSimon Schubert WR(stub_zero, pthread_resume_np); 13682205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_destroy); 13782205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_init); 13882205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_rdlock); 1390490f058SHasso Tepper WR(stub_zero, pthread_rwlock_timedrdlock); 1400490f058SHasso Tepper WR(stub_zero, pthread_rwlock_timedwrlock); 14182205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_tryrdlock); 14282205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_trywrlock); 14382205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_unlock); 14482205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_wrlock); 14582205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_destroy); 14682205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_getpshared); 14782205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_init); 14882205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_setpshared); 14982205bfbSSimon Schubert WR(stub_self, pthread_self); 15082205bfbSSimon Schubert WR(stub_zero, pthread_set_name_np); 151b2920380SSepherosa Ziehau WR(stub_zero, pthread_setaffinity_np); 15282205bfbSSimon Schubert WR(stub_zero, pthread_setcancelstate); 15382205bfbSSimon Schubert WR(stub_zero, pthread_setcanceltype); 15482205bfbSSimon Schubert WR(stub_zero, pthread_setconcurrency); 15582205bfbSSimon Schubert WR(stub_zero, pthread_setprio); 15682205bfbSSimon Schubert WR(stub_zero, pthread_setschedparam); 15782205bfbSSimon Schubert WR(stub_zero, pthread_setspecific); 15882205bfbSSimon Schubert WR(stub_zero, pthread_sigmask); 15982205bfbSSimon Schubert WR(stub_zero, pthread_single_np); 1600490f058SHasso Tepper WR(stub_zero, pthread_spin_destroy); 1610490f058SHasso Tepper WR(stub_zero, pthread_spin_init); 1620490f058SHasso Tepper WR(stub_zero, pthread_spin_lock); 1630490f058SHasso Tepper WR(stub_zero, pthread_spin_trylock); 1640490f058SHasso Tepper WR(stub_zero, pthread_spin_unlock); 16582205bfbSSimon Schubert WR(stub_zero, pthread_suspend_all_np); 16682205bfbSSimon Schubert WR(stub_zero, pthread_suspend_np); 16782205bfbSSimon Schubert WR(stub_zero, pthread_switch_add_np); 16882205bfbSSimon Schubert WR(stub_zero, pthread_switch_delete_np); 16982205bfbSSimon Schubert WR(stub_zero, pthread_testcancel); 1700490f058SHasso Tepper WR(stub_zero, pthread_timedjoin_np); 17182205bfbSSimon Schubert WR(stub_zero, pthread_yield); 17282205bfbSSimon Schubert WR(stub_zero, sched_yield); 17382205bfbSSimon Schubert WR(stub_zero, sem_close); 17482205bfbSSimon Schubert WR(stub_zero, sem_destroy); 17582205bfbSSimon Schubert WR(stub_zero, sem_getvalue); 17682205bfbSSimon Schubert WR(stub_zero, sem_init); 17782205bfbSSimon Schubert WR(stub_zero, sem_open); 17882205bfbSSimon Schubert WR(stub_zero, sem_post); 17982205bfbSSimon Schubert WR(stub_zero, sem_trywait); 180775923a8SSascha Wildner WR(stub_zero, sem_timedwait); 18182205bfbSSimon Schubert WR(stub_zero, sem_unlink); 18282205bfbSSimon Schubert WR(stub_zero, sem_wait); 18382205bfbSSimon Schubert 18482205bfbSSimon Schubert 18582205bfbSSimon Schubert static int __used 18682205bfbSSimon Schubert stub_zero(void) 187ce0e08e2SPeter Avalos { 188ce0e08e2SPeter Avalos return (0); 189ce0e08e2SPeter Avalos } 190ce0e08e2SPeter Avalos 1916692f0a1SAlex Hornung static int __used 1926692f0a1SAlex Hornung stub_once(pthread_once_t *o, void (*r)(void)) 1936692f0a1SAlex Hornung { 1946692f0a1SAlex Hornung if (o->state != PTHREAD_DONE_INIT) { 1956692f0a1SAlex Hornung (*r)(); 1966692f0a1SAlex Hornung o->state = PTHREAD_DONE_INIT; 1976692f0a1SAlex Hornung } 1986692f0a1SAlex Hornung 1996692f0a1SAlex Hornung return (0); 2006692f0a1SAlex Hornung } 2016692f0a1SAlex Hornung 20282205bfbSSimon Schubert static void * __used 20382205bfbSSimon Schubert stub_null(void) 20417ea2221SMatthew Dillon { 20517ea2221SMatthew Dillon return (NULL); 20617ea2221SMatthew Dillon } 20717ea2221SMatthew Dillon 20882205bfbSSimon Schubert static void * __used 20982205bfbSSimon Schubert stub_self(void) 21017ea2221SMatthew Dillon { 21182205bfbSSimon Schubert static struct {} main_thread; 21282205bfbSSimon Schubert 21382205bfbSSimon Schubert return (&main_thread); 21417ea2221SMatthew Dillon } 21517ea2221SMatthew Dillon 21682205bfbSSimon Schubert static int __used 21782205bfbSSimon Schubert stub_main(void) 218ce0e08e2SPeter Avalos { 219ce0e08e2SPeter Avalos return (-1); 220ce0e08e2SPeter Avalos } 221ce0e08e2SPeter Avalos 22282205bfbSSimon Schubert static int __used 22382205bfbSSimon Schubert stub_true(void) 22417ea2221SMatthew Dillon { 22582205bfbSSimon Schubert return (1); 22617ea2221SMatthew Dillon } 22717ea2221SMatthew Dillon 22882205bfbSSimon Schubert static void __used 229a809fd39SMarkus Pfeiffer stub_empty(void) 230a809fd39SMarkus Pfeiffer { 231a809fd39SMarkus Pfeiffer } 232a809fd39SMarkus Pfeiffer 233a809fd39SMarkus Pfeiffer static void __used 23482205bfbSSimon Schubert stub_exit(void) 23517ea2221SMatthew Dillon { 23682205bfbSSimon Schubert exit(0); 237ce0e08e2SPeter Avalos } 238a809fd39SMarkus Pfeiffer 239a809fd39SMarkus Pfeiffer /* 240a809fd39SMarkus Pfeiffer * If libpthread is loaded, make sure it is initialised before 241a809fd39SMarkus Pfeiffer * other libraries call pthread functions 242a809fd39SMarkus Pfeiffer */ 243450f08dbSSascha Wildner void _pthread_init(void) __constructor(101); 244450f08dbSSascha Wildner void _pthread_init_early(void); 245a809fd39SMarkus Pfeiffer void 246a809fd39SMarkus Pfeiffer _pthread_init(void) 247a809fd39SMarkus Pfeiffer { 248a809fd39SMarkus Pfeiffer _pthread_init_early(); 249a809fd39SMarkus Pfeiffer } 25082657471SMarkus Pfeiffer 25182657471SMarkus Pfeiffer extern void (*cb_prepare)(void); 25282657471SMarkus Pfeiffer extern void (*cb_parent)(void); 25382657471SMarkus Pfeiffer extern void (*cb_child)(void); 25482657471SMarkus Pfeiffer extern int __isthreaded; 25582657471SMarkus Pfeiffer 2560d67d370SSascha Wildner static int __used 25782657471SMarkus Pfeiffer __atfork(void (*prepare)(void), void (*parent)(void), 25882657471SMarkus Pfeiffer void (*child)(void)) 25982657471SMarkus Pfeiffer { 26082657471SMarkus Pfeiffer if (__isthreaded) 26182657471SMarkus Pfeiffer return (-1); 26282657471SMarkus Pfeiffer cb_prepare = prepare; 26382657471SMarkus Pfeiffer cb_parent = parent; 26482657471SMarkus Pfeiffer cb_child = child; 26582657471SMarkus Pfeiffer return (0); 26682657471SMarkus Pfeiffer } 267