Overview
| Artifact ID: | fef2b0d4c98a2d051c1ce0d53e9b630c2d78fd214ba02b85e86a4a2e79297d0b |
|---|---|
| Ticket: | 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
Nested mutexes following TIP 509 |
| User & Date: | sebres on 2025-09-22 12:33:18 |
Changes
- icomment:
<blockquote>> What if we let "counter" only count the additional locks after the first one in a thread. Then we only update "counter" when we already have the lock</blockquote> But exactly that happened in original PMutexLock (what Ashok complained) and that was exactly my point - the counter would be only incremented if the thread acquired lock. The difficulty thereby (which is the potential for the hypothetical RC at the same time) to do this second time without locking twice (because otherwise it'd be an UB or even a deadlock). Thus one'd need 2 variables (lock owner and counter), which state cannot ever be changed atomically, only in lock and the safe handling will be achieved solely through correct and well thought out logic. The issues with RC start always on border line under certain circumstances (especially if there are many factors, like compiler optimization etc). Your change looks fine at first glance, however for unix, since in both of else-cases in PMutexLock, the mutex gets always locked (with pthread_mutex_lock), the atomic exchange is not really needed and then we are again by initial version. Windows version of WMutexLock looks similar (however doesn't compare `wmPtr->thread == mythread` in atomic way). As already said, I don't think even original version was affected by RC, although my doubts still remain more orthogonal to it - the performance losses, the cache washout and increased context switches. So I'd not stop to repeat - Tcl doesn't need recursive mutexes at all. And if yes - it'd be a "feature" either and one shall provide it as completely new API, not as replacement for non-reentrant mutexes.
- login: "sebres"
- mimetype: "text/x-fossil-wiki"