summaryrefslogtreecommitdiff
path: root/win32_timer.c
diff options
context:
space:
mode:
authorPeter Fors <peter.fors@mindkiller.com>2025-10-16 04:19:32 +0200
committerPeter Fors <peter.fors@mindkiller.com>2025-10-16 04:19:32 +0200
commita4c261c6ee3940099e653a6f448dc952dfd5899f (patch)
tree7b14cfde56d735259f6e852a6d337228e00db0f5 /win32_timer.c
parentdcaf169691cfbb865241e96a4786af0862424701 (diff)
optimized, but bug with rasterdemos
Diffstat (limited to 'win32_timer.c')
-rw-r--r--win32_timer.c35
1 files changed, 17 insertions, 18 deletions
diff --git a/win32_timer.c b/win32_timer.c
index c89000f..250861b 100644
--- a/win32_timer.c
+++ b/win32_timer.c
@@ -18,8 +18,6 @@ struct timer_handle {
#ifdef TIMER_DEBUG
uint64_t last_wait_start_ns;
- uint32_t overshoot_log[1000000];
- uint32_t overshoot_index;
#endif
};
@@ -75,12 +73,19 @@ static DWORD WINAPI timer_thread_func(LPVOID arg) {
set_realtime_priority(&t->mmcss_handle);
while(t->running) {
+#ifdef TIMER_DEBUG
+ int64_t remaining_after_sleep_ns = -1;
+#endif
uint64_t now = qpc_now_ns(t->qpc_frequency);
if(now < t->next_deadline) {
uint64_t diff = t->next_deadline - now;
if(diff > SPIN_THRESHOLD_NS) {
timer_sleep(diff - SPIN_THRESHOLD_NS);
+#ifdef TIMER_DEBUG
+ now = qpc_now_ns(t->qpc_frequency);
+ remaining_after_sleep_ns = (int64_t)(t->next_deadline - now);
+#endif
}
while(qpc_now_ns(t->qpc_frequency) < t->next_deadline) {
_mm_pause();
@@ -92,9 +97,14 @@ static DWORD WINAPI timer_thread_func(LPVOID arg) {
#ifdef TIMER_DEBUG
if(t->last_wait_start_ns > 0) {
- uint64_t overshoot_ns = (now > t->next_deadline) ? (now - t->next_deadline) : 0;
- t->overshoot_log[t->overshoot_index % 1000000] = (uint32_t)overshoot_ns;
- t->overshoot_index++;
+ int64_t overshoot_ns = (int64_t)(now - t->next_deadline);
+ if(overshoot_ns < 0) overshoot_ns = 0;
+
+ if(remaining_after_sleep_ns >= 0) {
+ DEBUG_PRINT("[DEBUG] Woke up with %lld ns left. Overshoot: %5lld ns\n", remaining_after_sleep_ns, overshoot_ns);
+ } else {
+ DEBUG_PRINT("[DEBUG] No sleep. Overshoot: %lld ns\n", overshoot_ns);
+ }
}
t->last_wait_start_ns = now;
#endif
@@ -131,7 +141,6 @@ static struct timer_handle *timer_new(uint64_t interval_ns) {
#ifdef TIMER_DEBUG
t->last_wait_start_ns = 0;
- t->overshoot_index = 0;
#endif
t->event = CreateEvent(0, FALSE, FALSE, 0);
@@ -147,6 +156,8 @@ static uint32_t timer_wait(struct timer_handle *t) {
static void timer_destroy(struct timer_handle *t) {
t->running = 0;
+
+ SetEvent(t->event);
WaitForSingleObject(t->timer_thread, INFINITE);
CloseHandle(t->timer_thread);
CloseHandle(t->event);
@@ -155,17 +166,5 @@ static void timer_destroy(struct timer_handle *t) {
AvRevertMmThreadCharacteristics(t->mmcss_handle);
}
-#ifdef TIMER_DEBUG
- uint32_t threshold = 10000; // 10µs
- uint32_t overshoot_count = 0;
- for(uint32_t i = 0; i < t->overshoot_index && i < 1000000; i++) {
- if(t->overshoot_log[i] >= threshold) {
- DEBUG_PRINT("Frame %u: overshoot %u ns\n", i, t->overshoot_log[i]);
- overshoot_count++;
- }
- }
- DEBUG_PRINT("Total frames: %u, Overshoots >= %u ns: %u\n", t->overshoot_index, threshold, overshoot_count);
-#endif
-
free(t);
}