Overview
| Artifact ID: | 5704afa52a0ca31e78693e0efcdafd30d38dfb5db933bd76acfeeb68b90f1040 |
|---|---|
| Ticket: | 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
Nested mutexes following TIP 509 |
| User & Date: | jan.nijtmans on 2025-10-03 11:06:34 |
Changes
- closedate changed to: "2460951.96289685"
- closer changed to: "jan.nijtmans"
- icomment:
> So the conclusion is that we do not need atomic reads for the tests. I share that conclusion, but let's elaborate. We can see the pMutex->thread variable as a state machine. When thread MM does some operation after thread YY, the state transitions are: 00 => MM => 00 => YY => 00 Now take into account, that the pMutex->thread variable is not written atomically, then we could have: 00 => 0M => MM => M0 => 00 => 0Y => YY => Y0 => 00 That's a lot more states to consider. Now consider how thread MM sees this. Within the same thread, the processor always guarantees that operations involving the same memory are always kept in order. When thread MM sets the pMutex->thread variable to 'MM' and later to '00', it will never see the intermediate states as other threads might see them. So, we get: 00 => MM => 00 => 0Y => YY => Y0 => 00 Now the only situation left we have to worry about is when thread MM tries to acuire the lock. Before that, it has to check that pMutex->thread == 'MM', then it already has the lock. But the states '00', '0Y', 'YY' and 'Y0' are all not equal to 'MM'. So it doesn't really matter whether thread MM sees the intermediate states, the result of the == operator stays the same. The conclusion is that we don't need the atomic states for the reads. I also changed the code to use THREAD_NULL and pthread_equal(), as suggested. See: [8d7d7b1c144bfca2|here] Ashok's testcases already succeed: [https://github.com/tcltk/tcl/actions/runs/18186353547]! - login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"