nrelease - fix/improve livecd
[dragonfly.git] / lib / libc / gen / _pthread_stubs.c
CommitLineData
17ea2221
MD
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 *
ce0e08e2 26 * $FreeBSD: src/lib/libc/gen/_pthread_stubs.c,v 1.5 2001/06/11 23:18:22 iedowse Exp $
17ea2221
MD
27 */
28
f8406b33 29#include <sys/cdefs.h>
8b3200a9 30#include "namespace.h"
31/* following should not be included in namespace here */
32#undef pthread_kill
33#undef pthread_sigmask
34#include <errno.h>
82205bfb 35#include <stdlib.h>
8b3200a9 36#include <signal.h>
37#include "un-namespace.h"
6692f0a1 38#include <pthread.h>
19c7a913 39
17ea2221
MD
40/*
41 * Weak symbols: All libc internal usage of these functions should
42 * use the weak symbol versions (_pthread_XXX). If libpthread is
43 * linked, it will override these functions with (non-weak) routines.
44 * The _pthread_XXX functions are provided solely for internal libc
45 * usage to avoid unwanted cancellation points and to differentiate
46 * between application locks and libc locks (threads holding the
47 * latter can't be allowed to exit/terminate).
48 */
ce0e08e2 49
ceeadccd 50#define WRlc(f, n) \
f8406b33 51 __weak_reference_asm(f, _ ## n);
82205bfb 52#define WR(f, n) \
f8406b33 53 __weak_reference_asm(f, _ ## n); \
54 __weak_reference_asm(f, n)
82205bfb 55
f8406b33 56/* XXX this needs something more clever, should not some of these return errors? */
82657471 57WR(__atfork, pthread_atfork);
82205bfb
SS
58WR(stub_zero, pthread_attr_destroy);
59WR(stub_zero, pthread_attr_get_np);
3ae362f2 60WR(stub_zero, pthread_attr_getaffinity_np);
82205bfb 61WR(stub_zero, pthread_attr_getdetachstate);
0490f058 62WR(stub_zero, pthread_attr_getguardsize);
82205bfb
SS
63WR(stub_zero, pthread_attr_getinheritsched);
64WR(stub_zero, pthread_attr_getschedparam);
65WR(stub_zero, pthread_attr_getschedpolicy);
66WR(stub_zero, pthread_attr_getscope);
67WR(stub_zero, pthread_attr_getstack);
68WR(stub_zero, pthread_attr_getstackaddr);
69WR(stub_zero, pthread_attr_getstacksize);
70WR(stub_zero, pthread_attr_init);
3ae362f2 71WR(stub_zero, pthread_attr_setaffinity_np);
82205bfb
SS
72WR(stub_zero, pthread_attr_setcreatesuspend_np);
73WR(stub_zero, pthread_attr_setdetachstate);
0490f058 74WR(stub_zero, pthread_attr_setguardsize);
82205bfb
SS
75WR(stub_zero, pthread_attr_setinheritsched);
76WR(stub_zero, pthread_attr_setschedparam);
77WR(stub_zero, pthread_attr_setschedpolicy);
78WR(stub_zero, pthread_attr_setscope);
79WR(stub_zero, pthread_attr_setstack);
80WR(stub_zero, pthread_attr_setstackaddr);
81WR(stub_zero, pthread_attr_setstacksize);
0490f058
HT
82WR(stub_zero, pthread_barrier_destroy);
83WR(stub_zero, pthread_barrier_init);
84WR(stub_zero, pthread_barrier_wait);
85WR(stub_zero, pthread_barrierattr_destroy);
86WR(stub_zero, pthread_barrierattr_getpshared);
87WR(stub_zero, pthread_barrierattr_init);
88WR(stub_zero, pthread_barrierattr_setpshared);
ceeadccd 89WRlc(stub_zero, pthread_cancel);
82205bfb
SS
90WR(stub_zero, pthread_cleanup_pop);
91WR(stub_zero, pthread_cleanup_push);
92WR(stub_zero, pthread_cond_broadcast);
93WR(stub_zero, pthread_cond_destroy);
94WR(stub_zero, pthread_cond_init);
95WR(stub_zero, pthread_cond_signal);
96WR(stub_zero, pthread_cond_timedwait);
97WR(stub_zero, pthread_cond_wait);
98WR(stub_zero, pthread_condattr_destroy);
0490f058
HT
99WR(stub_zero, pthread_condattr_getclock);
100WR(stub_zero, pthread_condattr_getpshared);
82205bfb 101WR(stub_zero, pthread_condattr_init);
0490f058
HT
102WR(stub_zero, pthread_condattr_setclock);
103WR(stub_zero, pthread_condattr_setpshared);
82205bfb 104WR(stub_zero, pthread_detach);
b58f145c 105WR(stub_equal, pthread_equal);
82205bfb 106WR(stub_exit, pthread_exit);
b2920380 107WR(stub_zero, pthread_getaffinity_np);
55f11bd6 108WR(stub_zero, pthread_getattr_np);
82205bfb 109WR(stub_zero, pthread_getconcurrency);
725edadf 110WR(stub_zero, pthread_getcpuclockid);
82205bfb
SS
111WR(stub_zero, pthread_getprio);
112WR(stub_zero, pthread_getschedparam);
113WR(stub_null, pthread_getspecific);
c0f25c26 114WR(stub_zero, pthread_getthreadid_np);
a809fd39 115WR(stub_empty, pthread_init_early);
139374e6 116WR(stub_isthreaded, pthread_is_threaded_np);
82205bfb
SS
117WR(stub_zero, pthread_join);
118WR(stub_zero, pthread_key_create);
119WR(stub_zero, pthread_key_delete);
120WR(stub_zero, pthread_kill);
121WR(stub_main, pthread_main_np);
122WR(stub_zero, pthread_multi_np);
123WR(stub_zero, pthread_mutex_destroy);
124WR(stub_zero, pthread_mutex_getprioceiling);
125WR(stub_zero, pthread_mutex_init);
126WR(stub_zero, pthread_mutex_lock);
127WR(stub_zero, pthread_mutex_setprioceiling);
0490f058 128WR(stub_zero, pthread_mutex_timedlock);
82205bfb
SS
129WR(stub_zero, pthread_mutex_trylock);
130WR(stub_zero, pthread_mutex_unlock);
131WR(stub_zero, pthread_mutexattr_destroy);
132WR(stub_zero, pthread_mutexattr_getkind_np);
133WR(stub_zero, pthread_mutexattr_getprioceiling);
134WR(stub_zero, pthread_mutexattr_getprotocol);
135WR(stub_zero, pthread_mutexattr_getpshared);
136WR(stub_zero, pthread_mutexattr_gettype);
137WR(stub_zero, pthread_mutexattr_init);
138WR(stub_zero, pthread_mutexattr_setkind_np);
139WR(stub_zero, pthread_mutexattr_setprioceiling);
140WR(stub_zero, pthread_mutexattr_setprotocol);
141WR(stub_zero, pthread_mutexattr_setpshared);
142WR(stub_zero, pthread_mutexattr_settype);
6692f0a1 143WR(stub_once, pthread_once);
82205bfb
SS
144WR(stub_zero, pthread_resume_all_np);
145WR(stub_zero, pthread_resume_np);
146WR(stub_zero, pthread_rwlock_destroy);
147WR(stub_zero, pthread_rwlock_init);
148WR(stub_zero, pthread_rwlock_rdlock);
0490f058
HT
149WR(stub_zero, pthread_rwlock_timedrdlock);
150WR(stub_zero, pthread_rwlock_timedwrlock);
82205bfb
SS
151WR(stub_zero, pthread_rwlock_tryrdlock);
152WR(stub_zero, pthread_rwlock_trywrlock);
153WR(stub_zero, pthread_rwlock_unlock);
154WR(stub_zero, pthread_rwlock_wrlock);
155WR(stub_zero, pthread_rwlockattr_destroy);
156WR(stub_zero, pthread_rwlockattr_getpshared);
157WR(stub_zero, pthread_rwlockattr_init);
158WR(stub_zero, pthread_rwlockattr_setpshared);
159WR(stub_self, pthread_self);
eb396e51 160WR(stub_pthread_get_name_np, pthread_get_name_np);
ab5dc9ac 161WR(stub_pthread_getname_np, pthread_getname_np);
82205bfb 162WR(stub_zero, pthread_set_name_np);
ab5dc9ac 163WR(stub_zero, pthread_setname_np);
b2920380 164WR(stub_zero, pthread_setaffinity_np);
82205bfb
SS
165WR(stub_zero, pthread_setcancelstate);
166WR(stub_zero, pthread_setcanceltype);
167WR(stub_zero, pthread_setconcurrency);
168WR(stub_zero, pthread_setprio);
169WR(stub_zero, pthread_setschedparam);
170WR(stub_zero, pthread_setspecific);
8b3200a9 171WR(stub_sigmask, pthread_sigmask);
82205bfb 172WR(stub_zero, pthread_single_np);
0490f058
HT
173WR(stub_zero, pthread_spin_destroy);
174WR(stub_zero, pthread_spin_init);
175WR(stub_zero, pthread_spin_lock);
176WR(stub_zero, pthread_spin_trylock);
177WR(stub_zero, pthread_spin_unlock);
82205bfb
SS
178WR(stub_zero, pthread_suspend_all_np);
179WR(stub_zero, pthread_suspend_np);
180WR(stub_zero, pthread_switch_add_np);
181WR(stub_zero, pthread_switch_delete_np);
182WR(stub_zero, pthread_testcancel);
0490f058 183WR(stub_zero, pthread_timedjoin_np);
82205bfb 184WR(stub_zero, pthread_yield);
9d94a5bb 185WR(stub_zero, pthread_yield_np);
82205bfb
SS
186WR(stub_zero, sem_close);
187WR(stub_zero, sem_destroy);
188WR(stub_zero, sem_getvalue);
189WR(stub_zero, sem_init);
190WR(stub_zero, sem_open);
191WR(stub_zero, sem_post);
192WR(stub_zero, sem_trywait);
775923a8 193WR(stub_zero, sem_timedwait);
82205bfb
SS
194WR(stub_zero, sem_unlink);
195WR(stub_zero, sem_wait);
196
197
198static int __used
199stub_zero(void)
200{
201 return (0);
202}
203
6692f0a1
AH
204static int __used
205stub_once(pthread_once_t *o, void (*r)(void))
206{
cf8046a9 207 if (o->__state != PTHREAD_DONE_INIT) {
6692f0a1 208 (*r)();
cf8046a9 209 o->__state = PTHREAD_DONE_INIT;
6692f0a1
AH
210 }
211
212 return (0);
213}
214
82205bfb
SS
215static void * __used
216stub_null(void)
17ea2221
MD
217{
218 return (NULL);
219}
220
82205bfb
SS
221static void * __used
222stub_self(void)
17ea2221 223{
82205bfb 224 static struct {} main_thread;
17ea2221 225
82205bfb 226 return (&main_thread);
17ea2221
MD
227}
228
82205bfb
SS
229static int __used
230stub_main(void)
ce0e08e2
PA
231{
232 return (-1);
233}
234
82205bfb 235static int __used
b58f145c 236stub_equal(pthread_t a, pthread_t b)
17ea2221 237{
b58f145c 238 return (a == b);
17ea2221
MD
239}
240
a809fd39
MP
241static void __used
242stub_empty(void)
243{
244}
245
82205bfb
SS
246static void __used
247stub_exit(void)
17ea2221 248{
82205bfb 249 exit(0);
ce0e08e2 250}
a809fd39 251
8b3200a9 252static int __used
253stub_sigmask(int how, const sigset_t *set, sigset_t *oset)
254{
255 if (_sigprocmask(how, set, oset))
256 return (errno);
257 return (0);
258}
259
eb396e51
MD
260static void __used
261stub_pthread_get_name_np(pthread_t a, char *name, size_t len)
262{
263 if (len)
264 name[0] = 0;
265}
266
ab5dc9ac
SW
267static int __used
268stub_pthread_getname_np(pthread_t a, char *name, size_t len)
269{
270 if (len)
271 name[0] = 0;
272 return (0);
273}
274
a809fd39
MP
275/*
276 * If libpthread is loaded, make sure it is initialised before
277 * other libraries call pthread functions
278 */
450f08db
SW
279void _pthread_init(void) __constructor(101);
280void _pthread_init_early(void);
a809fd39
MP
281void
282_pthread_init(void)
283{
284 _pthread_init_early();
285}
82657471
MP
286
287extern void (*cb_prepare)(void);
288extern void (*cb_parent)(void);
289extern void (*cb_child)(void);
290extern int __isthreaded;
291
0d67d370 292static int __used
82657471
MP
293__atfork(void (*prepare)(void), void (*parent)(void),
294 void (*child)(void))
295{
296 if (__isthreaded)
297 return (-1);
298 cb_prepare = prepare;
299 cb_parent = parent;
300 cb_child = child;
301 return (0);
302}
139374e6 303
304static int __used
305stub_isthreaded(void)
306{
307 return (__isthreaded != 0);
308}