Changeset ca8c91ce for doc/theses


Ignore:
Timestamp:
May 4, 2023, 8:54:17 AM (12 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
cb69fba
Parents:
d5c5586
Message:

small changes to channel chapter

Location:
doc/theses/colby_parsons_MMAth
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/colby_parsons_MMAth/style/style.tex

    rd5c5586 rca8c91ce  
    77
    88\newcommand{\code}[1]{\lstinline[language=CFA]{#1}}
     9\newcommand{\Go}[1]{\lstinline[language=Go]{#1}}
    910\newcommand{\uC}{$\mu$\CC}
    1011\newcommand{\PAB}[1]{{\color{red}PAB: #1}}
  • doc/theses/colby_parsons_MMAth/text/channels.tex

    rd5c5586 rca8c91ce  
    1414Kahn's language restricts a reading process to only wait for data on a single channel at a time and different writing processes cannot send data on the same channel.
    1515Hoare's language restricts ...
    16 Channels as a programming language feature has been popularized in recent years due to the language Go, which encourages the use of channels as its fundamental concurrent feature.
     16Channels as a programming language feature has been popularized in recent years by the language Go, which encourages the use of channels as its fundamental concurrent feature.
    1717Go's restrictions are ...
    1818\CFA channels do not have these restrictions.
     
    3737\end{enumerate}
    3838
    39 The order values are processed by the consumer does not affect the correctness of the producer-consumer problem.
     39In general, the order values are processed by the consumer does not affect the correctness of the producer-consumer problem.
    4040For example, the buffer can be LIFO, FIFO, or prioritized with respect to insertion and removal.
    4141However, like MX, a buffer should ensure every value is eventually removed after some reasonable bounded time (no long-term starvation).
     
    4747Fairness among threads is called \gls{fcfs} and was defined by Lamport~\cite[p.~454]{Lamport74}.
    4848\gls{fcfs} is defined in relation to a doorway~\cite[p.~330]{Lamport86II}, which is the point at which an ordering among threads can be established.
    49 Given this doorway, a critical section is said to be \gls{fcfs}, if threads access the shared resource in the order they proceed through the doorway.
    50 A consequence of \gls{fcfs} execution is the elimination of \Newterm{barging}, where barging means a thread arrives at a CS with waiting threads, and the MX protecting the CS allows the thread to enter the CS ahead of one or more of the waiting threads.
    51 
    52 \gls{fcfs} is a fairness property which prevents unequal access to the shared resource and prevents starvation, however it can come at a cost.
    53 Implementing an algorithm with \gls{fcfs} can lead to double blocking, where entering threads may need to block to allow other threads to proceed first, resulting in blocking both inside and outside the doorway.
    54 As such algorithms that are not \gls{fcfs} may be more performant but that performance comes with the downside of likely introducing starvation and unfairness.
     49Given this doorway, a CS is said to be \gls{fcfs}, if threads access the shared resource in the order they proceed through the doorway.
     50A consequence of \gls{fcfs} execution is the elimination of \Newterm{barging}, where barging means a thread arrives at a CS with waiting threads, and the MX protecting the CS allows the arriving thread to enter the CS ahead of one or more of the waiting threads.
     51
     52\gls{fcfs} is a fairness property that prevents unequal access to the shared resource and prevents starvation, however it comes at a cost.
     53Implementing an algorithm with \gls{fcfs} can lead to double blocking, where arriving threads block outside the doorway waiting for a thread in the lock entry-protocol and inside the doorway waiting for a thread in the CS.
     54An analogue is boarding an airplane: first you wait to get through security to the departure gates (short term), and then wait again at the departure gate for the airplane (long term).
     55As such, algorithms that are not \gls{fcfs} (barging) can be more performant by skipping the wait for the CS and entering directly;
     56however, this performance gain comes by introducing unfairness with possible starvation for waiting threads.
    5557
    5658\section{Channel Implementation}
    5759Currently, only the Go programming language~\cite{Go} provides user-level threading where the primary communication mechanism is channels.
    58 Furthermore, my analysis Go's channel communication is that it is very performant.
    59 Therefore, the low-level channel implementation in \CFA is largely copied from the Go implementation, but adapted to the \CFA type and runtime systems.
    60 A number of alternative implementations were tried in \CFA, but the Go implementation always beat other approaches.
    61 
    62 \PAB{Discuss the Go channel implementation. Need to tie in FIFO buffer and FCFS locking.}
    63 In this work, all channels are implemented with bounded buffers.
    64 
    6560Experiments were conducted that varied the producer-consumer problem algorithm and lock type used inside the channel.
    6661With the exception of non-\gls{fcfs} algorithms, no algorithm or lock usage in the channel implementation was found to be consistently more performant that Go's choice of algorithm and lock implementation.
     62Therefore, the low-level channel implementation in \CFA is largely copied from the Go implementation, but adapted to the \CFA type and runtime systems.
    6763As such the research contributions added by \CFA's channel implementation lie in the realm of safety and productivity features.
     64
     65\PAB{Discuss the Go channel implementation. Need to tie in FIFO buffer and FCFS locking.}
     66In this work, all channels are implemented with bounded buffers, so there is no zero-sized buffering.
    6867
    6968\section{Safety and Productivity}
     
    7271
    7372\begin{itemize}
    74 \item Toggle-able statistic collection on channel behaviour that counts channel operations, and the number of the operations that block.
    75 Tracking blocking operations helps users tune their channel size or channel usage when the channel is used for buffering, where the aim is to have as few blocking operations as possible.
     73\item Toggle-able statistic collection on channel behaviour that count channel and blocking operations.
     74Tracking blocking operations helps illustrate usage and then tune the channel size, where the aim is to reduce blocking.
    7675\item Deadlock detection on deallocation of the channel.
    77 If any threads are blocked inside the channel when it terminates it is detected and informs the user, as this would cause a deadlock.
    78 \item A \code{flush} routine that delivers copies of an element to all waiting consumers, flushing the buffer.
     76If threads are blocked inside the channel when it terminates, this case is detected and the user is informed, as this can cause a deadlock.
     77\item A @flush@ routine that delivers copies of an element to all waiting consumers, flushing the buffer.
    7978Programmers can use this to easily to broadcast data to multiple consumers.
    80 Additionally, the \code{flush} routine is more performant then looping around the \code{insert} operation since it can deliver the elements without having to reacquire mutual exclusion for each element sent.
     79Additionally, the @flush@ routine is more performant then looping around the @insert@ operation since it can deliver the elements without having to reacquire mutual exclusion for each element sent.
    8180\end{itemize}
    8281
     
    9291% C_TODO: add reference to select chapter, add citation to go channels info
    9392Go channels provide a set of tools to help with concurrent shutdown.
    94 Channels in Go have a \code{close} operation and a \code{select} statement that both can be used to help threads terminate.
    95 The \code{select} statement will be discussed in \ref{}, where \CFA's \code{waituntil} statement will be compared with the Go \code{select} statement.
    96 The \code{close} operation on a channel in Go changes the state of the channel.
    97 When a channel is closed, sends to the channel will panic and additional calls to \code{close} will panic.
     93Channels in Go have a @close@ operation and a \Go{select} statement that both can be used to help threads terminate.
     94The \Go{select} statement will be discussed in \ref{}, where \CFA's @waituntil@ statement will be compared with the Go \Go{select} statement.
     95The @close@ operation on a channel in Go changes the state of the channel.
     96When a channel is closed, sends to the channel will panic and additional calls to @close@ will panic.
    9897Receives are handled differently where receivers will never block on a closed channel and will continue to remove elements from the channel.
    9998Once a channel is empty, receivers can continue to remove elements, but will receive the zero-value version of the element type.
    10099To aid in avoiding unwanted zero-value elements, Go provides the ability to iterate over a closed channel to remove the remaining elements.
    101 These design choices for Go channels enforce a specific interaction style with channels during termination, where careful thought is needed to ensure that additional \code{close} calls don't occur and that no sends occur after channels are closed.
     100These design choices for Go channels enforce a specific interaction style with channels during termination, where careful thought is needed to ensure that additional @close@ calls don't occur and that no sends occur after channels are closed.
    102101These design choices fit Go's paradigm of error management, where users are expected to explicitly check for errors, rather than letting errors occur and catching them.
    103102If errors need to occur in Go, return codes are used to pass error information where they are needed.
     
    106105While Go's channel closing semantics are powerful enough to perform any concurrent termination needed by a program, their lack of ease of use leaves much to be desired.
    107106Since both closing and sending panic, once a channel is closed, a user often has to synchronize the senders to a channel before the channel can be closed to avoid panics.
    108 However, in doing so it renders the \code{close} operation nearly useless, as the only utilities it provides are the ability to ensure that receivers no longer block on the channel, and will receive zero-valued elements.
    109 This can be useful if the zero-typed element is recognized as a sentinel value, but if another sentinel value is preferred, then \code{close} only provides its non-blocking feature.
    110 To avoid \gls{toctou} issues during shutdown, a busy wait with a \code{select} statement is often used to add or remove elements from a channel.
     107However, in doing so it renders the @close@ operation nearly useless, as the only utilities it provides are the ability to ensure that receivers no longer block on the channel, and will receive zero-valued elements.
     108This can be useful if the zero-typed element is recognized as a sentinel value, but if another sentinel value is preferred, then @close@ only provides its non-blocking feature.
     109To avoid \gls{toctou} issues during shutdown, a busy wait with a \Go{select} statement is often used to add or remove elements from a channel.
    111110Due to Go's asymmetric approach to channel shutdown, separate synchronization between producers and consumers of a channel has to occur during shutdown.
    112111
     
    116115The difference between the exception handling mechanisms arises after the exception is handled.
    117116In termination handling, the control flow continues into the code following the catch after the exception is handled.
    118 In resumption handling, the control flow returns to the site of the \code{throw}, allowing the control to continue where it left off.
     117In resumption handling, the control flow returns to the site of the @throw@, allowing the control to continue where it left off.
    119118Note that in resumption, since control can return to the point of error propagation, the stack is not unwound during resumption propagation.
    120119In \CFA if a resumption is not handled, it is reraised as a termination.
     
    127126These termination exceptions allow for non-local transfer that can be used to great effect to eagerly and gracefully shut down a thread.
    128127When a channel is closed, if there are any blocked producers or consumers inside the channel, they are woken up and also have a resumption thrown at them.
    129 The resumption exception, \code{channel_closed}, has a couple fields to aid in handling the exception.
     128The resumption exception, @channel_closed@, has a couple fields to aid in handling the exception.
    130129The exception contains a pointer to the channel it was thrown from, and a pointer to an element.
    131130In exceptions thrown from remove the element pointer will be null.
     
    141140Both of these examples are implemented using \CFA syntax so that they can be easily compared.
    142141Listing~\ref{l:go_chan_bar} uses go-style channel close semantics and Listing~\ref{l:cfa_chan_bar} uses \CFA close semantics.
    143 In this problem it is infeasible to use the Go \code{close} call since all tasks are both potentially producers and consumers, causing panics on close to be unavoidable.
     142In this problem it is infeasible to use the Go @close@ call since all tasks are both potentially producers and consumers, causing panics on close to be unavoidable.
    144143As such in Listing~\ref{l:go_chan_bar} to implement a flush routine for the buffer, a sentinel value of $-1$ has to be used to indicate to threads that they need to leave the barrier.
    145144This sentinel value has to be checked at two points.
    146 Furthermore, an additional flag \code{done} is needed to communicate to threads once they have left the barrier that they are done.
     145Furthermore, an additional flag @done@ is needed to communicate to threads once they have left the barrier that they are done.
    147146This use of an additional flag or communication method is common in Go channel shutdown code, since to avoid panics on a channel, the shutdown of a channel often has to be communicated with threads before it occurs.
    148147In the \CFA version~\ref{l:cfa_chan_bar}, the barrier shutdown results in an exception being thrown at threads operating on it, which informs the threads that they must terminate.
     
    272271
    273272In Listing~\ref{l:cfa_resume} an example of channel closing with resumption is used.
    274 This program uses resumption in the \code{Consumer} thread main to ensure that all elements in the channel are removed before the consumer thread terminates.
    275 The producer only has a \code{catch} so the moment it receives an exception it terminates, whereas the consumer will continue to remove from the closed channel via handling resumptions until the buffer is empty, which then throws a termination exception.
     273This program uses resumption in the @Consumer@ thread main to ensure that all elements in the channel are removed before the consumer thread terminates.
     274The producer only has a @catch@ so the moment it receives an exception it terminates, whereas the consumer will continue to remove from the closed channel via handling resumptions until the buffer is empty, which then throws a termination exception.
    276275If the same program was implemented in Go it would require explicit synchronization with both producers and consumers by some mechanism outside the channel to ensure that all elements were removed before task termination.
    277276
Note: See TracChangeset for help on using the changeset viewer.