Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 55d85e623b72bcd76a03f3cf8cac1fb1e098194a7f61f1266588b4fed4645bf8
Ticket: 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
Nested mutexes following TIP 509
User & Date: sebres on 2025-09-26 10:19:36
Changes

  1. closedate changed to: "2460944.93028029"
  2. closer changed to: "sebres"
  3. icomment:
    <blockquote>> Still, in this case no harm is done because even in the very unlikely case this happens, the only effect is a needless encoding change and epoch bump.</blockquote>
    
    Or vice versa, it may not detect the change of encoding by another thread, but then, it'd return before lock (and think the encoding is still the same as given).
    Both effects are simple a timing issue and a lock has nothing to do with that. Few nanoseconds earlier or later it'd behave different and the last thread wins here, no matter with or without lock.
    
    <blockquote>> So we have 2 dogma's: "thy shall not use recursive mutex" and "thy shall use a single lock within a compare-and-modify operation". </blockquote>
    
    No. The "usage of single lock" is mostly the same as "not use recursive mutex"... in that particular case it'd be enough to provide GetEncoding() without lock (in opposite to Tcl_GetEncoding(), similar to FreeEncoding()/Tcl_FreeEncoding() pair) and use that GetEncoding within the single lock inside Tcl_SetSystemEncoding. But as already convinced - unneeded here, because there is no harm at all.
    
    Recursive mutex can be only needed if in some case is too hard to predict whether mutex was already locked by current thread or not. 99.99% of any cases may be rewritten to guarantee that, especially if one'd keep in mind that to be optimal a lock must be as short as possible, what is contradicting with a recursion a bit. Yes, there are surely cases where a recursion shall be protected by mutex, but it is never unavoidable, so either it can be protected once at first level (or outside) or much better unfolded to cycle or data items somehow marked as busy on every iteration... There are many known programming patterns to achieve that.
    
    <blockquote>> we could implement an internal TclMutexStrictLock() function which does the same as Tcl_MutexLock(), except it panics when it is called recursively.</blockquote>
    
    Well, the implementation like this would get all cons of recursive mutex and has no pros at all, excepting a possible panic instead of hang in case of nesting invocation. The idea is not bad, but for debug (+ assert) mode maybe.
    
    Anyway, I think the best we can do would be to revert everything to non-recursive mutexes and to provide recursive mutexes as new API, e. g. like Tcl_RecMutexLock() / Tcl_RecMutexUnlock(). But, as already said it is something outside of best practice and the percent of real use-cases for that are so measly, and if really needed can be almost always achieved on user side without to have reentrant mutexes.
    
  4. login: "sebres"
  5. mimetype: "text/x-fossil-wiki"