xref: /freebsd/share/man/man3/pthread_testcancel.3 (revision 4bf53d0b4672dca72c809b44349fb82657f482f5)
1.\" $FreeBSD$
2.Dd March 29, 2015
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 fsync
118.It Fn kevent
119The
120.Fn kevent
121function is a cancellation point if it is potentially blocking,
122i.e. when the
123.Fa nevents
124argument  is non-zero.
125.It Fn mq_receive
126.It Fn mq_send
127.It Fn mq_timedreceive
128.It Fn mq_timedsend
129.It Fn msync
130.It Fn nanosleep
131.It Fn open
132.It Fn openat
133.It Fn pause
134.It Fn poll
135.It Fn pselect
136.It Fn pthread_cond_timedwait
137.It Fn pthread_cond_wait
138.It Fn pthread_join
139.It Fn pthread_testcancel
140.It Fn read
141.It Fn readv
142.It Fn recv
143.It Fn recvfrom
144.It Fn recvmsg
145.It Fn select
146.It Fn sem_timedwait
147.It Fn sem_wait
148.It Fn send
149.It Fn sendmsg
150.It Fn sendto
151.It Fn sigsuspend
152.It Fn sigtimedwait
153.It Fn sigwaitinfo
154.It Fn sigwait
155.It Fn sleep
156.It Fn system
157.It Fn tcdrain
158.It Fn usleep
159.It Fn wait
160.It Fn wait3
161.It Fn wait4
162.It Fn waitpid
163.It Fn write
164.It Fn writev
165.El
166.Sh NOTES
167The
168.Fn pthread_setcancelstate
169and
170.Fn pthread_setcanceltype
171functions are used to control the points at which a thread may be
172asynchronously canceled.
173For cancellation control to be usable in modular
174fashion, some rules must be followed.
175.Pp
176For purposes of this discussion, consider an object to be a generalization
177of a procedure.
178It is a set of procedures and global variables written as
179a unit and called by clients not known by the object.
180Objects may depend
181on other objects.
182.Pp
183First, cancelability should only be disabled on entry to an object, never
184explicitly enabled.
185On exit from an object, the cancelability state should
186always be restored to its value on entry to the object.
187.Pp
188This follows from a modularity argument: if the client of an object (or the
189client of an object that uses that object) has disabled cancelability, it is
190because the client does not want to have to worry about how to clean up if the
191thread is canceled while executing some sequence of actions.
192If an object
193is called in such a state and it enables cancelability and a cancellation
194request is pending for that thread, then the thread will be canceled,
195contrary to the wish of the client that disabled.
196.Pp
197Second, the cancelability type may be explicitly set to either
198.Em deferred
199or
200.Em asynchronous
201upon entry to an object.
202But as with the cancelability state, on exit from
203an object that cancelability type should always be restored to its value on
204entry to the object.
205.Pp
206Finally, only functions that are cancel-safe may be called from a thread that
207is asynchronously cancelable.
208.Sh RETURN VALUES
209If successful, the
210.Fn pthread_setcancelstate
211and
212.Fn pthread_setcanceltype
213functions will return zero.
214Otherwise, an error number shall be returned to
215indicate the error.
216.Sh ERRORS
217The function
218.Fn pthread_setcancelstate
219may fail with:
220.Bl -tag -width Er
221.It Bq Er EINVAL
222The specified state is not
223.Dv PTHREAD_CANCEL_ENABLE
224or
225.Dv PTHREAD_CANCEL_DISABLE .
226.El
227.Pp
228The function
229.Fn pthread_setcanceltype
230may fail with:
231.Bl -tag -width Er
232.It Bq Er EINVAL
233The specified state is not
234.Dv PTHREAD_CANCEL_DEFERRED
235or
236.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
237.El
238.Sh SEE ALSO
239.Xr pthread_cancel 3
240.Sh STANDARDS
241The
242.Fn pthread_testcancel
243function conforms to
244.St -p1003.1-96 .
245The standard allows implementations to make many more functions
246cancellation points.
247.Sh AUTHORS
248This manual page was written by
249.An David Leonard Aq Mt d@openbsd.org
250for the
251.Ox
252implementation of
253.Xr pthread_cancel 3 .
254