% ======================================================================
% ======================================================================
\chapter{Introduction}\label{s:intro}
% ======================================================================
% ======================================================================

Concurrent programs are the wild west of programming because determinism and simple ordering of program operations go out the window.
To seize the reins and write performant and safe concurrent code, high-level concurrent-language features are needed.
Like any other craftsmen, programmers are only as good as their tools, and concurrent tooling and features are no exception.

This thesis presents a suite of high-level concurrent-language features implemented in the new programming-language \CFA.
These features aim to improve the performance of concurrent programs, aid in writing safe programs, and assist user productivity by improving the ease of concurrent programming.
The groundwork for concurrent features in \CFA was designed and implemented by Thierry Delisle~\cite{Delisle18}, who contributed the threading system, coroutines, monitors and other basic concurrency tools.
This thesis builds on top of that foundation by providing a suite of high-level concurrent features.
The features include a @mutex@ statement, channels, a @waituntil@ statement, and an actor system.
All of these features exist in other programming languages in some shape or form;
however, this thesis extends the original ideas by improving performance, productivity, and safety.

\section{The Need For Concurrent Features}

% Asking a programmer to write a complex concurrent program without any concurrent language features is asking them to undertake a very difficult task.
% They would only be able to rely on the atomicity that their hardware provides and would have to build up from there.
% This would be like asking a programmer to write a complex sequential program only in assembly.
% Both are doable, but would often be easier and less error prone with higher level tooling.

Concurrent programming has many unique pitfalls that do not appear in sequential programming:
\begin{enumerate}
\item Race conditions, where thread orderings can result in arbitrary behaviours, resulting in correctness problems.
\item Livelock, where threads constantly attempt a concurrent operation unsuccessfully, resulting in no progress being made.
\item Starvation, where \emph{some} threads constantly attempt a concurrent operation unsuccessfully, resulting in partial progress being made.
\item Deadlock, where some threads wait for an event that cannot occur, blocking them indefinitely, resulting in no progress being made.
\end{enumerate}
Even with the guiding hand of concurrent tools these pitfalls still catch unwary programmers, but good language support helps significantly to prevent, detect, and mitigate these problems.

\section{Thesis Overview}

Chapter~\ref{s:cfa} of this thesis aims to familiarize the reader with the language \CFA.
In this chapter, syntax and features of the \CFA language that appear in this work are discussed.
Chapter~\ref{s:cfa_concurrency} briefly discusses prior concurrency work in \CFA, and how the work in this thesis builds on top of the existing framework.
Each remaining chapter introduces an additional \CFA concurrent-language feature, which includes discussing prior related work for the feature, extensions over prior features, and uses benchmarks to compare the performance the feature with corresponding or similar features in other languages and systems.

Chapter~\ref{s:mutexstmt} discusses the @mutex@ statement, a language feature that provides safe and simple lock usage.
The @mutex@ statement is compared both in terms of safety and performance with similar mechanisms in \CC and Java.
Chapter~\ref{s:channels} discusses channels, a message passing concurrency primitive that provides for safe synchronous and asynchronous communication among threads.
Channels in \CFA are compared to Go's channels, which popularized the use of channels in modern concurrent programs.
Chapter~\ref{s:actors} discusses the \CFA actor system.
An actor system is a close cousin of channels, as it also belongs to the message passing paradigm of concurrency.
However, an actor system provides a greater degree of abstraction and ease of scalability, making it useful for a different range of problems than channels.
The actor system in \CFA is compared with a variety of other systems on a suite of benchmarks.
Chapter~\ref{s:waituntil} discusses the \CFA @waituntil@ statement, which provides the ability to synchronize while waiting for a resource, such as acquiring a lock, accessing a future, or writing to a channel.
The \CFA @waituntil@ statement provides greater flexibility and expressibility than similar features in other languages.
All in all, the features presented aim to fill in gaps in the current \CFA concurrent-language support, enabling users to write a wider range of complex concurrent programs with ease.

\section{Contributions}
This work presents the following contributions within each of the additional language features:
\begin{enumerate}[leftmargin=*]
\item The @mutex@ statement that:
    \begin{itemize}[itemsep=0pt]
    \item
    provides deadlock-free multiple lock acquisition,
    \item
    clearly denotes lock acquisition and release,
    \item
    and has good performance irrespective of lock ordering.
    \end{itemize}
\item The channel that:
\begin{itemize}[itemsep=0pt]
    \item
    achieves comparable performance to Go, the gold standard for concurrent channels,
    \item
    has deadlock detection,
    \item
    introduces easy-to-use exception-based @close@ semantics,
    \item
    and provides toggle-able statistics for performance tuning.
\end{itemize}
\item The in-memory actor system that:
\begin{itemize}[itemsep=0pt]
    \item
    achieves the lowest latency message send of all tested systems,
    \item
    is the first inverted actor system to introduce queue stealing,
    \item
    attains zero-victim-cost stealing through a carefully constructed stealing mechanism,
    \item
    gains performance through static-typed message sends, eliminating the need for dynamic dispatch,
    \item
    introduces the copy queue, an array-based queue specialized for the actor use-case to minimize calls to the memory allocator,
    \item
    has robust detection of six tricky, but common actor programming errors,
    \item
    achieves very good performance on a diverse benchmark suite compared to other actor systems,
    \item
    and provides toggle-able statistics for performance tuning.
\end{itemize}
\item The @waituntil@ statement that:
\begin{itemize}[itemsep=0pt]
    \item
    is the only known polymorphic synchronous multiplexing language feature,
    \item
    provides greater expressibility for waiting conditions than other languages,
    \item
    and achieves comparable performance to similar features in two other languages.
\end{itemize}
\end{enumerate}
