Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a580cf581d426279b85e2d712e10e111393ee36e64d148fa82d68b0a4aaa7b01
Ticket: 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
Nested mutexes following TIP 509
User & Date: jan.nijtmans on 2025-10-01 09:01:53
Changes

  1. closedate changed to: "2460949.87631012"
  2. closer changed to: "jan.nijtmans"
  3. icomment:
    @ashok, +1 to all your remarks. You testcases are great, I'm a happy user.
    
    Next step in Christians reasoning (it's indeed about over-engineering) can be found [27ba494cc3a179e0|here]. We need to assure that pmutexPtr->thread is fully initialized when the mutex is used first. Idea: initialize pmutexPtr->thread _before_ initializing pmutexPtr->mutex. The resulting assembler-code for PMutexInit becomes much shorter:
    
    <pre>
            mov     QWORD PTR [rdi+40], 0; # pthread->thread = 0
            xor     esi, esi             ; # NULL argument of pthread_mutex_init
            mov     DWORD PTR [rdi+48], 0; # pthread->counter = 0
            jmp     pthread_mutex_init
    </pre>
    
    Since pthread_mutex_init addresses [rdi+0] and memory-operations are not re-ordered by the processor, this also assures that [rdi+40] is fully written to memory before [rdi+0]. We see that the null-argument of pthread_mutex_init() is set before pthread->counter is set, this gives pthread->thread a little bit more time to be really written to memory.
    
    Since memory-operations are not re-ordered by the processor, we can be sure this way that pthread->thread (and pthread->counter) are initialized before the lock is used. Without using any extra locks.
    
    With this change, PMutexLock() is the last function which uses atomic operations. But that's for another day.
    
  4. login: "jan.nijtmans"
  5. mimetype: "text/x-fossil-wiki"