3 .Dt PTHREAD_TESTCANCEL 3
6 .Nm pthread_setcancelstate ,
7 .Nm pthread_setcanceltype ,
9 .Nd set cancelability state
17 .Fn pthread_setcancelstate "int state" "int *oldstate"
19 .Fn pthread_setcanceltype "int type" "int *oldtype"
21 .Fn pthread_testcancel "void"
24 .Fn pthread_setcancelstate
25 function atomically both sets the calling thread's cancelability state
32 returns the previous cancelability state at the location referenced by
37 .Dv PTHREAD_CANCEL_ENABLE
39 .Dv PTHREAD_CANCEL_DISABLE .
42 .Fn pthread_setcanceltype
43 function atomically both sets the calling thread's cancelability type
50 returns the previous cancelability type at the location referenced by
55 .Dv PTHREAD_CANCEL_DEFERRED
57 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
59 The cancelability state and type of any newly created threads, including the
62 was first invoked, are
63 .Dv PTHREAD_CANCEL_ENABLE
65 .Dv PTHREAD_CANCEL_DEFERRED
69 .Fn pthread_testcancel
70 function creates a cancellation point in the calling thread.
72 .Fn pthread_testcancel
73 function has no effect if cancelability is disabled.
75 .Ss Cancelability States
76 The cancelability state of a thread determines the action taken upon
77 receipt of a cancellation request.
78 The thread may control cancellation in
81 Each thread maintains its own
82 .Dq cancelability state
83 which may be encoded in two bits:
85 .It Em Cancelability Enable
87 .Dv PTHREAD_CANCEL_DISABLE ,
88 cancellation requests against the target thread are held pending.
89 .It Em Cancelability Type
90 When cancelability is enabled and the cancelability type is
91 .Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
92 new or pending cancellation requests may be acted upon at any time.
93 When cancelability is enabled and the cancelability type is
94 .Dv PTHREAD_CANCEL_DEFERRED ,
95 cancellation requests are held pending until a cancellation point (see
97 If cancelability is disabled, the setting of the
98 cancelability type has no immediate effect as all cancellation requests
99 are held pending; however, once cancelability is enabled again the new
100 type will be in effect.
102 .Ss Cancellation Points
103 Cancellation points will occur when a thread is executing the following
113 .Fn pthread_cond_timedwait ,
114 .Fn pthread_cond_wait ,
116 .Fn pthread_testcancel ,
129 .Fn pthread_setcancelstate
131 .Fn pthread_setcanceltype
132 functions will return zero.
133 Otherwise, an error number shall be returned to
137 .Fn pthread_setcancelstate
139 .Fn pthread_setcanceltype
140 functions are used to control the points at which a thread may be
141 asynchronously canceled.
142 For cancellation control to be usable in modular
143 fashion, some rules must be followed.
145 For purposes of this discussion, consider an object to be a generalization
147 It is a set of procedures and global variables written as
148 a unit and called by clients not known by the object.
152 First, cancelability should only be disabled on entry to an object, never
154 On exit from an object, the cancelability state should
155 always be restored to its value on entry to the object.
157 This follows from a modularity argument: if the client of an object (or the
158 client of an object that uses that object) has disabled cancelability, it is
159 because the client does not want to have to worry about how to clean up if the
160 thread is canceled while executing some sequence of actions.
162 is called in such a state and it enables cancelability and a cancellation
163 request is pending for that thread, then the thread will be canceled,
164 contrary to the wish of the client that disabled.
166 Second, the cancelability type may be explicitly set to either
170 upon entry to an object.
171 But as with the cancelability state, on exit from
172 an object that cancelability type should always be restored to its value on
175 Finally, only functions that are cancel-safe may be called from a thread that
176 is asynchronously cancelable.
179 .Fn pthread_setcancelstate
183 The specified state is not
184 .Dv PTHREAD_CANCEL_ENABLE
186 .Dv PTHREAD_CANCEL_DISABLE .
190 .Fn pthread_setcanceltype
194 The specified state is not
195 .Dv PTHREAD_CANCEL_DEFERRED
197 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
203 .Fn pthread_testcancel
207 This manual page was written by
208 .An David Leonard Aq d@openbsd.org
212 .Xr pthread_cancel 3 .