The sensor's poll thread waited between readings on a condition under
a mutex, which a rate change and close took to wake it, and which the
thread took and dropped around every reading to look at the rate and
the two flags.
The rate, a count of rate changes and the done flag are an atomic
word each, and the thread waits on an eventcount with the time to its
next reading as the bound. A rate change stores the rate, bumps the
count and notifies; close stores done and notifies. The thread reads
the count before the rate, so a change landing between the two is
read again rather than missed.
The behaviour is the one illuminance_sensor_test pins: a new rate
applies at once, and close returns as soon as a read in progress
does. The sample links retro_eventcount.c now.
Statics rely on implicit zero initialisation instead of {0}, and local
aggregates whose first member is an array get the matching braces, so
GCC 4.x -Wmissing-braces stays quiet. Locals the compiler cannot prove
written are initialised (rh265 keep[], rflac block header, query.c
lengths), loops older VRP misreads are reshaped, the unused SDL2
geometry forward declaration is gone, and task_queue no longer names a
variable main.
rzstd probes BMI2 with __get_cpuid_max/__cpuid_count, which every
cpuid.h the branch accepts provides. Hole punching needs uClibc-ng 1.0
with its Linux-specific API on uClibc. The sinc resampler's pop_options
does not warn on GCC 4.x MIPS. generate_wayland_protos.sh strips the
event type and deprecated-since attributes for scanners whose DTD
predates them, which ignore them anyway.
The IIO illuminance poller slept a whole period after every reading and
was stopped by pthread_cancel() into that sleep. A core that raised the
rate had to wait out the rest of the old period first - up to a second
at 1 Hz - and the only way to end the thread was cancellation, with a
cancel-enable dance around the file read and an EINTR check on a sleep
to tell a cancel from a wakeup.
The thread now waits between readings on a condition that close and
rate changes signal, with a timeout to the next scheduled reading:
- Close sets done and signals; the thread wakes at once and leaves, so
the join lasts no longer than a read already in progress. No
cancellation.
- A rate change signals too; the thread reads at once and schedules
from there at the new rate.
- Readings are scheduled a period apart rather than a period after the
previous read finished, so the rate holds however long a read takes;
a reader that falls behind skips ahead instead of bursting.
poll_rate, done and the new rate_changed flag are guarded by the
sensor's lock. The reading itself is published with retro_atomic
acquire/release instead of relying on aligned int writes being atomic.
Without HAVE_THREADS nothing changes: one reading at open.
IIO_DEVICES_DIR can now be overridden at build time, which is how the
new samples/input/illuminance_sensor test points the sensor at a fake
sysfs tree. It pins that a raised rate applies at once (it fails on the
old code: the change waited out the 1 s sleep), that close at 1 Hz
returns at once, that readings follow a changing value, and that open
reports no sensor when there is none. Clean under ASan, UBSan and
TSan; run by a new CI Linux samples/input job.
Three breaks, found by building and linking with --disable-threads
rather than compiling single files: the wrapper header declared
video_thread_swap_count() as a function while video_driver.h's
threadless branch defines it as a macro, so any includer of both failed
to parse - the header's body is now under HAVE_THREADS, since the object
it declares only exists then, and callers keep the macro stand-ins; the
microphone capture path read worker_sample_size, a field that only
exists with a worker; and the Linux illuminance sensor created and
joined its polling thread unconditionally, which predates all of this.
Two defects in the sensor poll thread, both reachable from a core: the
rate is whatever it passes to RETRO_ENVIRONMENT_SET_SENSOR_STATE, and
it arrives here unexamined.
The interval is 1000 / poll_rate in whole milliseconds, so any rate
above 1000 Hz gives zero and the thread reads the sensor as fast as
the CPU allows - and each read is an open/read/close of a sysfs file,
so that is a core of CPU and a hammered filesystem for a device that
cannot answer at that rate anyway. 2000 Hz is not an absurd thing for
a core to ask a sensor API for. The rate is clamped to 1 kHz, at the
two places it enters and again in the loop, so the figure the log
prints is the figure that is used. Zero was already handled, by an
assert in the loop and a default at the entry points; the loop now
defaults rather than asserting, since a release build has no assert
and a division by zero is a worse outcome than 5 Hz.
The loop also tested errno after retro_sleep() without clearing it
first. errno is only meaningful directly after a call that failed, and
the sensor read immediately above goes through open/read/close, each
of which can leave one behind - so an EINTR from something else
entirely would end the sensor thread and the sensor would go quiet for
the rest of the session with a misleading line in the log. Cleared
before the sleep.
Checked against every rate up to 2^20 plus the boundaries and
UINT_MAX: no rate now produces a zero interval, 0 still means 5 Hz,
and anything past 1 kHz becomes 1 ms. Before the patch, 1001 Hz
produced a 0 ms sleep.
Every strtod/strtof in first-party code becomes rstrtod/rstrtof: the
signatures match, and the parse no longer depends on the process
locale. Under a comma-decimal locale, strtod reads "59.94" as 59 and
"0.005" as 0 -- demonstrated through config_get_float/get_double
against a real config -- which is the long-standing class of bug where
refresh rates, shader parameters, and overlay coordinates collapse to
integers on locales that RetroArch's C-locale enforcement does not
reach (threads that setlocale themselves, cores that flip it, or
platforms without the enforcement). Touched: config_file and its
userdata list parser, filestream_scanf's float conversions, shader
parameter parsing, overlay coordinates, menu setting input, the
video-processor and mpv bundled cores, and the linux battery reader.
rstrtod.o joins Makefile.common and griffin next to stdstring, and the
Salamander projects that compile config_file.c directly pick it up too.
Deliberately untouched: strtoul/strtol (integers have no locale
problem) and everything under deps/.
The illuminance-sensor code in input/common/linux_common.c used raw
pthreads: pthread_setcancelstate() around the file-reading critical
section, and pthread_cancel() on close via a pthread_t reconstructed by
casting sthread_get_thread_id() -- a fragile round-trip through the
opaque id.
Add cancellation primitives to rthreads and route the driver through
them:
void sthread_set_cancel_enable(bool enable);
bool sthread_cancel(sthread_t *thread);
sthread_cancel() takes the sthread_t directly and uses its backing handle,
dropping the pthread_t cast. Both are implemented with
pthread_setcancelstate()/pthread_cancel() on pthread backends that provide
them, gated by RTHREADS_HAVE_CANCEL which excludes Win32, GEKKO, 3DS and
Android/Bionic (Bionic has no pthread_cancel; rthreads.c is compiled there
even though linux_common.c is not). Where unsupported they are a no-op /
return false, and the existing cooperative sensor->done flag still stops
the thread.
linux_common.c no longer includes <pthread.h> or references pthread
directly. No functional change on Linux.
Compiling the C sources as C++ (CXX_BUILD=1) failed in 12 places where
the code relied on implicit conversions that C permits but C++ rejects.
All are mechanical and behaviour-preserving:
void* -> typed pointer (explicit cast):
- input/common/linux_common.c (illuminance sensor data)
- tasks/task_screenshot.c (copy.buffer -> uint32_t*)
- libretro-db/rmsgpack_dom.c (alloca result -> dom_value**)
const char* -> char* (strrchr/strchr return values):
- libretro-common/file/file_path.c (cast strrchr result; matches the
char* return contract of the C function)
- configuration.c (extra_path points at a mutable
stack buffer that is written through -> make it char*)
- menu/menu_displaylist.c (hash_type likewise -> char*)
integer -> enum (explicit cast at use site):
- runloop.c (unsigned pending_cmd -> enum event_command)
- runtime_file.c (settings uints -> last_played style/separator)
- tasks/task_database.c (int iterate result -> enum scan_verdict)
- menu/menu_driver.c (unsigned confirm_msg -> enum msg_hash_enums)
std::atomic copy:
- cheevos/cheevos.c: the static rcheevos_locals aggregate brace-init
of the two retro_atomic_int_t members used a bare '0', which under
CXX_BUILD (std::atomic<int>) selects the deleted copy constructor.
Use {0} so C++ list-initializes via the converting constructor;
for the C atomic/int/volatile backends {0} is an equivalent scalar
initializer.
With these, the tree compiles cleanly under CXX_BUILD=1 (every object
builds and the link stage is reached). This addresses the compilation
failures from #6009; note that CXX_BUILD has no CI coverage, so it will
regress again as new C-idiomatic code lands unless a build job is added.
* First crack at light sensor support for Linux
* Add light-sensor support to most Linux input drivers
* Fix a compiler error
- Whoops, forgot to declare `sdl`
* Refactor linux_illuminance_sensor_t
- Allow the poll rate to be specified
- Poll the sensor on a separate thread
- Open a file handle each time we poll the sensor, since sysfs doesn't update the contents of an existing handle
* Set the `done` flag when closing the light sensor
- Whoops
* Avoid a division by zero when updating the poll rate of an existing sensor
* Don't try to open illuminance sensors from ".", "..", or hidden files
* Never mind
* Fix some silly mistakes
* Skip hidden files, ".", and ".."
* Cancel the sensor poll thread mid-sleep when closing it
- POSIX says it's fine
* Add to CHANGES.md
* Address feedback given on PR
* Use libretro-common's file system instead of stdio