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