xtimer/xtimer.c: xtimer_mutex_lock_timeout fix with short timeout#13199
Conversation
5b08616 to
ba69a9c
Compare
| } | ||
|
|
||
| mutex_lock(mutex); | ||
| int ret = _mutex_lock(mutex, mt.blocking); |
There was a problem hiding this comment.
handing the value of the volatile variable blocking to _mutex_lock will not have the desired effect here. The timeout might deschedule you right before the irq_disable of _mutex_lock is called, leaving you with a blocking call where actually a non-blocking lock should happen. Is there any reason for not just simply doing a non-blocking lock before setting up the timeout timer? This way you would always try to lock the mutex and if the timeout then happens between setting up the timer and locking it, is actually desired behavior to not lock it, right?
There was a problem hiding this comment.
the timeout could happen before the mutex_lock but after the first non blocking mutex lock. Meaning there would be the same problem.
The problem with the irq_disable (interupt in function _mutex_lock but before irq_disable) will be fixed in the next PR. Tracking PR #11660
This PR fixes the problems with smaller timeouts.
This test checks if the function works when the timeout is smaller than XTIMER_BACKOFF and the mutex is already locked. This means the timer will spin and the timer will shoot before the mutex lock was called. Then the mutex lock gets called and the timer will not remove the thread from the mutex. Checking if this case is handled correctly.
Handling timeout smaller than XTIMER_BACKOFF (the timer spins) when the mutex is already locked. This fixes the test tests/xtimer_mutex_lock_timeout/main.c:mutex_timeout_spin_locked.
73bd77f to
f3f562b
Compare
This function tries to remove the thread from a mutex waiting queue. The value pointed to by unlocked will be set to 1 if the thread was removed from the waiting queue otherwise 0.
f3f562b to
4d85fa1
Compare
MichelRottleuthner
left a comment
There was a problem hiding this comment.
Code and commits look fine, re-tested -> ACK
Tracking: pr #11660
Contribution description
Fixes BUG
This PR implements a new function to test
xtimer_mutex_lock_timeoutwith a short timeout (spinning) and fixes the functionxtimer_mutex_lock_timeoutbecause without the fix in this PR the test fails.This PR also implements a new function
_mutex_remove_thread_from_waiting_queue. This function is implemented to remove all code out ofxtimer_mutex_lock_timeoutand_mutex_timeoutthat edits the mutex struct. The function_mutex_remove_thread_from_waiting_queueshould be moved to mutex.c in the future to removed all code out of xtimer that edits the mutex struct.Testing procedure
To test the bug fix run the test. To see the bug revert to commit: "tests/xtimer_mutex_lock_timeout: minimal xtimer_mutex_lock_timeout test" and run the test. (the test will fail)
BOARD=native make -C tests/xtimer_mutex_lock_timeout/ flash testoutput:
output without fix:
Issues/PRs references
Tracking PR #11660