xref: /freebsd/share/man/man3/pthread_testcancel.3 (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1.\" $FreeBSD$
2.Dd August 16, 2016
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.Pp
73.Ss Cancelability States
74The cancelability state of a thread determines the action taken upon
75receipt of a cancellation request.
76The thread may control cancellation in
77a number of ways.
78.Pp
79Each thread maintains its own
80.Dq cancelability state
81which may be encoded in two bits:
82.Bl -hang
83.It Em Cancelability Enable
84When cancelability is
85.Dv PTHREAD_CANCEL_DISABLE ,
86cancellation requests against the target thread are held pending.
87.It Em Cancelability Type
88When cancelability is enabled and the cancelability type is
89.Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
90new or pending cancellation requests may be acted upon at any time.
91When cancelability is enabled and the cancelability type is
92.Dv PTHREAD_CANCEL_DEFERRED ,
93cancellation requests are held pending until a cancellation point (see
94below) is reached.
95If cancelability is disabled, the setting of the
96cancelability type has no immediate effect as all cancellation requests
97are held pending; however, once cancelability is enabled again the new
98type will be in effect.
99.El
100.Ss Cancellation Points
101Cancellation points will occur when a thread is executing the following
102functions:
103.Bl -tag -width "Fn pthread_cond_timedwait" -compact
104.It Fn accept
105.It Fn accept4
106.It Fn aio_suspend
107.It Fn connect
108.It Fn close
109.It Fn creat
110.It Fn fcntl
111The
112.Fn fcntl
113function is a cancellation point if
114.Fa cmd
115is
116.Dv F_SETLKW .
117.It Fn fdatasync
118.It Fn fsync
119.It Fn kevent
120The
121.Fn kevent
122function is a cancellation point if it is potentially blocking,
123i.e. when the
124.Fa nevents
125argument  is non-zero.
126.It Fn mq_receive
127.It Fn mq_send
128.It Fn mq_timedreceive
129.It Fn mq_timedsend
130.It Fn msync
131.It Fn nanosleep
132.It Fn open
133.It Fn openat
134.It Fn pause
135.It Fn poll
136.It Fn ppoll
137.It Fn pselect
138.It Fn pthread_cond_timedwait
139.It Fn pthread_cond_wait
140.It Fn pthread_join
141.It Fn pthread_testcancel
142.It Fn read
143.It Fn readv
144.It Fn recv
145.It Fn recvfrom
146.It Fn recvmsg
147.It Fn select
148.It Fn sem_timedwait
149.It Fn sem_wait
150.It Fn send
151.It Fn sendmsg
152.It Fn sendto
153.It Fn sigsuspend
154.It Fn sigtimedwait
155.It Fn sigwaitinfo
156.It Fn sigwait
157.It Fn sleep
158.It Fn system
159.It Fn tcdrain
160.It Fn usleep
161.It Fn wait
162.It Fn wait3
163.It Fn wait4
164.It Fn wait6
165.It Fn waitid
166.It Fn waitpid
167.It Fn write
168.It Fn writev
169.El
170.Sh NOTES
171The
172.Fn pthread_setcancelstate
173and
174.Fn pthread_setcanceltype
175functions are used to control the points at which a thread may be
176asynchronously canceled.
177For cancellation control to be usable in modular
178fashion, some rules must be followed.
179.Pp
180For purposes of this discussion, consider an object to be a generalization
181of a procedure.
182It is a set of procedures and global variables written as
183a unit and called by clients not known by the object.
184Objects may depend
185on other objects.
186.Pp
187First, cancelability should only be disabled on entry to an object, never
188explicitly enabled.
189On exit from an object, the cancelability state should
190always be restored to its value on entry to the object.
191.Pp
192This follows from a modularity argument: if the client of an object (or the
193client of an object that uses that object) has disabled cancelability, it is
194because the client does not want to have to worry about how to clean up if the
195thread is canceled while executing some sequence of actions.
196If an object
197is called in such a state and it enables cancelability and a cancellation
198request is pending for that thread, then the thread will be canceled,
199contrary to the wish of the client that disabled.
200.Pp
201Second, the cancelability type may be explicitly set to either
202.Em deferred
203or
204.Em asynchronous
205upon entry to an object.
206But as with the cancelability state, on exit from
207an object that cancelability type should always be restored to its value on
208entry to the object.
209.Pp
210Finally, only functions that are cancel-safe may be called from a thread that
211is asynchronously cancelable.
212.Sh RETURN VALUES
213If successful, the
214.Fn pthread_setcancelstate
215and
216.Fn pthread_setcanceltype
217functions will return zero.
218Otherwise, an error number shall be returned to
219indicate the error.
220.Sh ERRORS
221The function
222.Fn pthread_setcancelstate
223may fail with:
224.Bl -tag -width Er
225.It Bq Er EINVAL
226The specified state is not
227.Dv PTHREAD_CANCEL_ENABLE
228or
229.Dv PTHREAD_CANCEL_DISABLE .
230.El
231.Pp
232The function
233.Fn pthread_setcanceltype
234may fail with:
235.Bl -tag -width Er
236.It Bq Er EINVAL
237The specified state is not
238.Dv PTHREAD_CANCEL_DEFERRED
239or
240.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
241.El
242.Sh SEE ALSO
243.Xr pthread_cancel 3
244.Sh STANDARDS
245The
246.Fn pthread_testcancel
247function conforms to
248.St -p1003.1-96 .
249The standard allows implementations to make many more functions
250cancellation points.
251.Sh AUTHORS
252This manual page was written by
253.An David Leonard Aq Mt d@openbsd.org
254for the
255.Ox
256implementation of
257.Xr pthread_cancel 3 .
258