milsko/src/abstract/time.c

79 lines
1.4 KiB
C
Raw Normal View History

2026-01-09 14:45:12 -07:00
#include <Mw/Milsko.h>
#if defined(_WIN32)
long MwTimeGetTick(void) {
2026-01-09 14:45:12 -07:00
return GetTickCount();
}
void MwTimeSleep(int ms) {
Sleep(ms);
}
2026-01-09 14:43:44 -07:00
#elif defined(__APPLE__)
long MwTimeGetTick(void) {
struct timespec ts;
long n = 0;
2026-01-09 14:45:12 -07:00
clock_serv_t cclock;
2026-01-09 14:43:44 -07:00
mach_timespec_t mts;
2026-01-09 14:45:12 -07:00
2026-01-09 14:43:44 -07:00
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
2026-01-09 14:45:12 -07:00
2026-01-09 14:43:44 -07:00
n += ts.tv_nsec / 1000 / 1000;
n += ts.tv_sec * 1000;
return n;
}
void MwTimeSleep(int ms) {
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000 * 1000;
2026-01-09 14:43:44 -07:00
nanosleep(&ts, NULL);
}
2026-04-15 20:21:20 -07:00
#elif defined(CLASSIC_MAC_OS)
// static TMTask tm;
// static int timer;
// void timerUpdateFunc(void) {
// timer += 1;
// PrimeTime((QElemPtr)&tm, 1);
// };
2026-04-15 20:21:20 -07:00
long MwTimeGetTick(void) {
UnsignedWide h;
Microseconds(&h);
2026-04-15 22:20:13 -07:00
return h.lo;
2026-04-15 20:21:20 -07:00
}
void MwTimeSleep(int ms) {
// tm.tmAddr = NewTimerProc(timerUpdateFunc);
// tm.tmWakeUp = 0;
// tm.tmReserved = 0;
2026-04-15 20:21:20 -07:00
// InsTime((QElemPtr)&tm);
// PrimeTime((QElemPtr)&tm, 1);
2026-04-15 22:20:13 -07:00
// DisposeTimerUPP(tm.tmAddr);
2026-04-15 20:21:20 -07:00
}
2026-01-09 14:43:44 -07:00
#elif defined(__unix__)
long MwTimeGetTick(void) {
struct timespec ts;
long n = 0;
2026-01-09 14:45:12 -07:00
clock_gettime(CLOCK_MONOTONIC, &ts);
n += ts.tv_nsec / 1000 / 1000;
n += ts.tv_sec * 1000;
2026-01-09 14:45:12 -07:00
return n;
}
void MwTimeSleep(int ms) {
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
#endif