]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_setcancelstate.3
intro.1, _exit.2, access.2, alarm.2, alloc_hugepages.2, arch_prctl.2, bind.2, chdir...
[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.\"
ee1534cb 26.TH PTHREAD_SETCANCELSTATE 3 2008-11-24 "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>
33
34.BI "int pthread_setcancelstate(int " state ", int *" oldstate );
35.BI "int pthread_setcanceltype(int " type ", int *" oldtype );
36.sp
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
113.SH CONFORMING TO
114POSIX.1-2001.
115.SH NOTES
116For details of what happens when a thread is canceled, see
117.BR pthread_cancel (3).
118
119Briefly disabling cancelability is useful
120if a thread performs some critical action
121that must not be interrupted by a cancellation request.
122Beware of disabling cancelability for long periods,
123or around operations that may block for long periods,
124since that will render the thread unresponsive to cancellation requests.
125
126Setting the cancelability type to
127.B PTHREAD_CANCEL_ASYNCHRONOUS
128is rarely useful.
129Since the thread could be canceled at
130.I any
ee1534cb
MK
131time, it cannot safely reserve resources (e.g., allocating memory with
132.BR malloc (3)),
133acquire mutexes, semaphores, or locks, and so on.
f5410853 134Reserving resources is unsafe because the application has no way of
ee1534cb
MK
135knowing what the state of these resources is when the thread is canceled;
136that is, did cancellation occur before the resources were reserved,
137while they were reserved, or after they were released?
a86f0a4b
MK
138Furthermore, some internal data structures
139(e.g., the linked list of free blocks managed by the
140.BR malloc (3)
141family of functions) may be left in an inconsistent state
142if cancellation occurs in the middle of the function call.
ee1534cb
MK
143Consequently, clean-up handlers cease to be useful.
144Functions that can be safely asynchronously canceled are called
a113945f 145.IR "async-cancel-safe functions" .
ee1534cb 146POSIX.1-2001 only requires that
ccb42cb8
MK
147.BR pthread_cancel (3),
148.BR pthread_setcancelstate (),
149and
150.BR pthread_setcanceltype ()
ee1534cb
MK
151be async-cancel-safe.
152In general, other library functions
153can't be safely called from an asynchronously cancelable thread.
ccb42cb8
MK
154One of the few circumstances in which asynchronous cancelability is useful
155is for cancellation of a thread that is in a pure compute-bound loop.
156
157The Linux threading implementations permit the
158.I oldstate
159argument of
160.BR pthread_setcancelstate ()
161to be NULL, in which case the information about the previous
162cancelability state is not returned to the caller.
163Many other implementations also permit a NULL
164.I oldstat
165argument,
166.\" It looks like at least Solaris, FreeBSD and Tru64 support this.
167but POSIX.1-2001 does not specify this point,
168so portable applications should always specify a non-NULL value in
169.IR oldstate .
170A precisely analogous set of statements applies for the
171.I oldtype
172argument of
173.BR pthread_setcanceltype ().
174.SH EXAMPLE
175See
176.BR pthread_cancel (3).
177.SH SEE ALSO
ccb42cb8 178.BR pthread_cancel (3),
3e5c319e 179.BR pthread_cleanup_push (3),
ccb42cb8
MK
180.BR pthread_testcancel (3),
181.BR pthreads (7)