Changeset 3d5fba21 for doc


Ignore:
Timestamp:
Apr 8, 2023, 3:49:44 PM (14 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
beabdf3
Parents:
16dff44
Message:

formatting, replace latex package subcaption with subfig

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/colby_parsons_MMAth/text/channels.tex

    r16dff44 r3d5fba21  
    55% ======================================================================
    66
    7 Channels were first introduced by Hoare in his paper Communicating Sequentual Processes~\cite{Hoare78}, where he proposes a concurrent language that communicates across processes using input/output channels to send data. 
    8 Channels are a concurrent language feature used to perform message passing concurrency, a model of concurrency where threads communicate by sending data as messages, and synchronizing via the message passing mechanism. 
    9 This is an alternative to shared memory concurrency, where threads can communicate directly by changing shared memory state. 
    10 Most modern concurrent programming languages do not subscribe to just one style of communication between threads, and provide features that support both. 
     7Channels were first introduced by Hoare in his paper Communicating Sequentual Processes~\cite{Hoare78}, where he proposes a concurrent language that communicates across processes using input/output channels to send data.
     8Channels are a concurrent language feature used to perform message passing concurrency, a model of concurrency where threads communicate by sending data as messages, and synchronizing via the message passing mechanism.
     9This is an alternative to shared memory concurrency, where threads can communicate directly by changing shared memory state.
     10Most modern concurrent programming languages do not subscribe to just one style of communication between threads, and provide features that support both.
    1111Channels 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.
    1212
    1313\section{Producer-Consumer Problem}
    14 Most channels in modern programming languages are built on top of a shared memory buffer. 
    15 While it is possible to create a channel that contains an unbounded buffer, most implementations opt to only support a fixed size channel, where the size is given at the time of channel creation. 
    16 This turns the implementation of a channel into the producer-consumer problem. 
    17 The producer-consumer problem, also known as the bounded buffer problem, was introduced by Dijkstra in his book Cooperating Sequential Processes\cite{Dijkstra65}. 
    18 In the problem threads interact with the buffer in two ways, either consuming values by removing them from the buffer, or producing values and inserting them in the buffer. 
    19 The buffer needs to be protected from concurrent access since each item in the buffer should only be produced and consumed once. 
     14Most channels in modern programming languages are built on top of a shared memory buffer.
     15While it is possible to create a channel that contains an unbounded buffer, most implementations opt to only support a fixed size channel, where the size is given at the time of channel creation.
     16This turns the implementation of a channel into the producer-consumer problem.
     17The producer-consumer problem, also known as the bounded buffer problem, was introduced by Dijkstra in his book Cooperating Sequential Processes\cite{Dijkstra65}.
     18In the problem threads interact with the buffer in two ways, either consuming values by removing them from the buffer, or producing values and inserting them in the buffer.
     19The buffer needs to be protected from concurrent access since each item in the buffer should only be produced and consumed once.
    2020Additionally, a consumer can only remove from a non-empty buffer and a producer can only insert into a non-full buffer.
    2121
    2222\section{First-Come First-Served}
    23 The channel implementations that will be discussed are \gls{fcfs}. 
    24 This term was defined by Lamport~\cite{Lamport74}. 
    25 \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. 
    26 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. 
    27 \gls{fcfs} is a fairness property which prevents unequal access to the shared resource and prevents starvation, however it can come at a cost. 
    28 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. 
     23The channel implementations that will be discussed are \gls{fcfs}.
     24This term was defined by Lamport~\cite{Lamport74}.
     25\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.
     26Given 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.
     27\gls{fcfs} is a fairness property which prevents unequal access to the shared resource and prevents starvation, however it can come at a cost.
     28Implementing 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.
    2929As such algorithms that are not \gls{fcfs} may be more performant but that performance comes with the downside of likely introducing starvation and unfairness.
    3030
    3131\section{Channel Implementation}
    32 The channel implementation in \CFA is a near carbon copy of the Go implementation. 
    33 Experimentation was conducted that varied the producer-consumer problem algorithm and lock type used inside the channel. 
    34 With 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. 
     32The channel implementation in \CFA is a near carbon copy of the Go implementation.
     33Experimentation was conducted that varied the producer-consumer problem algorithm and lock type used inside the channel.
     34With 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.
    3535As such the research contributions added by \CFA's channel implementation lie in the realm of safety and productivity features.
    3636
    3737\section{Safety and Productivity}
    38 Channels in \CFA come with safety and productivity features to aid users. 
     38Channels in \CFA come with safety and productivity features to aid users.
    3939The features include the following.
    4040
    4141\begin{itemize}
    42 \item Toggle-able statistic collection on channel behvaiour that counts channel operations, and the number of the operations that block. 
     42\item Toggle-able statistic collection on channel behvaiour that counts channel operations, and the number of the operations that block.
    4343Tracking 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.
    44 \item Deadlock detection on deallocation of the channel. 
     44\item Deadlock detection on deallocation of the channel.
    4545If any threads are blocked inside the channel when it terminates it is detected and informs the user, as this would cause a deadlock.
    46 \item A \code{flush} routine that delivers copies of an element to all waiting consumers, flushing the buffer. 
    47 Programmers can use this to easily to broadcast data to multiple consumers. 
     46\item A \code{flush} routine that delivers copies of an element to all waiting consumers, flushing the buffer.
     47Programmers can use this to easily to broadcast data to multiple consumers.
    4848Additionally, the \code{flush} routine is more performant then looping around the \code{insert} operation since it can deliver the elements without having to reaquire mutual exclusion for each element sent.
    4949\end{itemize}
    5050
    51 The other safety and productivity feature of \CFA channels deals with concurrent termination. 
    52 Terminating concurrent programs is often one of the most difficult parts of writing concurrent code, particularly if graceful termination is needed. 
    53 The difficulty of graceful termination often arises from the usage of synchronization primitives which need to be handled carefully during shutdown. 
    54 It is easy to deadlock during termination if threads are left behind on synchronization primitives. 
    55 Additionally, most synchronization primitives are prone to \gls{toctou} issues where there is race between one thread checking the state of a concurrent object and another thread changing the state. 
    56 \gls{toctou} issues with synchronization primitives often involve a race between one thread checking the primitive for blocked threads and another thread blocking on it. 
    57 Channels are a particularly hard synchronization primitive to terminate since both sending and receiving off a channel can block. 
     51The other safety and productivity feature of \CFA channels deals with concurrent termination.
     52Terminating concurrent programs is often one of the most difficult parts of writing concurrent code, particularly if graceful termination is needed.
     53The difficulty of graceful termination often arises from the usage of synchronization primitives which need to be handled carefully during shutdown.
     54It is easy to deadlock during termination if threads are left behind on synchronization primitives.
     55Additionally, most synchronization primitives are prone to \gls{toctou} issues where there is race between one thread checking the state of a concurrent object and another thread changing the state.
     56\gls{toctou} issues with synchronization primitives often involve a race between one thread checking the primitive for blocked threads and another thread blocking on it.
     57Channels are a particularly hard synchronization primitive to terminate since both sending and receiving off a channel can block.
    5858Thus, improperly handled \gls{toctou} issues with channels often result in deadlocks as threads trying to perform the termination may end up unexpectedly blocking in their attempt to help other threads exit the system.
    5959
    6060% C_TODO: add reference to select chapter, add citation to go channels info
    61 Go channels provide a set of tools to help with concurrent shutdown. 
    62 Channels in Go have a \code{close} operation and a \code{select} statement that both can be used to help threads terminate. 
    63 The \code{select} statement will be discussed in \ref{}, where \CFA's \code{waituntil} statement will be compared with the Go \code{select} statement. 
    64 The \code{close} operation on a channel in Go changes the state of the channel. 
    65 When a channel is closed, sends to the channel will panic and additional calls to \code{close} will panic. 
    66 Receives are handled differently where receivers will never block on a closed channel and will continue to remove elements from the channel. 
    67 Once a channel is empty, receivers can continue to remove elements, but will receive the zero-value version of the element type. 
    68 To aid in avoiding unwanted zero-value elements, Go provides the ability to iterate over a closed channel to remove the remaining elements. 
    69 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. 
    70 These 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. 
    71 If errors need to occur in Go, return codes are used to pass error information where they are needed. 
     61Go channels provide a set of tools to help with concurrent shutdown.
     62Channels in Go have a \code{close} operation and a \code{select} statement that both can be used to help threads terminate.
     63The \code{select} statement will be discussed in \ref{}, where \CFA's \code{waituntil} statement will be compared with the Go \code{select} statement.
     64The \code{close} operation on a channel in Go changes the state of the channel.
     65When a channel is closed, sends to the channel will panic and additional calls to \code{close} will panic.
     66Receives are handled differently where receivers will never block on a closed channel and will continue to remove elements from the channel.
     67Once a channel is empty, receivers can continue to remove elements, but will receive the zero-value version of the element type.
     68To aid in avoiding unwanted zero-value elements, Go provides the ability to iterate over a closed channel to remove the remaining elements.
     69These 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.
     70These 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.
     71If errors need to occur in Go, return codes are used to pass error information where they are needed.
    7272Note that panics in Go can be caught, but it is not considered an idiomatic way to write Go programs.
    7373
    74 While 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. 
    75 Since 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. 
    76 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. 
    77 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. 
    78 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. 
     74While 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.
     75Since 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.
     76However, 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.
     77This 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.
     78To 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.
    7979Due to Go's asymmetric approach to channel shutdown, separate synchronization between producers and consumers of a channel has to occur during shutdown.
    8080
     
    8282As such \CFA uses an exception based approach to channel shutdown that is symmetric for both producers and consumers, and supports graceful shutdown.Exceptions in \CFA support both termination and resumption.Termination exceptions operate in the same way as exceptions seen in many popular programming languages such as \CC, Python and Java.
    8383Resumption exceptions are a style of exception that when caught run the corresponding catch block in the same way that termination exceptions do.
    84 The difference between the exception handling mechanisms arises after the exception is handled. 
    85 In termination handling, the control flow continues into the code following the catch after the exception is handled. 
    86 In resumption handling, the control flow returns to the site of the \code{throw}, allowing the control to continue where it left off. 
    87 Note that in resumption, since control can return to the point of error propagation, the stack is not unwound during resumption propagation. 
    88 In \CFA if a resumption is not handled, it is reraised as a termination. 
     84The difference between the exception handling mechanisms arises after the exception is handled.
     85In termination handling, the control flow continues into the code following the catch after the exception is handled.
     86In resumption handling, the control flow returns to the site of the \code{throw}, allowing the control to continue where it left off.
     87Note that in resumption, since control can return to the point of error propagation, the stack is not unwound during resumption propagation.
     88In \CFA if a resumption is not handled, it is reraised as a termination.
    8989This mechanism can be used to create a flexible and robust termination system for channels.
    9090
    91 When a channel in \CFA is closed, all subsequent calls to the channel will throw a resumption exception at the caller. 
    92 If the resumption is handled, then the caller will proceed to attempt to complete their operation. 
    93 If the resumption is not handled it is then rethrown as a termination exception. 
    94 Or, if the resumption is handled, but the subsequent attempt at an operation would block, a termination exception is thrown. 
    95 These termination exceptions allow for non-local transfer that can be used to great effect to eagerly and gracefully shut down a thread. 
    96 When 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. 
    97 The resumption exception, \code{channel_closed}, has a couple fields to aid in handling the exception. 
    98 The exception contains a pointer to the channel it was thrown from, and a pointer to an element. 
    99 In exceptions thrown from remove the element pointer will be null. 
    100 In the case of insert the element pointer points to the element that the thread attempted to insert. 
    101 This element pointer allows the handler to know which operation failed and also allows the element to not be lost on a failed insert since it can be moved elsewhere in the handler. 
    102 Furthermore, due to \CFA's powerful exception system, this data can be used to choose handlers based which channel and operation failed. 
    103 Exception handlers in \CFA have an optional predicate after the exception type which can be used to optionally trigger or skip handlers based on the content of an exception. 
    104 It is worth mentioning that the approach of exceptions for termination may incur a larger performance cost during termination that the approach used in Go. 
     91When a channel in \CFA is closed, all subsequent calls to the channel will throw a resumption exception at the caller.
     92If the resumption is handled, then the caller will proceed to attempt to complete their operation.
     93If the resumption is not handled it is then rethrown as a termination exception.
     94Or, if the resumption is handled, but the subsequent attempt at an operation would block, a termination exception is thrown.
     95These termination exceptions allow for non-local transfer that can be used to great effect to eagerly and gracefully shut down a thread.
     96When 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.
     97The resumption exception, \code{channel_closed}, has a couple fields to aid in handling the exception.
     98The exception contains a pointer to the channel it was thrown from, and a pointer to an element.
     99In exceptions thrown from remove the element pointer will be null.
     100In the case of insert the element pointer points to the element that the thread attempted to insert.
     101This element pointer allows the handler to know which operation failed and also allows the element to not be lost on a failed insert since it can be moved elsewhere in the handler.
     102Furthermore, due to \CFA's powerful exception system, this data can be used to choose handlers based which channel and operation failed.
     103Exception handlers in \CFA have an optional predicate after the exception type which can be used to optionally trigger or skip handlers based on the content of an exception.
     104It is worth mentioning that the approach of exceptions for termination may incur a larger performance cost during termination that the approach used in Go.
    105105This should not be an issue, since termination is rarely an fast-path of an application and ensuring that termination can be implemented correctly with ease is the aim of the exception approach.
    106106
    107 To highlight the differences between \CFA's and Go's close semantics, an example program is presented. 
    108 The program is a barrier implemented using two channels shown in Listings~\ref{l:cfa_chan_bar} and \ref{l:go_chan_bar}. 
    109 Both of these exaples are implmented using \CFA syntax so that they can be easily compared. 
    110 Listing~\ref{l:go_chan_bar} uses go-style channel close semantics and Listing~\ref{l:cfa_chan_bar} uses \CFA close semantics. 
    111 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. 
    112 As 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. 
    113 This sentinel value has to be checked at two points. 
    114 Furthermore, an additional flag \code{done} is needed to communicate to threads once they have left the barrier that they are done. 
    115 This 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. 
    116 In 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. 
    117 This avoids the need to use a separate communication method other than the barrier, and avoids extra conditional checks on the fast path of the barrier implementation. 
     107To highlight the differences between \CFA's and Go's close semantics, an example program is presented.
     108The program is a barrier implemented using two channels shown in Listings~\ref{l:cfa_chan_bar} and \ref{l:go_chan_bar}.
     109Both of these exaples are implmented using \CFA syntax so that they can be easily compared.
     110Listing~\ref{l:go_chan_bar} uses go-style channel close semantics and Listing~\ref{l:cfa_chan_bar} uses \CFA close semantics.
     111In 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.
     112As 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.
     113This sentinel value has to be checked at two points.
     114Furthermore, an additional flag \code{done} is needed to communicate to threads once they have left the barrier that they are done.
     115This 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.
     116In 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.
     117This avoids the need to use a separate communication method other than the barrier, and avoids extra conditional checks on the fast path of the barrier implementation.
    118118Also note that in the Go version~\ref{l:go_chan_bar}, the size of the barrier channels has to be larger than in the \CFA version to ensure that the main thread does not block when attempting to clear the barrier.
    119119
    120120\begin{cfa}[tabsize=3,caption={\CFA channel barrier termination},label={l:cfa_chan_bar}]
    121121struct barrier {
    122     channel( int ) barWait;
    123     channel( int ) entryWait;
    124     int size;
     122        channel( int ) barWait;
     123        channel( int ) entryWait;
     124        int size;
    125125}
    126126void ?{}(barrier & this, int size) with(this) {
    127     barWait{size};
    128     entryWait{size};
    129     this.size = size;
    130     for ( j; size )
    131         insert( *entryWait, j );
     127        barWait{size};
     128        entryWait{size};
     129        this.size = size;
     130        for ( j; size )
     131                insert( *entryWait, j );
    132132}
    133133
    134134void flush(barrier & this) with(this) {
    135     close(barWait);
    136     close(entryWait);
     135        close(barWait);
     136        close(entryWait);
    137137}
    138138void wait(barrier & this) with(this) {
    139     int ticket = remove( *entryWait );
    140     if ( ticket == size - 1 ) {
    141         for ( j; size - 1 )
    142             insert( *barWait, j );
    143         return;
    144     }
    145     ticket = remove( *barWait );
    146 
    147     // last one out
    148     if ( size == 1 || ticket == size - 2 ) {
    149         for ( j; size )
    150             insert( *entryWait, j );
    151     }
     139        int ticket = remove( *entryWait );
     140        if ( ticket == size - 1 ) {
     141                for ( j; size - 1 )
     142                        insert( *barWait, j );
     143                return;
     144        }
     145        ticket = remove( *barWait );
     146
     147        // last one out
     148        if ( size == 1 || ticket == size - 2 ) {
     149                for ( j; size )
     150                        insert( *entryWait, j );
     151        }
    152152}
    153153barrier b{Tasks};
     
    155155// thread main
    156156void main(Task & this) {
    157     try {
    158         for ( ;; ) {
    159             wait( b );
    160         }
    161     } catch ( channel_closed * e ) {}
     157        try {
     158                for ( ;; ) {
     159                        wait( b );
     160                }
     161        } catch ( channel_closed * e ) {}
    162162}
    163163
    164164int main() {
    165     {
    166         Task t[Tasks];
    167 
    168         sleep(10`s);
    169         flush( b );
    170     } // wait for tasks to terminate
    171     return 0;
     165        {
     166                Task t[Tasks];
     167
     168                sleep(10`s);
     169                flush( b );
     170        } // wait for tasks to terminate
     171        return 0;
    172172}
    173173\end{cfa}
     
    176176
    177177struct barrier {
    178     channel( int ) barWait;
    179     channel( int ) entryWait;
    180     int size;
     178        channel( int ) barWait;
     179        channel( int ) entryWait;
     180        int size;
    181181}
    182182void ?{}(barrier & this, int size) with(this) {
    183     barWait{size + 1};
    184     entryWait{size + 1};
    185     this.size = size;
    186     for ( j; size )
    187         insert( *entryWait, j );
     183        barWait{size + 1};
     184        entryWait{size + 1};
     185        this.size = size;
     186        for ( j; size )
     187                insert( *entryWait, j );
    188188}
    189189
    190190void flush(barrier & this) with(this) {
    191     insert( *entryWait, -1 );
    192     insert( *barWait, -1 );
     191        insert( *entryWait, -1 );
     192        insert( *barWait, -1 );
    193193}
    194194void wait(barrier & this) with(this) {
    195     int ticket = remove( *entryWait );
    196     if ( ticket == -1 ) {
    197         insert( *entryWait, -1 );
    198         return;
    199     }
    200     if ( ticket == size - 1 ) {
    201         for ( j; size - 1 )
    202             insert( *barWait, j );
    203         return;
    204     }
    205     ticket = remove( *barWait );
    206     if ( ticket == -1 ) {
    207         insert( *barWait, -1 );
    208         return;
    209     }
    210 
    211     // last one out
    212     if ( size == 1 || ticket == size - 2 ) {
    213         for ( j; size )
    214             insert( *entryWait, j );
    215     }
     195        int ticket = remove( *entryWait );
     196        if ( ticket == -1 ) {
     197                insert( *entryWait, -1 );
     198                return;
     199        }
     200        if ( ticket == size - 1 ) {
     201                for ( j; size - 1 )
     202                        insert( *barWait, j );
     203                return;
     204        }
     205        ticket = remove( *barWait );
     206        if ( ticket == -1 ) {
     207                insert( *barWait, -1 );
     208                return;
     209        }
     210
     211        // last one out
     212        if ( size == 1 || ticket == size - 2 ) {
     213                for ( j; size )
     214                        insert( *entryWait, j );
     215        }
    216216}
    217217barrier b;
     
    220220// thread main
    221221void main(Task & this) {
    222     for ( ;; ) {
    223         if ( done ) break;
    224         wait( b );
    225     }
     222        for ( ;; ) {
     223                if ( done ) break;
     224                wait( b );
     225        }
    226226}
    227227
    228228int main() {
    229     {
    230         Task t[Tasks];
    231 
    232         sleep(10`s);
    233         done = true;
    234 
    235         flush( b );
    236     } // wait for tasks to terminate
    237     return 0;
     229        {
     230                Task t[Tasks];
     231
     232                sleep(10`s);
     233                done = true;
     234
     235                flush( b );
     236        } // wait for tasks to terminate
     237        return 0;
    238238}
    239239\end{cfa}
    240240
    241 In Listing~\ref{l:cfa_resume} an example of channel closing with resumption is used. 
    242 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. 
    243 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. 
     241In Listing~\ref{l:cfa_resume} an example of channel closing with resumption is used.
     242This program uses resumption in the \code{Consumer} thread main to ensure that all elements in the channel are removed before the consumer thread terminates.
     243The 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.
    244244If 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.
    245245
     
    249249// Consumer thread main
    250250void main(Consumer & this) {
    251     size_t runs = 0;
    252     try {
    253         for ( ;; ) {
    254             remove( chan );
    255         }
    256     } catchResume ( channel_closed * e ) {}
    257     catch ( channel_closed * e ) {}
     251        size_t runs = 0;
     252        try {
     253                for ( ;; ) {
     254                        remove( chan );
     255                }
     256        } catchResume ( channel_closed * e ) {}
     257        catch ( channel_closed * e ) {}
    258258}
    259259
    260260// Producer thread main
    261261void main(Producer & this) {
    262     int j = 0;
    263     try {
    264         for ( ;;j++ ) {
    265             insert( chan, j );
    266         }
    267     } catch ( channel_closed * e ) {}
     262        int j = 0;
     263        try {
     264                for ( ;;j++ ) {
     265                        insert( chan, j );
     266                }
     267        } catch ( channel_closed * e ) {}
    268268}
    269269
    270270int main( int argc, char * argv[] ) {
    271     {
    272         Consumers c[4];
    273         Producer p[4];
    274 
    275         sleep(10`s);
    276 
    277         for ( i; Channels )
    278             close( channels[i] );
    279     }
    280     return 0;
     271        {
     272                Consumers c[4];
     273                Producer p[4];
     274
     275                sleep(10`s);
     276
     277                for ( i; Channels )
     278                        close( channels[i] );
     279        }
     280        return 0;
    281281}
    282282\end{cfa}
     
    284284\section{Performance}
    285285
    286 Given that the base implementation of the \CFA channels is very similar to the Go implementation, this section aims to show that the performance of the two implementations are comparable. 
    287 One microbenchmark is conducted to compare Go and \CFA. 
    288 The benchmark is a ten second experiment where producers and consumers operate on a channel in parallel and throughput is measured. 
    289 The number of cores is varied to measure how throughtput scales. 
    290 The cores are divided equally between producers and consumers, with one producer or consumer owning each core. 
    291 The results of the benchmark are shown in Figure~\ref{f:chanPerf}. 
    292 The performance of Go and \CFA channels on this microbenchmark is comparable. 
     286Given that the base implementation of the \CFA channels is very similar to the Go implementation, this section aims to show that the performance of the two implementations are comparable.
     287One microbenchmark is conducted to compare Go and \CFA.
     288The benchmark is a ten second experiment where producers and consumers operate on a channel in parallel and throughput is measured.
     289The number of cores is varied to measure how throughtput scales.
     290The cores are divided equally between producers and consumers, with one producer or consumer owning each core.
     291The results of the benchmark are shown in Figure~\ref{f:chanPerf}.
     292The performance of Go and \CFA channels on this microbenchmark is comparable.
    293293Note, it is expected for the performance to decline as the number of cores increases as the channel operations all occur in a critical section so an increase in cores results in higher contention with no increase in parallelism.
    294294
    295295
    296296\begin{figure}
    297     \centering
    298     \begin{subfigure}{0.5\textwidth}
    299         \centering
    300         \scalebox{0.5}{\input{figures/nasus_Channel_Contention.pgf}}
    301         \subcaption{AMD \CFA Channel Benchmark}\label{f:chanAMD}
    302     \end{subfigure}\hfill
    303     \begin{subfigure}{0.5\textwidth}
    304         \centering
    305         \scalebox{0.5}{\input{figures/pyke_Channel_Contention.pgf}}
    306         \subcaption{Intel \CFA Channel Benchmark}\label{f:chanIntel}
    307     \end{subfigure}
    308     \caption{The channel contention benchmark comparing \CFA and Go channel throughput (higher is better).}
    309     \label{f:chanPerf}
     297        \centering
     298        \subfloat[AMD \CFA Channel Benchmark]{
     299                \resizebox{0.5\textwidth}{!}{\input{figures/nasus_Channel_Contention.pgf}}
     300                \label{f:chanAMD}
     301        }
     302        \subfloat[Intel \CFA Channel Benchmark]{
     303                \resizebox{0.5\textwidth}{!}{\input{figures/pyke_Channel_Contention.pgf}}
     304                \label{f:chanIntel}
     305        }
     306        \caption{The channel contention benchmark comparing \CFA and Go channel throughput (higher is better).}
     307        \label{f:chanPerf}
    310308\end{figure}
     309
     310% Local Variables: %
     311% tab-width: 4 %
     312% End: %
Note: See TracChangeset for help on using the changeset viewer.