-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Threading, Timers improvements #3865
Threading, Timers improvements #3865
Conversation
e43f87f
to
69a463d
Compare
@CookiePLMonster there is a catch: under CPU starvation you will never receive FuriThreadStateStopped signal.
|
Before this PR, in CPU starved scenarios I'm not sure if this is a bad catch in itself, as the main use case (waiting inside EDIT: |
@CookiePLMonster in general what you did is a movement in the right direction. It just we were having deadlock problems in specific conditions in a code that looks exactly like yours right now(I'm the person who added this ugly hack for that problem), I need some time to add additional unit_tests to make sure that we are not stepping into same shit. Current implementation is not perfect, but it forces your main thread to give some time to idle thread to release TCB. Which your implementation not doing |
Ah I see, before my changes, Could a yield before |
e58e92b
to
f51ae6a
Compare
Split off to #3881. |
0b685b0
to
bdbcc48
Compare
This combines the current synchronous behaviour (as we could have deferred the free call too) with a smaller FuriTimer - it's safe to pass a pointer to a local variable to this pending timer call, because we know it'll be finished before the caller returns
bdbcc48
to
2bac5b3
Compare
Event loop and Loader mixed those two, but the fact those are aliases should be an implementation detail. For this reason, thread.c is still allowed to mix them freely.
2bac5b3
to
bd05b53
Compare
* FuriTimer: Use a local variable to wait for deletion This combines the current synchronous behaviour (as we could have deferred the free call too) with a smaller FuriTimer - it's safe to pass a pointer to a local variable to this pending timer call, because we know it'll be finished before the caller returns * Tighten the use of FuriThread* vs FuriThreadId Event loop and Loader mixed those two, but the fact those are aliases should be an implementation detail. For this reason, thread.c is still allowed to mix them freely.
What's new
This PR introduces several changes to timers and threads:
FuriTimer::can_be_removed
has been replaced with a local variable to reduce the size of the structure. Passing a pointer to a local variable to a pending callback is safe infuri_timer_free
, because we are waiting for that pending call to finish before returning from the caller. This change reduces the memory usage ofFuriTimer
while keeping the blocking behaviour offuri_timer_free
.FuriThread
.Since currentlyDROPPED AS IT'S UNSAFEFuriThread*
,FuriThreadId
andTaskHandle_t
are aliases, the use of TLS to store a pointer toFuriThread
is not required anymore. I replaced all the instances of this and changed FreeRTOS config not to allocate that TLS slot.FuriThreadStateStopping
has been introduced, whileStateStopped
is now raised from the TCB cleanup. This meansfuri_thread_join
can just wait for the state to change toStopped
.Stopped
call, it is safe to releaseFuriThread
. Multiple call sites in the firmware have been updated to make use of this new assumption, instead of deferring their release to a pending timer call.FuriThread* thread
parameter - previously, callbacks assumed that they are always invoked from the thread that is changing its state, but this wasn't true forStateStarting
, and with this PR, it's not true forStateStopped
either.FuriThread*
andFuriThreadId
. While this is fine in the internal thread routines, IMO shouldn't be done by other modules.Thanks to the removal of a struct member in
FuriTimer
and a now-useless TLS slot, this change saves a bit of heap and pool space. I suspect the code should be a bit smaller too, but I haven't compared the sizes.Before:
After:
Verification
Checklist (For Reviewer)