June 19th, 2009

You can’t leave a critical section you never entered

If you call LeaveCriticalSection on a critical section you never entered, the behavior is undefined.

Critical sections are an extremly high-traffic code path. Intense work goes into making them as fast as possible. Customers like to ask questions like “Exactly how many cycles does it take to enter a critical section? We’re not going to use them if they’re too slow.” They don’t say what they are going to do if critical sections are too slow for them, but the threat is real (even if the justification is bogus). I’ve seen programmers respond to code feedback of the form “You need a critical section here” with “No, I’m not going to bother. I’m afraid it’ll slow down my code.”

Given that critical sections are so heavily used, the algorithm gets tweaked regularly in order to improve performance. Sometimes the tweaks are minor; other times, the algorithm gets a major overhaul. Of course, the hope is that by changing only the insides, nobody will notice. On the other hand, people who relied on undefined behavior (like exiting a critical section they never entered and hoping that something meaningful would happen) are going to see changes in behavior.

I don’t know the precise reasons why the internals of critical sections changed, but I suspect it had to do with mitigating the effect of lock convoy.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.