1999-11-28 05:38:13 +00:00
|
|
|
.\" $FreeBSD$
|
2017-03-19 00:51:12 +00:00
|
|
|
.Dd March 18, 2017
|
1999-11-28 05:38:13 +00:00
|
|
|
.Dt PTHREAD_TESTCANCEL 3
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm pthread_setcancelstate ,
|
|
|
|
.Nm pthread_setcanceltype ,
|
|
|
|
.Nm pthread_testcancel
|
|
|
|
.Nd set cancelability state
|
2000-04-22 15:50:29 +00:00
|
|
|
.Sh LIBRARY
|
2004-01-14 21:44:26 +00:00
|
|
|
.Lb libpthread
|
1999-11-28 05:38:13 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In pthread.h
|
1999-11-28 05:38:13 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn pthread_setcancelstate "int state" "int *oldstate"
|
|
|
|
.Ft int
|
|
|
|
.Fn pthread_setcanceltype "int type" "int *oldtype"
|
|
|
|
.Ft void
|
|
|
|
.Fn pthread_testcancel "void"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Fn pthread_setcancelstate
|
|
|
|
function atomically both sets the calling thread's cancelability state
|
|
|
|
to the indicated
|
|
|
|
.Fa state
|
2001-11-05 08:21:32 +00:00
|
|
|
and, if
|
|
|
|
.Fa oldstate
|
|
|
|
is not
|
|
|
|
.Dv NULL ,
|
|
|
|
returns the previous cancelability state at the location referenced by
|
1999-11-28 05:38:13 +00:00
|
|
|
.Fa oldstate .
|
|
|
|
Legal values for
|
|
|
|
.Fa state
|
|
|
|
are
|
|
|
|
.Dv PTHREAD_CANCEL_ENABLE
|
|
|
|
and
|
|
|
|
.Dv PTHREAD_CANCEL_DISABLE .
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn pthread_setcanceltype
|
|
|
|
function atomically both sets the calling thread's cancelability type
|
|
|
|
to the indicated
|
|
|
|
.Fa type
|
2001-11-05 08:21:32 +00:00
|
|
|
and, if
|
|
|
|
.Fa oldtype
|
|
|
|
is not
|
|
|
|
.Dv NULL ,
|
|
|
|
returns the previous cancelability type at the location referenced by
|
1999-11-28 05:38:13 +00:00
|
|
|
.Fa oldtype .
|
|
|
|
Legal values for
|
|
|
|
.Fa type
|
|
|
|
are
|
|
|
|
.Dv PTHREAD_CANCEL_DEFERRED
|
|
|
|
and
|
|
|
|
.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
|
|
|
|
.Pp
|
|
|
|
The cancelability state and type of any newly created threads, including the
|
|
|
|
thread in which
|
|
|
|
.Fn main
|
|
|
|
was first invoked, are
|
|
|
|
.Dv PTHREAD_CANCEL_ENABLE
|
|
|
|
and
|
|
|
|
.Dv PTHREAD_CANCEL_DEFERRED
|
|
|
|
respectively.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn pthread_testcancel
|
2000-03-02 09:14:21 +00:00
|
|
|
function creates a cancellation point in the calling thread.
|
|
|
|
The
|
1999-11-28 05:38:13 +00:00
|
|
|
.Fn pthread_testcancel
|
|
|
|
function has no effect if cancelability is disabled.
|
|
|
|
.Pp
|
|
|
|
.Ss Cancelability States
|
|
|
|
The cancelability state of a thread determines the action taken upon
|
2000-03-02 09:14:21 +00:00
|
|
|
receipt of a cancellation request.
|
|
|
|
The thread may control cancellation in
|
1999-11-28 05:38:13 +00:00
|
|
|
a number of ways.
|
|
|
|
.Pp
|
|
|
|
Each thread maintains its own
|
|
|
|
.Dq cancelability state
|
|
|
|
which may be encoded in two bits:
|
|
|
|
.Bl -hang
|
|
|
|
.It Em Cancelability Enable
|
|
|
|
When cancelability is
|
|
|
|
.Dv PTHREAD_CANCEL_DISABLE ,
|
|
|
|
cancellation requests against the target thread are held pending.
|
|
|
|
.It Em Cancelability Type
|
|
|
|
When cancelability is enabled and the cancelability type is
|
|
|
|
.Dv PTHREAD_CANCEL_ASYNCHRONOUS ,
|
|
|
|
new or pending cancellation requests may be acted upon at any time.
|
|
|
|
When cancelability is enabled and the cancelability type is
|
|
|
|
.Dv PTHREAD_CANCEL_DEFERRED ,
|
|
|
|
cancellation requests are held pending until a cancellation point (see
|
2000-03-02 09:14:21 +00:00
|
|
|
below) is reached.
|
|
|
|
If cancelability is disabled, the setting of the
|
1999-11-28 05:38:13 +00:00
|
|
|
cancelability type has no immediate effect as all cancellation requests
|
|
|
|
are held pending; however, once cancelability is enabled again the new
|
|
|
|
type will be in effect.
|
|
|
|
.El
|
|
|
|
.Ss Cancellation Points
|
|
|
|
Cancellation points will occur when a thread is executing the following
|
|
|
|
functions:
|
2015-03-29 19:37:41 +00:00
|
|
|
.Bl -tag -width "Fn pthread_cond_timedwait" -compact
|
|
|
|
.It Fn accept
|
|
|
|
.It Fn accept4
|
|
|
|
.It Fn aio_suspend
|
|
|
|
.It Fn connect
|
2017-03-19 00:51:12 +00:00
|
|
|
.It Fn clock_nanosleep
|
2015-03-29 19:37:41 +00:00
|
|
|
.It Fn close
|
|
|
|
.It Fn creat
|
|
|
|
.It Fn fcntl
|
2013-06-11 21:40:20 +00:00
|
|
|
The
|
|
|
|
.Fn fcntl
|
|
|
|
function is a cancellation point if
|
|
|
|
.Fa cmd
|
|
|
|
is
|
|
|
|
.Dv F_SETLKW .
|
2016-08-16 08:27:03 +00:00
|
|
|
.It Fn fdatasync
|
2015-03-29 19:37:41 +00:00
|
|
|
.It Fn fsync
|
|
|
|
.It Fn kevent
|
Make kevent(2) a cancellation point.
Note that to cancel blocked kevent(2) call, changelist must be empty,
since we cannot cancel a call which already made changes to the
process state. And in reverse, call which only makes changes to the
kqueue state, without waiting for an event, is not cancellable. This
makes a natural usage model to migrate kqueue loop to support
cancellation, where existing single kevent(2) call must be split into
two: first uncancellable update of kqueue, then cancellable wait for
events.
Note that this is ABI-incompatible change, but it is believed that
there is no cancel-safe code that relies on kevent(2) not being a
cancellation point. Option to preserve the ABI would be to keep
kevent(2) as is, but add new call with flags to specify cancellation
behaviour, which only value seems to add complications.
Suggested and reviewed by: jilles
Sponsored by: The FreeBSD Foundation
MFC after: 2 weeks
2015-03-29 19:14:41 +00:00
|
|
|
The
|
|
|
|
.Fn kevent
|
|
|
|
function is a cancellation point if it is potentially blocking,
|
2017-02-23 19:36:38 +00:00
|
|
|
such as when the
|
Make kevent(2) a cancellation point.
Note that to cancel blocked kevent(2) call, changelist must be empty,
since we cannot cancel a call which already made changes to the
process state. And in reverse, call which only makes changes to the
kqueue state, without waiting for an event, is not cancellable. This
makes a natural usage model to migrate kqueue loop to support
cancellation, where existing single kevent(2) call must be split into
two: first uncancellable update of kqueue, then cancellable wait for
events.
Note that this is ABI-incompatible change, but it is believed that
there is no cancel-safe code that relies on kevent(2) not being a
cancellation point. Option to preserve the ABI would be to keep
kevent(2) as is, but add new call with flags to specify cancellation
behaviour, which only value seems to add complications.
Suggested and reviewed by: jilles
Sponsored by: The FreeBSD Foundation
MFC after: 2 weeks
2015-03-29 19:14:41 +00:00
|
|
|
.Fa nevents
|
2017-02-23 19:36:38 +00:00
|
|
|
argument is non-zero.
|
2015-03-29 19:37:41 +00:00
|
|
|
.It Fn mq_receive
|
|
|
|
.It Fn mq_send
|
|
|
|
.It Fn mq_timedreceive
|
|
|
|
.It Fn mq_timedsend
|
|
|
|
.It Fn msync
|
|
|
|
.It Fn nanosleep
|
|
|
|
.It Fn open
|
|
|
|
.It Fn openat
|
|
|
|
.It Fn pause
|
|
|
|
.It Fn poll
|
2015-04-18 21:35:41 +00:00
|
|
|
.It Fn ppoll
|
2015-03-29 19:37:41 +00:00
|
|
|
.It Fn pselect
|
|
|
|
.It Fn pthread_cond_timedwait
|
|
|
|
.It Fn pthread_cond_wait
|
|
|
|
.It Fn pthread_join
|
|
|
|
.It Fn pthread_testcancel
|
|
|
|
.It Fn read
|
|
|
|
.It Fn readv
|
|
|
|
.It Fn recv
|
|
|
|
.It Fn recvfrom
|
|
|
|
.It Fn recvmsg
|
|
|
|
.It Fn select
|
|
|
|
.It Fn sem_timedwait
|
2017-02-23 19:36:38 +00:00
|
|
|
.It Fn sem_clockwait_np
|
2015-03-29 19:37:41 +00:00
|
|
|
.It Fn sem_wait
|
|
|
|
.It Fn send
|
|
|
|
.It Fn sendmsg
|
|
|
|
.It Fn sendto
|
|
|
|
.It Fn sigsuspend
|
|
|
|
.It Fn sigtimedwait
|
|
|
|
.It Fn sigwaitinfo
|
|
|
|
.It Fn sigwait
|
|
|
|
.It Fn sleep
|
|
|
|
.It Fn system
|
|
|
|
.It Fn tcdrain
|
|
|
|
.It Fn usleep
|
|
|
|
.It Fn wait
|
|
|
|
.It Fn wait3
|
|
|
|
.It Fn wait4
|
2015-04-18 21:35:41 +00:00
|
|
|
.It Fn wait6
|
|
|
|
.It Fn waitid
|
2015-03-29 19:37:41 +00:00
|
|
|
.It Fn waitpid
|
|
|
|
.It Fn write
|
|
|
|
.It Fn writev
|
|
|
|
.El
|
|
|
|
.Sh NOTES
|
1999-11-28 05:38:13 +00:00
|
|
|
The
|
|
|
|
.Fn pthread_setcancelstate
|
|
|
|
and
|
|
|
|
.Fn pthread_setcanceltype
|
|
|
|
functions are used to control the points at which a thread may be
|
2000-03-02 09:14:21 +00:00
|
|
|
asynchronously canceled.
|
|
|
|
For cancellation control to be usable in modular
|
1999-11-28 05:38:13 +00:00
|
|
|
fashion, some rules must be followed.
|
|
|
|
.Pp
|
|
|
|
For purposes of this discussion, consider an object to be a generalization
|
2000-03-02 09:14:21 +00:00
|
|
|
of a procedure.
|
|
|
|
It is a set of procedures and global variables written as
|
|
|
|
a unit and called by clients not known by the object.
|
|
|
|
Objects may depend
|
1999-11-28 05:38:13 +00:00
|
|
|
on other objects.
|
|
|
|
.Pp
|
|
|
|
First, cancelability should only be disabled on entry to an object, never
|
2000-03-02 09:14:21 +00:00
|
|
|
explicitly enabled.
|
|
|
|
On exit from an object, the cancelability state should
|
1999-11-28 05:38:13 +00:00
|
|
|
always be restored to its value on entry to the object.
|
|
|
|
.Pp
|
|
|
|
This follows from a modularity argument: if the client of an object (or the
|
|
|
|
client of an object that uses that object) has disabled cancelability, it is
|
2005-02-13 22:25:33 +00:00
|
|
|
because the client does not want to have to worry about how to clean up if the
|
2000-03-02 09:14:21 +00:00
|
|
|
thread is canceled while executing some sequence of actions.
|
|
|
|
If an object
|
1999-11-28 05:38:13 +00:00
|
|
|
is called in such a state and it enables cancelability and a cancellation
|
|
|
|
request is pending for that thread, then the thread will be canceled,
|
|
|
|
contrary to the wish of the client that disabled.
|
|
|
|
.Pp
|
|
|
|
Second, the cancelability type may be explicitly set to either
|
|
|
|
.Em deferred
|
|
|
|
or
|
|
|
|
.Em asynchronous
|
2000-03-02 09:14:21 +00:00
|
|
|
upon entry to an object.
|
|
|
|
But as with the cancelability state, on exit from
|
1999-11-28 05:38:13 +00:00
|
|
|
an object that cancelability type should always be restored to its value on
|
|
|
|
entry to the object.
|
|
|
|
.Pp
|
|
|
|
Finally, only functions that are cancel-safe may be called from a thread that
|
|
|
|
is asynchronously cancelable.
|
2015-03-29 19:37:41 +00:00
|
|
|
.Sh RETURN VALUES
|
|
|
|
If successful, the
|
|
|
|
.Fn pthread_setcancelstate
|
|
|
|
and
|
|
|
|
.Fn pthread_setcanceltype
|
|
|
|
functions will return zero.
|
|
|
|
Otherwise, an error number shall be returned to
|
|
|
|
indicate the error.
|
1999-11-28 05:38:13 +00:00
|
|
|
.Sh ERRORS
|
|
|
|
The function
|
|
|
|
.Fn pthread_setcancelstate
|
|
|
|
may fail with:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EINVAL
|
|
|
|
The specified state is not
|
|
|
|
.Dv PTHREAD_CANCEL_ENABLE
|
|
|
|
or
|
|
|
|
.Dv PTHREAD_CANCEL_DISABLE .
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The function
|
|
|
|
.Fn pthread_setcanceltype
|
|
|
|
may fail with:
|
|
|
|
.Bl -tag -width Er
|
|
|
|
.It Bq Er EINVAL
|
|
|
|
The specified state is not
|
|
|
|
.Dv PTHREAD_CANCEL_DEFERRED
|
|
|
|
or
|
|
|
|
.Dv PTHREAD_CANCEL_ASYNCHRONOUS .
|
|
|
|
.El
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr pthread_cancel 3
|
|
|
|
.Sh STANDARDS
|
2003-03-24 16:05:24 +00:00
|
|
|
The
|
1999-11-28 05:38:13 +00:00
|
|
|
.Fn pthread_testcancel
|
2003-03-24 16:05:24 +00:00
|
|
|
function conforms to
|
2001-02-26 15:16:43 +00:00
|
|
|
.St -p1003.1-96 .
|
2013-06-11 21:40:20 +00:00
|
|
|
The standard allows implementations to make many more functions
|
|
|
|
cancellation points.
|
1999-11-28 05:38:13 +00:00
|
|
|
.Sh AUTHORS
|
2005-06-30 13:18:15 +00:00
|
|
|
This manual page was written by
|
2014-06-23 08:27:27 +00:00
|
|
|
.An David Leonard Aq Mt d@openbsd.org
|
2001-08-13 16:43:02 +00:00
|
|
|
for the
|
|
|
|
.Ox
|
|
|
|
implementation of
|
|
|
|
.Xr pthread_cancel 3 .
|