Merge branch 'vendor/TCPDUMP'
[dragonfly.git] / lib / libpthread / pthread_testcancel.3
1 .\" $FreeBSD: src/share/man/man3/pthread_testcancel.3,v 1.15 2007/10/22 10:08:01 ru Exp $
2 .\" $DragonFly: src/lib/libc_r/man/pthread_testcancel.3,v 1.3 2006/03/26 22:56:56 swildner Exp $
3 .Dd July 10, 2009
4 .Dt PTHREAD_TESTCANCEL 3
5 .Os
6 .Sh NAME
7 .Nm pthread_setcancelstate ,
8 .Nm pthread_setcanceltype ,
9 .Nm pthread_testcancel
10 .Nd set cancelability state
11 .Sh LIBRARY
12 .Lb libpthread
13 .Sh SYNOPSIS
14 .In pthread.h
15 .Ft int
16 .Fn pthread_setcancelstate "int state" "int *oldstate"
17 .Ft int
18 .Fn pthread_setcanceltype "int type" "int *oldtype"
19 .Ft void
20 .Fn pthread_testcancel "void"
21 .Sh DESCRIPTION
22 The
23 .Fn pthread_setcancelstate
24 function atomically both sets the calling thread's cancelability state
25 to the indicated
26 .Fa state
27 and, if
28 .Fa oldstate
29 is not
30 .Dv NULL ,
31 returns the previous cancelability state at the location referenced by
32 .Fa oldstate .
33 Legal values for
34 .Fa state
35 are
36 .Dv PTHREAD_CANCEL_ENABLE
37 and
38 .Dv PTHREAD_CANCEL_DISABLE .
39 .Pp
40 The
41 .Fn pthread_setcanceltype
42 function atomically both sets the calling thread's cancelability type
43 to the indicated
44 .Fa type
45 and, if
46 .Fa oldtype
47 is not
48 .Dv NULL ,
49 returns the previous cancelability type at the location referenced by
50 .Fa oldtype .
51 Legal values for
52 .Fa type
53 are
54 .Dv PTHREAD_CANCEL_DEFERRED
55 and
56 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
57 .Pp
58 The cancelability state and type of any newly created threads, including the
59 thread in which
60 .Fn main
61 was first invoked, are
62 .Dv PTHREAD_CANCEL_ENABLE
63 and
64 .Dv PTHREAD_CANCEL_DEFERRED
65 respectively.
66 .Pp
67 The
68 .Fn pthread_testcancel
69 function creates a cancellation point in the calling thread.
70 The
71 .Fn pthread_testcancel
72 function has no effect if cancelability is disabled.
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 close ,
104 .Fn creat ,
105 .Fn fcntl ,
106 .Fn fsync ,
107 .Fn msync ,
108 .Fn nanosleep ,
109 .Fn open ,
110 .Fn pause ,
111 .Fn pthread_cond_timedwait ,
112 .Fn pthread_cond_wait ,
113 .Fn pthread_join ,
114 .Fn pthread_testcancel ,
115 .Fn read ,
116 .Fn sigwaitinfo ,
117 .Fn sigsuspend ,
118 .Fn sigwait ,
119 .Fn sleep ,
120 .Fn system ,
121 .Fn tcdrain ,
122 .Fn wait ,
123 .Fn waitpid ,
124 .Fn write .
125 .Sh RETURN VALUES
126 If successful, the
127 .Fn pthread_setcancelstate
128 and
129 .Fn pthread_setcanceltype
130 functions will return zero.
131 Otherwise, an error number shall be returned to
132 indicate the error.
133 .Pp
134 The
135 .Fn pthread_setcancelstate
136 and
137 .Fn pthread_setcanceltype
138 functions are used to control the points at which a thread may be
139 asynchronously canceled.
140 For cancellation control to be usable in modular
141 fashion, some rules must be followed.
142 .Pp
143 For purposes of this discussion, consider an object to be a generalization
144 of a procedure.
145 It is a set of procedures and global variables written as
146 a unit and called by clients not known by the object.
147 Objects may depend
148 on other objects.
149 .Pp
150 First, cancelability should only be disabled on entry to an object, never
151 explicitly enabled.
152 On exit from an object, the cancelability state should
153 always be restored to its value on entry to the object.
154 .Pp
155 This follows from a modularity argument: if the client of an object (or the
156 client of an object that uses that object) has disabled cancelability, it is
157 because the client does not want to have to worry about how to clean up if the
158 thread is canceled while executing some sequence of actions.
159 If an object
160 is called in such a state and it enables cancelability and a cancellation
161 request is pending for that thread, then the thread will be canceled,
162 contrary to the wish of the client that disabled.
163 .Pp
164 Second, the cancelability type may be explicitly set to either
165 .Em deferred
166 or
167 .Em asynchronous
168 upon entry to an object.
169 But as with the cancelability state, on exit from
170 an object that cancelability type should always be restored to its value on
171 entry to the object.
172 .Pp
173 Finally, only functions that are cancel-safe may be called from a thread that
174 is asynchronously cancelable.
175 .Sh ERRORS
176 The function
177 .Fn pthread_setcancelstate
178 may fail with:
179 .Bl -tag -width Er
180 .It Bq Er EINVAL
181 The specified state is not
182 .Dv PTHREAD_CANCEL_ENABLE
183 or
184 .Dv PTHREAD_CANCEL_DISABLE .
185 .El
186 .Pp
187 The function
188 .Fn pthread_setcanceltype
189 may fail with:
190 .Bl -tag -width Er
191 .It Bq Er EINVAL
192 The specified state is not
193 .Dv PTHREAD_CANCEL_DEFERRED
194 or
195 .Dv PTHREAD_CANCEL_ASYNCHRONOUS .
196 .El
197 .Sh SEE ALSO
198 .Xr pthread_cancel 3
199 .Sh STANDARDS
200 The
201 .Fn pthread_testcancel
202 function conforms to
203 .St -p1003.1-96 .
204 .Sh AUTHORS
205 This manual page was written by
206 .An David Leonard Aq d@openbsd.org
207 for the
208 .Ox
209 implementation of
210 .Xr pthread_cancel 3 .