Overview
| Artifact ID: | 8c7dda4a65d96f59bd08f70cbc81a42dccf1f6da09233c05f76938f2e1719a6e |
|---|---|
| Ticket: | 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
Nested mutexes following TIP 509 |
| User & Date: | apnadkarni on 2025-09-18 15:15:57 |
Changes
- icomment:
> I don't think so, and even with aggressive optimization level the compiler wouldn't generate the code doing that. My concerns had nothing to do with compilers or optimization levels, with or without volatile attributes. They have to do with cache coherency in multiprocessor systems. Thinking about it further, I'm even more convinced there is a race condition. Scenario: 1. Thread A on processor X successfully takes out the lock. 2. It updates the thread id and count fields in the mutex wrapper struct. However, **until there is a memory barrier or similar instruction, generally effected by synchronization primitives there is no guaranteed that those memory locations are written back into main memory or synchronized with other caches.** 3. Thread B on processor Y checks the value of the count and thread id BEFORE thread A releases the lock (which would have caused the processor cache flush). However, the values B sees are stale because neither main memory not processor Y cache have been updated with the new values from processor X cache. 4. What happens next depends on the stale values. If the stale thread id is X and thread count is not 0, B will think it already owns the mutex and proceed without trying to lock it. 5. Thus both A and B think they own the lock and have a race condition.
- login: "apnadkarni"
- mimetype: "text/x-markdown"