1.\" $FreeBSD: src/share/man/man3/pthread_testcancel.3,v 1.15 2007/10/22 10:08:01 ru Exp $
2.Dd September 14, 2021
3.Dt PTHREAD_TESTCANCEL 3
4.Os
5.Sh NAME
6.Nm pthread_setcancelstate ,
7.Nm pthread_setcanceltype ,
8.Nm pthread_testcancel
9.Nd set cancelability state
10.Sh LIBRARY
11.Lb libpthread
12.Sh SYNOPSIS
13.In pthread.h
14.Ft int
15.Fn pthread_setcancelstate "int state" "int *oldstate"
16.Ft int
17.Fn pthread_setcanceltype "int type" "int *oldtype"
18.Ft void
19.Fn pthread_testcancel "void"
20.Sh DESCRIPTION
21The
22.Fn pthread_setcancelstate
23function atomically both sets the calling thread's cancelability state
24to the indicated
25.Fa state
26and, if
27.Fa oldstate
28is not
29.Dv NULL ,
30returns the previous cancelability state at the location referenced by
31.Fa oldstate .
32Legal values for
33.Fa state
34are
35.Dv PTHREAD_CANCEL_ENABLE
36and
37.Dv PTHREAD_CANCEL_DISABLE .
38.Pp
39The
40.Fn pthread_setcanceltype
41function atomically both sets the calling thread's cancelability type
42to the indicated
43.Fa type
44and, if
45.Fa oldtype
46is not
47.Dv NULL ,
48returns the previous cancelability type at the location referenced by
49.Fa oldtype .
50Legal values for
51.Fa type
52are
53.Dv PTHREAD_CANCEL_DEFERRED
54and
55.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
56.Pp
57The cancelability state and type of any newly created threads, including the
58thread in which
59.Fn main
60was first invoked, are
61.Dv PTHREAD_CANCEL_ENABLE
62and
63.Dv PTHREAD_CANCEL_DEFERRED
64respectively.
65.Pp
66The
67.Fn pthread_testcancel
68function creates a cancellation point in the calling thread.
69The
70.Fn pthread_testcancel
71function has no effect if cancelability is disabled.
72.Ss Cancelability States
73The cancelability state of a thread determines the action taken upon
74receipt of a cancellation request.
75The thread may control cancellation in
76a number of ways.
77.Pp
78Each thread maintains its own
79.Dq cancelability state
80which may be encoded in two bits:
81.Bl -hang
82.It Em Cancelability Enable
83When cancelability is
84.Dv PTHREAD_CANCEL_DISABLE ,
85cancellation requests against the target thread are held pending.
86.It Em Cancelability Type
87When cancelability is enabled and the cancelability type is
88.Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
89new or pending cancellation requests may be acted upon at any time.
90When cancelability is enabled and the cancelability type is
91.Dv PTHREAD_CANCEL_DEFERRED ,
92cancellation requests are held pending until a cancellation point (see
93below) is reached.
94If cancelability is disabled, the setting of the
95cancelability type has no immediate effect as all cancellation requests
96are held pending; however, once cancelability is enabled again the new
97type will be in effect.
98.El
99.Ss Cancellation Points
100Cancellation points will occur when a thread is executing the following
101functions:
102.Fn clock_nanosleep ,
103.Fn close ,
104.Fn creat ,
105.Fn fcntl ,
106.Fn fdatasync ,
107.Fn fsync ,
108.Fn msync ,
109.Fn nanosleep ,
110.Fn open ,
111.Fn pause ,
112.Fn pthread_cond_timedwait ,
113.Fn pthread_cond_wait ,
114.Fn pthread_join ,
115.Fn pthread_testcancel ,
116.Fn read ,
117.Fn sigwaitinfo ,
118.Fn sigsuspend ,
119.Fn sigwait ,
120.Fn sleep ,
121.Fn system ,
122.Fn tcdrain ,
123.Fn wait ,
124.Fn waitpid ,
125.Fn write .
126.Sh RETURN VALUES
127If successful, the
128.Fn pthread_setcancelstate
129and
130.Fn pthread_setcanceltype
131functions will return zero.
132Otherwise, an error number shall be returned to
133indicate the error.
134.Pp
135The
136.Fn pthread_setcancelstate
137and
138.Fn pthread_setcanceltype
139functions are used to control the points at which a thread may be
140asynchronously canceled.
141For cancellation control to be usable in modular
142fashion, some rules must be followed.
143.Pp
144For purposes of this discussion, consider an object to be a generalization
145of a procedure.
146It is a set of procedures and global variables written as
147a unit and called by clients not known by the object.
148Objects may depend
149on other objects.
150.Pp
151First, cancelability should only be disabled on entry to an object, never
152explicitly enabled.
153On exit from an object, the cancelability state should
154always be restored to its value on entry to the object.
155.Pp
156This follows from a modularity argument: if the client of an object (or the
157client of an object that uses that object) has disabled cancelability, it is
158because the client does not want to have to worry about how to clean up if the
159thread is canceled while executing some sequence of actions.
160If an object
161is called in such a state and it enables cancelability and a cancellation
162request is pending for that thread, then the thread will be canceled,
163contrary to the wish of the client that disabled.
164.Pp
165Second, the cancelability type may be explicitly set to either
166.Em deferred
167or
168.Em asynchronous
169upon entry to an object.
170But as with the cancelability state, on exit from
171an object that cancelability type should always be restored to its value on
172entry to the object.
173.Pp
174Finally, only functions that are cancel-safe may be called from a thread that
175is asynchronously cancelable.
176.Sh ERRORS
177The function
178.Fn pthread_setcancelstate
179may fail with:
180.Bl -tag -width Er
181.It Bq Er EINVAL
182The specified state is not
183.Dv PTHREAD_CANCEL_ENABLE
184or
185.Dv PTHREAD_CANCEL_DISABLE .
186.El
187.Pp
188The function
189.Fn pthread_setcanceltype
190may fail with:
191.Bl -tag -width Er
192.It Bq Er EINVAL
193The specified state is not
194.Dv PTHREAD_CANCEL_DEFERRED
195or
196.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
197.El
198.Sh SEE ALSO
199.Xr pthread_cancel 3
200.Sh STANDARDS
201The
202.Fn pthread_testcancel
203function conforms to
204.St -p1003.1-96 .
205.Sh AUTHORS
206This manual page was written by
207.An David Leonard Aq Mt d@openbsd.org
208for the
209.Ox
210implementation of
211.Xr pthread_cancel 3 .
212