]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_setcancelstate.3
fanotify_init.2, fanotify.7: Document FAN_REPORT_TID
[thirdparty/man-pages.git] / man3 / pthread_setcancelstate.3
CommitLineData
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.\"
4b8c67d9 26.TH PTHREAD_SETCANCELSTATE 3 2017-09-15 "Linux" "Linux Programmer's Manual"
ccb42cb8
MK
27.SH NAME
28pthread_setcancelstate, pthread_setcanceltype \-
29set 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 37Compile and link with \fI\-pthread\fP.
6030f2d8 38.fi
ccb42cb8
MK
39.SH DESCRIPTION
40The
41.BR pthread_setcancelstate ()
42sets the cancelability state of the calling thread to the value
43given in
44.IR state .
45The previous cancelability state of the thread is returned
46in the buffer pointed to by
47.IR oldstate .
48The
49.I state
50argument must have one of the following values:
51.TP
52.B PTHREAD_CANCEL_ENABLE
53The thread is cancelable.
54This is the default cancelability state in all new threads,
55including the initial thread.
56The thread's cancelability type determines when a cancelable thread
57will respond to a cancellation request.
58.TP
59.B PTHREAD_CANCEL_DISABLE
60The thread is not cancelable.
61If a cancellation request is received,
62it is blocked until cancelability is enabled.
63.PP
64The
65.BR pthread_setcanceltype ()
66sets the cancelability type of the calling thread to the value
67given in
68.IR type .
69The previous cancelability type of the thread is returned
70in the buffer pointed to by
71.IR oldtype .
72The
73.I type
74argument must have one of the following values:
75.TP
76.B PTHREAD_CANCEL_DEFERRED
77A cancellation request is deferred until the thread next calls
78a function that is a cancellation point (see
79.BR pthreads (7)).
80This is the default cancelability type in all new threads,
81including the initial thread.
82.TP
83.B PTHREAD_CANCEL_ASYNCHRONOUS
84The thread can be canceled at any time.
85(Typically,
86it will be canceled immediately upon receiving a cancellation request,
87but the system doesn't guarantee this.)
88.PP
89The set-and-get operation performed by each of these functions
90is atomic with respect to other threads in the process
91calling the same function.
92.SH RETURN VALUE
93On success, these functions return 0;
c7094399 94on error, they return a nonzero error number.
ccb42cb8
MK
95.SH ERRORS
96The
97.BR pthread_setcancelstate ()
98can fail with the following error:
99.TP
100.B EINVAL
101Invalid value for
102.IR state .
103.PP
104The
105.BR pthread_setcanceltype ()
106can fail with the following error:
107.TP
108.B EINVAL
109Invalid value for
110.IR type .
111.\" .SH VERSIONS
112.\" Available since glibc 2.0
50b60123 113.SH ATTRIBUTES
dfa2186c
MK
114For an explanation of the terms used in this section, see
115.BR attributes (7).
116.ad l
117.TS
118allbox;
119lb lb lb
120lw25 l l.
121Interface Attribute Value
122T{
123.BR pthread_setcancelstate (),
50b60123 124.BR pthread_setcanceltype ()
dfa2186c
MK
125T} Thread safety T{
126MT-Safe
127T}
f385a71d
MK
128T{
129.BR pthread_setcancelstate (),
130.BR pthread_setcanceltype ()
131T} Async-cancel-safety T{
132AC-Safe
133T}
dfa2186c
MK
134.TE
135.ad
136.hy
ccb42cb8 137.SH CONFORMING TO
1d426e09 138POSIX.1-2001, POSIX.1-2008.
ccb42cb8
MK
139.SH NOTES
140For details of what happens when a thread is canceled, see
141.BR pthread_cancel (3).
847e0d88 142.PP
ccb42cb8
MK
143Briefly disabling cancelability is useful
144if a thread performs some critical action
145that must not be interrupted by a cancellation request.
146Beware of disabling cancelability for long periods,
147or around operations that may block for long periods,
148since that will render the thread unresponsive to cancellation requests.
fad1a267 149.SS Asynchronous cancelability
ccb42cb8
MK
150Setting the cancelability type to
151.B PTHREAD_CANCEL_ASYNCHRONOUS
152is rarely useful.
153Since the thread could be canceled at
154.I any
ee1534cb
MK
155time, it cannot safely reserve resources (e.g., allocating memory with
156.BR malloc (3)),
157acquire mutexes, semaphores, or locks, and so on.
f5410853 158Reserving resources is unsafe because the application has no way of
ee1534cb
MK
159knowing what the state of these resources is when the thread is canceled;
160that is, did cancellation occur before the resources were reserved,
161while they were reserved, or after they were released?
a86f0a4b
MK
162Furthermore, some internal data structures
163(e.g., the linked list of free blocks managed by the
164.BR malloc (3)
165family of functions) may be left in an inconsistent state
166if cancellation occurs in the middle of the function call.
ee1534cb 167Consequently, clean-up handlers cease to be useful.
847e0d88 168.PP
ee1534cb 169Functions that can be safely asynchronously canceled are called
a113945f 170.IR "async-cancel-safe functions" .
1d426e09 171POSIX.1-2001 and POSIX.1-2008 require only that
ccb42cb8
MK
172.BR pthread_cancel (3),
173.BR pthread_setcancelstate (),
174and
175.BR pthread_setcanceltype ()
ee1534cb
MK
176be async-cancel-safe.
177In general, other library functions
178can't be safely called from an asynchronously cancelable thread.
847e0d88 179.PP
ccb42cb8
MK
180One of the few circumstances in which asynchronous cancelability is useful
181is for cancellation of a thread that is in a pure compute-bound loop.
fad1a267 182.SS Portability notes
ccb42cb8
MK
183The Linux threading implementations permit the
184.I oldstate
185argument of
186.BR pthread_setcancelstate ()
187to be NULL, in which case the information about the previous
188cancelability state is not returned to the caller.
189Many other implementations also permit a NULL
190.I oldstat
191argument,
192.\" It looks like at least Solaris, FreeBSD and Tru64 support this.
1d426e09 193but POSIX.1 does not specify this point,
ccb42cb8
MK
194so portable applications should always specify a non-NULL value in
195.IR oldstate .
196A precisely analogous set of statements applies for the
197.I oldtype
198argument of
199.BR pthread_setcanceltype ().
200.SH EXAMPLE
201See
202.BR pthread_cancel (3).
203.SH SEE ALSO
ccb42cb8 204.BR pthread_cancel (3),
3e5c319e 205.BR pthread_cleanup_push (3),
ccb42cb8
MK
206.BR pthread_testcancel (3),
207.BR pthreads (7)