blob: 04d374219dba83ede446dbe8d8cddec5e10c3124 (
plain) (
blame)
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
|
#include "precomp.h"
#include "timeslicer.h"
#define TIMER_SLICE 0x7816
TimeSlicer::TimeSlicer(int percent_cpu_usage/* =50 */, int slice_duration/* =-1 */) {
max_cpu_usage = MIN((float)percent_cpu_usage, 99.0f) / 100.0f;
duration = slice_duration;
started = 0;
slicecount = 0;
firstslicetime = -1;
}
TimeSlicer::~TimeSlicer() {
}
void TimeSlicer::startSlicer() {
if (started) return;
started = 1;
timerclient_setTimer(TIMER_SLICE, duration);
onSlicerStart();
timerclient_timerCallback(TIMER_SLICE);
}
void TimeSlicer::stopSlicer() {
if (!started) return;
started = 0;
timerclient_killTimer(TIMER_SLICE);
onSlicerStop();
firstslicetime = -1;
}
void TimeSlicer::onSlicerStop() {
}
void TimeSlicer::onSlicerStart() {
}
int TimeSlicer::isSlicerStarted() {
return started;
}
void TimeSlicer::timerclient_timerCallback(int id) {
if (id == TIMER_SLICE) {
DWORD now = Std::getTickCount();
runSlice(now, now + (int)((float)duration * max_cpu_usage));
} else
TimerClient::timerclient_timerCallback(id);
}
void TimeSlicer::runSlice(DWORD start, DWORD stopwhen) {
DWORD now = Std::getTickCount();
slicecount = 0;
onBeginSliceBatch();
if (slicecount == 0 && firstslicetime != -1) stopwhen = now + firstslicetime;
while (Std::getTickCount() < stopwhen) {
onSlice();
slicecount++;
if (!started) break; // got aborted
}
onEndSliceBatch();
}
|