1 /* $OpenBSD: rthread_sched.c,v 1.14 2017/09/05 02:40:54 guenther 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
pthread_getschedparam(pthread_t thread,int * policy,struct sched_param * param)32 pthread_getschedparam(pthread_t thread, int *policy,
33 struct sched_param *param)
34 {
35 if (!_threads_ready)
36 _rthread_init();
37
38 *policy = thread->attr.sched_policy;
39 if (param)
40 *param = thread->attr.sched_param;
41
42 return (0);
43 }
44
45 int
pthread_setschedparam(pthread_t thread,int policy,const struct sched_param * param)46 pthread_setschedparam(pthread_t thread, int policy,
47 const struct sched_param *param)
48 {
49 if (!_threads_ready)
50 _rthread_init();
51
52 /* XXX return ENOTSUP for SCHED_{FIFO,RR}? */
53 if (policy != SCHED_OTHER && policy != SCHED_FIFO &&
54 policy != SCHED_RR)
55 return (EINVAL);
56 thread->attr.sched_policy = policy;
57 if (param)
58 thread->attr.sched_param = *param;
59
60 return (0);
61 }
62
63 int
pthread_attr_getschedparam(const pthread_attr_t * attrp,struct sched_param * param)64 pthread_attr_getschedparam(const pthread_attr_t *attrp,
65 struct sched_param *param)
66 {
67 *param = (*attrp)->sched_param;
68
69 return (0);
70 }
71
72 int
pthread_attr_setschedparam(pthread_attr_t * attrp,const struct sched_param * param)73 pthread_attr_setschedparam(pthread_attr_t *attrp,
74 const struct sched_param *param)
75 {
76 (*attrp)->sched_param = *param;
77
78 return (0);
79 }
80
81 int
pthread_attr_getschedpolicy(const pthread_attr_t * attrp,int * policy)82 pthread_attr_getschedpolicy(const pthread_attr_t *attrp, int *policy)
83 {
84 *policy = (*attrp)->sched_policy;
85
86 return (0);
87 }
88
89 int
pthread_attr_setschedpolicy(pthread_attr_t * attrp,int policy)90 pthread_attr_setschedpolicy(pthread_attr_t *attrp, int policy)
91 {
92 /* XXX return ENOTSUP for SCHED_{FIFO,RR}? */
93 if (policy != SCHED_OTHER && policy != SCHED_FIFO &&
94 policy != SCHED_RR)
95 return (EINVAL);
96 (*attrp)->sched_policy = policy;
97
98 return (0);
99 }
100
101 int
pthread_attr_getinheritsched(const pthread_attr_t * attrp,int * inherit)102 pthread_attr_getinheritsched(const pthread_attr_t *attrp, int *inherit)
103 {
104 *inherit = (*attrp)->sched_inherit;
105
106 return (0);
107 }
108
109 int
pthread_attr_setinheritsched(pthread_attr_t * attrp,int inherit)110 pthread_attr_setinheritsched(pthread_attr_t *attrp, int inherit)
111 {
112 if (inherit != PTHREAD_INHERIT_SCHED &&
113 inherit != PTHREAD_EXPLICIT_SCHED)
114 return (EINVAL);
115 (*attrp)->sched_inherit = inherit;
116
117 return (0);
118 }
119
120 int
pthread_getprio(pthread_t thread)121 pthread_getprio(pthread_t thread)
122 {
123 if (!_threads_ready)
124 _rthread_init();
125
126 return (thread->attr.sched_param.sched_priority);
127 }
128
129 int
pthread_setprio(pthread_t thread,int priority)130 pthread_setprio(pthread_t thread, int priority)
131 {
132 if (!_threads_ready)
133 _rthread_init();
134
135 thread->attr.sched_param.sched_priority = priority;
136
137 return (0);
138 }
139
140 void
pthread_yield(void)141 pthread_yield(void)
142 {
143 sched_yield();
144 }
145
146