\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#435: Safe Mutex Disposal API}
\date{May 16, 2015}
\author{Donal Fellows, Joe Mistachkin}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#435}&\textbf{Safe Mutex Disposal API}\\\hline
Author&

Donal Fellows \tipmail{donal.k.fellows}{manchester.ac.uk}
\par
Joe Mistachkin \tipmail{joe}{mistachkin.com}
\\
Created&Saturday, \(\text{16}^{\text{th}}\) May 2015\\
Type&Project\\
Tcl Version&\(8.6.5\)\\
State&Rejected\\
Vote&Done\\
Version&\tipversion$Revision: 1.7 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes a new C API for improving mutex deletion.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{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 \textit{global} lock that protects the disposal code, except that during the cleanup immediately after a fork (during the \textbf{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.

\section{Specification}

This TIP specifies a single new function:

\begin{quote}
void \textbf{Tcl\_MutexUnlockAndFinalize}(Tcl\_Mutex *\textit{mutex});
\end{quote}

The \textbf{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 \textbf{Tcl\_MutexLock}.

\section{Implementation}

See branch bug-57945b574a.

\section{Acknowlegement}

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

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
