Tcl Source Code

View Ticket
Login
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:

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).

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:

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.

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.