1 .\" $FreeBSD: src/lib/libc_r/man/pthread_testcancel.3,v 1.2.2.3 2001/08/17 15:42:52 ru Exp $
3 .Dt PTHREAD_TESTCANCEL 3
6 .Nm pthread_setcancelstate ,
7 .Nm pthread_setcanceltype ,
9 .Nd set cancelability state
11 .Fd #include <pthread.h>
13 .Fn pthread_setcancelstate "int state" "int *oldstate"
15 .Fn pthread_setcanceltype "int type" "int *oldtype"
17 .Fn pthread_testcancel "void"
20 .Fn pthread_setcancelstate
21 function atomically both sets the calling thread's cancelability state
24 and returns the previous cancelability state at the location referenced by
29 .Dv PTHREAD_CANCEL_ENABLE
31 .Dv PTHREAD_CANCEL_DISABLE .
34 .Fn pthread_setcanceltype
35 function atomically both sets the calling thread's cancelability type
38 and returns the previous cancelability type at the location referenced by
43 .Dv PTHREAD_CANCEL_DEFERRED
45 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
47 The cancelability state and type of any newly created threads, including the
50 was first invoked, are
51 .Dv PTHREAD_CANCEL_ENABLE
53 .Dv PTHREAD_CANCEL_DEFERRED
57 .Fn pthread_testcancel
58 function creates a cancellation point in the calling thread.
60 .Fn pthread_testcancel
61 function has no effect if cancelability is disabled.
63 .Ss Cancelability States
64 The cancelability state of a thread determines the action taken upon
65 receipt of a cancellation request.
66 The thread may control cancellation in
69 Each thread maintains its own
70 .Dq cancelability state
71 which may be encoded in two bits:
73 .It Em Cancelability Enable
75 .Dv PTHREAD_CANCEL_DISABLE ,
76 cancellation requests against the target thread are held pending.
77 .It Em Cancelability Type
78 When cancelability is enabled and the cancelability type is
79 .Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
80 new or pending cancellation requests may be acted upon at any time.
81 When cancelability is enabled and the cancelability type is
82 .Dv PTHREAD_CANCEL_DEFERRED ,
83 cancellation requests are held pending until a cancellation point (see
85 If cancelability is disabled, the setting of the
86 cancelability type has no immediate effect as all cancellation requests
87 are held pending; however, once cancelability is enabled again the new
88 type will be in effect.
90 .Ss Cancellation Points
91 Cancellation points will occur when a thread is executing the following
101 .Fn pthread_cond_timedwait ,
102 .Fn pthread_cond_wait ,
104 .Fn pthread_testcancel ,
117 .Fn pthread_setcancelstate
119 .Fn pthread_setcanceltype
120 functions will return zero.
121 Otherwise, an error number shall be returned to
125 .Fn pthread_setcancelstate
127 .Fn pthread_setcanceltype
128 functions are used to control the points at which a thread may be
129 asynchronously canceled.
130 For cancellation control to be usable in modular
131 fashion, some rules must be followed.
133 For purposes of this discussion, consider an object to be a generalization
135 It is a set of procedures and global variables written as
136 a unit and called by clients not known by the object.
140 First, cancelability should only be disabled on entry to an object, never
142 On exit from an object, the cancelability state should
143 always be restored to its value on entry to the object.
145 This follows from a modularity argument: if the client of an object (or the
146 client of an object that uses that object) has disabled cancelability, it is
147 because the client doesn't want to have to worry about how to clean up if the
148 thread is canceled while executing some sequence of actions.
150 is called in such a state and it enables cancelability and a cancellation
151 request is pending for that thread, then the thread will be canceled,
152 contrary to the wish of the client that disabled.
154 Second, the cancelability type may be explicitly set to either
158 upon entry to an object.
159 But as with the cancelability state, on exit from
160 an object that cancelability type should always be restored to its value on
163 Finally, only functions that are cancel-safe may be called from a thread that
164 is asynchronously cancelable.
167 .Fn pthread_setcancelstate
171 The specified state is not
172 .Dv PTHREAD_CANCEL_ENABLE
174 .Dv PTHREAD_CANCEL_DISABLE .
178 .Fn pthread_setcanceltype
182 The specified state is not
183 .Dv PTHREAD_CANCEL_DEFERRED
185 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
190 .Fn pthread_testcancel
194 This man page was written by
195 .An David Leonard Aq d@openbsd.org
199 .Xr pthread_cancel 3 .