xref: /openbsd-src/lib/librthread/rthread_sched.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: rthread_sched.c,v 1.13 2015/04/29 06:01:37 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
32 pthread_getschedparam(pthread_t thread, int *policy,
33     struct sched_param *param)
34 {
35 	*policy = thread->attr.sched_policy;
36 	if (param)
37 		*param = thread->attr.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->attr.sched_policy = policy;
51 	if (param)
52 		thread->attr.sched_param = *param;
53 
54 	return (0);
55 }
56 
57 int
58 pthread_attr_getschedparam(const pthread_attr_t *attrp,
59     struct sched_param *param)
60 {
61 	*param = (*attrp)->sched_param;
62 
63 	return (0);
64 }
65 
66 int
67 pthread_attr_setschedparam(pthread_attr_t *attrp,
68     const struct sched_param *param)
69 {
70 	(*attrp)->sched_param = *param;
71 
72 	return (0);
73 }
74 
75 int
76 pthread_attr_getschedpolicy(const pthread_attr_t *attrp, int *policy)
77 {
78 	*policy = (*attrp)->sched_policy;
79 
80 	return (0);
81 }
82 
83 int
84 pthread_attr_setschedpolicy(pthread_attr_t *attrp, int policy)
85 {
86 	/* XXX return ENOTSUP for SCHED_{FIFO,RR}? */
87 	if (policy != SCHED_OTHER && policy != SCHED_FIFO &&
88 	    policy != SCHED_RR)
89 		return (EINVAL);
90 	(*attrp)->sched_policy = policy;
91 
92 	return (0);
93 }
94 
95 int
96 pthread_attr_getinheritsched(const pthread_attr_t *attrp, int *inherit)
97 {
98 	*inherit = (*attrp)->sched_inherit;
99 
100 	return (0);
101 }
102 
103 int
104 pthread_attr_setinheritsched(pthread_attr_t *attrp, int inherit)
105 {
106 	if (inherit != PTHREAD_INHERIT_SCHED &&
107 	    inherit != PTHREAD_EXPLICIT_SCHED)
108 		return (EINVAL);
109 	(*attrp)->sched_inherit = inherit;
110 
111 	return (0);
112 }
113 
114 int
115 pthread_getprio(pthread_t thread)
116 {
117 	return (thread->attr.sched_param.sched_priority);
118 }
119 
120 int
121 pthread_setprio(pthread_t thread, int priority)
122 {
123 	thread->attr.sched_param.sched_priority = priority;
124 
125 	return (0);
126 }
127 
128 void
129 pthread_yield(void)
130 {
131 	sched_yield();
132 }
133 
134