1.\" $NetBSD: affinity.3,v 1.6 2010/07/09 20:58:38 wiz Exp $ 2.\" 3.\" Copyright (c) 2008 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Mindaugas Rasiukevicius <rmind at NetBSD org>. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd July 9, 2010 31.Dt AFFINITY 3 32.Os 33.Sh NAME 34.Nm pthread_setaffinity_np , 35.Nm pthread_getaffinity_np 36.Nd affinity of threads 37.Sh LIBRARY 38.Lb libpthread 39.Sh SYNOPSIS 40.In pthread.h 41.In sched.h 42.Ft int 43.Fn pthread_setaffinity_np "pthread_t thread" "size_t size" "cpuset_t *set" 44.Ft int 45.Fn pthread_getaffinity_np "pthread_t thread" "size_t size" "cpuset_t *set" 46.Sh DESCRIPTION 47Thread affinity allows to run the thread on specified CPU or CPUs only. 48.Pp 49The 50.Fn pthread_setaffinity_np 51function sets the affinity mask 52.Fa set 53for 54.Fa thread . 55At least one valid CPU must be set in the mask. 56.Pp 57The 58.Fn pthread_getaffinity_np 59function gets the affinity mask of 60.Fa thread 61into 62.Fa set . 63Note that 64.Fa set 65must be created and initialized using the 66.Xr cpuset 3 67functions. 68.Sh RETURN VALUES 69The 70.Fn pthread_setaffinity_np 71and 72.Fn pthread_getaffinity_np 73functions return 0 on success. 74Otherwise, an error number is returned to indicate the error. 75.Sh EXAMPLES 76An example of code fragment, which sets the affinity for the current 77thread to the CPU whose ID is 0: 78.Bd -literal 79 cpuset_t *cset; 80 pthread_t pth; 81 cpuid_t ci; 82 83 cset = cpuset_create(); 84 if (cset == NULL) { 85 err(EXIT_FAILURE, "cpuset_create"); 86 } 87 ci = 0; 88 cpuset_set(ci, cset); 89 90 pth = pthread_self(); 91 error = pthread_setaffinity_np(pth, cpuset_size(cset), cset); 92 if (error) { 93 ... 94 } 95 cpuset_destroy(cset); 96.Ed 97.Sh COMPATIBILITY 98Both functions are non-standard extensions. 99.Sh ERRORS 100Both functions may fail if: 101.Bl -tag -width Er 102.It Bq Er EINVAL 103The specified 104.Fa set 105was invalid. 106.It Bq Er EPERM 107The calling process lacks the appropriate privileges to perform 108the operation. 109.It Bq Er ESRCH 110No thread could be found corresponding to the one specified by 111.Fa thread . 112.El 113.Sh NOTES 114There is an alternative processor sets interface, see 115.Xr pset 3 . 116However, thread affinity and processor sets are mutually exclusive, 117hence mixing of these interfaces is prohibited. 118.Sh SEE ALSO 119.Xr cpuset 3 , 120.Xr pset 3 , 121.Xr pthread_getschedparam 3 , 122.Xr pthread_setschedparam 3 , 123.Xr sched 3 , 124.Xr schedctl 8 125