.\" Copyright (c) 2017 The DragonFly Project.  All rights reserved.
.\"
.\" This code is derived from software contributed to The DragonFly Project
.\" by Sepherosa Ziehau <sepherosa@gmail.com>.
.\"
.\" 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.
.\" 3. Neither the name of The DragonFly Project nor the names of its
.\"    contributors may be used to endorse or promote products derived
.\"    from this software without specific, prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
.\" COPYRIGHT HOLDERS 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 January 14, 2017
.Dt PTHREAD_SETAFFINITY_NP 3
.Os
.Sh NAME
.Nm pthread_setaffinity_np ,
.Nm pthread_getaffinity_np
.Nd set and get the thread's CPU affinity mask
.Sh LIBRARY
.Lb libpthread
.Sh SYNOPSIS
.In pthread.h
.In pthread_np.h
.Ft int
.Fn pthread_setaffinity_np "pthread_t tid" "size_t cpusetsize" "const cpu_set_t *mask"
.Ft int
.Fn pthread_getaffinity_np "pthread_t tid" "size_t cpusetsize" "cpu_set_t *mask"
.Sh DESCRIPTION
The
.Fn pthread_setaffinity_np
sets the CPU affinity mask of the thread identified by
.Fa tid
to the value specified by the
.Fa mask .
The
.Fa cpusetsize
is the length (in bytes) of the data pointed to by
.Fa mask .
Normally this argument would be specified as sizeof(cpu_set_t).
If the thread specified by the
.Fa tid
is the current thread,
it will be migrated immediately,
if necessary, otherwise it will be migrated by the scheduler later.
.Pp
A process created by
.Xr fork 2 ,
.Xr vfork 2 ,
and
.Xr rfork 2
inherits the calling thread's CPU affinity mask.
Same applies to the threads created by
.Xr lwp_create 2 ,
and the threads created by
.Xr pthread_create 3 .
.Pp
.Fn pthread_getaffinity_np
writes the affinity mask of the thread
identified by the
.Fa tid
to the cpu_set_t structure pointed to by
.Fa mask .
.Pp
The cpu_set_t data structure represents a set of CPUs.
See
.Xr sched_setaffinity 2
for details.
.Sh RETURN VALUES
If successful,
these functions will return zero.
Otherwise an error number will be returned to indicate the error.
.Sh ERRORS
The
.Fn pthread_setaffinity_np
and
.Fn pthread_getaffinity_np
will fail if:
.Bl -tag -width Er
.It Bq Er EFAULT
The
.Fa mask
parameter is outside the process's allocated address space.
.It Bq Er EINVAL
The
.Fa tid
parameter is invalid.
.It Bq Er ESRCH
The thread identified by the
.Fa tid
does not exist.
.It Bq Er EPERM
The
.Fa mask
does not contain a valid CPU.
.El
.Sh SEE ALSO
.Xr fork 2 ,
.Xr lwp_create 2 ,
.Xr lwp_getaffinity 2 ,
.Xr lwp_setaffinity 2 ,
.Xr rfork 2 ,
.Xr sched_getaffinity 2 ,
.Xr sched_setaffinity 2 ,
.Xr vfork 2 ,
.Xr pthread_create 3
.Sh HISTORY
The
.Fn pthread_setaffinity_np
and
.Fn pthread_getaffinity_np
functions first appeared in
.Dx 4.7 .
