1.\" $NetBSD: affinity.3,v 1.4 2009/01/20 01:57:36 rmind 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 October 18, 2008 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 49.Fn pthread_setaffinity_np 50function sets the affinity mask 51.Fa set 52for 53.Fa thread . 54At least one valid CPU must be set in the mask. 55.Pp 56The 57.Fn pthread_getaffinity_np 58function gets the affinity mask of 59.Fa thread 60into 61.Fa set . 62.Pp 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 ERRORS 98The 99.Fn pthread_setaffinity_np 100and 101.Fn pthread_getaffinity_np 102functions fail if: 103.Bl -tag -width Er 104.It Bq Er EINVAL 105The specified 106.Fa set 107was invalid. 108.It Bq Er EPERM 109The calling process lacks the appropriate privileges to perform 110the operation. 111.It Bq Er ESRCH 112No thread could be found corresponding to the one specified by 113.Fa thread . 114.El 115.Sh NOTES 116There is an alternative processor sets interface, see 117.Xr pset 3 . 118However, thread affinity and processor sets are mutually exclusive, 119hence mixing of these interfaces is prohibited. 120.Sh SEE ALSO 121.Xr cpuset 3 , 122.Xr pset 3 , 123.Xr pthread_getschedparam 3 , 124.Xr pthread_setschedparam 3 , 125.Xr sched 3 , 126.Xr schedctl 8 127