thr_affinity.c revision 285830
154359Sroberto/* 254359Sroberto * Copyright (c) 2008, David Xu <davidxu@freebsd.org> 354359Sroberto * All rights reserved. 454359Sroberto * 554359Sroberto * Redistribution and use in source and binary forms, with or without 654359Sroberto * modification, are permitted provided that the following conditions 754359Sroberto * are met: 854359Sroberto * 1. Redistributions of source code must retain the above copyright 954359Sroberto * notice unmodified, this list of conditions, and the following 1054359Sroberto * disclaimer. 1154359Sroberto * 2. Redistributions in binary form must reproduce the above copyright 1254359Sroberto * notice, this list of conditions and the following disclaimer in the 1354359Sroberto * documentation and/or other materials provided with the distribution. 1454359Sroberto * 1554359Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1654359Sroberto * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1754359Sroberto * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1854359Sroberto * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1954359Sroberto * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2054359Sroberto * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2154359Sroberto * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2254359Sroberto * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2354359Sroberto * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2454359Sroberto * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25290000Sglebius * 2654359Sroberto * $FreeBSD: releng/10.2/lib/libthr/thread/thr_affinity.c 212552 2010-09-13 11:58:42Z davidxu $ 2754359Sroberto * 2854359Sroberto */ 2954359Sroberto 3054359Sroberto#include "namespace.h" 31290000Sglebius#include <pthread_np.h> 3254359Sroberto#include <sys/param.h> 3354359Sroberto#include <sys/cpuset.h> 3454359Sroberto#include "un-namespace.h" 3554359Sroberto 3654359Sroberto#include "thr_private.h" 3754359Sroberto 3854359Sroberto__weak_reference(_pthread_getaffinity_np, pthread_getaffinity_np); 3954359Sroberto__weak_reference(_pthread_setaffinity_np, pthread_setaffinity_np); 4054359Sroberto 4154359Srobertoint 42290000Sglebius_pthread_setaffinity_np(pthread_t td, size_t cpusetsize, const cpuset_t *cpusetp) 4354359Sroberto{ 4454359Sroberto struct pthread *curthread = _get_curthread(); 4554359Sroberto lwpid_t tid; 4654359Sroberto int error; 4754359Sroberto 48290000Sglebius if (td == curthread) { 4954359Sroberto error = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, 5054359Sroberto -1, cpusetsize, cpusetp); 5154359Sroberto if (error == -1) 52290000Sglebius error = errno; 53290000Sglebius } else if ((error = _thr_find_thread(curthread, td, 0)) == 0) { 54290000Sglebius tid = TID(td); 5554359Sroberto error = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, tid, 5654359Sroberto cpusetsize, cpusetp); 5754359Sroberto if (error == -1) 5854359Sroberto error = errno; 5954359Sroberto THR_THREAD_UNLOCK(curthread, td); 6054359Sroberto } 6154359Sroberto return (error); 6254359Sroberto} 6354359Sroberto 6454359Srobertoint 6554359Sroberto_pthread_getaffinity_np(pthread_t td, size_t cpusetsize, cpuset_t *cpusetp) 6654359Sroberto{ 6754359Sroberto struct pthread *curthread = _get_curthread(); 6854359Sroberto lwpid_t tid; 6954359Sroberto int error; 7054359Sroberto 7154359Sroberto if (td == curthread) { 7254359Sroberto error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, 7354359Sroberto -1, cpusetsize, cpusetp); 7454359Sroberto if (error == -1) 75 error = errno; 76 } else if ((error = _thr_find_thread(curthread, td, 0)) == 0) { 77 tid = TID(td); 78 error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, tid, 79 cpusetsize, cpusetp); 80 if (error == -1) 81 error = errno; 82 THR_THREAD_UNLOCK(curthread, td); 83 } 84 return (error); 85} 86