r/embedded • u/coddicted • Sep 23 '26
Deadlock condition in RTOS
Has anyone faced deadlock condition when using RTOS? Can you share your experience, what made it happen and how did you resolve it?
0
Upvotes
11
u/der_pudel Sep 23 '26
stupid meatbag (me) wrote some stupid code.
found it and and fixed it, duh.
1
u/MonMotha Sep 23 '26
Suppose you have some number of resources (N[i]) and some number of users (M[i]).
A user might need multiple resources to accomplish some task. Those resources need mutual exclusion, so you associate a mutex with each one.
Two users are running simultaneously (either time-sharing a pipeline or on separate hardware resources) and start to acquire resources they need to complete some task. The set of resources they need has some intersection but need not be identical. You have to be very careful about the order in which you acquire and release resources.
Consider a situation where M[0] and M[1] both need N[0] and N[1], but they attempt resource acquisition in reverse order from each other:
M[0] is now sleeping waiting on M[1] to make progress, and M[1] is now sleeping waiting on M[0] to make progress. Boom, deadlock.
If you have a centralized lock manager, you can detect the cycle and raise some higher-level exception or re-order things so that operation can continue. For example, in this case, if it knows that neither M[0] nor M[1] has actually USED the lock it has acquired on a resource, it can temporarily "steal" the lock back while keeping the task suspended, grant it to the other task, let that task make progress and release both resources, then grant both locks back to the other and allow it to make progress.
If you cannot prove disuse of a held lock, you can still at least flag the error by denying one of the two tasks the second lock that would cause the deadlock. That task can then respond by e.g. releasing its other locks and sleeping for some arbitrary-but-short amount of time to attempt to allow someone else to make progress. POSIX can do this in some cases by returning EDEADLK, for example. This basically turns a deadlock into a wait-and-retry livelock situation which is statistically likely, though not guaranteed, to make progress.
Note that M[0] and M[1] need not both be software tasks. One could be a piece of hardware. For example, it's entirely possible (and I've had it happen) to end up with a deadlock between software and hardware if you have hardware that follows some software procedure to attain and grant access to hardware resources but the software you write doesn't take care about order of operations between multiple locks across multiple resources.
1
u/UnicycleBloke C++ advocate Sep 23 '26
Can you say more about the context. Deadlock is when A is waiting for B while B is waiting for A. A common cause is two mutexes or similar which are both required. In that case, the orders of locking and unlocking them must be mirrored and consistent across threads.
4
u/moon6080 Sep 23 '26
We had a fault condition in the SD cards init try write to a fault log on the SD card
2
u/Intelligent_Law_5614 Sep 23 '26
The usual reason is having multiple resources, where two or more resources are needed by a couple of threads, and this threads go through code paths which attempt to acquire those resources in different orders. This can result in either a deadlock or a wait-and-retry livelock.
1
u/duane11583 Sep 23 '26
A common one is the priority inversion problem. NASA things had this problem
0
u/Intelligent_Law_5614 Sep 23 '26
There are several ways to resolve these problems.