jcs's openbsd hax
openbsd
1.\" $OpenBSD: pthread_testcancel.3,v 1.18 2025/06/07 00:16:52 schwarze Exp $
2.\"
3.\"
4.\" David Leonard, 1999. Public Domain.
5.\"
6.Dd $Mdocdate: June 7 2025 $
7.Dt PTHREAD_TESTCANCEL 3
8.Os
9.Sh NAME
10.Nm pthread_setcancelstate ,
11.Nm pthread_setcanceltype ,
12.Nm pthread_testcancel
13.Nd set cancelability state
14.Sh SYNOPSIS
15.Lb libpthread
16.In pthread.h
17.Ft int
18.Fn pthread_setcancelstate "int state" "int *oldstate"
19.Ft int
20.Fn pthread_setcanceltype "int type" "int *oldtype"
21.Ft void
22.Fn pthread_testcancel "void"
23.Sh DESCRIPTION
24The
25.Fn pthread_setcancelstate
26function atomically both sets the calling thread's cancelability state
27to the indicated
28.Fa state
29and, if
30.Fa oldstate
31is not
32.Dv NULL ,
33returns the previous cancelability state at the location referenced by
34.Fa oldstate .
35Legal values for
36.Fa state
37are
38.Dv PTHREAD_CANCEL_ENABLE
39and
40.Dv PTHREAD_CANCEL_DISABLE .
41.Pp
42The
43.Fn pthread_setcanceltype
44function atomically both sets the calling thread's cancelability type
45to the indicated
46.Fa type
47and, if
48.Fa oldtype
49is not
50.Dv NULL ,
51returns the previous cancelability type at the location referenced by
52.Fa oldtype .
53Legal values for
54.Fa type
55are
56.Dv PTHREAD_CANCEL_DEFERRED
57and
58.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
59.Pp
60The cancelability state and type of any newly created threads, including the
61thread in which
62.Fn main
63was first invoked, are
64.Dv PTHREAD_CANCEL_ENABLE
65and
66.Dv PTHREAD_CANCEL_DEFERRED
67respectively.
68.Pp
69The
70.Fn pthread_testcancel
71function creates a cancellation point in the calling thread.
72The
73.Fn pthread_testcancel
74function has no effect if cancelability is disabled.
75.Ss Cancelability States
76The cancelability state of a thread determines the action taken upon
77receipt of a cancellation request.
78The thread may control cancellation in a number of ways.
79.Pp
80Each thread maintains its own
81.Dq cancelability state
82which may be encoded in two bits:
83.Bl -hang
84.It Em Cancelability Enable
85When cancelability is
86.Dv PTHREAD_CANCEL_DISABLE ,
87cancellation requests against the target thread are held pending.
88.It Em Cancelability Type
89When cancelability is enabled and the cancelability type is
90.Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
91new or pending cancellation requests may be acted upon at any time.
92When cancelability is enabled and the cancelability type is
93.Dv PTHREAD_CANCEL_DEFERRED ,
94cancellation requests are held pending until a cancellation point (see
95below) is reached.
96If cancelability is disabled, the setting of the
97cancelability type has no immediate effect as all cancellation requests
98are held pending; however, once cancelability is enabled again the new
99type will be in effect.
100.El
101.Ss Cancellation Points
102Cancellation points will occur when a thread is executing the following
103base interfaces:
104.Fn accept ,
105.Fn close ,
106.Fn connect ,
107.Fn creat ,
108.Fn fcntl "F_SETLKW" ,
109.Fn fdatasync ,
110.Fn fsync ,
111.Fn lockf ,
112.Fn msgrcv ,
113.Fn msgsnd ,
114.Fn msync ,
115.Fn nanosleep ,
116.Fn open ,
117.Fn openat ,
118.Fn pause ,
119.Fn poll ,
120.Fn pread ,
121.Fn pselect ,
122.Fn pthread_cond_timedwait ,
123.Fn pthread_cond_wait ,
124.Fn pthread_join ,
125.Fn pthread_testcancel ,
126.Fn pwrite ,
127.Fn read ,
128.Fn readv ,
129.Fn recv ,
130.Fn recvfrom ,
131.Fn recvmsg ,
132.Fn select ,
133.Fn sem_timedwait ,
134.Fn sem_wait ,
135.Fn send ,
136.Fn sendmsg ,
137.Fn sendto ,
138.Fn sigsuspend ,
139.Fn sigwait ,
140.Fn sleep ,
141.Fn system ,
142.Fn tcdrain ,
143.Fn wait ,
144.Fn waitid ,
145.Fn waitpid ,
146.Fn write ,
147.Fn writev .
148.Pp
149In addition,
150cancellation points will occur when a thread is executing the following
151extension interfaces:
152.Fn accept4 ,
153.Fn closefrom ,
154.Fn ppoll ,
155.Fn preadv ,
156.Fn pwritev ,
157.Fn recvmmsg ,
158.Fn sendmmsg ,
159.Fn wait3 ,
160.Fn wait4 .
161.Sh RETURN VALUES
162If successful, the
163.Fn pthread_setcancelstate
164and
165.Fn pthread_setcanceltype
166functions will return zero.
167Otherwise, an error number shall be returned to indicate the error.
168.Pp
169The
170.Fn pthread_setcancelstate
171and
172.Fn pthread_setcanceltype
173functions are used to control the points at which a thread may be
174asynchronously cancelled.
175For cancellation control to be usable in modular
176fashion, some rules must be followed.
177.Pp
178For purposes of this discussion, consider an object to be a generalization
179of a procedure.
180It is a set of procedures and global variables written as
181a unit and called by clients not known by the object.
182Objects may depend on other objects.
183.Pp
184First, cancelability should only be disabled on entry to an object, never
185explicitly enabled.
186On exit from an object, the cancelability state should
187always be restored to its value on entry to the object.
188.Pp
189This follows from a modularity argument: if the client of an object (or the
190client of an object that uses that object) has disabled cancelability, it is
191because the client doesn't want to have to worry about how to clean up if the
192thread is cancelled while executing some sequence of actions.
193If an object
194is called in such a state and it enables cancelability and a cancellation
195request is pending for that thread, then the thread will be cancelled,
196contrary to the wish of the client that disabled.
197.Pp
198Second, the cancelability type may be explicitly set to either
199.Em deferred
200or
201.Em asynchronous
202upon entry to an object.
203But as with the cancelability state, on exit from
204an object that cancelability type should always be restored to its value on
205entry to the object.
206.Pp
207Finally, only functions that are cancel-safe may be called from a thread that
208is asynchronously cancelable.
209.Sh ERRORS
210The function
211.Fn pthread_setcancelstate
212may fail with:
213.Bl -tag -width Er
214.It Bq Er EINVAL
215The specified state is not
216.Dv PTHREAD_CANCEL_ENABLE
217or
218.Dv PTHREAD_CANCEL_DISABLE .
219.El
220.Pp
221The function
222.Fn pthread_setcanceltype
223may fail with:
224.Bl -tag -width Er
225.It Bq Er EINVAL
226The specified state is not
227.Dv PTHREAD_CANCEL_DEFERRED
228or
229.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
230.El
231.Sh SEE ALSO
232.Xr pthread_cancel 3
233.Sh STANDARDS
234.Fn pthread_testcancel
235conforms to
236.St -p1003.1-96