Page 1 of 1

Linux timer implementation uses wrong API

Posted: Sat Nov 27, 2021 7:24 pm
by alex7900
Hi,

I was playing the awesome Irrlicht-based game The End of Dyeus on Linux, and noticed that the game speed would get messed up if my screensaver appeared. The dev suggested that it's possibly due to Irrlicht's timer implementation on Linux, so I did some digging and found that Irrlicht is using the wrong API for timers.

In the os.cpp source file, the Linux implementation is using gettimeofday, which is designed for obtaining calendar time, so it's susceptible to changing during runtime. For interval calculations (e.g. frame times, profiling, etc) it should really be using clock_gettime with either CLOCK_MONOTONIC (or a variant). The man page has a good description of all the available clocks.

I don't know if this will fix the particular game bug I encountered, but since win32's QueryPerformanceCounter is monotonic, the Linux/Mac backend should be as well for better portability.

For reference, here's some sample code ripped out of one of my old projects. That also has an implementation specific for Mac, which might be useful because IIRC even though it's POSIX, some older versions of Mac don't support this API.

Re: Linux timer implementation uses wrong API

Posted: Sat Nov 27, 2021 11:54 pm
by CuteAlien
Got it on my post Irrlicht 1.9 todo's. There's been a longer discussion about this once on reddit a few years ago: https://old.reddit.com/r/programming/co ... timemillis
Going over that one day.