xref: /netbsd-src/lib/libpthread/pthread_testcancel.3 (revision 6cb10275d08f045e872662c371fe2f2724f2f6e6)
1*6cb10275Sriastradh.\" $NetBSD: pthread_testcancel.3,v 1.9 2014/03/18 18:20:38 riastradh Exp $
255818196Snathanw.\"
355818196Snathanw.\" Copyright (c) 2002 The NetBSD Foundation, Inc.
455818196Snathanw.\" All rights reserved.
555818196Snathanw.\" Redistribution and use in source and binary forms, with or without
655818196Snathanw.\" modification, are permitted provided that the following conditions
755818196Snathanw.\" are met:
855818196Snathanw.\" 1. Redistributions of source code must retain the above copyright
955818196Snathanw.\"    notice, this list of conditions and the following disclaimer.
1055818196Snathanw.\" 2. Redistributions in binary form must reproduce the above copyright
1155818196Snathanw.\"    notice, this list of conditions and the following disclaimer in the
1255818196Snathanw.\"    documentation and/or other materials provided with the distribution.
1355818196Snathanw.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1455818196Snathanw.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1555818196Snathanw.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1655818196Snathanw.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1755818196Snathanw.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1855818196Snathanw.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1955818196Snathanw.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2055818196Snathanw.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2155818196Snathanw.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2255818196Snathanw.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2355818196Snathanw.\" POSSIBILITY OF SUCH DAMAGE.
2455818196Snathanw.\"
2555818196Snathanw.\" $FreeBSD: src/lib/libpthread/man/pthread_testcancel.3,v 1.9 2002/09/16 19:29:29 mini Exp $
266abd2b9cSwiz.Dd August 6, 2010
2755818196Snathanw.Dt PTHREAD_TESTCANCEL 3
2855818196Snathanw.Os
2955818196Snathanw.Sh NAME
3055818196Snathanw.Nm pthread_setcancelstate ,
3155818196Snathanw.Nm pthread_setcanceltype ,
3255818196Snathanw.Nm pthread_testcancel
3355818196Snathanw.Nd set cancelability state
3455818196Snathanw.Sh LIBRARY
3555818196Snathanw.Lb libpthread
3655818196Snathanw.Sh SYNOPSIS
3755818196Snathanw.In pthread.h
3855818196Snathanw.Ft int
3955818196Snathanw.Fn pthread_setcancelstate "int state" "int *oldstate"
4055818196Snathanw.Ft int
4155818196Snathanw.Fn pthread_setcanceltype "int type" "int *oldtype"
4255818196Snathanw.Ft void
4355818196Snathanw.Fn pthread_testcancel "void"
4455818196Snathanw.Sh DESCRIPTION
4555818196SnathanwThe
4655818196Snathanw.Fn pthread_setcancelstate
4755818196Snathanwfunction atomically both sets the calling thread's cancelability state
4855818196Snathanwto the indicated
4955818196Snathanw.Fa state
5055818196Snathanwand, if
5155818196Snathanw.Fa oldstate
5255818196Snathanwis not
5355818196Snathanw.Dv NULL ,
5455818196Snathanwreturns the previous cancelability state at the location referenced by
5555818196Snathanw.Fa oldstate .
5655818196SnathanwLegal values for
5755818196Snathanw.Fa state
5855818196Snathanware
5955818196Snathanw.Dv PTHREAD_CANCEL_ENABLE
6055818196Snathanwand
6155818196Snathanw.Dv PTHREAD_CANCEL_DISABLE .
6255818196Snathanw.Pp
6355818196SnathanwThe
6455818196Snathanw.Fn pthread_setcanceltype
6555818196Snathanwfunction atomically both sets the calling thread's cancelability type
6655818196Snathanwto the indicated
6755818196Snathanw.Fa type
6855818196Snathanwand, if
6955818196Snathanw.Fa oldtype
7055818196Snathanwis not
7155818196Snathanw.Dv NULL ,
7255818196Snathanwreturns the previous cancelability type at the location referenced by
7355818196Snathanw.Fa oldtype .
7455818196SnathanwLegal values for
7555818196Snathanw.Fa type
7655818196Snathanware
7755818196Snathanw.Dv PTHREAD_CANCEL_DEFERRED
7855818196Snathanwand
7955818196Snathanw.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
8055818196Snathanw.Pp
8155818196SnathanwThe cancelability state and type of any newly created threads, including the
8255818196Snathanwthread in which
8355818196Snathanw.Fn main
8455818196Snathanwwas first invoked, are
8555818196Snathanw.Dv PTHREAD_CANCEL_ENABLE
8655818196Snathanwand
8755818196Snathanw.Dv PTHREAD_CANCEL_DEFERRED
8855818196Snathanwrespectively.
8955818196Snathanw.Pp
9055818196SnathanwThe
9155818196Snathanw.Fn pthread_testcancel
9255818196Snathanwfunction creates a cancellation point in the calling thread.
9355818196SnathanwThe
9455818196Snathanw.Fn pthread_testcancel
9555818196Snathanwfunction has no effect if cancelability is disabled.
9655818196Snathanw.Ss Cancelability States
9755818196SnathanwThe cancelability state of a thread determines the action taken upon
9855818196Snathanwreceipt of a cancellation request.
9955818196SnathanwThe thread may control cancellation in
10055818196Snathanwa number of ways.
10155818196Snathanw.Pp
10255818196SnathanwEach thread maintains its own
10355818196Snathanw.Dq cancelability state
10455818196Snathanwwhich may be encoded in two bits:
10555818196Snathanw.Bl -hang
10655818196Snathanw.It Em Cancelability Enable
10755818196SnathanwWhen cancelability is
10855818196Snathanw.Dv PTHREAD_CANCEL_DISABLE ,
10955818196Snathanwcancellation requests against the target thread are held pending.
11055818196Snathanw.It Em Cancelability Type
11155818196SnathanwWhen cancelability is enabled and the cancelability type is
11255818196Snathanw.Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
11355818196Snathanwnew or pending cancellation requests may be acted upon at any time.
11455818196SnathanwWhen cancelability is enabled and the cancelability type is
11555818196Snathanw.Dv PTHREAD_CANCEL_DEFERRED ,
11655818196Snathanwcancellation requests are held pending until a cancellation point (see
11755818196Snathanwbelow) is reached.
11855818196SnathanwIf cancelability is disabled, the setting of the
11955818196Snathanwcancelability type has no immediate effect as all cancellation requests
12055818196Snathanware held pending; however, once cancelability is enabled again the new
12155818196Snathanwtype will be in effect.
12255818196Snathanw.El
12355818196Snathanw.Ss Cancellation Points
12455818196SnathanwCancellation points will occur when a thread is executing the following
12555818196Snathanwfunctions:
12655818196Snathanw.Fn accept ,
127f6cf4693Schristos.Fn aio_suspend ,
128f6cf4693Schristos.Fn clock_nanosleep ,
12955818196Snathanw.Fn close ,
13055818196Snathanw.Fn connect ,
13155818196Snathanw.Fn creat ,
13255818196Snathanw.Fn fcntl ,
133f6cf4693Schristos.Fn fdatasync ,
13455818196Snathanw.Fn fsync ,
135137c808bSthorpej.Fn fsync_range ,
13655818196Snathanw.\".Fn getmsg ,
13755818196Snathanw.\".Fn getpmsg ,
138f6cf4693Schristos.Fn kevent ,
13955818196Snathanw.\".Fn lockf ,
140f6cf4693Schristos.Fn mq_receive ,
141f6cf4693Schristos.Fn mq_send ,
142f6cf4693Schristos.Fn mq_timedreceive ,
143f6cf4693Schristos.Fn mq_timedsend ,
14455818196Snathanw.Fn msgrcv ,
14555818196Snathanw.Fn msgsnd ,
14655818196Snathanw.Fn msync ,
14755818196Snathanw.Fn nanosleep ,
14855818196Snathanw.Fn open ,
14955818196Snathanw.Fn pause ,
15055818196Snathanw.Fn poll ,
151f6cf4693Schristos.Fn pollts ,
15255818196Snathanw.Fn pread ,
15355818196Snathanw.Fn pselect ,
15455818196Snathanw.Fn pthread_cond_timedwait ,
15555818196Snathanw.Fn pthread_cond_wait ,
15655818196Snathanw.Fn pthread_join ,
15755818196Snathanw.Fn pthread_testcancel ,
15855818196Snathanw.\".Fn putmsg ,
15955818196Snathanw.\".Fn putpmsg ,
16055818196Snathanw.Fn pwrite ,
16155818196Snathanw.Fn read ,
16255818196Snathanw.Fn readv ,
16355818196Snathanw.Fn recv ,
16455818196Snathanw.Fn recvfrom ,
16555818196Snathanw.Fn recvmsg ,
16655818196Snathanw.Fn select ,
16755818196Snathanw.Fn sem_timedwait ,
16855818196Snathanw.Fn sem_wait ,
16955818196Snathanw.Fn send ,
17055818196Snathanw.Fn sendmsg ,
17155818196Snathanw.Fn sendto ,
17255818196Snathanw.Fn sigpause ,
17355818196Snathanw.Fn sigsuspend ,
17455818196Snathanw.Fn sigtimedwait ,
17555818196Snathanw.Fn sigwait ,
17655818196Snathanw.Fn sigwaitinfo ,
17755818196Snathanw.Fn sleep ,
17855818196Snathanw.Fn system ,
17955818196Snathanw.Fn tcdrain ,
18055818196Snathanw.Fn usleep ,
18155818196Snathanw.Fn wait ,
182f6cf4693Schristos.Fn wait4 ,
18355818196Snathanw.Fn waitid ,
18455818196Snathanw.Fn waitpid ,
18555818196Snathanw.Fn write ,
18655818196Snathanwand
18755818196Snathanw.Fn writev .
18855818196Snathanw.Sh RETURN VALUES
18955818196SnathanwIf successful, the
19055818196Snathanw.Fn pthread_setcancelstate
19155818196Snathanwand
19255818196Snathanw.Fn pthread_setcanceltype
19355818196Snathanwfunctions will return zero.
19455818196SnathanwOtherwise, an error number shall be returned to
19555818196Snathanwindicate the error.
19655818196Snathanw.Pp
19755818196SnathanwThe
19855818196Snathanw.Fn pthread_setcancelstate
19955818196Snathanwand
20055818196Snathanw.Fn pthread_setcanceltype
20155818196Snathanwfunctions are used to control the points at which a thread may be
20255818196Snathanwasynchronously canceled.
20355818196SnathanwFor cancellation control to be usable in modular
20455818196Snathanwfashion, some rules must be followed.
20555818196Snathanw.Pp
20655818196SnathanwFor purposes of this discussion, consider an object to be a generalization
20755818196Snathanwof a procedure.
20855818196SnathanwIt is a set of procedures and global variables written as
20955818196Snathanwa unit and called by clients not known by the object.
21055818196SnathanwObjects may depend
21155818196Snathanwon other objects.
21255818196Snathanw.Pp
21355818196SnathanwFirst, cancelability should only be disabled on entry to an object, never
21455818196Snathanwexplicitly enabled.
21555818196SnathanwOn exit from an object, the cancelability state should
21655818196Snathanwalways be restored to its value on entry to the object.
21755818196Snathanw.Pp
21855818196SnathanwThis follows from a modularity argument: if the client of an object (or the
21955818196Snathanwclient of an object that uses that object) has disabled cancelability, it is
22055818196Snathanwbecause the client doesn't want to have to worry about how to clean up if the
22155818196Snathanwthread is canceled while executing some sequence of actions.
22255818196SnathanwIf an object
22355818196Snathanwis called in such a state and it enables cancelability and a cancellation
22455818196Snathanwrequest is pending for that thread, then the thread will be canceled,
22555818196Snathanwcontrary to the wish of the client that disabled.
22655818196Snathanw.Pp
22755818196SnathanwSecond, the cancelability type may be explicitly set to either
22855818196Snathanw.Em deferred
22955818196Snathanwor
23055818196Snathanw.Em asynchronous
23155818196Snathanwupon entry to an object.
23255818196SnathanwBut as with the cancelability state, on exit from
23355818196Snathanwan object that cancelability type should always be restored to its value on
23455818196Snathanwentry to the object.
23555818196Snathanw.Pp
23655818196SnathanwFinally, only functions that are cancel-safe may be called from a thread that
23755818196Snathanwis asynchronously cancelable.
23855818196Snathanw.Sh ERRORS
23955818196SnathanwThe function
24055818196Snathanw.Fn pthread_setcancelstate
24155818196Snathanwmay fail with:
24255818196Snathanw.Bl -tag -width Er
24355818196Snathanw.It Bq Er EINVAL
24455818196SnathanwThe specified state is not
24555818196Snathanw.Dv PTHREAD_CANCEL_ENABLE
24655818196Snathanwor
24755818196Snathanw.Dv PTHREAD_CANCEL_DISABLE .
24855818196Snathanw.El
24955818196Snathanw.Pp
25055818196SnathanwThe function
25155818196Snathanw.Fn pthread_setcanceltype
25255818196Snathanwmay fail with:
25355818196Snathanw.Bl -tag -width Er
25455818196Snathanw.It Bq Er EINVAL
25555818196SnathanwThe specified state is not
25655818196Snathanw.Dv PTHREAD_CANCEL_DEFERRED
25755818196Snathanwor
25855818196Snathanw.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
25955818196Snathanw.El
26055818196Snathanw.Sh SEE ALSO
26155818196Snathanw.Xr pthread_cancel 3
26255818196Snathanw.Sh STANDARDS
2639c4ae7f5SjruohoThese functions conform to
2649c4ae7f5Sjruoho.St -p1003.1-2001 .
26555818196Snathanw.Sh AUTHORS
26655818196SnathanwThis man page was written by
267a5684d07Swiz.An David Leonard Aq Mt d@openbsd.org
26855818196Snathanwfor the
26955818196Snathanw.Ox
27055818196Snathanwimplementation of
27155818196Snathanw.Xr pthread_cancel 3 .
272