Simple DirectMedia Layer https://libsdl.org
Find a file
Blaž Tomažič 62d82ffc15 fix: don't use CLOCK_MONOTONIC_RAW on Android
Older Android phones have a kernel bug where time is not properly calculated
when calling `clock_gettime(CLOCK_MONOTONIC_RAW, ...)`. The returned time
either has nanoseconds out of range (outside of [0, 999999999]) or the returned
time is not monotonic in regards to previous call or both.

The issue is reproducible in Android Studio on arm64 emulators from at least
Android 7.0 (didn't try older versions) up to including Android 8.1 (kernel
3.18.94). The issue is in the kernel, these are just the versions of Android
emulator with buggy kernels.

The kernel commit that fixed this is [1]. Because this fix was backported to
various LTS releases of kernels it's hard to find out exactly which kernels are
buggy and which not. Therefore always use `CLOCK_MONOTONIC` on Android.

The `CLOCK_MONOTONIC` is slowly adjusted in case of NTP changes but not for
more than 0.5ms per second [2]. So it should be good enough for measuring
elapsed time.

---

An option would be to dynamically select
`CLOCK_MONOTONIC_RAW`/`CLOCK_MONOTONIC` at runtime by checking at init time
that `clock_gettime(CLOCK_MONOTONIC_RAW, ...)` returns nanoseconds in range.
Testing showed that nanosecons are out of range for 999/1000 cases. I guess
this could be good enough for support on older phones.

But because this would introduce a small perf hit by always reading a global
variable for first argument to `clock_gettime` and because `CLOCK_MONOTONIC`
does not have that high time deviation, this commit uses `CLOCK_MONOTONIC` on
Android always. It is also less burden to maintain.

---

[1] dbb236c1ce
[2] https://github.com/torvalds/linux/blob/master/include/linux/timex.h#L136
2025-07-16 10:36:14 -07:00
.github ci: build MSVC release binary on windows-2025 2025-07-12 01:54:05 +02:00
android-project use SDL style - else on same line as closing brace 2025-05-20 17:12:01 -07:00
build-scripts wikiheaders: Let each subproject specify how to find their property symbols. 2025-06-17 16:41:24 -04:00
cmake cmake: Android always needs a native HIDAPI implementation 2025-06-26 22:34:30 +00:00
docs README-macos.md: replace old API calls 2025-07-07 18:37:19 -04:00
examples Restore support for the Nokia N-Gage (#12148) 2025-05-22 11:07:22 -07:00
include Sync SDL3 wiki -> header 2025-07-16 01:36:42 +00:00
src fix: don't use CLOCK_MONOTONIC_RAW on Android 2025-07-16 10:36:14 -07:00
test testcontroller: use the correct label for face buttons 2025-07-15 15:52:40 -07:00
VisualC Implement SInput Device Support (#13343) 2025-07-15 18:35:47 -07:00
VisualC-GDK Implement SInput Device Support (#13343) 2025-07-15 18:35:47 -07:00
wayland-protocols wayland: Add support for the key repeat event (seat v10) 2025-07-06 11:28:04 -04:00
Xcode Implement SInput Device Support (#13343) 2025-07-15 18:35:47 -07:00
.clang-format Add 'wl_list_for_each_safe' to the clang-format macro list 2024-03-14 10:22:23 -04:00
.clang-tidy Fix size of memcpy in SDL_AudioDeviceFormatChangedAlreadyLocked 2023-08-05 14:14:45 -04:00
.editorconfig .editorconfig: Remove excess apostrophe at *.cocci 2024-06-16 09:18:17 -07:00
.gitignore Allow in-tree build 2024-10-30 00:04:17 +01:00
.wikiheaders-options wikiheaders: Let each subproject specify how to find their property symbols. 2025-06-17 16:41:24 -04:00
Android.mk [SDL3] Adding input and FFB support for Logitech G29(PS3) on hidapi (#11598) 2025-03-17 07:24:39 -07:00
BUGS.txt Added a Discord link to BUGS.txt 2025-01-13 16:42:50 -08:00
CMakeLists.txt cmake: remove /RTC1 from CXX flags when building with SDL_LIBC=OFF 2025-07-11 03:25:58 +02:00
CREDITS.md Updated the credits for SDL 3.0 2025-01-13 16:42:50 -08:00
INSTALL.md Add a link to docs/INTRO-mingw.md 2025-03-06 16:25:17 -08:00
LICENSE.txt Updated copyright for 2025 2025-01-01 07:45:52 -08:00
README.md Updating documentation for the 3.2.0 release 2025-01-15 20:38:16 -08:00
WhatsNew.txt Added SDL_HINT_VIDEO_X11_EXTERNAL_WINDOW_INPUT 2025-03-05 09:07:05 -08:00

Simple DirectMedia Layer (SDL for short) is a cross-platform library designed to make it easy to write multi-media software, such as games and emulators.

You can find the latest release and additional information at: https://www.libsdl.org/

Installation instructions and a quick introduction is available in INSTALL.md

This library is distributed under the terms of the zlib license, available in LICENSE.txt.

Enjoy!

Sam Lantinga (slouken@libsdl.org)