2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "namespace.h"
28 #include <sys/signalvar.h>
29 #include <machine/tls.h>
34 #include "un-namespace.h"
36 #include "thr_private.h"
38 #if defined(_PTHREADS_DEBUGGING) || defined(_PTHREADS_DEBUGGING2)
43 /* #define DEBUG_SIGNAL */
45 #define DBG_MSG stdout_debug
50 int __sigwait(const sigset_t *set, int *sig);
51 int __sigwaitinfo(const sigset_t *set, siginfo_t *info);
52 int __sigtimedwait(const sigset_t *set, siginfo_t *info,
53 const struct timespec * timeout);
55 #if defined(_PTHREADS_DEBUGGING) || defined(_PTHREADS_DEBUGGING2)
56 static void _thr_debug_sig(int signo);
60 sigcancel_handler(int sig __unused, siginfo_t *info __unused,
61 ucontext_t *ucp __unused)
63 struct pthread *curthread = tls_get_curthread();
65 if (curthread->cancelflags & THR_CANCEL_AT_POINT)
66 _pthread_testcancel();
71 _thr_ast(struct pthread *curthread)
73 if (!THR_IN_CRITICAL(curthread)) {
74 if (__predict_false((curthread->flags &
75 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
76 == THR_FLAGS_NEED_SUSPEND))
77 _thr_suspend_check(curthread);
82 _thr_suspend_check(struct pthread *curthread)
87 * Blocks SIGCANCEL which other threads must send.
89 _thr_signal_block(curthread);
92 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
93 * because we are leaf code, we don't want to recursively call
96 curthread->critical_count++;
97 THR_UMTX_LOCK(curthread, &(curthread)->lock);
98 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
99 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
101 cycle = curthread->cycle;
103 /* Wake the thread suspending us. */
104 _thr_umtx_wake(&curthread->cycle, 0);
107 * if we are from pthread_exit, we don't want to
108 * suspend, just go and die.
110 if (curthread->state == PS_DEAD)
112 curthread->flags |= THR_FLAGS_SUSPENDED;
113 THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
114 _thr_umtx_wait(&curthread->cycle, cycle, NULL, 0);
115 THR_UMTX_LOCK(curthread, &(curthread)->lock);
116 curthread->flags &= ~THR_FLAGS_SUSPENDED;
118 THR_UMTX_UNLOCK(curthread, &(curthread)->lock);
119 curthread->critical_count--;
121 _thr_signal_unblock(curthread);
125 _thr_signal_init(void)
127 struct sigaction act;
129 /* Install cancel handler. */
130 SIGEMPTYSET(act.sa_mask);
131 act.sa_flags = SA_SIGINFO | SA_RESTART;
132 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
133 __sys_sigaction(SIGCANCEL, &act, NULL);
135 #if defined(_PTHREADS_DEBUGGING) || defined(_PTHREADS_DEBUGGING2)
137 * If enabled, rwlock, mutex, and condition variable operations
138 * are recorded in a text buffer and signal 63 dumps the buffer
139 * to /tmp/cond${pid}.log.
141 act.sa_flags = SA_RESTART;
142 act.sa_handler = _thr_debug_sig;
143 __sys_sigaction(63, &act, NULL);
148 _thr_signal_deinit(void)
153 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
155 /* Check if the signal number is out of range: */
156 if (sig < 1 || sig > _SIG_MAXSIG || sig == SIGCANCEL) {
157 /* Return an invalid argument: */
162 return __sys_sigaction(sig, act, oact);
165 __strong_reference(_sigaction, sigaction);
168 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
170 const sigset_t *p = set;
173 if (how != SIG_UNBLOCK) {
176 SIGDELSET(newset, SIGCANCEL);
180 return (__sys_sigprocmask(how, p, oset));
183 __strong_reference(_sigprocmask, sigprocmask);
186 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
188 if (_sigprocmask(how, set, oset))
193 __strong_reference(_pthread_sigmask, pthread_sigmask);
196 _sigsuspend(const sigset_t * set)
198 struct pthread *curthread = tls_get_curthread();
200 const sigset_t *pset;
204 if (SIGISMEMBER(*set, SIGCANCEL)) {
206 SIGDELSET(newset, SIGCANCEL);
211 oldcancel = _thr_cancel_enter(curthread);
212 ret = __sys_sigsuspend(pset);
213 _thr_cancel_leave(curthread, oldcancel);
218 __strong_reference(_sigsuspend, sigsuspend);
221 __sigtimedwait(const sigset_t *set, siginfo_t *info,
222 const struct timespec * timeout)
224 struct pthread *curthread = tls_get_curthread();
226 const sigset_t *pset;
230 if (SIGISMEMBER(*set, SIGCANCEL)) {
232 SIGDELSET(newset, SIGCANCEL);
236 oldcancel = _thr_cancel_enter(curthread);
237 ret = __sys_sigtimedwait(pset, info, timeout);
238 _thr_cancel_leave(curthread, oldcancel);
242 __strong_reference(__sigtimedwait, sigtimedwait);
245 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
247 struct pthread *curthread = tls_get_curthread();
249 const sigset_t *pset;
253 if (SIGISMEMBER(*set, SIGCANCEL)) {
255 SIGDELSET(newset, SIGCANCEL);
260 oldcancel = _thr_cancel_enter(curthread);
261 ret = __sys_sigwaitinfo(pset, info);
262 _thr_cancel_leave(curthread, oldcancel);
266 __strong_reference(__sigwaitinfo, sigwaitinfo);
269 __sigwait(const sigset_t *set, int *sig)
273 s = __sigwaitinfo(set, NULL);
281 __strong_reference(__sigwait, sigwait);
283 #if defined(_PTHREADS_DEBUGGING) || defined(_PTHREADS_DEBUGGING2)
285 #define LOGBUF_SIZE (4 * 1024 * 1024)
286 #define LOGBUF_MASK (LOGBUF_SIZE - 1)
288 char LogBuf[LOGBUF_SIZE];
289 unsigned long LogWIndex;
292 _thr_log(const char *buf, size_t bytes)
294 struct pthread *curthread;
299 curthread = tls_get_curthread();
301 plen = snprintf(prefix, sizeof(prefix), "%d.%d: ",
305 plen = snprintf(prefix, sizeof(prefix), "unknown: ");
310 i = atomic_fetchadd_long(&LogWIndex, plen + bytes);
312 if (plen <= (size_t)(LOGBUF_SIZE - i)) {
313 bcopy(prefix, LogBuf + i, plen);
315 bcopy(prefix, LogBuf + i, LOGBUF_SIZE - i);
316 plen -= LOGBUF_SIZE - i;
317 bcopy(prefix, LogBuf, plen);
322 if (bytes <= (size_t)(LOGBUF_SIZE - i)) {
323 bcopy(buf, LogBuf + i, bytes);
325 bcopy(buf, LogBuf + i, LOGBUF_SIZE - i);
326 bytes -= LOGBUF_SIZE - i;
327 bcopy(buf, LogBuf, bytes);
332 _thr_debug_sig(int signo __unused)
338 snprintf(buf, sizeof(buf), "/tmp/cond%d.log", (int)__sys_getpid());
339 fd = open(buf, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0666);
342 if (i < LOGBUF_SIZE) {
343 write(fd, LogBuf, i);
346 write(fd, LogBuf + i, LOGBUF_SIZE - i);
347 write(fd, LogBuf, i);