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 <stdlib.h> 30 #include <pthread.h> 31 32 /* 33 * Weak symbols: All libc internal usage of these functions should 34 * use the weak symbol versions (_pthread_XXX). If libpthread is 35 * linked, it will override these functions with (non-weak) routines. 36 * The _pthread_XXX functions are provided solely for internal libc 37 * usage to avoid unwanted cancellation points and to differentiate 38 * between application locks and libc locks (threads holding the 39 * latter can't be allowed to exit/terminate). 40 */ 41 42 #define WR(f, n) \ 43 __weak_reference(f, _ ## n); \ 44 __weak_reference(f, n) 45 46 WR(__atfork, pthread_atfork); 47 WR(stub_zero, pthread_attr_destroy); 48 WR(stub_zero, pthread_attr_get_np); 49 WR(stub_zero, pthread_attr_getdetachstate); 50 WR(stub_zero, pthread_attr_getguardsize); 51 WR(stub_zero, pthread_attr_getinheritsched); 52 WR(stub_zero, pthread_attr_getschedparam); 53 WR(stub_zero, pthread_attr_getschedpolicy); 54 WR(stub_zero, pthread_attr_getscope); 55 WR(stub_zero, pthread_attr_getstack); 56 WR(stub_zero, pthread_attr_getstackaddr); 57 WR(stub_zero, pthread_attr_getstacksize); 58 WR(stub_zero, pthread_attr_init); 59 WR(stub_zero, pthread_attr_setcreatesuspend_np); 60 WR(stub_zero, pthread_attr_setdetachstate); 61 WR(stub_zero, pthread_attr_setguardsize); 62 WR(stub_zero, pthread_attr_setinheritsched); 63 WR(stub_zero, pthread_attr_setschedparam); 64 WR(stub_zero, pthread_attr_setschedpolicy); 65 WR(stub_zero, pthread_attr_setscope); 66 WR(stub_zero, pthread_attr_setstack); 67 WR(stub_zero, pthread_attr_setstackaddr); 68 WR(stub_zero, pthread_attr_setstacksize); 69 WR(stub_zero, pthread_barrier_destroy); 70 WR(stub_zero, pthread_barrier_init); 71 WR(stub_zero, pthread_barrier_wait); 72 WR(stub_zero, pthread_barrierattr_destroy); 73 WR(stub_zero, pthread_barrierattr_getpshared); 74 WR(stub_zero, pthread_barrierattr_init); 75 WR(stub_zero, pthread_barrierattr_setpshared); 76 WR(stub_zero, pthread_cancel); 77 WR(stub_zero, pthread_cleanup_pop); 78 WR(stub_zero, pthread_cleanup_push); 79 WR(stub_zero, pthread_cond_broadcast); 80 WR(stub_zero, pthread_cond_destroy); 81 WR(stub_zero, pthread_cond_init); 82 WR(stub_zero, pthread_cond_signal); 83 WR(stub_zero, pthread_cond_timedwait); 84 WR(stub_zero, pthread_cond_wait); 85 WR(stub_zero, pthread_condattr_destroy); 86 WR(stub_zero, pthread_condattr_getclock); 87 WR(stub_zero, pthread_condattr_getpshared); 88 WR(stub_zero, pthread_condattr_init); 89 WR(stub_zero, pthread_condattr_setclock); 90 WR(stub_zero, pthread_condattr_setpshared); 91 WR(stub_zero, pthread_detach); 92 WR(stub_true, pthread_equal); 93 WR(stub_exit, pthread_exit); 94 WR(stub_zero, pthread_getconcurrency); 95 WR(stub_zero, pthread_getprio); 96 WR(stub_zero, pthread_getschedparam); 97 WR(stub_null, pthread_getspecific); 98 WR(stub_empty, pthread_init_early); 99 WR(stub_zero, pthread_join); 100 WR(stub_zero, pthread_key_create); 101 WR(stub_zero, pthread_key_delete); 102 WR(stub_zero, pthread_kill); 103 WR(stub_main, pthread_main_np); 104 WR(stub_zero, pthread_multi_np); 105 WR(stub_zero, pthread_mutex_destroy); 106 WR(stub_zero, pthread_mutex_getprioceiling); 107 WR(stub_zero, pthread_mutex_init); 108 WR(stub_zero, pthread_mutex_lock); 109 WR(stub_zero, pthread_mutex_setprioceiling); 110 WR(stub_zero, pthread_mutex_timedlock); 111 WR(stub_zero, pthread_mutex_trylock); 112 WR(stub_zero, pthread_mutex_unlock); 113 WR(stub_zero, pthread_mutexattr_destroy); 114 WR(stub_zero, pthread_mutexattr_getkind_np); 115 WR(stub_zero, pthread_mutexattr_getprioceiling); 116 WR(stub_zero, pthread_mutexattr_getprotocol); 117 WR(stub_zero, pthread_mutexattr_getpshared); 118 WR(stub_zero, pthread_mutexattr_gettype); 119 WR(stub_zero, pthread_mutexattr_init); 120 WR(stub_zero, pthread_mutexattr_setkind_np); 121 WR(stub_zero, pthread_mutexattr_setprioceiling); 122 WR(stub_zero, pthread_mutexattr_setprotocol); 123 WR(stub_zero, pthread_mutexattr_setpshared); 124 WR(stub_zero, pthread_mutexattr_settype); 125 WR(stub_once, pthread_once); 126 WR(stub_zero, pthread_resume_all_np); 127 WR(stub_zero, pthread_resume_np); 128 WR(stub_zero, pthread_rwlock_destroy); 129 WR(stub_zero, pthread_rwlock_init); 130 WR(stub_zero, pthread_rwlock_rdlock); 131 WR(stub_zero, pthread_rwlock_timedrdlock); 132 WR(stub_zero, pthread_rwlock_timedwrlock); 133 WR(stub_zero, pthread_rwlock_tryrdlock); 134 WR(stub_zero, pthread_rwlock_trywrlock); 135 WR(stub_zero, pthread_rwlock_unlock); 136 WR(stub_zero, pthread_rwlock_wrlock); 137 WR(stub_zero, pthread_rwlockattr_destroy); 138 WR(stub_zero, pthread_rwlockattr_getpshared); 139 WR(stub_zero, pthread_rwlockattr_init); 140 WR(stub_zero, pthread_rwlockattr_setpshared); 141 WR(stub_self, pthread_self); 142 WR(stub_zero, pthread_set_name_np); 143 WR(stub_zero, pthread_setcancelstate); 144 WR(stub_zero, pthread_setcanceltype); 145 WR(stub_zero, pthread_setconcurrency); 146 WR(stub_zero, pthread_setprio); 147 WR(stub_zero, pthread_setschedparam); 148 WR(stub_zero, pthread_setspecific); 149 WR(stub_zero, pthread_sigmask); 150 WR(stub_zero, pthread_single_np); 151 WR(stub_zero, pthread_spin_destroy); 152 WR(stub_zero, pthread_spin_init); 153 WR(stub_zero, pthread_spin_lock); 154 WR(stub_zero, pthread_spin_trylock); 155 WR(stub_zero, pthread_spin_unlock); 156 WR(stub_zero, pthread_suspend_all_np); 157 WR(stub_zero, pthread_suspend_np); 158 WR(stub_zero, pthread_switch_add_np); 159 WR(stub_zero, pthread_switch_delete_np); 160 WR(stub_zero, pthread_testcancel); 161 WR(stub_zero, pthread_timedjoin_np); 162 WR(stub_zero, pthread_yield); 163 WR(stub_zero, sched_yield); 164 WR(stub_zero, sem_close); 165 WR(stub_zero, sem_destroy); 166 WR(stub_zero, sem_getvalue); 167 WR(stub_zero, sem_init); 168 WR(stub_zero, sem_open); 169 WR(stub_zero, sem_post); 170 WR(stub_zero, sem_trywait); 171 WR(stub_zero, sem_timedwait); 172 WR(stub_zero, sem_unlink); 173 WR(stub_zero, sem_wait); 174 175 176 static int __used 177 stub_zero(void) 178 { 179 return (0); 180 } 181 182 static int __used 183 stub_once(pthread_once_t *o, void (*r)(void)) 184 { 185 if (o->state != PTHREAD_DONE_INIT) { 186 (*r)(); 187 o->state = PTHREAD_DONE_INIT; 188 } 189 190 return (0); 191 } 192 193 static void * __used 194 stub_null(void) 195 { 196 return (NULL); 197 } 198 199 static void * __used 200 stub_self(void) 201 { 202 static struct {} main_thread; 203 204 return (&main_thread); 205 } 206 207 static int __used 208 stub_main(void) 209 { 210 return (-1); 211 } 212 213 static int __used 214 stub_true(void) 215 { 216 return (1); 217 } 218 219 static void __used 220 stub_empty(void) 221 { 222 } 223 224 static void __used 225 stub_exit(void) 226 { 227 exit(0); 228 } 229 230 /* 231 * If libpthread is loaded, make sure it is initialised before 232 * other libraries call pthread functions 233 */ 234 void _pthread_init(void) __constructor(101); 235 void _pthread_init_early(void); 236 void 237 _pthread_init(void) 238 { 239 _pthread_init_early(); 240 } 241 242 extern void (*cb_prepare)(void); 243 extern void (*cb_parent)(void); 244 extern void (*cb_child)(void); 245 extern int __isthreaded; 246 247 static int __used 248 __atfork(void (*prepare)(void), void (*parent)(void), 249 void (*child)(void)) 250 { 251 if (__isthreaded) 252 return (-1); 253 cb_prepare = prepare; 254 cb_parent = parent; 255 cb_child = child; 256 return (0); 257 } 258