15e53a4f9SPedro F. Giffuni /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
457030e10SDavid Xu * Copyright (c) 2008, David Xu <davidxu@freebsd.org>
557030e10SDavid Xu * All rights reserved.
657030e10SDavid Xu *
757030e10SDavid Xu * Redistribution and use in source and binary forms, with or without
857030e10SDavid Xu * modification, are permitted provided that the following conditions
957030e10SDavid Xu * are met:
1057030e10SDavid Xu * 1. Redistributions of source code must retain the above copyright
1157030e10SDavid Xu * notice unmodified, this list of conditions, and the following
1257030e10SDavid Xu * disclaimer.
1357030e10SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright
1457030e10SDavid Xu * notice, this list of conditions and the following disclaimer in the
1557030e10SDavid Xu * documentation and/or other materials provided with the distribution.
1657030e10SDavid Xu *
1757030e10SDavid Xu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1857030e10SDavid Xu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1957030e10SDavid Xu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2057030e10SDavid Xu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2157030e10SDavid Xu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2257030e10SDavid Xu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2357030e10SDavid Xu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2457030e10SDavid Xu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2557030e10SDavid Xu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2657030e10SDavid Xu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2757030e10SDavid Xu */
2857030e10SDavid Xu
2957030e10SDavid Xu #include "namespace.h"
307abb97dcSDavid Xu #include <pthread_np.h>
3157030e10SDavid Xu #include <sys/param.h>
3257030e10SDavid Xu #include <sys/cpuset.h>
3357030e10SDavid Xu #include "un-namespace.h"
3457030e10SDavid Xu
3557030e10SDavid Xu #include "thr_private.h"
3657030e10SDavid Xu
3757030e10SDavid Xu __weak_reference(_pthread_getaffinity_np, pthread_getaffinity_np);
3857030e10SDavid Xu __weak_reference(_pthread_setaffinity_np, pthread_setaffinity_np);
3957030e10SDavid Xu
4057030e10SDavid Xu int
_pthread_setaffinity_np(pthread_t td,size_t cpusetsize,const cpuset_t * cpusetp)41d0aa4fd3SXin LI _pthread_setaffinity_np(pthread_t td, size_t cpusetsize, const cpuset_t *cpusetp)
4257030e10SDavid Xu {
4357030e10SDavid Xu struct pthread *curthread = _get_curthread();
4457030e10SDavid Xu lwpid_t tid;
4557030e10SDavid Xu int error;
4657030e10SDavid Xu
4786a06c60SDavid Xu if (td == curthread) {
4886a06c60SDavid Xu error = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
49d0aa4fd3SXin LI -1, cpusetsize, cpusetp);
5086a06c60SDavid Xu if (error == -1)
5186a06c60SDavid Xu error = errno;
52a9b764e2SDavid Xu } else if ((error = _thr_find_thread(curthread, td, 0)) == 0) {
5357030e10SDavid Xu tid = TID(td);
5457030e10SDavid Xu error = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, tid,
55d0aa4fd3SXin LI cpusetsize, cpusetp);
5657030e10SDavid Xu if (error == -1)
5757030e10SDavid Xu error = errno;
5857030e10SDavid Xu THR_THREAD_UNLOCK(curthread, td);
5986a06c60SDavid Xu }
6057030e10SDavid Xu return (error);
6157030e10SDavid Xu }
6257030e10SDavid Xu
6357030e10SDavid Xu int
_pthread_getaffinity_np(pthread_t td,size_t cpusetsize,cpuset_t * cpusetp)64d0aa4fd3SXin LI _pthread_getaffinity_np(pthread_t td, size_t cpusetsize, cpuset_t *cpusetp)
6557030e10SDavid Xu {
6686a06c60SDavid Xu struct pthread *curthread = _get_curthread();
6757030e10SDavid Xu lwpid_t tid;
6857030e10SDavid Xu int error;
6957030e10SDavid Xu
70a9b764e2SDavid Xu if (td == curthread) {
71a9b764e2SDavid Xu error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
727820a711SDavid Xu -1, cpusetsize, cpusetp);
73a9b764e2SDavid Xu if (error == -1)
74a9b764e2SDavid Xu error = errno;
75a9b764e2SDavid Xu } else if ((error = _thr_find_thread(curthread, td, 0)) == 0) {
7657030e10SDavid Xu tid = TID(td);
777820a711SDavid Xu error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, tid,
787820a711SDavid Xu cpusetsize, cpusetp);
7957030e10SDavid Xu if (error == -1)
8057030e10SDavid Xu error = errno;
81a9b764e2SDavid Xu THR_THREAD_UNLOCK(curthread, td);
82a9b764e2SDavid Xu }
8357030e10SDavid Xu return (error);
8457030e10SDavid Xu }
85