1 /* 2 * Copyright (c) 2001 Daniel Eischen <deischen@FreeBSD.org>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/lib/libc/gen/_pthread_stubs.c,v 1.5 2001/06/11 23:18:22 iedowse Exp $ 27 */ 28 29 #include <sys/cdefs.h> 30 #include <stdlib.h> 31 #include <pthread.h> 32 33 /* 34 * Weak symbols: All libc internal usage of these functions should 35 * use the weak symbol versions (_pthread_XXX). If libpthread is 36 * linked, it will override these functions with (non-weak) routines. 37 * The _pthread_XXX functions are provided solely for internal libc 38 * usage to avoid unwanted cancellation points and to differentiate 39 * between application locks and libc locks (threads holding the 40 * latter can't be allowed to exit/terminate). 41 */ 42 43 #define WRlc(f, n) \ 44 __weak_reference_asm(f, _ ## n); 45 #define WR(f, n) \ 46 __weak_reference_asm(f, _ ## n); \ 47 __weak_reference_asm(f, n) 48 49 /* XXX this needs something more clever, should not some of these return errors? */ 50 WR(__atfork, pthread_atfork); 51 WR(stub_zero, pthread_attr_destroy); 52 WR(stub_zero, pthread_attr_get_np); 53 WR(stub_zero, pthread_attr_getaffinity_np); 54 WR(stub_zero, pthread_attr_getdetachstate); 55 WR(stub_zero, pthread_attr_getguardsize); 56 WR(stub_zero, pthread_attr_getinheritsched); 57 WR(stub_zero, pthread_attr_getschedparam); 58 WR(stub_zero, pthread_attr_getschedpolicy); 59 WR(stub_zero, pthread_attr_getscope); 60 WR(stub_zero, pthread_attr_getstack); 61 WR(stub_zero, pthread_attr_getstackaddr); 62 WR(stub_zero, pthread_attr_getstacksize); 63 WR(stub_zero, pthread_attr_init); 64 WR(stub_zero, pthread_attr_setaffinity_np); 65 WR(stub_zero, pthread_attr_setcreatesuspend_np); 66 WR(stub_zero, pthread_attr_setdetachstate); 67 WR(stub_zero, pthread_attr_setguardsize); 68 WR(stub_zero, pthread_attr_setinheritsched); 69 WR(stub_zero, pthread_attr_setschedparam); 70 WR(stub_zero, pthread_attr_setschedpolicy); 71 WR(stub_zero, pthread_attr_setscope); 72 WR(stub_zero, pthread_attr_setstack); 73 WR(stub_zero, pthread_attr_setstackaddr); 74 WR(stub_zero, pthread_attr_setstacksize); 75 WR(stub_zero, pthread_barrier_destroy); 76 WR(stub_zero, pthread_barrier_init); 77 WR(stub_zero, pthread_barrier_wait); 78 WR(stub_zero, pthread_barrierattr_destroy); 79 WR(stub_zero, pthread_barrierattr_getpshared); 80 WR(stub_zero, pthread_barrierattr_init); 81 WR(stub_zero, pthread_barrierattr_setpshared); 82 WRlc(stub_zero, pthread_cancel); 83 WR(stub_zero, pthread_cleanup_pop); 84 WR(stub_zero, pthread_cleanup_push); 85 WR(stub_zero, pthread_cond_broadcast); 86 WR(stub_zero, pthread_cond_destroy); 87 WR(stub_zero, pthread_cond_init); 88 WR(stub_zero, pthread_cond_signal); 89 WR(stub_zero, pthread_cond_timedwait); 90 WR(stub_zero, pthread_cond_wait); 91 WR(stub_zero, pthread_condattr_destroy); 92 WR(stub_zero, pthread_condattr_getclock); 93 WR(stub_zero, pthread_condattr_getpshared); 94 WR(stub_zero, pthread_condattr_init); 95 WR(stub_zero, pthread_condattr_setclock); 96 WR(stub_zero, pthread_condattr_setpshared); 97 WR(stub_zero, pthread_detach); 98 WR(stub_equal, pthread_equal); 99 WR(stub_exit, pthread_exit); 100 WR(stub_zero, pthread_getaffinity_np); 101 WR(stub_zero, pthread_getconcurrency); 102 WR(stub_zero, pthread_getcpuclockid); 103 WR(stub_zero, pthread_getprio); 104 WR(stub_zero, pthread_getschedparam); 105 WR(stub_null, pthread_getspecific); 106 WR(stub_zero, pthread_getthreadid_np); 107 WR(stub_empty, pthread_init_early); 108 WR(stub_zero, pthread_join); 109 WR(stub_zero, pthread_key_create); 110 WR(stub_zero, pthread_key_delete); 111 WR(stub_zero, pthread_kill); 112 WR(stub_main, pthread_main_np); 113 WR(stub_zero, pthread_multi_np); 114 WR(stub_zero, pthread_mutex_destroy); 115 WR(stub_zero, pthread_mutex_getprioceiling); 116 WR(stub_zero, pthread_mutex_init); 117 WR(stub_zero, pthread_mutex_lock); 118 WR(stub_zero, pthread_mutex_setprioceiling); 119 WR(stub_zero, pthread_mutex_timedlock); 120 WR(stub_zero, pthread_mutex_trylock); 121 WR(stub_zero, pthread_mutex_unlock); 122 WR(stub_zero, pthread_mutexattr_destroy); 123 WR(stub_zero, pthread_mutexattr_getkind_np); 124 WR(stub_zero, pthread_mutexattr_getprioceiling); 125 WR(stub_zero, pthread_mutexattr_getprotocol); 126 WR(stub_zero, pthread_mutexattr_getpshared); 127 WR(stub_zero, pthread_mutexattr_gettype); 128 WR(stub_zero, pthread_mutexattr_init); 129 WR(stub_zero, pthread_mutexattr_setkind_np); 130 WR(stub_zero, pthread_mutexattr_setprioceiling); 131 WR(stub_zero, pthread_mutexattr_setprotocol); 132 WR(stub_zero, pthread_mutexattr_setpshared); 133 WR(stub_zero, pthread_mutexattr_settype); 134 WR(stub_once, pthread_once); 135 WR(stub_zero, pthread_resume_all_np); 136 WR(stub_zero, pthread_resume_np); 137 WR(stub_zero, pthread_rwlock_destroy); 138 WR(stub_zero, pthread_rwlock_init); 139 WR(stub_zero, pthread_rwlock_rdlock); 140 WR(stub_zero, pthread_rwlock_timedrdlock); 141 WR(stub_zero, pthread_rwlock_timedwrlock); 142 WR(stub_zero, pthread_rwlock_tryrdlock); 143 WR(stub_zero, pthread_rwlock_trywrlock); 144 WR(stub_zero, pthread_rwlock_unlock); 145 WR(stub_zero, pthread_rwlock_wrlock); 146 WR(stub_zero, pthread_rwlockattr_destroy); 147 WR(stub_zero, pthread_rwlockattr_getpshared); 148 WR(stub_zero, pthread_rwlockattr_init); 149 WR(stub_zero, pthread_rwlockattr_setpshared); 150 WR(stub_self, pthread_self); 151 WR(stub_zero, pthread_set_name_np); 152 WR(stub_zero, pthread_setaffinity_np); 153 WR(stub_zero, pthread_setcancelstate); 154 WR(stub_zero, pthread_setcanceltype); 155 WR(stub_zero, pthread_setconcurrency); 156 WR(stub_zero, pthread_setprio); 157 WR(stub_zero, pthread_setschedparam); 158 WR(stub_zero, pthread_setspecific); 159 WR(stub_zero, pthread_sigmask); 160 WR(stub_zero, pthread_single_np); 161 WR(stub_zero, pthread_spin_destroy); 162 WR(stub_zero, pthread_spin_init); 163 WR(stub_zero, pthread_spin_lock); 164 WR(stub_zero, pthread_spin_trylock); 165 WR(stub_zero, pthread_spin_unlock); 166 WR(stub_zero, pthread_suspend_all_np); 167 WR(stub_zero, pthread_suspend_np); 168 WR(stub_zero, pthread_switch_add_np); 169 WR(stub_zero, pthread_switch_delete_np); 170 WR(stub_zero, pthread_testcancel); 171 WR(stub_zero, pthread_timedjoin_np); 172 WR(stub_zero, pthread_yield); 173 WR(stub_zero, sem_close); 174 WR(stub_zero, sem_destroy); 175 WR(stub_zero, sem_getvalue); 176 WR(stub_zero, sem_init); 177 WR(stub_zero, sem_open); 178 WR(stub_zero, sem_post); 179 WR(stub_zero, sem_trywait); 180 WR(stub_zero, sem_timedwait); 181 WR(stub_zero, sem_unlink); 182 WR(stub_zero, sem_wait); 183 184 185 static int __used 186 stub_zero(void) 187 { 188 return (0); 189 } 190 191 static int __used 192 stub_once(pthread_once_t *o, void (*r)(void)) 193 { 194 if (o->state != PTHREAD_DONE_INIT) { 195 (*r)(); 196 o->state = PTHREAD_DONE_INIT; 197 } 198 199 return (0); 200 } 201 202 static void * __used 203 stub_null(void) 204 { 205 return (NULL); 206 } 207 208 static void * __used 209 stub_self(void) 210 { 211 static struct {} main_thread; 212 213 return (&main_thread); 214 } 215 216 static int __used 217 stub_main(void) 218 { 219 return (-1); 220 } 221 222 static int __used 223 stub_equal(pthread_t a, pthread_t b) 224 { 225 return (a == b); 226 } 227 228 static void __used 229 stub_empty(void) 230 { 231 } 232 233 static void __used 234 stub_exit(void) 235 { 236 exit(0); 237 } 238 239 /* 240 * If libpthread is loaded, make sure it is initialised before 241 * other libraries call pthread functions 242 */ 243 void _pthread_init(void) __constructor(101); 244 void _pthread_init_early(void); 245 void 246 _pthread_init(void) 247 { 248 _pthread_init_early(); 249 } 250 251 extern void (*cb_prepare)(void); 252 extern void (*cb_parent)(void); 253 extern void (*cb_child)(void); 254 extern int __isthreaded; 255 256 static int __used 257 __atfork(void (*prepare)(void), void (*parent)(void), 258 void (*child)(void)) 259 { 260 if (__isthreaded) 261 return (-1); 262 cb_prepare = prepare; 263 cb_parent = parent; 264 cb_child = child; 265 return (0); 266 } 267