8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / man / man3c / pthread_sigmask.3c
blob3f3d13a318732a3cb4a75e7ac18c5f35e89a43c2
1 '\" te
2 .\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
3 .\" Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
4 .\" Portions Copyright (c) 1995 IEEE.  All Rights Reserved
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
6 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
8 .\"  This notice shall appear on any product containing this material.
9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 .TH PTHREAD_SIGMASK 3C "Mar 23, 2005"
13 .SH NAME
14 pthread_sigmask \- change or examine calling thread's signal mask
15 .SH SYNOPSIS
16 .LP
17 .nf
18 cc -mt [ \fIflag\fR... ] \fIfile\fR... -lpthread [ \fIlibrary\fR... ]
19 #include <pthread.h>
20 #include <signal.h>
22 \fBint\fR \fBpthread_sigmask\fR(\fBint\fR \fIhow\fR, \fBconst sigset_t *\fR\fIset\fR, \fBsigset_t *\fR\fIoset\fR);
23 .fi
25 .SH DESCRIPTION
26 .sp
27 .LP
28 The \fBpthread_sigmask()\fR function changes or examines a calling thread's
29 signal mask. Each thread has its own signal mask. A new thread inherits the
30 calling thread's signal mask and priority; however, pending signals are not
31 inherited. Signals pending for a new thread will be empty.
32 .sp
33 .LP
34 If the value of the argument \fIset\fR is not \fINULL\fR, \fIset\fR points to a
35 set of signals that can modify the currently blocked set. If the value of
36 \fIset\fR is \fINULL\fR, the value of \fIhow\fR is insignificant and the
37 thread's signal mask is unmodified; thus, \fBpthread_sigmask()\fR can be used
38 to inquire about the currently blocked signals.
39 .sp
40 .LP
41 The value of the argument \fIhow\fR specifies the method in which the set is
42 changed and  takes one of the following values:
43 .sp
44 .ne 2
45 .na
46 \fB\fBSIG_BLOCK\fR\fR
47 .ad
48 .RS 15n
49 \fIset\fR corresponds to a set of signals to block. They are added to the
50 current signal mask.
51 .RE
53 .sp
54 .ne 2
55 .na
56 \fB\fBSIG_UNBLOCK\fR\fR
57 .ad
58 .RS 15n
59 \fIset\fR corresponds to a set of signals to unblock. These signals are deleted
60 from the current signal mask.
61 .RE
63 .sp
64 .ne 2
65 .na
66 \fB\fBSIG_SETMASK\fR\fR
67 .ad
68 .RS 15n
69 \fIset\fR corresponds to the new signal mask. The current signal mask is
70 replaced by \fBset\fR.
71 .RE
73 .sp
74 .LP
75 If the value of \fIoset\fR is not \fINULL\fR, it points to the location where
76 the previous signal mask is stored.
77 .SH RETURN VALUES
78 .sp
79 .LP
80 Upon successful completion, the \fBpthread_sigmask()\fR function returns
81 \fB0\fR. Otherwise, it returns a non-zero value.
82 .SH ERRORS
83 .sp
84 .LP
85 The \fBpthread_sigmask()\fR function will fail if:
86 .sp
87 .ne 2
88 .na
89 \fB\fBEINVAL\fR\fR
90 .ad
91 .RS 10n
92 The value of \fIhow\fR is not defined and \fIoset\fR is \fINULL\fR.
93 .RE
95 .SH EXAMPLES
96 .LP
97 \fBExample 1 \fRCreate a default thread that  can serve as a signal
98 catcher/handler with its own signal mask.
99 .sp
101 The following example shows how to create a default thread that  can serve as a
102 signal catcher/handler with its own signal mask. \fBnew\fR will have a
103 different value from the creator's signal mask.
107 As POSIX threads and Solaris threads are fully compatible even within the same
108 process, this example uses \fBpthread_create\fR(3C) if you execute \fBa.out
109 0\fR, or \fBthr_create\fR(3C) if you execute \fBa.out 1\fR.
113 In this example:
115 .RS +4
117 .ie t \(bu
118 .el o
119 The \fBsigemptyset\fR(3C) function initializes a null signal set, \fBnew\fR.
120 The \fBsigaddset\fR(3C) function packs the signal, \fBSIGINT\fR, into that new
121 set.
123 .RS +4
125 .ie t \(bu
126 .el o
127 Either \fBpthread_sigmask()\fR or \fBthr_sigsetmask()\fR is used to mask the
128 signal, \fBSIGINT\fR (CTRL-C), from the calling thread, which is \fBmain()\fR.
129 The signal is masked to guarantee that only the new thread will  receive this
130 signal.
132 .RS +4
134 .ie t \(bu
135 .el o
136 \fBpthread_create()\fR or \fBthr_create()\fR creates the signal-handling
137 thread.
139 .RS +4
141 .ie t \(bu
142 .el o
143 Using \fBpthread_join\fR(3C) or \fBthr_join\fR(3C), \fBmain()\fR then waits for
144 the termination of that signal-handling thread, whose \fBID\fR number is
145 \fBuser_threadID\fR; \fBmain()\fR will then \fBsleep\fR(3C) for 2 seconds,
146 after which the program terminates.
148 .RS +4
150 .ie t \(bu
151 .el o
152 The signal-handling thread, \fBhandler\fR:
153 .RS +4
155 .ie t \(bu
156 .el o
157 Assigns the handler \fBinterrupt()\fR to handle the signal \fBSIGINT\fR, by the
158 call to  \fBsigaction\fR(2).
160 .RS +4
162 .ie t \(bu
163 .el o
164 Resets its own signal set to \fInot block\fR the signal, \fBSIGINT\fR.
166 .RS +4
168 .ie t \(bu
169 .el o
170 Sleeps for 8 seconds to allow time for the user to deliver the signal,
171 \fBSIGINT\fR, by pressing the  \fBCTRL-C\fR.
175 .in +2
177 /* cc thisfile.c -lthread -lpthread */
178 #define _REENTRANT    /* basic first 3-lines for threads */
179 #include <pthread.h>
180 #include <thread.h>
181 thread_t user_threadID;
182 sigset_t new;
183 void *handler(\|), interrupt(\|);
186 main( int argc, char *argv[\|] )  {
187         test_argv(argv[1]);
189         sigemptyset(&new);
190         sigaddset(&new, SIGINT);
191         switch(*argv[1])  {
193                 case '0':   /* POSIX */
194                         pthread_sigmask(SIG_BLOCK, &new, NULL);
195                         pthread_create(&user_threadID, NULL, handler,
196             argv[1]);
197                         pthread_join(user_threadID, NULL);
198                         break;
200                 case '1':   /* Solaris */
201                         thr_sigsetmask(SIG_BLOCK, &new, NULL);
202                         thr_create(NULL, 0, handler, argv[1], 0,
203             &user_threadID);
204                         thr_join(user_threadID, NULL, NULL);
205                         break;
206                 }  /* switch */
208         printf("thread handler, # %d, has exited\en",user_threadID);
209         sleep(2);
210         printf("main thread, # %d is done\en", thr_self(\|));
211  return (0)
212 } /* end main */
214 struct sigaction act;
216 void *
217 handler(char *argv1)
219         act.sa_handler = interrupt;
220         sigaction(SIGINT, &act, NULL);
221         switch(*argv1)  {
222                 case '0':     /* POSIX */
223                         pthread_sigmask(SIG_UNBLOCK, &new, NULL);
224                         break;
225                 case '1':   /* Solaris */
226                         thr_sigsetmask(SIG_UNBLOCK, &new, NULL);
227                         break;
228         }
229         printf("\en Press CTRL-C to deliver SIGINT signal to the
230      process\en");
231         sleep(8);  /* give user time to hit CTRL-C */
232  return (NULL)
235 void
236 interrupt(int sig)
238  printf("thread %d caught signal %d\en", thr_self(\|), sig);
241 void test_argv(char argv1[\|])    {
242         if(argv1 == NULL)  {
243                 printf("use 0 as arg1 to use thr_create(\|);\en \e
244                 or use 1 as arg1 to use pthread_create(\|)\en");
245                 exit(NULL);
246         }
249 .in -2
253 In the last example, the \fBhandler\fR thread served as a signal-handler while
254 also taking care of activity of its own (in this case, sleeping, although it
255 could have been some other activity). A thread could be completely dedicated to
256 signal-handling simply by waiting for the delivery of a selected signal by
257 blocking with \fBsigwait\fR(2). The two subroutines in the previous example,
258 \fBhandler()\fR and \fBinterrupt()\fR, could have been replaced with the
259 following routine:
262 .in +2
264 void *
265 handler(void *unused)
267     int signal;
268     printf("thread %d is waiting for you to press the CTRL-C keys\en",
269             thr_self(\|));
270     sigwait(&new, &signal);
271     printf("thread %d has received the signal %d \en", thr_self(\|),
272         signal);
273     return (NULL);
275 /* pthread_create(\|) and thr_create(\|) would use NULL instead
276    of argv[1] for the arg passed to handler(\|) */
278 .in -2
282 In this routine, one thread is dedicated to catching and handling the  signal
283 specified by the set \fBnew\fR, which allows  \fBmain()\fR and all of its other
284 sub-threads, created \fIafter\fR \fBpthread_sigmask()\fR or
285 \fBthr_sigsetmask()\fR masked that signal, to continue uninterrupted. Any use
286 of  \fBsigwait\fR(2) should be such that all threads block the signals passed
287 to \fBsigwait\fR(2) at all times. Only the thread that calls \fBsigwait()\fR
288 will get the signals. The call to \fBsigwait\fR(2) takes two arguments.
292 For this type of background dedicated signal-handling routine, a Solaris daemon
293 thread can be used by passing the argument \fBTHR_DAEMON\fR to
294 \fBthr_create\fR(3C).
296 .SH ATTRIBUTES
299 See \fBattributes\fR(5) for descriptions of the following attributes:
304 box;
305 c | c
306 l | l .
307 ATTRIBUTE TYPE  ATTRIBUTE VALUE
309 Interface Stability     Standard
311 MT-Level        MT-Safe and Async-Signal-Safe
314 .SH SEE ALSO
317 \fBsigaction\fR(2), \fBsigprocmask\fR(2), \fBsigwait\fR(2),
318 \fBcond_wait\fR(3C), \fBpthread_cancel\fR(3C), \fBpthread_create\fR(3C),
319 \fBpthread_join\fR(3C), \fBpthread_self\fR(3C), \fBsigaddset\fR(3C),
320 \fBsigemptyset\fR(3C), \fBsigsetops\fR(3C), \fBsleep\fR(3C),
321 \fBattributes\fR(5), \fBcancellation\fR(5), \fBstandards\fR(5)
322 .SH NOTES
325 It is not possible to block signals that cannot be caught or ignored (see
326 \fBsigaction\fR(2)). It is also not possible to block or unblock
327 \fBSIGCANCEL\fR, as \fBSIGCANCEL\fR is reserved for the implementation of POSIX
328 thread cancellation (see \fBpthread_cancel\fR(3C) and \fBcancellation\fR(5)).
329 This restriction is quietly enforced by the standard C library.
332 Using \fBsigwait\fR(2) in a dedicated thread allows asynchronously generated
333 signals to be managed synchronously; however, \fBsigwait\fR(2) should never be
334 used to manage synchronously generated signals.
337 Synchronously generated signals are exceptions that are generated by a thread
338 and are directed at the thread causing the exception. Since \fBsigwait()\fR
339 blocks waiting for signals, the blocking thread cannot receive a synchronously
340 generated signal.
343 The \fBsigprocmask\fR(2) function behaves the same as if
344 \fBpthread_sigmask()\fR has been called. POSIX leaves the semantics of the call
345 to \fBsigprocmask\fR(2) unspecified in a multi-threaded process, so programs
346 that care about POSIX portability should not depend on this semantic.
349 If a signal is delivered while a thread is waiting on a condition variable, the
350 \fBcond_wait\fR(3C) function will be interrupted and the handler will be
351 executed. The state of the lock protecting the condition variable is undefined
352 while the thread is executing the signal handler.
355 Although \fBpthread_sigmask()\fR is Async-Signal-Safe with respect to the
356 Solaris environment, this safeness is not guaranteed to be portable to other
357 POSIX domains.
360 Signals that are generated synchronously should not be masked. If such a signal
361 is blocked and delivered, the receiving process is killed.