-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathkscheduler.cpp
245 lines (207 loc) · 7.06 KB
/
kscheduler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (C) 2012-2025 The BoxedWine Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "boxedwine.h"
#include "../x11/x11.h"
#include "knativeaudio.h"
#ifdef BOXEDWINE_MULTI_THREADED
static KList<KTimerCallback*> timers;
static BOXEDWINE_MUTEX timerMutex;
void runTimers() {
BOXEDWINE_CRITICAL_SECTION_WITH_MUTEX(timerMutex);
U32 millies = KSystem::getMilliesSinceStart();
timers.for_each([millies](KListNode<KTimerCallback*>* node) {
KTimerCallback* timer = node->data;
if (timer->millies <= millies) {
if (timer->run()) {
timer->node.remove();
}
}
});
}
U32 getNextTimer() {
BOXEDWINE_CRITICAL_SECTION_WITH_MUTEX(timerMutex);
U32 millies = KSystem::getMilliesSinceStart();
U32 result = 0xFFFFFFFF;
timers.for_each([millies, &result](KListNode<KTimerCallback*>* node) {
KTimerCallback* timer = node->data;
U32 next = 0;
if (timer->millies <= millies) {
next = 0;
}
else {
next = timer->millies - millies;
}
if (next < result) {
result = next;
}
});
return result;
}
void addTimer(KTimerCallback* timer) {
BOXEDWINE_CRITICAL_SECTION_WITH_MUTEX(timerMutex);
timers.addToBack(&timer->node);
timer->active = true;
}
void removeTimer(KTimerCallback* timer) {
BOXEDWINE_CRITICAL_SECTION_WITH_MUTEX(timerMutex);
timer->node.remove();
timer->active = false;
}
#else
#include "kscheduler.h"
#include "knativesystem.h"
#include <stdio.h>
//#define LOG_SCHEDULER
KList<KThread*> scheduledThreads;
KList<KThread*> waitThreads;
KList<KTimerCallback*> timers;
void addTimer(KTimerCallback* timer) {
timers.addToBack(&timer->node);
timer->active = true;
}
void removeTimer(KTimerCallback* timer) {
timer->node.remove();
timer->active = false;
}
void scheduleThread(KThread* thread) {
#ifdef _DEBUG
if (thread->waitingCond) {
kpanic("can't schedule a thread that is waiting");
}
if (thread->condTimer.active) {
kpanic("can't schedule a thread that is waiting on a timer");
}
#endif
thread->cpu->yield = false;
scheduledThreads.addToFront(&thread->scheduledThreadNode);
}
void unscheduleThread(KThread* thread) {
thread->scheduledThreadNode.remove();
thread->cpu->yield = true;
}
void terminateOtherThread(const KProcessPtr& process, U32 threadId) {
KThread* thread = process->getThreadById(threadId);
if (thread) {
unscheduleThread(thread);
delete thread;
}
}
void terminateCurrentThread(KThread* thread) {
thread->terminating = true;
unscheduleThread(thread);
}
S32 contextTime = 100000;
S32 contextTimeRemaining = 100000;
int count;
extern struct Block emptyBlock;
void runThreadSlice(KThread* thread) {
CPU* cpu;
cpu = thread->cpu;
cpu->blockInstructionCount = 0;
cpu->yield = false;
cpu->nextBlock = cpu->getNextBlock(); // another thread that just ran could have modified this
try {
do {
cpu->run();
} while ((int)cpu->blockInstructionCount < contextTimeRemaining && !cpu->yield);
} catch (...) {
cpu->nextBlock = nullptr;
}
cpu->instructionCount+=cpu->blockInstructionCount;
}
void runTimers() {
U32 millies = KSystem::getMilliesSinceStart();
timers.for_each([millies] (KListNode<KTimerCallback*>* node) {
KTimerCallback* timer = node->data;
if (timer->millies<=millies) {
if (timer->run()) {
timer->node.remove();
}
}
});
}
extern U64 sysCallTime;
U64 elapsedTimeMIPS;
U64 elapsedInstructionsMIPS;
bool runSlice() {
runTimers();
if (scheduledThreads.isEmpty())
return false;
XServer* server = XServer::getServer(true);
if (server) {
server->isDisplayDirty = true; // a bit of a hack, sometimes popups in Basstour get missed and don't draw
server->draw();
}
U64 elapsedTime = 0;
contextTimeRemaining = contextTime;
while (!scheduledThreads.isEmpty() && elapsedTime<9000) {
U64 threadStartTime = KSystem::getMicroCounter();
KListNode<KThread*>* node = scheduledThreads.front();
KThread* currentThread = (KThread*)node->data;
KNativeSystem::scheduledNewThread(currentThread);
sysCallTime = 0;
ChangeThread c(currentThread);
static U64 rdtsc;
currentThread->cpu->instructionCount = rdtsc;
runThreadSlice(currentThread);
rdtsc = currentThread->cpu->instructionCount;
U64 threadEndTime = KSystem::getMicroCounter();
U64 diff = threadEndTime - threadStartTime;
elapsedTime+=diff;
elapsedTimeMIPS+=diff;
elapsedInstructionsMIPS+=currentThread->cpu->blockInstructionCount;
currentThread->userTime+=diff-sysCallTime;
currentThread->kernelTime+=sysCallTime;
if (currentThread->cpu->blockInstructionCount) {
contextTimeRemaining = (U32)(contextTime * (10000-elapsedTime) / 10000);
}
// this is how we signal to delete the current thread, since we can't delete it in the syscall, maybe we should use smart_ptr for threads
if (currentThread->terminating) {
delete currentThread;
} else if (!currentThread->waitingCond) {
// make sure we are behind any threads that were recently scheduled
node->remove();
scheduledThreads.addToBack(node);
}
}
if (!scheduledThreads.isEmpty()) {
if (elapsedTime>11000) {
if (contextTime>100000)
contextTime-=20000;
} else if (elapsedTime<9500) {
contextTime+=20000;
}
}
//klog("ran slice in %dus %d", (U32)elapsedTime, contextTime);
return true;
}
U32 getMIPS() {
U32 result = 0;
if (elapsedTimeMIPS) {
result = (U32)(elapsedInstructionsMIPS/elapsedTimeMIPS);
elapsedTimeMIPS = 0;
elapsedInstructionsMIPS = 0;
}
return result;
}
void waitForProcessToFinish(const KProcessPtr& process, KThread* thread) {
while (!process->terminated) {
runThreadSlice(thread);
}
}
#endif