1*a5511fa9Sguenther /* $OpenBSD: rthread_sched.c,v 1.14 2017/09/05 02:40:54 guenther Exp $ */
21a251377Stedu /*
355aa0b8cStedu * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
41a251377Stedu * All Rights Reserved.
51a251377Stedu *
61a251377Stedu * Permission to use, copy, modify, and distribute this software for any
71a251377Stedu * purpose with or without fee is hereby granted, provided that the above
81a251377Stedu * copyright notice and this permission notice appear in all copies.
91a251377Stedu *
101a251377Stedu * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
111a251377Stedu * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121a251377Stedu * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
131a251377Stedu * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141a251377Stedu * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
151a251377Stedu * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
161a251377Stedu * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
171a251377Stedu */
181a251377Stedu /*
191a251377Stedu * scheduling routines
201a251377Stedu */
211a251377Stedu
221a251377Stedu #include <unistd.h>
231a251377Stedu #include <signal.h>
241a251377Stedu #include <errno.h>
251a251377Stedu
261a251377Stedu #include <pthread.h>
278af1ee89Sotto #include <pthread_np.h>
281a251377Stedu
291a251377Stedu #include "rthread.h"
301a251377Stedu
311a251377Stedu int
pthread_getschedparam(pthread_t thread,int * policy,struct sched_param * param)321a251377Stedu pthread_getschedparam(pthread_t thread, int *policy,
331a251377Stedu struct sched_param *param)
341a251377Stedu {
35*a5511fa9Sguenther if (!_threads_ready)
36*a5511fa9Sguenther _rthread_init();
37*a5511fa9Sguenther
38b33c17d3Sguenther *policy = thread->attr.sched_policy;
391a251377Stedu if (param)
40b33c17d3Sguenther *param = thread->attr.sched_param;
411a251377Stedu
421a251377Stedu return (0);
431a251377Stedu }
441a251377Stedu
451a251377Stedu int
pthread_setschedparam(pthread_t thread,int policy,const struct sched_param * param)461a251377Stedu pthread_setschedparam(pthread_t thread, int policy,
471a251377Stedu const struct sched_param *param)
481a251377Stedu {
49*a5511fa9Sguenther if (!_threads_ready)
50*a5511fa9Sguenther _rthread_init();
51*a5511fa9Sguenther
52e9783381Sguenther /* XXX return ENOTSUP for SCHED_{FIFO,RR}? */
53e9783381Sguenther if (policy != SCHED_OTHER && policy != SCHED_FIFO &&
54e9783381Sguenther policy != SCHED_RR)
55e9783381Sguenther return (EINVAL);
56b33c17d3Sguenther thread->attr.sched_policy = policy;
571a251377Stedu if (param)
58b33c17d3Sguenther thread->attr.sched_param = *param;
591a251377Stedu
601a251377Stedu return (0);
611a251377Stedu }
621a251377Stedu
631a251377Stedu int
pthread_attr_getschedparam(const pthread_attr_t * attrp,struct sched_param * param)64b33c17d3Sguenther pthread_attr_getschedparam(const pthread_attr_t *attrp,
65b33c17d3Sguenther struct sched_param *param)
661a251377Stedu {
671a251377Stedu *param = (*attrp)->sched_param;
681a251377Stedu
691a251377Stedu return (0);
701a251377Stedu }
711a251377Stedu
721a251377Stedu int
pthread_attr_setschedparam(pthread_attr_t * attrp,const struct sched_param * param)73b33c17d3Sguenther pthread_attr_setschedparam(pthread_attr_t *attrp,
74b33c17d3Sguenther const struct sched_param *param)
751a251377Stedu {
761a251377Stedu (*attrp)->sched_param = *param;
771a251377Stedu
781a251377Stedu return (0);
791a251377Stedu }
801a251377Stedu
811a251377Stedu int
pthread_attr_getschedpolicy(const pthread_attr_t * attrp,int * policy)821a251377Stedu pthread_attr_getschedpolicy(const pthread_attr_t *attrp, int *policy)
831a251377Stedu {
841a251377Stedu *policy = (*attrp)->sched_policy;
851a251377Stedu
861a251377Stedu return (0);
871a251377Stedu }
881a251377Stedu
891a251377Stedu int
pthread_attr_setschedpolicy(pthread_attr_t * attrp,int policy)901a251377Stedu pthread_attr_setschedpolicy(pthread_attr_t *attrp, int policy)
911a251377Stedu {
92e9783381Sguenther /* XXX return ENOTSUP for SCHED_{FIFO,RR}? */
93e9783381Sguenther if (policy != SCHED_OTHER && policy != SCHED_FIFO &&
94e9783381Sguenther policy != SCHED_RR)
95e9783381Sguenther return (EINVAL);
961a251377Stedu (*attrp)->sched_policy = policy;
971a251377Stedu
981a251377Stedu return (0);
991a251377Stedu }
1001a251377Stedu
1011a251377Stedu int
pthread_attr_getinheritsched(const pthread_attr_t * attrp,int * inherit)1021a251377Stedu pthread_attr_getinheritsched(const pthread_attr_t *attrp, int *inherit)
1031a251377Stedu {
1041a251377Stedu *inherit = (*attrp)->sched_inherit;
1051a251377Stedu
1061a251377Stedu return (0);
1071a251377Stedu }
1081a251377Stedu
1091a251377Stedu int
pthread_attr_setinheritsched(pthread_attr_t * attrp,int inherit)1101a251377Stedu pthread_attr_setinheritsched(pthread_attr_t *attrp, int inherit)
1111a251377Stedu {
112e9783381Sguenther if (inherit != PTHREAD_INHERIT_SCHED &&
113e9783381Sguenther inherit != PTHREAD_EXPLICIT_SCHED)
114e9783381Sguenther return (EINVAL);
1151a251377Stedu (*attrp)->sched_inherit = inherit;
1161a251377Stedu
1171a251377Stedu return (0);
1181a251377Stedu }
1191a251377Stedu
1201a251377Stedu int
pthread_getprio(pthread_t thread)1211a251377Stedu pthread_getprio(pthread_t thread)
1221a251377Stedu {
123*a5511fa9Sguenther if (!_threads_ready)
124*a5511fa9Sguenther _rthread_init();
125*a5511fa9Sguenther
126b33c17d3Sguenther return (thread->attr.sched_param.sched_priority);
1271a251377Stedu }
1281a251377Stedu
1291a251377Stedu int
pthread_setprio(pthread_t thread,int priority)1301a251377Stedu pthread_setprio(pthread_t thread, int priority)
1311a251377Stedu {
132*a5511fa9Sguenther if (!_threads_ready)
133*a5511fa9Sguenther _rthread_init();
134*a5511fa9Sguenther
135b33c17d3Sguenther thread->attr.sched_param.sched_priority = priority;
1361a251377Stedu
1371a251377Stedu return (0);
1381a251377Stedu }
1391a251377Stedu
1401a251377Stedu void
pthread_yield(void)1411a251377Stedu pthread_yield(void)
1421a251377Stedu {
14383b666cfStedu sched_yield();
1441a251377Stedu }
1458af1ee89Sotto
146