Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 749570eea46ef458f918bb4d12a7b69cfae25ff45cbf62302c1a6865584d5e92
Ticket: 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
Nested mutexes following TIP 509
User & Date: chw on 2025-10-01 12:06:19
Changes

  1. closedate changed to: "2460950.00438751"
  2. closer changed to: "chw"
  3. icomment:
    Now lemme refine my reasoning:
    
    0. We understand from the opengroup.org explanation, that both, mutex lock and unlock operations perform some memory barrier or fence magic to ensure memory consistency of the memory protected by the mutex.
    
    1. Then in our mutex init function we can do  
       pthread_init();  
       pthread_mutex_lock();  
       set thread id to PTHREAD_NULL  
       set counter to 0  
       pthread_mutex_unlock();  
    
    2. We have ensured, that our home grown mutex is usable only after its init function has completed. Therefore the thread id in our freshly initialized mutex is safely set to PTHREAD_NULL.
    
    3. In our mutex lock function we test the thread id of the mutex against our own thread id. This need not be atomic since we guarantee that we write the mutexes' thread id only when we really locked the native mutex.
    
    4. In our mutex unlock function we write the mutexes' thread id to PTHREAD_NULL before we unlock the native mutex to indicate that we gave up ownership.
    
    When the mutexes' thread id is read unlocked for testing of ownership, it thus can have these three possible values:
    
    1. PTHREAD_NULL
    2. own thread id
    3. some other thread id
    
    The test in lock and unlock (and in the condition variable cases) looks for the mutexes' thread id being equal to our own thread id (which implies that we hold the native mutex) or anything else including PTHREAD_NULL.
    
    So the conclusion is that we do not need atomic reads for the tests.
    
    Other than that, to enhance portability, please use pthread_equal() for testing thread ids and use PTHREAD_NULL (with an own definition to 0 if needed) instead of assigning 0 directly.
    
  4. login: "chw"
  5. mimetype: "text/x-markdown"