NetBSD/lib/libpthread/thrd.3

235 lines
5.6 KiB
Groff

.\" $NetBSD: thrd.3,v 1.3 2019/04/27 10:57:11 wiz Exp $
.\"
.\" Copyright (c) 2016 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Kamil Rytarowski.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd October 16, 2016
.Dt THRD 3
.Os
.Sh NAME
.Nm thrd
.Nd thread functions
.Sh LIBRARY
.Lb libpthread
.Sh SYNOPSIS
.In threads.h
.Vt typedef "int" "(*thrd_start_t)" "(void *)"
.Ft int
.Fn thrd_create "thrd_t *thr" "thrd_start_t func" "void *arg"
.Ft thrd_t
.Fn thrd_current "void"
.Ft int
.Fn thrd_detach "thrd_t thr"
.Ft int
.Fn thrd_equal "thrd_t t1" "thrd_t t2"
.Ft _Noreturn void
.Fn thrd_exit "int res"
.Ft int
.Fn thrd_join "thrd_t thr" "int *res"
.Ft int
.Fn thrd_sleep "const struct timespec *duration" "struct timespec *remaining"
.Ft void
.Fn thrd_yield "void"
.Sh DESCRIPTION
The
.Nm
interface operates over opaque objects of the
.Dv thrd_t
type.
.Pp
The
.Fn thrd_create
function is used to create a new thread, calling
.Fa func
with the
.Fa arg
parameter.
This function initializes the
.Fa thr
object with the identifier of the newly created thread.
The completion of
.Fn thrd_create
is synchronized with the start of the newly produced thread.
It is possible to reuse the
.Fa thr
object once the thread has terminated either by joining another thread
operation or been detached.
.Pp
The
.Fn thrd_current
function returns the thread identifier of the current thread.
.Pp
The
.Fn thrd_detach
function is used to indicate that storage for the
.Fa thr
object can be reclaimed on the thread's termination.
The
.Fa thr
object cannot be already detached or joined.
.Pp
The
.Fn thrd_equal
function is used to check whether two
.Fa t1
and
.Fa t2
objects refer to the same thread.
.Pp
The
.Fn thrd_exit
function terminates the calling thread and passes the
.Fa res
value that might be read by the
.Fn thrd_join
function.
The program terminates once all threads have been terminated with
an exit code equivalent to calling the
.Xr exit 3
function with the
.Dv EXIT_SUCCESS
status.
The
.Fn thrd_join
function joins the
.Fa thr
thread, waiting and blocking until it has terminated.
The
.Fa res
parameter points to a variable that will store the status passed from the
joined function.
If
.Fa res
is
.Dv NULL
then the status from the
.Fn thrd_exit
function is ignored.
.Pp
The
.Fn thrd_sleep
function suspends execution of the calling thread until either
a signal is received or the interval specified in the
.Fa duration
argument has been passed.
The
.Fa remaining
parameter stores requested timeout reduced with the time actually suspended.
This argument is used when
.Fn thrd_sleep
has been interrupted.
It is valid code to point both arguments
.Fa duration
and
.Fa remaining
to the same object.
It is not guaranteed that sleep will not take longer than specified in
.Fa duration ,
however unless interrupted with a signal it will not take shorter
than the specified interval.
.Pp
The
.Fn thrd_yield
function yields a process voluntarily and gives other threads a chance to run
without waiting for any involuntary preemptive switch.
.Sh RETURN VALUES
The
.Fn thrd_create
function returns
.Dv thrd_success
on success, otherwise
.Dv thrd_nomem
if not sufficient memory could be allocated, or
.Dv thrd_error
on other errors.
.Pp
The
.Fn thrd_current
function returns the identifier of the calling thread.
.Pp
The
.Fn thrd_detach
function returns
.Dv thrd_current
on success or
.Dv thrd_error
on failure.
.Pp
The
.Fn thrd_equal
function returns zero if
.Fa t0
and
.Fa t1
refer to the different threads,
otherwise it will return non-zero.
.Pp
The
.Fn thrd_exit
function does not return.
.Pp
The
.Fn thrd_join
function returns
.Dv thrd_success
on success or
.Dv thrd_error
on failure.
.Pp
The
.Fn thrd_sleep
function returns 0 on success (as the requested time has elapsed),
\-1 once the function was interrupted by a signal,
or a negative value to indicate error.
The
.Nx
implementation returns \-2 on error.
.Pp
The
.Fn thrd_yield
function returns no value.
.Sh SEE ALSO
.Xr nanosleep 2 ,
.Xr pthread_create 3 ,
.Xr pthread_detach 3 ,
.Xr pthread_equal 3 ,
.Xr pthread_exit 3 ,
.Xr pthread_join 3 ,
.Xr pthread_self 3 ,
.Xr sched 3 ,
.Xr threads 3
.Sh STANDARDS
The
.Nm
interface conforms to
.St -isoC-2011 .
.Sh HISTORY
This interface first appeared in
.Nx 9 .
.Sh AUTHORS
.An Kamil Rytarowski Aq Mt kamil@NetBSD.org