There are three types of context imbalance warnings:
- warning: context imbalance – unexpected unlock
- warning: context imbalance – wrong count at exit
- warning: context imbalance – different lock contexts for basic block
How to annotate
This is the easiest of all, what happens here is that the function enters the critical section at entry without exiting or it can be the function exit the critical section without entering, both ways Sparse will throw a warning. Locking is not balanced, for each entry to the critical section, there should be a corresponding exit from it.
Adding __releases() or __acquires() annotation to the function will generally solve the issue .
Wrong count a exit
This situation generally happens when Sparse gets confused of the logic function. It thinks the function releases the lock at exit when it s still holding the lock or acquires when it releases or finally hold the lock when
Different lock contexts for basic block
This situation happens when the function has many branches or conditions with different locking outcomes. This may be a bug or just a logic that hit the limit of Sparse.
To solve this issue one may consider refactoring the code if necessary.
Please consider that the situation described above are general, this implies that one may encounter more complex situation throwing one of the above warnings.