Artifacts Associated With Ticket 112af76b090286f9
Ticket change [01355e6e5c] (rid 215216) by konrad.schwarz on 2023-05-21 20:35:27:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-markdown"
- comment initialized to:
"Double-checked locking" pervades the Tcl implementation but is broken under POSIX and many processor memory models. ======================================================================================================================= ## Problem description "Double-checked locking" is a discredited programming pattern that goes like this: ``` if (0 == var) { acquire lock; if (0 == var) { /* double check */ temp = allocate (sizeof *var); initialize *temp; var = temp; } release lock; } /* var points to an initialized object that has been initialized exactly once NOT */ ``` A processor operating under a relaxed memory model can read a non-null `var`, yet see a non-initialized `*var`. (See the references in [double-checked locking](https://en.wikipedia.org/wiki/Double-checked_locking)). 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](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_12): > Applications shall ensure that access to any memory location by more than one thread of control (threads or processes) is restricted such that no thread of control can read or modify a memory location while another thread of control may be modifying it. Such access is restricted using functions that synchronize thread execution and also synchronize memory with respect to other threads. The following functions synchronize memory with respect to other threads: \[The list comprises the POSIX mutex/condition variable/rw-locks/barrier/spin-lock/semaphore and thread (un-)locking/signaling/waiting/create/joining operations, as well as the once object.\] 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 `Tcl_Condition`, `Tcl_Mutex`, and `Tcl_ThreadDataKey` types all suffer from this problem: these types are (opaque) pointers that are initialized implicitly with exactly the logic shown above: if on e.g. a mutex lock operation, the variable contains a null pointer, a central lock is taken and the variable re-checked. ## How to solve Explicit initialization functions for these data types must be provided (i.e., counterparts to the `Tcl_MutexFinalize`, etc. routines). POSIX provides the `pthread_once_t` type to manage initialization in a multi-threaded process: invoking `pthread_once()` on a variable of this type together with a callback routine ensures the callback routine is executed exactly once in a multi-threaded program. A Tcl version of this type must be provided. A complication arises because a `pthread_once_t` variable must be initialized via an initializer specified in POSIX as the macro `PTHREAD_ONCE_INIT`. As the Tcl `Once` implementation must wrap the POSIX (resp. Windows, which has a analogous type and macro) implementation, `pthread_once_t` must be visible when including `tcl.h`, which has up to now not included either `<pthread.h>` or `<windows.h>`, at least if `pthread_once_t` (resp. the Windows variant) are to be understood by the compiler (i.e., referred to by `typedef` or member of an aggregate type). Since a Tcl `Tcl_Once` type cannot be opaque any more -- it must be initialized via `TCL_ONCE_INIT`, the question arises if `Tcl_Mutex`, `Tcl_Condition` and `Tcl_ThreadKeyData` should remain pointers to opaque types. The drawback is the extra indirection required; the advantage of the current system is that the size of the underlying objects is not visible in the API and hence can be changed from version to version. As these objects are very stable (as are the OS-objects underlying them; they have the same constraints on binary compatibility), I would argue to make these objects non-opaque, i.e., have `Tcl_Mutex` etc. be typedefs to structures. Some analysis of the `win` and `generic` portions of the source base show many, many instances of double-checked locking, especially with regards to initialization. Newer Unix portions do use `pthread_once_t` internally, but the code needs review as well. ## I think this should be resolved as part of the Tcl 9.0 releases, since changes are required to the API. - foundin initialized to: "all"
- is_private initialized to: "0"
- login: "konrad.schwarz"
- priority initialized to: "5 Medium"
- private_contact initialized to: "80ea11e9d893f90ae1b5d3c2e6c94600b62863e9"
- resolution initialized to: "None"
- severity initialized to: "Critical"
- status initialized to: "Open"
- submitter initialized to: "konrad.schwarz"
- subsystem initialized to: "41. Memory Allocation"
- title initialized to:
Double-checked locking pervades the Tcl implemetation
- type initialized to: "Bug"
Ticket change [044c20d98d] (rid 258043) by serhiy.storchaka on 2026-10-14 15:57:48:
- icomment:
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 [/timeline?r=double-checked-lock-ordering|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.
- login: "serhiy.storchaka"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [eb0f6f0fd9] (rid 261428) by jan.nijtmans on 2026-11-11 14:27:19:
- assignee changed to: "jan.nijtmans"
- login: "jan.nijtmans"
- mimetype: "text/plain"