Home | History | Annotate | Line # | Download | only in libpthread
pthread_misc.c revision 1.15.30.1
      1 /*	$NetBSD: pthread_misc.c,v 1.15.30.1 2020/04/08 14:07:15 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nathan J. Williams.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __RCSID("$NetBSD: pthread_misc.c,v 1.15.30.1 2020/04/08 14:07:15 martin Exp $");
     34 
     35 #include <errno.h>
     36 #include <string.h>
     37 #include <unistd.h>
     38 
     39 #include <sys/types.h>
     40 #include <sys/pset.h>
     41 #include <sys/signal.h>
     42 #include <sys/time.h>
     43 
     44 #include <lwp.h>
     45 #include <sched.h>
     46 
     47 #include "pthread.h"
     48 #include "pthread_int.h"
     49 #include "reentrant.h"
     50 
     51 int	pthread__sched_yield(void);
     52 
     53 int	_sys___sigprocmask14(int, const sigset_t *, sigset_t *);
     54 int	_sys_sched_yield(void);
     55 
     56 __strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
     57 __strong_alias(__sigprocmask14,pthread_sigmask)
     58 __strong_alias(__libc_thr_yield,pthread__sched_yield)
     59 
     60 int
     61 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
     62 {
     63 
     64 	pthread__error(EINVAL, "Invalid thread",
     65 	    thread->pt_magic == PT_MAGIC);
     66 
     67 	if (pthread__find(thread) != 0)
     68 		return ESRCH;
     69 
     70 	if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0)
     71 		return errno;
     72 
     73 	return 0;
     74 }
     75 
     76 int
     77 pthread_setschedparam(pthread_t thread, int policy,
     78     const struct sched_param *param)
     79 {
     80 	struct sched_param sp;
     81 
     82 	pthread__error(EINVAL, "Invalid thread",
     83 	    thread->pt_magic == PT_MAGIC);
     84 
     85 	if (pthread__find(thread) != 0)
     86 		return ESRCH;
     87 
     88 	memcpy(&sp, param, sizeof(struct sched_param));
     89 	if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0)
     90 		return errno;
     91 
     92 	return 0;
     93 }
     94 
     95 int
     96 pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
     97 {
     98 
     99 	pthread__error(EINVAL, "Invalid thread",
    100 	    thread->pt_magic == PT_MAGIC);
    101 
    102 	if (pthread__find(thread) != 0)
    103 		return ESRCH;
    104 
    105 	if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
    106 		return errno;
    107 
    108 	return 0;
    109 }
    110 
    111 int
    112 pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
    113 {
    114 
    115 	pthread__error(EINVAL, "Invalid thread",
    116 	    thread->pt_magic == PT_MAGIC);
    117 
    118 	if (pthread__find(thread) != 0)
    119 		return ESRCH;
    120 
    121 	if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
    122 		return errno;
    123 
    124 	return 0;
    125 }
    126 
    127 int
    128 pthread_setschedprio(pthread_t thread, int prio)
    129 {
    130 	struct sched_param sp;
    131 
    132 	pthread__error(EINVAL, "Invalid thread",
    133 	    thread->pt_magic == PT_MAGIC);
    134 
    135 	if (pthread__find(thread) != 0)
    136 		return ESRCH;
    137 
    138 	sp.sched_priority = prio;
    139 	if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0)
    140 		return errno;
    141 
    142 	return 0;
    143 }
    144 
    145 int
    146 pthread_kill(pthread_t thread, int sig)
    147 {
    148 
    149 	pthread__error(EINVAL, "Invalid thread",
    150 	    thread->pt_magic == PT_MAGIC);
    151 
    152 	if ((sig < 0) || (sig >= _NSIG))
    153 		return EINVAL;
    154 	if (pthread__find(thread) != 0)
    155 		return ESRCH;
    156 	if (_lwp_kill(thread->pt_lid, sig))
    157 		return errno;
    158 	return 0;
    159 }
    160 
    161 int
    162 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
    163 {
    164 	if (_sys___sigprocmask14(how, set, oset))
    165 		return errno;
    166 	return 0;
    167 }
    168 
    169 int
    170 pthread__sched_yield(void)
    171 {
    172 
    173 	if (__predict_false(__uselibcstub))
    174 		return __libc_thr_yield();
    175 
    176 	return _sys_sched_yield();
    177 }
    178