xref: /dragonfly/lib/libc/sys/sched_setaffinity.c (revision 35ca622f)
1907281d1SSepherosa Ziehau /*
2907281d1SSepherosa Ziehau  * Copyright (c) 2017 The DragonFly Project.  All rights reserved.
3907281d1SSepherosa Ziehau  *
4907281d1SSepherosa Ziehau  * This code is derived from software contributed to The DragonFly Project
5907281d1SSepherosa Ziehau  * by Sepherosa Ziehau <sepherosa@gmail.com>
6907281d1SSepherosa Ziehau  *
7907281d1SSepherosa Ziehau  * Redistribution and use in source and binary forms, with or without
8907281d1SSepherosa Ziehau  * modification, are permitted provided that the following conditions
9907281d1SSepherosa Ziehau  * are met:
10907281d1SSepherosa Ziehau  *
11907281d1SSepherosa Ziehau  * 1. Redistributions of source code must retain the above copyright
12907281d1SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer.
13907281d1SSepherosa Ziehau  * 2. Redistributions in binary form must reproduce the above copyright
14907281d1SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer in
15907281d1SSepherosa Ziehau  *    the documentation and/or other materials provided with the
16907281d1SSepherosa Ziehau  *    distribution.
17907281d1SSepherosa Ziehau  * 3. Neither the name of The DragonFly Project nor the names of its
18907281d1SSepherosa Ziehau  *    contributors may be used to endorse or promote products derived
19907281d1SSepherosa Ziehau  *    from this software without specific, prior written permission.
20907281d1SSepherosa Ziehau  *
21907281d1SSepherosa Ziehau  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22907281d1SSepherosa Ziehau  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23907281d1SSepherosa Ziehau  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24907281d1SSepherosa Ziehau  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25907281d1SSepherosa Ziehau  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26907281d1SSepherosa Ziehau  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27907281d1SSepherosa Ziehau  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28907281d1SSepherosa Ziehau  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29907281d1SSepherosa Ziehau  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30907281d1SSepherosa Ziehau  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31907281d1SSepherosa Ziehau  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32907281d1SSepherosa Ziehau  * SUCH DAMAGE.
33907281d1SSepherosa Ziehau  */
34907281d1SSepherosa Ziehau 
35*0d9899e7SSepherosa Ziehau #include <sys/lwp.h>
36907281d1SSepherosa Ziehau #include <sched.h>
37907281d1SSepherosa Ziehau #include <string.h>
38907281d1SSepherosa Ziehau 
39907281d1SSepherosa Ziehau int
sched_setaffinity(pid_t pid,size_t cpusetsize,const cpu_set_t * mask)40fe54de69SSepherosa Ziehau sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask)
41907281d1SSepherosa Ziehau {
42907281d1SSepherosa Ziehau 	size_t cplen = cpusetsize;
43907281d1SSepherosa Ziehau 	cpu_set_t mask1;
44907281d1SSepherosa Ziehau 
45907281d1SSepherosa Ziehau 	if (cplen > sizeof(mask1))
46907281d1SSepherosa Ziehau 		cplen = sizeof(mask1);
47907281d1SSepherosa Ziehau 	CPU_ZERO(&mask1);
48907281d1SSepherosa Ziehau 	memcpy(&mask1, mask, cplen);
49907281d1SSepherosa Ziehau 
50907281d1SSepherosa Ziehau 	/* Change affinity for all LWPs of the current process. */
51907281d1SSepherosa Ziehau 	return (lwp_setaffinity(pid, -1, &mask1));
52907281d1SSepherosa Ziehau }
53