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