Overview
| Artifact ID: | 01355e6e5c6b5e15d62d91523476799beb5c6aedf9b7f8c838b8223974137f69 |
|---|---|
| Ticket: | 112af76b090286f9f0f04c8b994c252bb1b82bd8
Double-checked locking pervades the Tcl implemetation |
| User & Date: | konrad.schwarz on 2023-05-21 20:35:27 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-markdown"
- comment changed 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 changed to: "all"
- is_private changed to: "0"
- login: "konrad.schwarz"
- priority changed to: "5 Medium"
- private_contact changed to: "80ea11e9d893f90ae1b5d3c2e6c94600b62863e9"
- resolution changed to: "None"
- severity changed to: "Critical"
- status changed to: "Open"
- submitter changed to: "konrad.schwarz"
- subsystem changed to: "41. Memory Allocation"
- title changed to:
Double-checked locking pervades the Tcl implemetation
- type changed to: "Bug"