3 .Dt PTHREAD_TESTCANCEL 3
6 .Nm pthread_setcancelstate ,
7 .Nm pthread_setcanceltype ,
9 .Nd set cancelability state
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
28 returns the previous cancelability state at the location referenced by
33 .Dv PTHREAD_CANCEL_ENABLE
35 .Dv PTHREAD_CANCEL_DISABLE .
38 .Fn pthread_setcanceltype
39 function atomically both sets the calling thread's cancelability type
46 returns the previous cancelability type at the location referenced by
51 .Dv PTHREAD_CANCEL_DEFERRED
53 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
55 The cancelability state and type of any newly created threads, including the
58 was first invoked, are
59 .Dv PTHREAD_CANCEL_ENABLE
61 .Dv PTHREAD_CANCEL_DEFERRED
65 .Fn pthread_testcancel
66 function creates a cancellation point in the calling thread.
68 .Fn pthread_testcancel
69 function has no effect if cancelability is disabled.
71 .Ss Cancelability States
72 The cancelability state of a thread determines the action taken upon
73 receipt of a cancellation request.
74 The thread may control cancellation in
77 Each thread maintains its own
78 .Dq cancelability state
79 which may be encoded in two bits:
81 .It Em Cancelability Enable
83 .Dv PTHREAD_CANCEL_DISABLE ,
84 cancellation requests against the target thread are held pending.
85 .It Em Cancelability Type
86 When cancelability is enabled and the cancelability type is
87 .Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
88 new or pending cancellation requests may be acted upon at any time.
89 When cancelability is enabled and the cancelability type is
90 .Dv PTHREAD_CANCEL_DEFERRED ,
91 cancellation requests are held pending until a cancellation point (see
93 If cancelability is disabled, the setting of the
94 cancelability type has no immediate effect as all cancellation requests
95 are held pending; however, once cancelability is enabled again the new
96 type will be in effect.
98 .Ss Cancellation Points
99 Cancellation points will occur when a thread is executing the following
104 .\" .Fn clock_nanosleep ,
115 .\" .Fn mq_timedreceive ,
116 .\" .Fn mq_timedsend ,
126 .Fn pthread_cond_timedwait ,
127 .Fn pthread_cond_wait ,
129 .Fn pthread_testcancel ,
139 .\" .Fn sem_timedwait ,
146 .\" .Fn sigtimedwait ,
148 .\" .Fn sigwaitinfo ,
159 .Fn pthread_setcancelstate
161 .Fn pthread_setcanceltype
162 functions will return zero.
163 Otherwise, an error number shall be returned to
167 .Fn pthread_setcancelstate
169 .Fn pthread_setcanceltype
170 functions are used to control the points at which a thread may be
171 asynchronously canceled.
172 For cancellation control to be usable in modular
173 fashion, some rules must be followed.
175 For purposes of this discussion, consider an object to be a generalization
177 It is a set of procedures and global variables written as
178 a unit and called by clients not known by the object.
182 First, cancelability should only be disabled on entry to an object, never
184 On exit from an object, the cancelability state should
185 always be restored to its value on entry to the object.
187 This follows from a modularity argument: if the client of an object (or the
188 client of an object that uses that object) has disabled cancelability, it is
189 because the client does not want to have to worry about how to clean up if the
190 thread is canceled while executing some sequence of actions.
192 is called in such a state and it enables cancelability and a cancellation
193 request is pending for that thread, then the thread will be canceled,
194 contrary to the wish of the client that disabled.
196 Second, the cancelability type may be explicitly set to either
200 upon entry to an object.
201 But as with the cancelability state, on exit from
202 an object that cancelability type should always be restored to its value on
205 Finally, only functions that are cancel-safe may be called from a thread that
206 is asynchronously cancelable.
209 .Fn pthread_setcancelstate
213 The specified state is not
214 .Dv PTHREAD_CANCEL_ENABLE
216 .Dv PTHREAD_CANCEL_DISABLE .
220 .Fn pthread_setcanceltype
224 The specified state is not
225 .Dv PTHREAD_CANCEL_DEFERRED
227 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
233 .Fn pthread_testcancel
236 The standard allows implementations to make many more functions
239 This manual page was written by
240 .An David Leonard Aq Mt d@openbsd.org
244 .Xr pthread_cancel 3 .