1 .\" $FreeBSD: src/share/man/man3/pthread_testcancel.3,v 1.15 2007/10/22 10:08:01 ru Exp $
3 .Dt PTHREAD_TESTCANCEL 3
6 .Nm pthread_setcancelstate ,
7 .Nm pthread_setcanceltype ,
9 .Nd set cancelability state
15 .Fn pthread_setcancelstate "int state" "int *oldstate"
17 .Fn pthread_setcanceltype "int type" "int *oldtype"
19 .Fn pthread_testcancel "void"
22 .Fn pthread_setcancelstate
23 function atomically both sets the calling thread's cancelability state
30 returns the previous cancelability state at the location referenced by
35 .Dv PTHREAD_CANCEL_ENABLE
37 .Dv PTHREAD_CANCEL_DISABLE .
40 .Fn pthread_setcanceltype
41 function atomically both sets the calling thread's cancelability type
48 returns the previous cancelability type at the location referenced by
53 .Dv PTHREAD_CANCEL_DEFERRED
55 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
57 The cancelability state and type of any newly created threads, including the
60 was first invoked, are
61 .Dv PTHREAD_CANCEL_ENABLE
63 .Dv PTHREAD_CANCEL_DEFERRED
67 .Fn pthread_testcancel
68 function creates a cancellation point in the calling thread.
70 .Fn pthread_testcancel
71 function has no effect if cancelability is disabled.
72 .Ss Cancelability States
73 The cancelability state of a thread determines the action taken upon
74 receipt of a cancellation request.
75 The thread may control cancellation in
78 Each thread maintains its own
79 .Dq cancelability state
80 which may be encoded in two bits:
82 .It Em Cancelability Enable
84 .Dv PTHREAD_CANCEL_DISABLE ,
85 cancellation requests against the target thread are held pending.
86 .It Em Cancelability Type
87 When cancelability is enabled and the cancelability type is
88 .Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
89 new or pending cancellation requests may be acted upon at any time.
90 When cancelability is enabled and the cancelability type is
91 .Dv PTHREAD_CANCEL_DEFERRED ,
92 cancellation requests are held pending until a cancellation point (see
94 If cancelability is disabled, the setting of the
95 cancelability type has no immediate effect as all cancellation requests
96 are held pending; however, once cancelability is enabled again the new
97 type will be in effect.
99 .Ss Cancellation Points
100 Cancellation points will occur when a thread is executing the following
102 .Fn clock_nanosleep ,
112 .Fn pthread_cond_timedwait ,
113 .Fn pthread_cond_wait ,
115 .Fn pthread_testcancel ,
128 .Fn pthread_setcancelstate
130 .Fn pthread_setcanceltype
131 functions will return zero.
132 Otherwise, an error number shall be returned to
136 .Fn pthread_setcancelstate
138 .Fn pthread_setcanceltype
139 functions are used to control the points at which a thread may be
140 asynchronously canceled.
141 For cancellation control to be usable in modular
142 fashion, some rules must be followed.
144 For purposes of this discussion, consider an object to be a generalization
146 It is a set of procedures and global variables written as
147 a unit and called by clients not known by the object.
151 First, cancelability should only be disabled on entry to an object, never
153 On exit from an object, the cancelability state should
154 always be restored to its value on entry to the object.
156 This follows from a modularity argument: if the client of an object (or the
157 client of an object that uses that object) has disabled cancelability, it is
158 because the client does not want to have to worry about how to clean up if the
159 thread is canceled while executing some sequence of actions.
161 is called in such a state and it enables cancelability and a cancellation
162 request is pending for that thread, then the thread will be canceled,
163 contrary to the wish of the client that disabled.
165 Second, the cancelability type may be explicitly set to either
169 upon entry to an object.
170 But as with the cancelability state, on exit from
171 an object that cancelability type should always be restored to its value on
174 Finally, only functions that are cancel-safe may be called from a thread that
175 is asynchronously cancelable.
178 .Fn pthread_setcancelstate
182 The specified state is not
183 .Dv PTHREAD_CANCEL_ENABLE
185 .Dv PTHREAD_CANCEL_DISABLE .
189 .Fn pthread_setcanceltype
193 The specified state is not
194 .Dv PTHREAD_CANCEL_DEFERRED
196 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
202 .Fn pthread_testcancel
206 This manual page was written by
207 .An David Leonard Aq Mt d@openbsd.org
211 .Xr pthread_cancel 3 .