Debian Codegraph @

upstream/debian-trixie/man3/pthread_setcancelstate.3

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