-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SDL_Delay() requires a &mut TimerSubsystem? #1268
Comments
pub fn delay(&mut self, ms: u32) {
// Google says this is probably not thread-safe (TODO: prove/disprove this).
unsafe { sys::SDL_Delay(ms) }
} While I'm here, I'd like to address this In Rust's standard library on Windows, unsafe { c::Sleep(super::dur2timeout(dur)) } SDL2 is more complicated. WindowsIn if (!ticks_started) {
SDL_TicksInit();
}
Sleep(ms);
If static HANDLE mutex = 0;
if (!mutex) {
mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
}
WaitForSingleObjectEx(mutex, ms, FALSE); This creates a dummy mutex and waits for it to timeout. Clever, but there is a race condition that can cause a memory leak if multiple threads create a mutex and save it to However, WinRT also runs on ARM. Atomicity of simple loads and stores on ARM is not like on x86/64, so unfortunately I do not think this is thread safe, because
HOWEVERI still don't see why
Thoughts? |
I don't have the answer to any of this, your best bet would be to blame the line, get who did this, try to find a related issue or series of commit that are related, and maybe you'll get answer there. It's possible that this is a mistake yes, but it's also possible that there is a hidden reason. |
Hello @Cobrand, it's good to see your quick response. When I blamed the line, I see it was added in 0a3453b by @nukep in 2015 and never touched again. Prior to that, it really was a freestanding
Couple that with the I was exasperated going through Here's what I noticed on all platforms for
Conclusion:
Conclusion: In the C codebase, ticks and timer are technically separate systems, but the timer subsystem may make calls to Proposal
Code-wise this should be a very simple fix, it just took a lot of time for me to audit it. :) Thoughts? |
Thanks for looking it up! Your proposal sounds good to me, feel free to drop a PR and add the changes to the changelog as well. |
While fixing I noticed a timer callback could cause UB if it panics, so I added a |
Hello,
I know that the typical solution to this is to use something like
std::thread::sleep()
instead of the SDL-provided delay function, but this is a weird quirk in the bindings nonetheless.I have a short project where I have a running window and create a timer callback that runs on another thread to print ongoing FPS data via
let _fps_timer = video_subsystem.add_timer()
here on line 57._fps_timer
must live throughout the game loop in order to print FPS data.At the end of the game loop I call
timer_subsystem.delay()
, and get a compiler error because_fps_timer
is already holding an immutable reference totimer_subsystem
. (Note thattimer_subsystem
would bemut
on line 56.)I found it strange that
delay()
expects a mutable reference toTimerSubsystem
. Most other subsystem methods only require immutable&self
references. In fact, in the SDL2 code,SDL_Delay()
doesn't even initialize the Timer subsystem, and doesn't even need a call toSDL_Init()
!So I have
twoone questions:delay()
take an immutable reference to&self
instead of&mut self
?Furthermore, couldn'tdelay()
just be a freestanding function likesdl2::timer::delay()
or evensdl2::delay()
?Thank you.
The text was updated successfully, but these errors were encountered: