# TIP 435: Safe Mutex Disposal API
	Author:		Donal Fellows <donal.k.fellows@manchester.ac.uk>
	Author:		Joe Mistachkin <joe@mistachkin.com>
	State:		Rejected
	Type:		Project
	Tcl-Version:	8.6.5
	Vote:		Done
	Created:	16-May-2015
	Post-History:	
-----

# Abstract

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

# Rationale

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.

# Specification

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**.

# Implementation

See branch bug-57945b574a.

# Acknowlegement

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

# Copyright

This document has been placed in the public domain.

