Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: eb085770be9f8eb607d3d8914222818feba14ab0a848e208d698e883cea95a14
Ticket: 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
Nested mutexes following TIP 509
User & Date: chw on 2025-08-24 05:37:10
Changes

  1. icomment:
    It gets worse every day: now read this fine print quoted from
    https://forums.oracle.com/ords/apexds/post/recursive-mutex-and-conditional-wait-4703
    
      In pthread_mutexattr_settype man page on Solaris 7, it is stated that
      "It is advised that an application should not use a PTHREAD_MUTEX_RECURSIVE mutex
      with condition variables PTHREAD_MUTEX_RECURSIVE because the implicit unlock performed
      for a pthread_cond_wait() or pthread_cond_timedwait() will not actually release the
      mutex (if it had been locked multiple times)...
    
    So the combination of recursive mutex and condition variable is
    a classical two component explosive. And my initial thoughts on
    trying to fix it for systems which natively do not support
    recursive mutexes thus were most likely a complete waste of time.
    
    Now, when we take the quoted statement (which is 25 years old) serious
    we should provide both recursive and plain mutexes or ditch recursive
    mutexes entirely. I'm in favor of the latter (and am still not sorry
    for my notorious noise).
    
    BTW: here is a snippet with which the quoted statement can nicely be
    reproduced (it panics) when the snippet is compiled in tcl trunk's ready
    built unix subdirectory:
    
    ---snip---
    #include <tcl.h>
    
    TCL_DECLARE_MUTEX(mutex);
    Tcl_Condition cond;
    static int deadlock;
    static int stopit;
    static int state;
    
    static Tcl_ThreadCreateType
    WatchDog(void *arg)
    {
        int n = 0;
    
        while (!stopit && n < 20) {
            Tcl_Sleep(100);
            ++n;
        }
        if (deadlock) {
            Tcl_Panic("Deadlock detected");
        }
        TCL_THREAD_CREATE_RETURN;
    }
    
    static Tcl_ThreadCreateType
    StrayCat(void *arg)
    {
        while (!stopit) {
            Tcl_MutexLock(&mutex);
            if (state == 0) {
                state = 1;
                Tcl_ConditionNotify(&cond);
            } else if (state == 1) {
                state = 2;
                Tcl_ConditionNotify(&cond);
            }
            Tcl_MutexUnlock(&mutex);
            Tcl_Sleep(100);
        }
        TCL_THREAD_CREATE_RETURN;
    }
    
    static void
    TestFunc(void)
    {
        Tcl_ThreadId tid1, tid2;
        int dummy;
    
        deadlock = 1;
        stopit = 0;
        state = 0;
        Tcl_CreateThread(&tid1, WatchDog, NULL,
                TCL_THREAD_STACK_DEFAULT, TCL_THREAD_JOINABLE);
        Tcl_MutexLock(&mutex);
        Tcl_CreateThread(&tid1, StrayCat, NULL,
                TCL_THREAD_STACK_DEFAULT, TCL_THREAD_JOINABLE);
        while (state != 1) {
            Tcl_ConditionWait(&cond, &mutex, NULL);
        }
        Tcl_MutexLock(&mutex);
        while (state != 2) {
            Tcl_ConditionWait(&cond, &mutex, NULL);
        }
        Tcl_MutexUnlock(&mutex);
        Tcl_MutexUnlock(&mutex);
        stopit = 1;
        Tcl_JoinThread(tid2, &dummy);
        deadlock = 0;
        Tcl_JoinThread(tid1, &dummy);
    }
    
    int
    main(int argc, char **argv)
    {
        TestFunc();
        return 0;
    }
    
    /*
     * Local Variables:
     * mode: c
     * c-basic-offset: 4
     * fill-column: 78
     * compile-command: "cc test.c -I../generic -I. -L. -ltcl9.1"
     * End:
     */
    
  2. login: "chw"
  3. mimetype: "text/plain"