Linux timer implementation uses wrong API

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
alex7900
Posts: 1
Joined: Sat Nov 27, 2021 2:35 pm

Linux timer implementation uses wrong API

Post 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.
CuteAlien
Admin
Posts: 9646
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Linux timer implementation uses wrong API

Post 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.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply