A small RTOS investigation into unnecessary synchronization.
I was looking into CPU utilization in a real-time embedded application. While tracing the execution path, I noticed the same pattern in several places: handlers taking and releasing mutexes to access application state.
At first, that seemed reasonable. Multiple handlers used the same data, and in embedded software you don't remove synchronization casually.
But I wanted to answer a more basic question: could those handlers actually run at the same time?
I mapped the relevant execution paths. Although the code lived in different modules, the execution model already guaranteed serialized access to that state.
That's an important distinction. Code accessed by multiple modules isn't necessarily accessed concurrently. A mutex is useful when there's concurrency to coordinate. For the accesses in question, the execution model already provided the ordering.
Of course, seeing handlers run sequentially in one trace isn't enough to remove a lock. I needed to understand whether the same state could also be accessed from another task, an interrupt, a callback, or a fault path. If any of those paths could touch it concurrently, the mutex might still be necessary.
Once the ownership boundary was clear, I removed synchronization that was not protecting against concurrent access. CPU utilization improved measurably in the scenario I was investigating.
The code change itself was simple. Figuring out why it was safe took most of the work.
This isn't an argument against mutexes. An uncontended mutex doesn't necessarily cause a context switch, and plenty of shared data genuinely needs synchronization. The cost depends on the RTOS and how frequently the operations execute.
What stuck with me was the order of the investigation. I could have spent time making the locking faster. Instead, understanding who owned the data and when the code could execute showed that some of the locking didn't need to exist at all.
Before optimizing synchronization, make sure you know what concurrency it's protecting.
The implementation details are generalized; no proprietary code or traces are included.