xref: /freebsd/lib/libthr/thread/thr_kern.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/types.h>
31 #include <sys/signalvar.h>
32 #include <pthread.h>
33 
34 #include "thr_private.h"
35 
36 /*#define DEBUG_THREAD_KERN */
37 #ifdef DEBUG_THREAD_KERN
38 #define DBG_MSG		stdout_debug
39 #else
40 #define DBG_MSG(x...)
41 #endif
42 
43 /*
44  * This is called when the first thread (other than the initial
45  * thread) is created.
46  */
47 int
48 _thr_setthreaded(int threaded)
49 {
50 	if (((threaded == 0) ^ (__isthreaded == 0)) == 0)
51 		return (0);
52 
53 	__isthreaded = threaded;
54 	if (threaded != 0) {
55 		_thr_rtld_init();
56 	} else {
57 		_thr_rtld_fini();
58 	}
59 	return (0);
60 }
61 
62 void
63 _thr_signal_block(struct pthread *curthread)
64 {
65 	sigset_t set;
66 
67 	if (curthread->sigblock > 0) {
68 		curthread->sigblock++;
69 		return;
70 	}
71 	SIGFILLSET(set);
72 	SIGDELSET(set, SIGBUS);
73 	SIGDELSET(set, SIGILL);
74 	SIGDELSET(set, SIGFPE);
75 	SIGDELSET(set, SIGSEGV);
76 	SIGDELSET(set, SIGTRAP);
77 	__sys_sigprocmask(SIG_BLOCK, &set, &curthread->sigmask);
78 	curthread->sigblock++;
79 }
80 
81 void
82 _thr_signal_unblock(struct pthread *curthread)
83 {
84 	if (--curthread->sigblock == 0)
85 		__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
86 }
87 
88 int
89 _thr_send_sig(struct pthread *thread, int sig)
90 {
91 	return thr_kill(thread->tid, sig);
92 }
93 
94 void
95 _thr_assert_lock_level()
96 {
97 	PANIC("locklevel <= 0");
98 }
99