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