1 /*
2 Copyright (c) 2011-2016 mingw-w64 project
3
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <windows.h>
24 #include <stdio.h>
25 #include "pthread.h"
26 #include "thread.h"
27
28 #include "misc.h"
29
sched_get_priority_min(int pol)30 int sched_get_priority_min(int pol)
31 {
32 if (pol < SCHED_MIN || pol > SCHED_MAX) {
33 errno = EINVAL;
34 return -1;
35 }
36
37 return THREAD_PRIORITY_IDLE;
38 }
39
sched_get_priority_max(int pol)40 int sched_get_priority_max(int pol)
41 {
42 if (pol < SCHED_MIN || pol > SCHED_MAX) {
43 errno = EINVAL;
44 return -1;
45 }
46
47 return THREAD_PRIORITY_TIME_CRITICAL;
48 }
49
pthread_attr_setschedparam(pthread_attr_t * attr,const struct sched_param * p)50 int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *p)
51 {
52 int r = 0;
53
54 if (attr == NULL || p == NULL) {
55 return EINVAL;
56 }
57 memcpy(&attr->param, p, sizeof (*p));
58 return r;
59 }
60
pthread_attr_getschedparam(const pthread_attr_t * attr,struct sched_param * p)61 int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *p)
62 {
63 int r = 0;
64
65 if (attr == NULL || p == NULL) {
66 return EINVAL;
67 }
68 memcpy(p, &attr->param, sizeof (*p));
69 return r;
70 }
71
pthread_attr_setschedpolicy(pthread_attr_t * attr,int pol)72 int pthread_attr_setschedpolicy (pthread_attr_t *attr, int pol)
73 {
74 if (!attr || pol < SCHED_MIN || pol > SCHED_MAX)
75 return EINVAL;
76 if (pol != SCHED_OTHER)
77 return ENOTSUP;
78 return 0;
79 }
80
pthread_attr_getschedpolicy(const pthread_attr_t * attr,int * pol)81 int pthread_attr_getschedpolicy (const pthread_attr_t *attr, int *pol)
82 {
83 if (!attr || !pol)
84 return EINVAL;
85 *pol = SCHED_OTHER;
86 return 0;
87 }
88
pthread_check(pthread_t t)89 static int pthread_check(pthread_t t)
90 {
91 struct _pthread_v *pv;
92 DWORD dwFlags;
93 if (!t)
94 return ESRCH;
95 pv = __pth_gpointer_locked (t);
96 if (!(pv->h) || pv->h == INVALID_HANDLE_VALUE)
97 {
98 if (pv->ended == 0)
99 return 0;
100 return ESRCH;
101 }
102 else if ((!GetHandleInformation(pv->h, &dwFlags) && pv->ended))
103 return ESRCH;
104 return 0;
105 }
106
pthread_getschedparam(pthread_t t,int * pol,struct sched_param * p)107 int pthread_getschedparam(pthread_t t, int *pol, struct sched_param *p)
108 {
109 int r;
110 //if (!t)
111 // t = pthread_self();
112
113 if ((r = pthread_check(t)) != 0)
114 {
115 return r;
116 }
117
118 if (!p || !pol)
119 {
120 return EINVAL;
121 }
122 *pol = __pth_gpointer_locked (t)->sched_pol;
123 p->sched_priority = __pth_gpointer_locked (t)->sched.sched_priority;
124
125 return 0;
126 }
127
pthread_setschedparam(pthread_t t,int pol,const struct sched_param * p)128 int pthread_setschedparam(pthread_t t, int pol, const struct sched_param *p)
129 {
130 struct _pthread_v *pv;
131 int r, pr = 0;
132 //if (!t.p) t = pthread_self();
133
134 if ((r = pthread_check(t)) != 0)
135 return r;
136
137 if (pol < SCHED_MIN || pol > SCHED_MAX || p == NULL)
138 return EINVAL;
139 if (pol != SCHED_OTHER)
140 return ENOTSUP;
141 pr = p->sched_priority;
142 if (pr < sched_get_priority_min(pol) || pr > sched_get_priority_max(pol))
143 return EINVAL;
144
145 /* See msdn: there are actually 7 priorities:
146 THREAD_PRIORITY_IDLE - -15
147 THREAD_PRIORITY_LOWEST -2
148 THREAD_PRIORITY_BELOW_NORMAL -1
149 THREAD_PRIORITY_NORMAL 0
150 THREAD_PRIORITY_ABOVE_NORMAL 1
151 THREAD_PRIORITY_HIGHEST 2
152 THREAD_PRIORITY_TIME_CRITICAL 15
153 */
154 if (pr <= THREAD_PRIORITY_IDLE) {
155 pr = THREAD_PRIORITY_IDLE;
156 } else if (pr <= THREAD_PRIORITY_LOWEST) {
157 pr = THREAD_PRIORITY_LOWEST;
158 } else if (pr >= THREAD_PRIORITY_TIME_CRITICAL) {
159 pr = THREAD_PRIORITY_TIME_CRITICAL;
160 } else if (pr >= THREAD_PRIORITY_HIGHEST) {
161 pr = THREAD_PRIORITY_HIGHEST;
162 }
163 pv = __pth_gpointer_locked (t);
164 if (SetThreadPriority(pv->h, pr)) {
165 pv->sched_pol = pol;
166 pv->sched.sched_priority = p->sched_priority;
167 } else
168 r = EINVAL;
169 return r;
170 }
171
sched_getscheduler(pid_t pid)172 int sched_getscheduler(pid_t pid)
173 {
174 if (pid != 0)
175 {
176 HANDLE h = NULL;
177 int selfPid = (int) GetCurrentProcessId ();
178
179 if (pid != (pid_t) selfPid && (h = OpenProcess (PROCESS_QUERY_INFORMATION, 0, (DWORD) pid)) == NULL)
180 {
181 errno = (GetLastError () == (0xFF & ERROR_ACCESS_DENIED)) ? EPERM : ESRCH;
182 return -1;
183 }
184 if (h)
185 CloseHandle (h);
186 }
187 return SCHED_OTHER;
188 }
189
sched_setscheduler(pid_t pid,int pol,const struct sched_param * param)190 int sched_setscheduler(pid_t pid, int pol, const struct sched_param *param)
191 {
192 if (!param)
193 {
194 errno = EINVAL;
195 return -1;
196 }
197 if (pid != 0)
198 {
199 HANDLE h = NULL;
200 int selfPid = (int) GetCurrentProcessId ();
201
202 if (pid != (pid_t) selfPid && (h = OpenProcess (PROCESS_SET_INFORMATION, 0, (DWORD) pid)) == NULL)
203 {
204 errno = (GetLastError () == (0xFF & ERROR_ACCESS_DENIED)) ? EPERM : ESRCH;
205 return -1;
206 }
207 if (h)
208 CloseHandle (h);
209 }
210
211 if (pol != SCHED_OTHER)
212 {
213 errno = ENOSYS;
214 return -1;
215 }
216 return SCHED_OTHER;
217 }
218
sched_yield(void)219 int sched_yield(void)
220 {
221 Sleep(0);
222 return 0;
223 }
224