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); 98b58f145cSzrj WR(stub_equal, pthread_equal); 9982205bfbSSimon Schubert WR(stub_exit, pthread_exit); 100b2920380SSepherosa Ziehau WR(stub_zero, pthread_getaffinity_np); 10182205bfbSSimon Schubert WR(stub_zero, pthread_getconcurrency); 102725edadfSSascha Wildner WR(stub_zero, pthread_getcpuclockid); 10382205bfbSSimon Schubert WR(stub_zero, pthread_getprio); 10482205bfbSSimon Schubert WR(stub_zero, pthread_getschedparam); 10582205bfbSSimon Schubert WR(stub_null, pthread_getspecific); 106c0f25c26SImre Vadász WR(stub_zero, pthread_getthreadid_np); 107a809fd39SMarkus Pfeiffer WR(stub_empty, pthread_init_early); 10882205bfbSSimon Schubert WR(stub_zero, pthread_join); 10982205bfbSSimon Schubert WR(stub_zero, pthread_key_create); 11082205bfbSSimon Schubert WR(stub_zero, pthread_key_delete); 11182205bfbSSimon Schubert WR(stub_zero, pthread_kill); 11282205bfbSSimon Schubert WR(stub_main, pthread_main_np); 11382205bfbSSimon Schubert WR(stub_zero, pthread_multi_np); 11482205bfbSSimon Schubert WR(stub_zero, pthread_mutex_destroy); 11582205bfbSSimon Schubert WR(stub_zero, pthread_mutex_getprioceiling); 11682205bfbSSimon Schubert WR(stub_zero, pthread_mutex_init); 11782205bfbSSimon Schubert WR(stub_zero, pthread_mutex_lock); 11882205bfbSSimon Schubert WR(stub_zero, pthread_mutex_setprioceiling); 1190490f058SHasso Tepper WR(stub_zero, pthread_mutex_timedlock); 12082205bfbSSimon Schubert WR(stub_zero, pthread_mutex_trylock); 12182205bfbSSimon Schubert WR(stub_zero, pthread_mutex_unlock); 12282205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_destroy); 12382205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getkind_np); 12482205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getprioceiling); 12582205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getprotocol); 12682205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getpshared); 12782205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_gettype); 12882205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_init); 12982205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setkind_np); 13082205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setprioceiling); 13182205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setprotocol); 13282205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setpshared); 13382205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_settype); 1346692f0a1SAlex Hornung WR(stub_once, pthread_once); 13582205bfbSSimon Schubert WR(stub_zero, pthread_resume_all_np); 13682205bfbSSimon Schubert WR(stub_zero, pthread_resume_np); 13782205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_destroy); 13882205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_init); 13982205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_rdlock); 1400490f058SHasso Tepper WR(stub_zero, pthread_rwlock_timedrdlock); 1410490f058SHasso Tepper WR(stub_zero, pthread_rwlock_timedwrlock); 14282205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_tryrdlock); 14382205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_trywrlock); 14482205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_unlock); 14582205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_wrlock); 14682205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_destroy); 14782205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_getpshared); 14882205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_init); 14982205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_setpshared); 15082205bfbSSimon Schubert WR(stub_self, pthread_self); 151eb396e51SMatthew Dillon WR(stub_pthread_get_name_np, pthread_get_name_np); 15282205bfbSSimon Schubert WR(stub_zero, pthread_set_name_np); 153b2920380SSepherosa Ziehau WR(stub_zero, pthread_setaffinity_np); 15482205bfbSSimon Schubert WR(stub_zero, pthread_setcancelstate); 15582205bfbSSimon Schubert WR(stub_zero, pthread_setcanceltype); 15682205bfbSSimon Schubert WR(stub_zero, pthread_setconcurrency); 15782205bfbSSimon Schubert WR(stub_zero, pthread_setprio); 15882205bfbSSimon Schubert WR(stub_zero, pthread_setschedparam); 15982205bfbSSimon Schubert WR(stub_zero, pthread_setspecific); 16082205bfbSSimon Schubert WR(stub_zero, pthread_sigmask); 16182205bfbSSimon Schubert WR(stub_zero, pthread_single_np); 1620490f058SHasso Tepper WR(stub_zero, pthread_spin_destroy); 1630490f058SHasso Tepper WR(stub_zero, pthread_spin_init); 1640490f058SHasso Tepper WR(stub_zero, pthread_spin_lock); 1650490f058SHasso Tepper WR(stub_zero, pthread_spin_trylock); 1660490f058SHasso Tepper WR(stub_zero, pthread_spin_unlock); 16782205bfbSSimon Schubert WR(stub_zero, pthread_suspend_all_np); 16882205bfbSSimon Schubert WR(stub_zero, pthread_suspend_np); 16982205bfbSSimon Schubert WR(stub_zero, pthread_switch_add_np); 17082205bfbSSimon Schubert WR(stub_zero, pthread_switch_delete_np); 17182205bfbSSimon Schubert WR(stub_zero, pthread_testcancel); 1720490f058SHasso Tepper WR(stub_zero, pthread_timedjoin_np); 17382205bfbSSimon Schubert WR(stub_zero, pthread_yield); 17482205bfbSSimon Schubert WR(stub_zero, sem_close); 17582205bfbSSimon Schubert WR(stub_zero, sem_destroy); 17682205bfbSSimon Schubert WR(stub_zero, sem_getvalue); 17782205bfbSSimon Schubert WR(stub_zero, sem_init); 17882205bfbSSimon Schubert WR(stub_zero, sem_open); 17982205bfbSSimon Schubert WR(stub_zero, sem_post); 18082205bfbSSimon Schubert WR(stub_zero, sem_trywait); 181775923a8SSascha Wildner WR(stub_zero, sem_timedwait); 18282205bfbSSimon Schubert WR(stub_zero, sem_unlink); 18382205bfbSSimon Schubert WR(stub_zero, sem_wait); 18482205bfbSSimon Schubert 18582205bfbSSimon Schubert 18682205bfbSSimon Schubert static int __used 18782205bfbSSimon Schubert stub_zero(void) 188ce0e08e2SPeter Avalos { 189ce0e08e2SPeter Avalos return (0); 190ce0e08e2SPeter Avalos } 191ce0e08e2SPeter Avalos 1926692f0a1SAlex Hornung static int __used 1936692f0a1SAlex Hornung stub_once(pthread_once_t *o, void (*r)(void)) 1946692f0a1SAlex Hornung { 195*cf8046a9Szrj if (o->__state != PTHREAD_DONE_INIT) { 1966692f0a1SAlex Hornung (*r)(); 197*cf8046a9Szrj o->__state = PTHREAD_DONE_INIT; 1986692f0a1SAlex Hornung } 1996692f0a1SAlex Hornung 2006692f0a1SAlex Hornung return (0); 2016692f0a1SAlex Hornung } 2026692f0a1SAlex Hornung 20382205bfbSSimon Schubert static void * __used 20482205bfbSSimon Schubert stub_null(void) 20517ea2221SMatthew Dillon { 20617ea2221SMatthew Dillon return (NULL); 20717ea2221SMatthew Dillon } 20817ea2221SMatthew Dillon 20982205bfbSSimon Schubert static void * __used 21082205bfbSSimon Schubert stub_self(void) 21117ea2221SMatthew Dillon { 21282205bfbSSimon Schubert static struct {} main_thread; 21382205bfbSSimon Schubert 21482205bfbSSimon Schubert return (&main_thread); 21517ea2221SMatthew Dillon } 21617ea2221SMatthew Dillon 21782205bfbSSimon Schubert static int __used 21882205bfbSSimon Schubert stub_main(void) 219ce0e08e2SPeter Avalos { 220ce0e08e2SPeter Avalos return (-1); 221ce0e08e2SPeter Avalos } 222ce0e08e2SPeter Avalos 22382205bfbSSimon Schubert static int __used 224b58f145cSzrj stub_equal(pthread_t a, pthread_t b) 22517ea2221SMatthew Dillon { 226b58f145cSzrj return (a == b); 22717ea2221SMatthew Dillon } 22817ea2221SMatthew Dillon 22982205bfbSSimon Schubert static void __used 230a809fd39SMarkus Pfeiffer stub_empty(void) 231a809fd39SMarkus Pfeiffer { 232a809fd39SMarkus Pfeiffer } 233a809fd39SMarkus Pfeiffer 234a809fd39SMarkus Pfeiffer static void __used 23582205bfbSSimon Schubert stub_exit(void) 23617ea2221SMatthew Dillon { 23782205bfbSSimon Schubert exit(0); 238ce0e08e2SPeter Avalos } 239a809fd39SMarkus Pfeiffer 240eb396e51SMatthew Dillon static void __used 241eb396e51SMatthew Dillon stub_pthread_get_name_np(pthread_t a, char *name, size_t len) 242eb396e51SMatthew Dillon { 243eb396e51SMatthew Dillon if (len) 244eb396e51SMatthew Dillon name[0] = 0; 245eb396e51SMatthew Dillon } 246eb396e51SMatthew Dillon 247a809fd39SMarkus Pfeiffer /* 248a809fd39SMarkus Pfeiffer * If libpthread is loaded, make sure it is initialised before 249a809fd39SMarkus Pfeiffer * other libraries call pthread functions 250a809fd39SMarkus Pfeiffer */ 251450f08dbSSascha Wildner void _pthread_init(void) __constructor(101); 252450f08dbSSascha Wildner void _pthread_init_early(void); 253a809fd39SMarkus Pfeiffer void 254a809fd39SMarkus Pfeiffer _pthread_init(void) 255a809fd39SMarkus Pfeiffer { 256a809fd39SMarkus Pfeiffer _pthread_init_early(); 257a809fd39SMarkus Pfeiffer } 25882657471SMarkus Pfeiffer 25982657471SMarkus Pfeiffer extern void (*cb_prepare)(void); 26082657471SMarkus Pfeiffer extern void (*cb_parent)(void); 26182657471SMarkus Pfeiffer extern void (*cb_child)(void); 26282657471SMarkus Pfeiffer extern int __isthreaded; 26382657471SMarkus Pfeiffer 2640d67d370SSascha Wildner static int __used 26582657471SMarkus Pfeiffer __atfork(void (*prepare)(void), void (*parent)(void), 26682657471SMarkus Pfeiffer void (*child)(void)) 26782657471SMarkus Pfeiffer { 26882657471SMarkus Pfeiffer if (__isthreaded) 26982657471SMarkus Pfeiffer return (-1); 27082657471SMarkus Pfeiffer cb_prepare = prepare; 27182657471SMarkus Pfeiffer cb_parent = parent; 27282657471SMarkus Pfeiffer cb_child = child; 27382657471SMarkus Pfeiffer return (0); 27482657471SMarkus Pfeiffer } 275