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