]> git.saurik.com Git - apple/libc.git/blame - pthreads/pthread_setcancelstate.3
Libc-498.1.5.tar.gz
[apple/libc.git] / pthreads / pthread_setcancelstate.3
CommitLineData
5b2abdfb
A
1.\" $FreeBSD: src/lib/libc_r/man/pthread_testcancel.3,v 1.2.2.3 2001/08/17 15:42:52 ru Exp $
2.Dd January 17, 1999
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 SYNOPSIS
11.Fd #include <pthread.h>
12.Ft int
224c7076
A
13.Fo pthread_setcancelstate
14.Fa "int state"
15.Fa "int *oldstate"
16.Fc
5b2abdfb 17.Ft int
224c7076
A
18.Fo pthread_setcanceltype
19.Fa "int type"
20.Fa "int *oldtype"
21.Fc
5b2abdfb 22.Ft void
224c7076
A
23.Fo pthread_testcancel
24.Fa "void"
25.Fc
5b2abdfb
A
26.Sh DESCRIPTION
27The
28.Fn pthread_setcancelstate
29function atomically both sets the calling thread's cancelability state
30to the indicated
31.Fa state
32and returns 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 returns the previous cancelability type at the location referenced by
47.Fa oldtype .
48Legal values for
49.Fa type
50are
51.Dv PTHREAD_CANCEL_DEFERRED
52and
53.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
54.Pp
55The cancelability state and type of any newly created threads, including the
56thread in which
57.Fn main
58was first invoked, are
59.Dv PTHREAD_CANCEL_ENABLE
60and
61.Dv PTHREAD_CANCEL_DEFERRED
62respectively.
63.Pp
64The
65.Fn pthread_testcancel
66function creates a cancellation point in the calling thread.
67The
68.Fn pthread_testcancel
69function has no effect if cancelability is disabled.
70.Pp
71.Ss Cancelability States
72The cancelability state of a thread determines the action taken upon
73receipt of a cancellation request.
74The thread may control cancellation in
75a number of ways.
76.Pp
77Each thread maintains its own
78.Dq cancelability state
79which may be encoded in two bits:
80.Bl -hang
81.It Em Cancelability Enable
82When cancelability is
83.Dv PTHREAD_CANCEL_DISABLE ,
84cancellation requests against the target thread are held pending.
85.It Em Cancelability Type
86When cancelability is enabled and the cancelability type is
87.Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
88new or pending cancellation requests may be acted upon at any time.
89When cancelability is enabled and the cancelability type is
90.Dv PTHREAD_CANCEL_DEFERRED ,
91cancellation requests are held pending until a cancellation point (see
92below) is reached.
93If cancelability is disabled, the setting of the
94cancelability type has no immediate effect as all cancellation requests
95are held pending; however, once cancelability is enabled again the new
96type will be in effect.
97.El
98.Ss Cancellation Points
99Cancellation points will occur when a thread is executing the following
100functions:
101.Fn close ,
102.Fn creat ,
103.Fn fcntl ,
104.Fn fsync ,
105.Fn msync ,
106.Fn nanosleep ,
107.Fn open ,
108.Fn pause ,
109.Fn pthread_cond_timedwait ,
110.Fn pthread_cond_wait ,
111.Fn pthread_join ,
112.Fn pthread_testcancel ,
113.Fn read ,
114.Fn sigwaitinfo ,
115.Fn sigsuspend ,
116.Fn sigwait ,
117.Fn sleep ,
118.Fn system ,
119.Fn tcdrain ,
120.Fn wait ,
121.Fn waitpid ,
122.Fn write .
123.Sh RETURN VALUES
124If successful, the
125.Fn pthread_setcancelstate
126and
127.Fn pthread_setcanceltype
128functions will return zero.
129Otherwise, an error number shall be returned to
130indicate the error.
131.Pp
132The
133.Fn pthread_setcancelstate
134and
135.Fn pthread_setcanceltype
136functions are used to control the points at which a thread may be
137asynchronously canceled.
138For cancellation control to be usable in modular
139fashion, some rules must be followed.
140.Pp
141For purposes of this discussion, consider an object to be a generalization
142of a procedure.
143It is a set of procedures and global variables written as
144a unit and called by clients not known by the object.
145Objects may depend
146on other objects.
147.Pp
148First, cancelability should only be disabled on entry to an object, never
149explicitly enabled.
150On exit from an object, the cancelability state should
151always be restored to its value on entry to the object.
152.Pp
153This follows from a modularity argument: if the client of an object (or the
154client of an object that uses that object) has disabled cancelability, it is
155because the client doesn't want to have to worry about how to clean up if the
156thread is canceled while executing some sequence of actions.
157If an object
158is called in such a state and it enables cancelability and a cancellation
159request is pending for that thread, then the thread will be canceled,
160contrary to the wish of the client that disabled.
161.Pp
162Second, the cancelability type may be explicitly set to either
163.Em deferred
164or
165.Em asynchronous
166upon entry to an object.
224c7076 167But, as with the cancelability state, on exit from
5b2abdfb
A
168an object that cancelability type should always be restored to its value on
169entry to the object.
170.Pp
171Finally, only functions that are cancel-safe may be called from a thread that
172is asynchronously cancelable.
173.Sh ERRORS
174The function
175.Fn pthread_setcancelstate
176may fail with:
177.Bl -tag -width Er
178.It Bq Er EINVAL
179The specified state is not
180.Dv PTHREAD_CANCEL_ENABLE
181or
182.Dv PTHREAD_CANCEL_DISABLE .
183.El
184.Pp
185The function
186.Fn pthread_setcanceltype
187may fail with:
188.Bl -tag -width Er
189.It Bq Er EINVAL
190The specified state is not
191.Dv PTHREAD_CANCEL_DEFERRED
192or
193.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
194.El
195.Sh SEE ALSO
196.Xr pthread_cancel 3
197.Sh STANDARDS
198.Fn pthread_testcancel
199conforms to
200.St -p1003.1-96 .
201.Sh AUTHORS
202This man page was written by
203.An David Leonard Aq d@openbsd.org
204for the
205.Ox
206implementation of
207.Xr pthread_cancel 3 .