1 .\" $NetBSD: pthread_testcancel.3,v 1.3 2003/11/18 00:56:57 thorpej Exp $
3 .\" Copyright (c) 2002 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
17 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 .\" POSSIBILITY OF SUCH DAMAGE.
25 .\" $FreeBSD: src/lib/libpthread/man/pthread_testcancel.3,v 1.9 2002/09/16 19:29:29 mini Exp $
27 .Dt PTHREAD_TESTCANCEL 3
30 .Nm pthread_setcancelstate ,
31 .Nm pthread_setcanceltype ,
32 .Nm pthread_testcancel
33 .Nd set cancelability state
39 .Fn pthread_setcancelstate "int state" "int *oldstate"
41 .Fn pthread_setcanceltype "int type" "int *oldtype"
43 .Fn pthread_testcancel "void"
46 .Fn pthread_setcancelstate
47 function atomically both sets the calling thread's cancelability state
54 returns the previous cancelability state at the location referenced by
59 .Dv PTHREAD_CANCEL_ENABLE
61 .Dv PTHREAD_CANCEL_DISABLE .
64 .Fn pthread_setcanceltype
65 function atomically both sets the calling thread's cancelability type
72 returns the previous cancelability type at the location referenced by
77 .Dv PTHREAD_CANCEL_DEFERRED
79 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
81 The cancelability state and type of any newly created threads, including the
84 was first invoked, are
85 .Dv PTHREAD_CANCEL_ENABLE
87 .Dv PTHREAD_CANCEL_DEFERRED
91 .Fn pthread_testcancel
92 function creates a cancellation point in the calling thread.
94 .Fn pthread_testcancel
95 function has no effect if cancelability is disabled.
96 .Ss Cancelability States
97 The cancelability state of a thread determines the action taken upon
98 receipt of a cancellation request.
99 The thread may control cancellation in
102 Each thread maintains its own
103 .Dq cancelability state
104 which may be encoded in two bits:
106 .It Em Cancelability Enable
107 When cancelability is
108 .Dv PTHREAD_CANCEL_DISABLE ,
109 cancellation requests against the target thread are held pending.
110 .It Em Cancelability Type
111 When cancelability is enabled and the cancelability type is
112 .Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
113 new or pending cancellation requests may be acted upon at any time.
114 When cancelability is enabled and the cancelability type is
115 .Dv PTHREAD_CANCEL_DEFERRED ,
116 cancellation requests are held pending until a cancellation point (see
118 If cancelability is disabled, the setting of the
119 cancelability type has no immediate effect as all cancellation requests
120 are held pending; however, once cancelability is enabled again the new
121 type will be in effect.
123 .Ss Cancellation Points
124 Cancellation points will occur when a thread is executing the following
128 .\".Fn clock_nanosleep ,
140 .\".Fn mq_timedreceive ,
141 .\".Fn mq_timedsend ,
151 .Fn pthread_cond_timedwait ,
152 .Fn pthread_cond_wait ,
154 .Fn pthread_testcancel ,
186 .Fn pthread_setcancelstate
188 .Fn pthread_setcanceltype
189 functions will return zero.
190 Otherwise, an error number shall be returned to
194 .Fn pthread_setcancelstate
196 .Fn pthread_setcanceltype
197 functions are used to control the points at which a thread may be
198 asynchronously canceled.
199 For cancellation control to be usable in modular
200 fashion, some rules must be followed.
202 For purposes of this discussion, consider an object to be a generalization
204 It is a set of procedures and global variables written as
205 a unit and called by clients not known by the object.
209 First, cancelability should only be disabled on entry to an object, never
211 On exit from an object, the cancelability state should
212 always be restored to its value on entry to the object.
214 This follows from a modularity argument: if the client of an object (or the
215 client of an object that uses that object) has disabled cancelability, it is
216 because the client doesn't want to have to worry about how to clean up if the
217 thread is canceled while executing some sequence of actions.
219 is called in such a state and it enables cancelability and a cancellation
220 request is pending for that thread, then the thread will be canceled,
221 contrary to the wish of the client that disabled.
223 Second, the cancelability type may be explicitly set to either
227 upon entry to an object.
228 But as with the cancelability state, on exit from
229 an object that cancelability type should always be restored to its value on
232 Finally, only functions that are cancel-safe may be called from a thread that
233 is asynchronously cancelable.
236 .Fn pthread_setcancelstate
240 The specified state is not
241 .Dv PTHREAD_CANCEL_ENABLE
243 .Dv PTHREAD_CANCEL_DISABLE .
247 .Fn pthread_setcanceltype
251 The specified state is not
252 .Dv PTHREAD_CANCEL_DEFERRED
254 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
259 .Fn pthread_testcancel
263 This man page was written by
264 .An David Leonard Aq d@openbsd.org
268 .Xr pthread_cancel 3 .