| Ticket UUID: | 112af76b090286f9f0f04c8b994c252bb1b82bd8 | ||
| Title: | Double-checked locking pervades the Tcl implemetation | ||
| Type: | Bug | Created on: | 2023-05-21 20:35:27 |
| Submitter: | konrad.schwarz | Assigned to: | jan.nijtmans |
| Subsystem: | 41. Memory Allocation | Severity: | Critical |
| Priority: | 5 Medium | Last modified: | 2026-11-11 14:27:19 |
| Status: | Open | Closed by: | nobody |
| Resolution: | None | Closed on: | |
| Version: | all | ||
| Description: | ||||
Problem description"Double-checked locking" is a discredited programming pattern that goes like this:
A processor operating under a relaxed memory model can read a non-null It may be that this bug does not occur with processors implementing the Total-Store Ordering (TSO) memory model, i.e., x86, and as an optional feature on SPARC, RISC-V and some ARM models from Apple, but that is something I would not put my hand in the fire for. POSIX compliant systems are not required to support this pattern, see Memory Synchronization:
I am unaware if Windows/ARM requires TSO. As standard ARM cores do not support TSO, I would be surprised. Unfortunately, this pattern pervades the Tcl implementation. Clean up is a major undertaking. I can only speculate to why double-checked locking was used by the original Tcl implementers: it certainly was a extremely popular pattern around the millennium; the fact that Windows introduced Once variables (and condition variables) only with Vista and Solaris Threads not at all probably played a role. The changes also affect the public C API.
The How to solveExplicit initialization functions for these data types must be provided (i.e., counterparts to the POSIX provides the A complication arises because a Since a Tcl Some analysis of the I think this should be resolved as part of the Tcl 9.0 releases, since changes are required to the API. | ||||
| User Comments: | ||||
serhiy.storchaka added on 2026-10-14 15:57:48:
The lazy initialization of Tcl_Mutex and Tcl_Condition can be made correct without changing the API: an acquire load for the unlocked check, and a release store when the new object is published. On x86 these are ordinary loads and stores, so the fast path is not slower. Tcl_ThreadDataKey only stores an index into a table owned by each thread, so it does not need this. Proposed fix in branch double-checked-lock-ordering for Tcl_MutexLock() and Tcl_ConditionWait2() on Unix and Windows, and for the same pattern in the notifier thread start (select notifier) and in the Windows pipe and serial initialization. A Tcl_Once API would need a TIP. | ||||
