xref: /openbsd-src/lib/librthread/rthread_sched.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: rthread_sched.c,v 1.12 2012/03/22 15:26:04 kurt Exp $ */
2 /*
3  * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 /*
19  * scheduling routines
20  */
21 
22 #include <unistd.h>
23 #include <signal.h>
24 #include <errno.h>
25 
26 #include <pthread.h>
27 #include <pthread_np.h>
28 
29 #include "rthread.h"
30 
31 int
32 pthread_getschedparam(pthread_t thread, int *policy,
33     struct sched_param *param)
34 {
35 	*policy = thread->sched_policy;
36 	if (param)
37 		*param = thread->sched_param;
38 
39 	return (0);
40 }
41 
42 int
43 pthread_setschedparam(pthread_t thread, int policy,
44     const struct sched_param *param)
45 {
46 	/* XXX return ENOTSUP for SCHED_{FIFO,RR}? */
47 	if (policy != SCHED_OTHER && policy != SCHED_FIFO &&
48 	    policy != SCHED_RR)
49 		return (EINVAL);
50 	thread->sched_policy = policy;
51 	if (param)
52 		thread->sched_param = *param;
53 
54 	return (0);
55 }
56 
57 int
58 pthread_attr_getschedparam(const pthread_attr_t *attrp, struct sched_param *param)
59 {
60 	*param = (*attrp)->sched_param;
61 
62 	return (0);
63 }
64 
65 int
66 pthread_attr_setschedparam(pthread_attr_t *attrp, const struct sched_param *param)
67 {
68 	(*attrp)->sched_param = *param;
69 
70 	return (0);
71 }
72 
73 int
74 pthread_attr_getschedpolicy(const pthread_attr_t *attrp, int *policy)
75 {
76 	*policy = (*attrp)->sched_policy;
77 
78 	return (0);
79 }
80 
81 int
82 pthread_attr_setschedpolicy(pthread_attr_t *attrp, int policy)
83 {
84 	/* XXX return ENOTSUP for SCHED_{FIFO,RR}? */
85 	if (policy != SCHED_OTHER && policy != SCHED_FIFO &&
86 	    policy != SCHED_RR)
87 		return (EINVAL);
88 	(*attrp)->sched_policy = policy;
89 
90 	return (0);
91 }
92 
93 int
94 pthread_attr_getinheritsched(const pthread_attr_t *attrp, int *inherit)
95 {
96 	*inherit = (*attrp)->sched_inherit;
97 
98 	return (0);
99 }
100 
101 int
102 pthread_attr_setinheritsched(pthread_attr_t *attrp, int inherit)
103 {
104 	if (inherit != PTHREAD_INHERIT_SCHED &&
105 	    inherit != PTHREAD_EXPLICIT_SCHED)
106 		return (EINVAL);
107 	(*attrp)->sched_inherit = inherit;
108 
109 	return (0);
110 }
111 
112 int
113 pthread_getprio(pthread_t thread)
114 {
115 	return (thread->sched_param.sched_priority);
116 }
117 
118 int
119 pthread_setprio(pthread_t thread, int priority)
120 {
121 	thread->sched_param.sched_priority = priority;
122 
123 	return (0);
124 }
125 
126 void
127 pthread_yield(void)
128 {
129 	sched_yield();
130 }
131 
132