Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 746811083a280b5d317537d84de88d4a6a65fc3bfe7e3e58f4081a4685754aa5
Ticket: 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
Nested mutexes following TIP 509
User & Date: chw on 2025-10-03 16:07:34
Changes

  1. closedate changed to: "2460952.17193252"
  2. closer changed to: "chw"
  3. icomment:
    > Now take into account, that the pMutex->thread variable is not written atomically, then we could have:
    >
    >    00 => 0M => MM => M0 => 00 => 0Y => YY => Y0 => 00 
    
    If this claim were true, atomic read operations are most likely at no avail and thus no remedy and we then have the need of a pthread_spin_lock_t. This could indeed happen, when on a target platform a pthread_t were a complex struct, which is explicitly possible according to the opengroup.org explanation.
    
    To handle this case we could do: 
    
    ```
    #ifndef PTHREAD_NULL
    #   define PTHREAD_NULL (pthread_t)NULL
    #endif
    
    typedef struct PMutex {
        pthread_mutex_t mutex;
        pthread_spin_lock_t lock;  
        volatile pthread_t thread;
        int counter;
    } PMutex;
    
    static void
    PMutexInit(PMutex *pmutexPtr)
    {
        pthread_mutex_init(&pmutexPtr->mutex, NULL);
        if (sizeof(pmutexPtr->thread) != sizeof(void *)) {
    	pthread_spin_init(&pmutexPtr->lock, PTHREAD_PROCESS_PRIVATE);
        }
        pthread_mutex_lock(&pmutexPtr->mutex);
        pmutexPtr->thread = PTHREAD_NULL;
        pmutexPtr->counter = 0;
        pthread_mutex_unlock(&pmutexPtr->mutex);
    }
    
    static void
    PMutexDestroy(PMutex *pmutexPtr)
    {
        assert(pthread_equal(pmutexPtr->thread, PTHREAD_NULL) && !pmutexPtr->counter);
        if (sizeof(pmutexPtr->thread) != sizeof(void *)) {
    	pthread_spin_destroy(&pmutexPtr->lock);
        }
        pthread_mutex_destroy(&pmutexPtr->mutex);
    }
    
    static void
    PMutexLock(PMutex *pmutexPtr)
    {
        pthread_t mythread = pthread_self();
        int equal;
    
        if (sizeof(pmutexPtr->thread) != sizeof(void *)) {
            pthread_spin_lock(&pmutexPtr->lock);
        }
        equal = pthread_equal(pmutexPtr->thread, mythread);
        if (sizeof(mythread) != sizeof(void *)) {
            pthread_spin_unlock(&pmutexPtr->lock);
        }
        if (equal) {
    	pmutexPtr->counter++;
        } else {
    	pthread_mutex_lock(&pmutexPtr->mutex);
    	pmutexPtr->thread = mythread;
        }
    }
      
    static void
    PMutexUnlock(PMutex *pmutexPtr)
    {
        if (sizeof(pmutexPtr->thread) != sizeof(void *)) {
    #ifndef NDEBUG
    	int equal;
    #endif
    	assert(
    	    pthread_spin_lock(&pmutexPtr->lock),
    	    equal = pthread_equal(pmutexPtr->thread, pthread_self()),
    	    pthread_spin_unlock(&pmutexPtr->lock),
    	    equal
    	);
        } else {
    	assert(pthread_equal(pmutexPtr->thread, pthread_self()));
        }
        if (pmutexPtr->counter) {
    	pmutexPtr->counter--;
        } else {
    	pmutexPtr->thread = PTHREAD_NULL;
    	pthread_mutex_unlock(&pmutexPtr->mutex);
        }
    }  
    ```
    
    OK, you could argue, that there could be a pathological case were a sixteen core 128 bit system has a 16 bit memory interface. But does such a beast exist in practice?
    
  4. login: "chw"
  5. mimetype: "text/x-markdown"