TIP 435: Safe Mutex Disposal API

EuroTcl/OpenACS 11 - 12 JULY 2024, VIENNA
Author:		Donal Fellows <[email protected]>
Author:		Joe Mistachkin <[email protected]>
State:		Rejected
Type:		Project
Tcl-Version:	8.6.5
Vote:		Done
Created:	16-May-2015


This TIP proposes a new C API for improving mutex deletion.


Context: Bug #57945b574a

There is a race condition in the code that disposes of mutexes, in that a mutex must only be disposed of when it is not in use by another thread, yet the disposal code does not lock it. This would not be a particular problem as there is a global lock that protects the disposal code, except that during the cleanup immediately after a fork (during the exec command, for example) things can get deeply confused, and trigger deadlocks under heavy load. We need to be careful and make sure that we really hold the global lock when unlocking and disposing mutexes.

Because the pipeline-opening code isn't the only thing that might ever fork internally, we should provide the capability to do this correctly as part of Tcl's public API.


This TIP specifies a single new function:

void Tcl_MutexUnlockAndFinalize(Tcl_Mutex *mutex);

The Tcl_MutexUnlockAndFinalize function (which takes a single argument, the mutex to operate on) will atomically unlock the mutex and dispose of it without giving an opportunity for another thread to lock the mutex between unlocking and disposal. The mutex must have previously been locked by Tcl_MutexLock.


See branch bug-57945b574a.


Thanks to Gustaf Neumann for his trouble tracking this down, and apologies for the problems the fault has caused him.


This document has been placed in the public domain.