]>
Commit | Line | Data |
---|---|---|
ccb42cb8 MK |
1 | .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk |
2 | .\" <mtk.manpages@gmail.com> | |
3 | .\" | |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
ccb42cb8 MK |
5 | .\" Permission is granted to make and distribute verbatim copies of this |
6 | .\" manual provided the copyright notice and this permission notice are | |
7 | .\" preserved on all copies. | |
8 | .\" | |
9 | .\" Permission is granted to copy and distribute modified versions of this | |
10 | .\" manual under the conditions for verbatim copying, provided that the | |
11 | .\" entire resulting derived work is distributed under the terms of a | |
12 | .\" permission notice identical to this one. | |
13 | .\" | |
14 | .\" Since the Linux kernel and libraries are constantly changing, this | |
15 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
16 | .\" responsibility for errors or omissions, or for damages resulting from | |
17 | .\" the use of the information contained herein. The author(s) may not | |
18 | .\" have taken the same level of care in the production of this manual, | |
19 | .\" which is licensed free of charge, as they might when working | |
20 | .\" professionally. | |
21 | .\" | |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by | |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
ccb42cb8 | 25 | .\" |
867c9b34 | 26 | .TH PTHREAD_SETCANCELSTATE 3 2019-10-10 "Linux" "Linux Programmer's Manual" |
ccb42cb8 MK |
27 | .SH NAME |
28 | pthread_setcancelstate, pthread_setcanceltype \- | |
29 | set cancelability state and type | |
30 | .SH SYNOPSIS | |
31 | .nf | |
32 | .B #include <pthread.h> | |
dbfe9c70 | 33 | .PP |
ccb42cb8 MK |
34 | .BI "int pthread_setcancelstate(int " state ", int *" oldstate ); |
35 | .BI "int pthread_setcanceltype(int " type ", int *" oldtype ); | |
68e4db0a | 36 | .PP |
ccb42cb8 | 37 | Compile and link with \fI\-pthread\fP. |
6030f2d8 | 38 | .fi |
ccb42cb8 MK |
39 | .SH DESCRIPTION |
40 | The | |
41 | .BR pthread_setcancelstate () | |
42 | sets the cancelability state of the calling thread to the value | |
43 | given in | |
44 | .IR state . | |
45 | The previous cancelability state of the thread is returned | |
46 | in the buffer pointed to by | |
47 | .IR oldstate . | |
48 | The | |
49 | .I state | |
50 | argument must have one of the following values: | |
51 | .TP | |
52 | .B PTHREAD_CANCEL_ENABLE | |
53 | The thread is cancelable. | |
54 | This is the default cancelability state in all new threads, | |
55 | including the initial thread. | |
56 | The thread's cancelability type determines when a cancelable thread | |
57 | will respond to a cancellation request. | |
58 | .TP | |
59 | .B PTHREAD_CANCEL_DISABLE | |
60 | The thread is not cancelable. | |
61 | If a cancellation request is received, | |
62 | it is blocked until cancelability is enabled. | |
63 | .PP | |
64 | The | |
65 | .BR pthread_setcanceltype () | |
66 | sets the cancelability type of the calling thread to the value | |
67 | given in | |
68 | .IR type . | |
69 | The previous cancelability type of the thread is returned | |
70 | in the buffer pointed to by | |
71 | .IR oldtype . | |
72 | The | |
73 | .I type | |
74 | argument must have one of the following values: | |
75 | .TP | |
76 | .B PTHREAD_CANCEL_DEFERRED | |
77 | A cancellation request is deferred until the thread next calls | |
78 | a function that is a cancellation point (see | |
79 | .BR pthreads (7)). | |
80 | This is the default cancelability type in all new threads, | |
50639a2a | 81 | including the initial thread. |
0b6cf5d2 MK |
82 | .IP |
83 | Even with deferred cancellation, a | |
dbb01cbb | 84 | cancellation point in an asynchronous signal handler may still |
0b6cf5d2 | 85 | be acted upon and the effect is as if it was an asynchronous |
dbb01cbb | 86 | cancellation. |
ccb42cb8 MK |
87 | .TP |
88 | .B PTHREAD_CANCEL_ASYNCHRONOUS | |
89 | The thread can be canceled at any time. | |
90 | (Typically, | |
91 | it will be canceled immediately upon receiving a cancellation request, | |
92 | but the system doesn't guarantee this.) | |
93 | .PP | |
94 | The set-and-get operation performed by each of these functions | |
95 | is atomic with respect to other threads in the process | |
96 | calling the same function. | |
97 | .SH RETURN VALUE | |
98 | On success, these functions return 0; | |
c7094399 | 99 | on error, they return a nonzero error number. |
ccb42cb8 MK |
100 | .SH ERRORS |
101 | The | |
102 | .BR pthread_setcancelstate () | |
103 | can fail with the following error: | |
104 | .TP | |
105 | .B EINVAL | |
106 | Invalid value for | |
107 | .IR state . | |
108 | .PP | |
109 | The | |
110 | .BR pthread_setcanceltype () | |
111 | can fail with the following error: | |
112 | .TP | |
113 | .B EINVAL | |
114 | Invalid value for | |
115 | .IR type . | |
116 | .\" .SH VERSIONS | |
117 | .\" Available since glibc 2.0 | |
50b60123 | 118 | .SH ATTRIBUTES |
dfa2186c MK |
119 | For an explanation of the terms used in this section, see |
120 | .BR attributes (7). | |
121 | .ad l | |
122 | .TS | |
123 | allbox; | |
124 | lb lb lb | |
125 | lw25 l l. | |
126 | Interface Attribute Value | |
127 | T{ | |
128 | .BR pthread_setcancelstate (), | |
50b60123 | 129 | .BR pthread_setcanceltype () |
dfa2186c MK |
130 | T} Thread safety T{ |
131 | MT-Safe | |
132 | T} | |
f385a71d MK |
133 | T{ |
134 | .BR pthread_setcancelstate (), | |
135 | .BR pthread_setcanceltype () | |
136 | T} Async-cancel-safety T{ | |
137 | AC-Safe | |
138 | T} | |
dfa2186c MK |
139 | .TE |
140 | .ad | |
141 | .hy | |
ccb42cb8 | 142 | .SH CONFORMING TO |
1d426e09 | 143 | POSIX.1-2001, POSIX.1-2008. |
ccb42cb8 MK |
144 | .SH NOTES |
145 | For details of what happens when a thread is canceled, see | |
146 | .BR pthread_cancel (3). | |
847e0d88 | 147 | .PP |
ccb42cb8 MK |
148 | Briefly disabling cancelability is useful |
149 | if a thread performs some critical action | |
150 | that must not be interrupted by a cancellation request. | |
151 | Beware of disabling cancelability for long periods, | |
152 | or around operations that may block for long periods, | |
153 | since that will render the thread unresponsive to cancellation requests. | |
fad1a267 | 154 | .SS Asynchronous cancelability |
ccb42cb8 MK |
155 | Setting the cancelability type to |
156 | .B PTHREAD_CANCEL_ASYNCHRONOUS | |
157 | is rarely useful. | |
158 | Since the thread could be canceled at | |
159 | .I any | |
ee1534cb MK |
160 | time, it cannot safely reserve resources (e.g., allocating memory with |
161 | .BR malloc (3)), | |
162 | acquire mutexes, semaphores, or locks, and so on. | |
f5410853 | 163 | Reserving resources is unsafe because the application has no way of |
ee1534cb MK |
164 | knowing what the state of these resources is when the thread is canceled; |
165 | that is, did cancellation occur before the resources were reserved, | |
166 | while they were reserved, or after they were released? | |
a86f0a4b MK |
167 | Furthermore, some internal data structures |
168 | (e.g., the linked list of free blocks managed by the | |
169 | .BR malloc (3) | |
170 | family of functions) may be left in an inconsistent state | |
171 | if cancellation occurs in the middle of the function call. | |
ee1534cb | 172 | Consequently, clean-up handlers cease to be useful. |
847e0d88 | 173 | .PP |
ee1534cb | 174 | Functions that can be safely asynchronously canceled are called |
a113945f | 175 | .IR "async-cancel-safe functions" . |
1d426e09 | 176 | POSIX.1-2001 and POSIX.1-2008 require only that |
ccb42cb8 MK |
177 | .BR pthread_cancel (3), |
178 | .BR pthread_setcancelstate (), | |
179 | and | |
180 | .BR pthread_setcanceltype () | |
ee1534cb MK |
181 | be async-cancel-safe. |
182 | In general, other library functions | |
183 | can't be safely called from an asynchronously cancelable thread. | |
847e0d88 | 184 | .PP |
ccb42cb8 MK |
185 | One of the few circumstances in which asynchronous cancelability is useful |
186 | is for cancellation of a thread that is in a pure compute-bound loop. | |
fad1a267 | 187 | .SS Portability notes |
ccb42cb8 MK |
188 | The Linux threading implementations permit the |
189 | .I oldstate | |
190 | argument of | |
191 | .BR pthread_setcancelstate () | |
192 | to be NULL, in which case the information about the previous | |
193 | cancelability state is not returned to the caller. | |
194 | Many other implementations also permit a NULL | |
195 | .I oldstat | |
196 | argument, | |
197 | .\" It looks like at least Solaris, FreeBSD and Tru64 support this. | |
1d426e09 | 198 | but POSIX.1 does not specify this point, |
ccb42cb8 MK |
199 | so portable applications should always specify a non-NULL value in |
200 | .IR oldstate . | |
201 | A precisely analogous set of statements applies for the | |
202 | .I oldtype | |
203 | argument of | |
204 | .BR pthread_setcanceltype (). | |
a14af333 | 205 | .SH EXAMPLES |
ccb42cb8 MK |
206 | See |
207 | .BR pthread_cancel (3). | |
208 | .SH SEE ALSO | |
ccb42cb8 | 209 | .BR pthread_cancel (3), |
3e5c319e | 210 | .BR pthread_cleanup_push (3), |
ccb42cb8 MK |
211 | .BR pthread_testcancel (3), |
212 | .BR pthreads (7) |