Home | History | Annotate | Line # | Download | only in libpthread
pthread_misc.c revision 1.7.4.1
      1 /*	$NetBSD: pthread_misc.c,v 1.7.4.1 2008/05/18 12:30:40 yamt 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.7.4.1 2008/05/18 12:30:40 yamt 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 
     50 int	pthread__sched_yield(void);
     51 
     52 int	_sys___sigprocmask14(int, const sigset_t *, sigset_t *);
     53 int	_sys_nanosleep(const struct timespec *, struct timespec *);
     54 int	_sys_sched_yield(void);
     55 
     56 __strong_alias(_nanosleep, nanosleep)
     57 __strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
     58 __strong_alias(__sigprocmask14,pthread_sigmask)
     59 __strong_alias(__libc_thr_yield,pthread__sched_yield)
     60 
     61 int
     62 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
     63 {
     64 
     65 	if (pthread__find(thread) != 0)
     66 		return ESRCH;
     67 
     68 	if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0)
     69 		return errno;
     70 
     71 	return 0;
     72 }
     73 
     74 int
     75 pthread_setschedparam(pthread_t thread, int policy,
     76     const struct sched_param *param)
     77 {
     78 	struct sched_param sp;
     79 
     80 	if (pthread__find(thread) != 0)
     81 		return ESRCH;
     82 
     83 	memcpy(&sp, param, sizeof(struct sched_param));
     84 	if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0)
     85 		return errno;
     86 
     87 	return 0;
     88 }
     89 
     90 int
     91 pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
     92 {
     93 
     94 	if (pthread__find(thread) != 0)
     95 		return ESRCH;
     96 
     97 	if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
     98 		return errno;
     99 
    100 	return 0;
    101 }
    102 
    103 int
    104 pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
    105 {
    106 
    107 	if (pthread__find(thread) != 0)
    108 		return ESRCH;
    109 
    110 	if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
    111 		return errno;
    112 
    113 	return 0;
    114 }
    115 
    116 int
    117 pthread_setschedprio(pthread_t thread, int prio)
    118 {
    119 	struct sched_param sp;
    120 
    121 	if (pthread__find(thread) != 0)
    122 		return ESRCH;
    123 
    124 	sp.sched_priority = prio;
    125 	if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0)
    126 		return errno;
    127 
    128 	return 0;
    129 }
    130 
    131 int
    132 pthread_kill(pthread_t thread, int sig)
    133 {
    134 
    135 	if ((sig < 0) || (sig >= _NSIG))
    136 		return EINVAL;
    137 	if (pthread__find(thread) != 0)
    138 		return ESRCH;
    139 
    140 	return _lwp_kill(thread->pt_lid, sig);
    141 }
    142 
    143 int
    144 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
    145 {
    146 
    147 	return _sys___sigprocmask14(how, set, oset);
    148 }
    149 
    150 int
    151 nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
    152 {
    153 
    154 	/*
    155 	 * For now, just nanosleep.  In the future, maybe pass a ucontext_t
    156 	 * to _lwp_nanosleep() and allow it to recycle our kernel stack.
    157 	 */
    158 	return  _sys_nanosleep(rqtp, rmtp);
    159 }
    160 
    161 int
    162 pthread__sched_yield(void)
    163 {
    164 	pthread_t self;
    165 	int error;
    166 
    167 	self = pthread__self();
    168 
    169 	/* Memory barrier for unlocked mutex release. */
    170 	membar_producer();
    171 	self->pt_blocking++;
    172 	error = _sys_sched_yield();
    173 	self->pt_blocking--;
    174 	membar_sync();
    175 
    176 	return error;
    177 }
    178