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> 308b3200a9Szrj #include "namespace.h" 318b3200a9Szrj /* following should not be included in namespace here */ 328b3200a9Szrj #undef pthread_kill 338b3200a9Szrj #undef pthread_sigmask 348b3200a9Szrj #include <errno.h> 3582205bfbSSimon Schubert #include <stdlib.h> 368b3200a9Szrj #include <signal.h> 378b3200a9Szrj #include "un-namespace.h" 386692f0a1SAlex Hornung #include <pthread.h> 3919c7a913SDavid Xu 4017ea2221SMatthew Dillon /* 4117ea2221SMatthew Dillon * Weak symbols: All libc internal usage of these functions should 4217ea2221SMatthew Dillon * use the weak symbol versions (_pthread_XXX). If libpthread is 4317ea2221SMatthew Dillon * linked, it will override these functions with (non-weak) routines. 4417ea2221SMatthew Dillon * The _pthread_XXX functions are provided solely for internal libc 4517ea2221SMatthew Dillon * usage to avoid unwanted cancellation points and to differentiate 4617ea2221SMatthew Dillon * between application locks and libc locks (threads holding the 4717ea2221SMatthew Dillon * latter can't be allowed to exit/terminate). 4817ea2221SMatthew Dillon */ 49ce0e08e2SPeter Avalos 50ceeadccdSzrj #define WRlc(f, n) \ 51f8406b33Szrj __weak_reference_asm(f, _ ## n); 5282205bfbSSimon Schubert #define WR(f, n) \ 53f8406b33Szrj __weak_reference_asm(f, _ ## n); \ 54f8406b33Szrj __weak_reference_asm(f, n) 5582205bfbSSimon Schubert 56f8406b33Szrj /* XXX this needs something more clever, should not some of these return errors? */ 5782657471SMarkus Pfeiffer WR(__atfork, pthread_atfork); 5882205bfbSSimon Schubert WR(stub_zero, pthread_attr_destroy); 5982205bfbSSimon Schubert WR(stub_zero, pthread_attr_get_np); 603ae362f2SSepherosa Ziehau WR(stub_zero, pthread_attr_getaffinity_np); 6182205bfbSSimon Schubert WR(stub_zero, pthread_attr_getdetachstate); 620490f058SHasso Tepper WR(stub_zero, pthread_attr_getguardsize); 6382205bfbSSimon Schubert WR(stub_zero, pthread_attr_getinheritsched); 6482205bfbSSimon Schubert WR(stub_zero, pthread_attr_getschedparam); 6582205bfbSSimon Schubert WR(stub_zero, pthread_attr_getschedpolicy); 6682205bfbSSimon Schubert WR(stub_zero, pthread_attr_getscope); 6782205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstack); 6882205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstackaddr); 6982205bfbSSimon Schubert WR(stub_zero, pthread_attr_getstacksize); 7082205bfbSSimon Schubert WR(stub_zero, pthread_attr_init); 713ae362f2SSepherosa Ziehau WR(stub_zero, pthread_attr_setaffinity_np); 7282205bfbSSimon Schubert WR(stub_zero, pthread_attr_setcreatesuspend_np); 7382205bfbSSimon Schubert WR(stub_zero, pthread_attr_setdetachstate); 740490f058SHasso Tepper WR(stub_zero, pthread_attr_setguardsize); 7582205bfbSSimon Schubert WR(stub_zero, pthread_attr_setinheritsched); 7682205bfbSSimon Schubert WR(stub_zero, pthread_attr_setschedparam); 7782205bfbSSimon Schubert WR(stub_zero, pthread_attr_setschedpolicy); 7882205bfbSSimon Schubert WR(stub_zero, pthread_attr_setscope); 7982205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstack); 8082205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstackaddr); 8182205bfbSSimon Schubert WR(stub_zero, pthread_attr_setstacksize); 820490f058SHasso Tepper WR(stub_zero, pthread_barrier_destroy); 830490f058SHasso Tepper WR(stub_zero, pthread_barrier_init); 840490f058SHasso Tepper WR(stub_zero, pthread_barrier_wait); 850490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_destroy); 860490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_getpshared); 870490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_init); 880490f058SHasso Tepper WR(stub_zero, pthread_barrierattr_setpshared); 89ceeadccdSzrj WRlc(stub_zero, pthread_cancel); 9082205bfbSSimon Schubert WR(stub_zero, pthread_cleanup_pop); 9182205bfbSSimon Schubert WR(stub_zero, pthread_cleanup_push); 9282205bfbSSimon Schubert WR(stub_zero, pthread_cond_broadcast); 9382205bfbSSimon Schubert WR(stub_zero, pthread_cond_destroy); 9482205bfbSSimon Schubert WR(stub_zero, pthread_cond_init); 9582205bfbSSimon Schubert WR(stub_zero, pthread_cond_signal); 9682205bfbSSimon Schubert WR(stub_zero, pthread_cond_timedwait); 9782205bfbSSimon Schubert WR(stub_zero, pthread_cond_wait); 9882205bfbSSimon Schubert WR(stub_zero, pthread_condattr_destroy); 990490f058SHasso Tepper WR(stub_zero, pthread_condattr_getclock); 1000490f058SHasso Tepper WR(stub_zero, pthread_condattr_getpshared); 10182205bfbSSimon Schubert WR(stub_zero, pthread_condattr_init); 1020490f058SHasso Tepper WR(stub_zero, pthread_condattr_setclock); 1030490f058SHasso Tepper WR(stub_zero, pthread_condattr_setpshared); 10482205bfbSSimon Schubert WR(stub_zero, pthread_detach); 105b58f145cSzrj WR(stub_equal, pthread_equal); 10682205bfbSSimon Schubert WR(stub_exit, pthread_exit); 107b2920380SSepherosa Ziehau WR(stub_zero, pthread_getaffinity_np); 10855f11bd6Szrj WR(stub_zero, pthread_getattr_np); 10982205bfbSSimon Schubert WR(stub_zero, pthread_getconcurrency); 110725edadfSSascha Wildner WR(stub_zero, pthread_getcpuclockid); 11182205bfbSSimon Schubert WR(stub_zero, pthread_getprio); 11282205bfbSSimon Schubert WR(stub_zero, pthread_getschedparam); 11382205bfbSSimon Schubert WR(stub_null, pthread_getspecific); 114c0f25c26SImre Vadász WR(stub_zero, pthread_getthreadid_np); 115a809fd39SMarkus Pfeiffer WR(stub_empty, pthread_init_early); 116139374e6Szrj WR(stub_isthreaded, pthread_is_threaded_np); 11782205bfbSSimon Schubert WR(stub_zero, pthread_join); 11882205bfbSSimon Schubert WR(stub_zero, pthread_key_create); 11982205bfbSSimon Schubert WR(stub_zero, pthread_key_delete); 12082205bfbSSimon Schubert WR(stub_zero, pthread_kill); 12182205bfbSSimon Schubert WR(stub_main, pthread_main_np); 12282205bfbSSimon Schubert WR(stub_zero, pthread_multi_np); 12382205bfbSSimon Schubert WR(stub_zero, pthread_mutex_destroy); 12482205bfbSSimon Schubert WR(stub_zero, pthread_mutex_getprioceiling); 12582205bfbSSimon Schubert WR(stub_zero, pthread_mutex_init); 12682205bfbSSimon Schubert WR(stub_zero, pthread_mutex_lock); 12782205bfbSSimon Schubert WR(stub_zero, pthread_mutex_setprioceiling); 1280490f058SHasso Tepper WR(stub_zero, pthread_mutex_timedlock); 12982205bfbSSimon Schubert WR(stub_zero, pthread_mutex_trylock); 13082205bfbSSimon Schubert WR(stub_zero, pthread_mutex_unlock); 13182205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_destroy); 13282205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getkind_np); 13382205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getprioceiling); 13482205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getprotocol); 13582205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_getpshared); 13682205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_gettype); 13782205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_init); 13882205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setkind_np); 13982205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setprioceiling); 14082205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setprotocol); 14182205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_setpshared); 14282205bfbSSimon Schubert WR(stub_zero, pthread_mutexattr_settype); 1436692f0a1SAlex Hornung WR(stub_once, pthread_once); 14482205bfbSSimon Schubert WR(stub_zero, pthread_resume_all_np); 14582205bfbSSimon Schubert WR(stub_zero, pthread_resume_np); 14682205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_destroy); 14782205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_init); 14882205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_rdlock); 1490490f058SHasso Tepper WR(stub_zero, pthread_rwlock_timedrdlock); 1500490f058SHasso Tepper WR(stub_zero, pthread_rwlock_timedwrlock); 15182205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_tryrdlock); 15282205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_trywrlock); 15382205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_unlock); 15482205bfbSSimon Schubert WR(stub_zero, pthread_rwlock_wrlock); 15582205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_destroy); 15682205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_getpshared); 15782205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_init); 15882205bfbSSimon Schubert WR(stub_zero, pthread_rwlockattr_setpshared); 15982205bfbSSimon Schubert WR(stub_self, pthread_self); 160eb396e51SMatthew Dillon WR(stub_pthread_get_name_np, pthread_get_name_np); 16182205bfbSSimon Schubert WR(stub_zero, pthread_set_name_np); 162b2920380SSepherosa Ziehau WR(stub_zero, pthread_setaffinity_np); 16382205bfbSSimon Schubert WR(stub_zero, pthread_setcancelstate); 16482205bfbSSimon Schubert WR(stub_zero, pthread_setcanceltype); 16582205bfbSSimon Schubert WR(stub_zero, pthread_setconcurrency); 16682205bfbSSimon Schubert WR(stub_zero, pthread_setprio); 16782205bfbSSimon Schubert WR(stub_zero, pthread_setschedparam); 16882205bfbSSimon Schubert WR(stub_zero, pthread_setspecific); 1698b3200a9Szrj WR(stub_sigmask, pthread_sigmask); 17082205bfbSSimon Schubert WR(stub_zero, pthread_single_np); 1710490f058SHasso Tepper WR(stub_zero, pthread_spin_destroy); 1720490f058SHasso Tepper WR(stub_zero, pthread_spin_init); 1730490f058SHasso Tepper WR(stub_zero, pthread_spin_lock); 1740490f058SHasso Tepper WR(stub_zero, pthread_spin_trylock); 1750490f058SHasso Tepper WR(stub_zero, pthread_spin_unlock); 17682205bfbSSimon Schubert WR(stub_zero, pthread_suspend_all_np); 17782205bfbSSimon Schubert WR(stub_zero, pthread_suspend_np); 17882205bfbSSimon Schubert WR(stub_zero, pthread_switch_add_np); 17982205bfbSSimon Schubert WR(stub_zero, pthread_switch_delete_np); 18082205bfbSSimon Schubert WR(stub_zero, pthread_testcancel); 1810490f058SHasso Tepper WR(stub_zero, pthread_timedjoin_np); 18282205bfbSSimon Schubert WR(stub_zero, pthread_yield); 183*9d94a5bbSzrj WR(stub_zero, pthread_yield_np); 18482205bfbSSimon Schubert WR(stub_zero, sem_close); 18582205bfbSSimon Schubert WR(stub_zero, sem_destroy); 18682205bfbSSimon Schubert WR(stub_zero, sem_getvalue); 18782205bfbSSimon Schubert WR(stub_zero, sem_init); 18882205bfbSSimon Schubert WR(stub_zero, sem_open); 18982205bfbSSimon Schubert WR(stub_zero, sem_post); 19082205bfbSSimon Schubert WR(stub_zero, sem_trywait); 191775923a8SSascha Wildner WR(stub_zero, sem_timedwait); 19282205bfbSSimon Schubert WR(stub_zero, sem_unlink); 19382205bfbSSimon Schubert WR(stub_zero, sem_wait); 19482205bfbSSimon Schubert 19582205bfbSSimon Schubert 19682205bfbSSimon Schubert static int __used 19782205bfbSSimon Schubert stub_zero(void) 198ce0e08e2SPeter Avalos { 199ce0e08e2SPeter Avalos return (0); 200ce0e08e2SPeter Avalos } 201ce0e08e2SPeter Avalos 2026692f0a1SAlex Hornung static int __used 2036692f0a1SAlex Hornung stub_once(pthread_once_t *o, void (*r)(void)) 2046692f0a1SAlex Hornung { 205cf8046a9Szrj if (o->__state != PTHREAD_DONE_INIT) { 2066692f0a1SAlex Hornung (*r)(); 207cf8046a9Szrj o->__state = PTHREAD_DONE_INIT; 2086692f0a1SAlex Hornung } 2096692f0a1SAlex Hornung 2106692f0a1SAlex Hornung return (0); 2116692f0a1SAlex Hornung } 2126692f0a1SAlex Hornung 21382205bfbSSimon Schubert static void * __used 21482205bfbSSimon Schubert stub_null(void) 21517ea2221SMatthew Dillon { 21617ea2221SMatthew Dillon return (NULL); 21717ea2221SMatthew Dillon } 21817ea2221SMatthew Dillon 21982205bfbSSimon Schubert static void * __used 22082205bfbSSimon Schubert stub_self(void) 22117ea2221SMatthew Dillon { 22282205bfbSSimon Schubert static struct {} main_thread; 22382205bfbSSimon Schubert 22482205bfbSSimon Schubert return (&main_thread); 22517ea2221SMatthew Dillon } 22617ea2221SMatthew Dillon 22782205bfbSSimon Schubert static int __used 22882205bfbSSimon Schubert stub_main(void) 229ce0e08e2SPeter Avalos { 230ce0e08e2SPeter Avalos return (-1); 231ce0e08e2SPeter Avalos } 232ce0e08e2SPeter Avalos 23382205bfbSSimon Schubert static int __used 234b58f145cSzrj stub_equal(pthread_t a, pthread_t b) 23517ea2221SMatthew Dillon { 236b58f145cSzrj return (a == b); 23717ea2221SMatthew Dillon } 23817ea2221SMatthew Dillon 23982205bfbSSimon Schubert static void __used 240a809fd39SMarkus Pfeiffer stub_empty(void) 241a809fd39SMarkus Pfeiffer { 242a809fd39SMarkus Pfeiffer } 243a809fd39SMarkus Pfeiffer 244a809fd39SMarkus Pfeiffer static void __used 24582205bfbSSimon Schubert stub_exit(void) 24617ea2221SMatthew Dillon { 24782205bfbSSimon Schubert exit(0); 248ce0e08e2SPeter Avalos } 249a809fd39SMarkus Pfeiffer 2508b3200a9Szrj static int __used 2518b3200a9Szrj stub_sigmask(int how, const sigset_t *set, sigset_t *oset) 2528b3200a9Szrj { 2538b3200a9Szrj if (_sigprocmask(how, set, oset)) 2548b3200a9Szrj return (errno); 2558b3200a9Szrj return (0); 2568b3200a9Szrj } 2578b3200a9Szrj 258eb396e51SMatthew Dillon static void __used 259eb396e51SMatthew Dillon stub_pthread_get_name_np(pthread_t a, char *name, size_t len) 260eb396e51SMatthew Dillon { 261eb396e51SMatthew Dillon if (len) 262eb396e51SMatthew Dillon name[0] = 0; 263eb396e51SMatthew Dillon } 264eb396e51SMatthew Dillon 265a809fd39SMarkus Pfeiffer /* 266a809fd39SMarkus Pfeiffer * If libpthread is loaded, make sure it is initialised before 267a809fd39SMarkus Pfeiffer * other libraries call pthread functions 268a809fd39SMarkus Pfeiffer */ 269450f08dbSSascha Wildner void _pthread_init(void) __constructor(101); 270450f08dbSSascha Wildner void _pthread_init_early(void); 271a809fd39SMarkus Pfeiffer void 272a809fd39SMarkus Pfeiffer _pthread_init(void) 273a809fd39SMarkus Pfeiffer { 274a809fd39SMarkus Pfeiffer _pthread_init_early(); 275a809fd39SMarkus Pfeiffer } 27682657471SMarkus Pfeiffer 27782657471SMarkus Pfeiffer extern void (*cb_prepare)(void); 27882657471SMarkus Pfeiffer extern void (*cb_parent)(void); 27982657471SMarkus Pfeiffer extern void (*cb_child)(void); 28082657471SMarkus Pfeiffer extern int __isthreaded; 28182657471SMarkus Pfeiffer 2820d67d370SSascha Wildner static int __used 28382657471SMarkus Pfeiffer __atfork(void (*prepare)(void), void (*parent)(void), 28482657471SMarkus Pfeiffer void (*child)(void)) 28582657471SMarkus Pfeiffer { 28682657471SMarkus Pfeiffer if (__isthreaded) 28782657471SMarkus Pfeiffer return (-1); 28882657471SMarkus Pfeiffer cb_prepare = prepare; 28982657471SMarkus Pfeiffer cb_parent = parent; 29082657471SMarkus Pfeiffer cb_child = child; 29182657471SMarkus Pfeiffer return (0); 29282657471SMarkus Pfeiffer } 293139374e6Szrj 294139374e6Szrj static int __used 295139374e6Szrj stub_isthreaded(void) 296139374e6Szrj { 297139374e6Szrj return (__isthreaded != 0); 298139374e6Szrj } 299