Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libpthread / pthread_cancelstub.c
blobe36396853c678186d354316a6f36a4757f43d18c
1 /* $NetBSD: pthread_cancelstub.c,v 1.26 2009/01/13 01:50:04 christos Exp $ */
3 /*-
4 * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams and Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: pthread_cancelstub.c,v 1.26 2009/01/13 01:50:04 christos Exp $");
35 #ifndef lint
39 * This is necessary because the names are always weak (they are not
40 * POSIX functions).
42 #define fsync_range _fsync_range
43 #define pollts _pollts
46 * XXX this is necessary to get the prototypes for the __sigsuspend14
47 * XXX and __msync13 internal names, instead of the application-visible
48 * XXX sigsuspend and msync names. It's kind of gross, but we're pretty
49 * XXX intimate with libc already.
51 #define __LIBC12_SOURCE__
53 #include <sys/msg.h>
54 #include <sys/types.h>
55 #include <sys/uio.h>
56 #include <sys/wait.h>
57 #include <aio.h>
58 #include <fcntl.h>
59 #include <mqueue.h>
60 #include <poll.h>
61 #include <stdarg.h>
62 #include <unistd.h>
64 #include <signal.h>
65 #include <sys/mman.h>
66 #include <sys/select.h>
67 #include <sys/socket.h>
69 #include <compat/sys/mman.h>
70 #include <compat/sys/poll.h>
71 #include <compat/sys/select.h>
72 #include <compat/sys/wait.h>
73 #include <compat/include/mqueue.h>
74 #include <compat/include/signal.h>
76 #include "pthread.h"
77 #include "pthread_int.h"
79 int pthread__cancel_stub_binder;
81 int _sys_accept(int, struct sockaddr *, socklen_t *);
82 int _sys___aio_suspend50(const struct aiocb * const [], int,
83 const struct timespec *);
84 int __aio_suspend50(const struct aiocb * const [], int,
85 const struct timespec *);
86 int _sys_close(int);
87 int _sys_connect(int, const struct sockaddr *, socklen_t);
88 int _sys_fcntl(int, int, ...);
89 int _sys_fdatasync(int);
90 int _sys_fsync(int);
91 int _sys_fsync_range(int, int, off_t, off_t);
92 int _sys_mq_send(mqd_t, const char *, size_t, unsigned);
93 ssize_t _sys_mq_receive(mqd_t, char *, size_t, unsigned *);
94 int _sys___mq_timedsend50(mqd_t, const char *, size_t, unsigned,
95 const struct timespec *);
96 ssize_t _sys___mq_timedreceive50(mqd_t, char *, size_t, unsigned *,
97 const struct timespec *);
98 ssize_t _sys_msgrcv(int, void *, size_t, long, int);
99 int _sys_msgsnd(int, const void *, size_t, int);
100 int _sys___msync13(void *, size_t, int);
101 int _sys___nanosleep50(const struct timespec *, struct timespec *);
102 int __nanosleep50(const struct timespec *, struct timespec *);
103 int _sys_open(const char *, int, ...);
104 int _sys_poll(struct pollfd *, nfds_t, int);
105 int _sys___pollts50(struct pollfd *, nfds_t, const struct timespec *,
106 const sigset_t *);
107 ssize_t _sys_pread(int, void *, size_t, off_t);
108 int _sys___pselect50(int, fd_set *, fd_set *, fd_set *,
109 const struct timespec *, const sigset_t *);
110 ssize_t _sys_pwrite(int, const void *, size_t, off_t);
111 ssize_t _sys_read(int, void *, size_t);
112 ssize_t _sys_readv(int, const struct iovec *, int);
113 int _sys___select50(int, fd_set *, fd_set *, fd_set *, struct timeval *);
114 int _sys___wait450(pid_t, int *, int, struct rusage *);
115 ssize_t _sys_write(int, const void *, size_t);
116 ssize_t _sys_writev(int, const struct iovec *, int);
117 int _sys___sigsuspend14(const sigset_t *);
118 int ____sigtimedwait50(const sigset_t * __restrict, siginfo_t * __restrict,
119 struct timespec * __restrict);
120 int __sigsuspend14(const sigset_t *);
122 #define TESTCANCEL(id) do { \
123 if (__predict_false((id)->pt_cancel)) \
124 pthread__cancelled(); \
125 } while (/*CONSTCOND*/0)
129 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
131 int retval;
132 pthread_t self;
134 self = pthread__self();
135 TESTCANCEL(self);
136 retval = _sys_accept(s, addr, addrlen);
137 TESTCANCEL(self);
139 return retval;
143 __aio_suspend50(const struct aiocb * const list[], int nent,
144 const struct timespec *timeout)
146 int retval;
147 pthread_t self;
149 self = pthread__self();
150 TESTCANCEL(self);
151 retval = _sys___aio_suspend50(list, nent, timeout);
152 TESTCANCEL(self);
154 return retval;
158 close(int d)
160 int retval;
161 pthread_t self;
163 self = pthread__self();
164 TESTCANCEL(self);
165 retval = _sys_close(d);
166 TESTCANCEL(self);
168 return retval;
172 connect(int s, const struct sockaddr *addr, socklen_t namelen)
174 int retval;
175 pthread_t self;
177 self = pthread__self();
178 TESTCANCEL(self);
179 retval = _sys_connect(s, addr, namelen);
180 TESTCANCEL(self);
182 return retval;
186 fcntl(int fd, int cmd, ...)
188 int retval;
189 pthread_t self;
190 va_list ap;
192 self = pthread__self();
193 TESTCANCEL(self);
194 va_start(ap, cmd);
195 retval = _sys_fcntl(fd, cmd, va_arg(ap, void *));
196 va_end(ap);
197 TESTCANCEL(self);
199 return retval;
203 fdatasync(int d)
205 int retval;
206 pthread_t self;
208 self = pthread__self();
209 TESTCANCEL(self);
210 retval = _sys_fdatasync(d);
211 TESTCANCEL(self);
213 return retval;
217 fsync(int d)
219 int retval;
220 pthread_t self;
222 self = pthread__self();
223 TESTCANCEL(self);
224 retval = _sys_fsync(d);
225 TESTCANCEL(self);
227 return retval;
231 fsync_range(int d, int f, off_t s, off_t e)
233 int retval;
234 pthread_t self;
236 self = pthread__self();
237 TESTCANCEL(self);
238 retval = _sys_fsync_range(d, f, s, e);
239 TESTCANCEL(self);
241 return retval;
245 mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
247 int retval;
248 pthread_t self;
250 self = pthread__self();
251 TESTCANCEL(self);
252 retval = _sys_mq_send(mqdes, msg_ptr, msg_len, msg_prio);
253 TESTCANCEL(self);
255 return retval;
258 ssize_t
259 mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio)
261 ssize_t retval;
262 pthread_t self;
264 self = pthread__self();
265 TESTCANCEL(self);
266 retval = _sys_mq_receive(mqdes, msg_ptr, msg_len, msg_prio);
267 TESTCANCEL(self);
269 return retval;
273 __mq_timedsend50(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
274 unsigned msg_prio, const struct timespec *abst)
276 int retval;
277 pthread_t self;
279 self = pthread__self();
280 TESTCANCEL(self);
281 retval = _sys___mq_timedsend50(mqdes, msg_ptr, msg_len, msg_prio, abst);
282 TESTCANCEL(self);
284 return retval;
287 ssize_t
288 __mq_timedreceive50(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio,
289 const struct timespec *abst)
291 ssize_t retval;
292 pthread_t self;
294 self = pthread__self();
295 TESTCANCEL(self);
296 retval = _sys___mq_timedreceive50(mqdes, msg_ptr, msg_len, msg_prio, abst);
297 TESTCANCEL(self);
299 return retval;
302 ssize_t
303 msgrcv(int msgid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
305 ssize_t retval;
306 pthread_t self;
308 self = pthread__self();
309 TESTCANCEL(self);
310 retval = _sys_msgrcv(msgid, msgp, msgsz, msgtyp, msgflg);
311 TESTCANCEL(self);
313 return retval;
317 msgsnd(int msgid, const void *msgp, size_t msgsz, int msgflg)
319 int retval;
320 pthread_t self;
322 self = pthread__self();
323 TESTCANCEL(self);
324 retval = _sys_msgsnd(msgid, msgp, msgsz, msgflg);
325 TESTCANCEL(self);
327 return retval;
331 __msync13(void *addr, size_t len, int flags)
333 int retval;
334 pthread_t self;
336 self = pthread__self();
337 TESTCANCEL(self);
338 retval = _sys___msync13(addr, len, flags);
339 TESTCANCEL(self);
341 return retval;
345 open(const char *path, int flags, ...)
347 int retval;
348 pthread_t self;
349 va_list ap;
351 self = pthread__self();
352 TESTCANCEL(self);
353 va_start(ap, flags);
354 retval = _sys_open(path, flags, va_arg(ap, mode_t));
355 va_end(ap);
356 TESTCANCEL(self);
358 return retval;
362 __nanosleep50(const struct timespec *rqtp, struct timespec *rmtp)
364 int retval;
365 pthread_t self;
367 self = pthread__self();
368 TESTCANCEL(self);
370 * For now, just nanosleep. In the future, maybe pass a ucontext_t
371 * to _lwp_nanosleep() and allow it to recycle our kernel stack.
373 retval = _sys___nanosleep50(rqtp, rmtp);
374 TESTCANCEL(self);
376 return retval;
380 poll(struct pollfd *fds, nfds_t nfds, int timeout)
382 int retval;
383 pthread_t self;
385 self = pthread__self();
386 TESTCANCEL(self);
387 retval = _sys_poll(fds, nfds, timeout);
388 TESTCANCEL(self);
390 return retval;
394 __pollts50(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
395 const sigset_t *sigmask)
397 int retval;
398 pthread_t self;
400 self = pthread__self();
401 TESTCANCEL(self);
402 retval = _sys___pollts50(fds, nfds, ts, sigmask);
403 TESTCANCEL(self);
405 return retval;
408 ssize_t
409 pread(int d, void *buf, size_t nbytes, off_t offset)
411 ssize_t retval;
412 pthread_t self;
414 self = pthread__self();
415 TESTCANCEL(self);
416 retval = _sys_pread(d, buf, nbytes, offset);
417 TESTCANCEL(self);
419 return retval;
423 __pselect50(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
424 const struct timespec *timeout, const sigset_t *sigmask)
426 int retval;
427 pthread_t self;
429 self = pthread__self();
430 TESTCANCEL(self);
431 retval = _sys___pselect50(nfds, readfds, writefds, exceptfds, timeout,
432 sigmask);
433 TESTCANCEL(self);
435 return retval;
438 ssize_t
439 pwrite(int d, const void *buf, size_t nbytes, off_t offset)
441 ssize_t retval;
442 pthread_t self;
444 self = pthread__self();
445 TESTCANCEL(self);
446 retval = _sys_pwrite(d, buf, nbytes, offset);
447 TESTCANCEL(self);
449 return retval;
452 #ifdef _FORTIFY_SOURCE
453 #undef read
454 #endif
456 ssize_t
457 read(int d, void *buf, size_t nbytes)
459 ssize_t retval;
460 pthread_t self;
462 self = pthread__self();
463 TESTCANCEL(self);
464 retval = _sys_read(d, buf, nbytes);
465 TESTCANCEL(self);
467 return retval;
470 ssize_t
471 readv(int d, const struct iovec *iov, int iovcnt)
473 ssize_t retval;
474 pthread_t self;
476 self = pthread__self();
477 TESTCANCEL(self);
478 retval = _sys_readv(d, iov, iovcnt);
479 TESTCANCEL(self);
481 return retval;
485 __select50(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
486 struct timeval *timeout)
488 int retval;
489 pthread_t self;
491 self = pthread__self();
492 TESTCANCEL(self);
493 retval = _sys___select50(nfds, readfds, writefds, exceptfds, timeout);
494 TESTCANCEL(self);
496 return retval;
499 pid_t
500 __wait450(pid_t wpid, int *status, int options, struct rusage *rusage)
502 pid_t retval;
503 pthread_t self;
505 self = pthread__self();
506 TESTCANCEL(self);
507 retval = _sys___wait450(wpid, status, options, rusage);
508 TESTCANCEL(self);
510 return retval;
513 ssize_t
514 write(int d, const void *buf, size_t nbytes)
516 ssize_t retval;
517 pthread_t self;
519 self = pthread__self();
520 TESTCANCEL(self);
521 retval = _sys_write(d, buf, nbytes);
522 TESTCANCEL(self);
524 return retval;
527 ssize_t
528 writev(int d, const struct iovec *iov, int iovcnt)
530 ssize_t retval;
531 pthread_t self;
533 self = pthread__self();
534 TESTCANCEL(self);
535 retval = _sys_writev(d, iov, iovcnt);
536 TESTCANCEL(self);
538 return retval;
542 __sigsuspend14(const sigset_t *sigmask)
544 pthread_t self;
545 int retval;
547 self = pthread__self();
548 TESTCANCEL(self);
549 retval = _sys___sigsuspend14(sigmask);
550 TESTCANCEL(self);
552 return retval;
556 __sigtimedwait50(const sigset_t * __restrict set, siginfo_t * __restrict info,
557 const struct timespec * __restrict timeout)
559 pthread_t self;
560 int retval;
561 struct timespec tout, *tp;
562 if (timeout) {
563 tout = *timeout;
564 tp = &tout;
565 } else
566 tp = NULL;
568 self = pthread__self();
569 TESTCANCEL(self);
570 retval = ____sigtimedwait50(set, info, tp);
571 TESTCANCEL(self);
573 return retval;
576 __strong_alias(_close, close)
577 __strong_alias(_fcntl, fcntl)
578 __strong_alias(_fdatasync, fdatasync)
579 __strong_alias(_fsync, fsync)
580 __weak_alias(fsync_range, _fsync_range)
581 __strong_alias(_mq_send, mq_send)
582 __strong_alias(_mq_receive, mq_receive)
583 __strong_alias(_msgrcv, msgrcv)
584 __strong_alias(_msgsnd, msgsnd)
585 __strong_alias(___msync13, __msync13)
586 __strong_alias(___nanosleep50, __nanosleep50)
587 __strong_alias(_open, open)
588 __strong_alias(_poll, poll)
589 __strong_alias(_pread, pread)
590 __strong_alias(_pwrite, pwrite)
591 __strong_alias(_read, read)
592 __strong_alias(_readv, readv)
593 __strong_alias(_write, write)
594 __strong_alias(_writev, writev)
596 #endif /* !lint */