Changes in / [2cb8bf71:8913de4]


Ignore:
Location:
doc/theses/colby_parsons_MMAth
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/colby_parsons_MMAth/Makefile

    r2cb8bf71 r8913de4  
    9898
    9999${BASE}.dvi : Makefile ${GRAPHS} ${PROGRAMS} ${PICTURES} ${FIGURES} ${SOURCES} ${DATA} \
    100                 glossary.tex style/style.tex ${Macros}/common.tex ${Macros}/indexstyle local.bib ../../bibliography/pl.bib | ${Build}
     100                style/style.tex ${Macros}/common.tex ${Macros}/indexstyle local.bib ../../bibliography/pl.bib | ${Build}
    101101        # Must have *.aux file containing citations for bibtex
    102102        if [ ! -r ${basename $@}.aux ] ; then ${LaTeX} ${basename $@}.tex ; fi
  • doc/theses/colby_parsons_MMAth/glossary.tex

    r2cb8bf71 r8913de4  
    3232% Examples from template above
    3333
    34 \newabbreviation{raii}{RAII}{\Newterm{resource acquisition is initialization}}
    35 \newabbreviation{rtti}{RTTI}{\Newterm{run-time type information}}
    36 \newabbreviation{fcfs}{FCFS}{\Newterm{first-come first-served}}
    37 \newabbreviation{toctou}{TOCTOU}{\Newterm{time-of-check to time-of-use}}
     34\newabbreviation{raii}{RAII}{Resource Acquisition Is Initialization}
     35\newabbreviation{rtti}{RTTI}{Run-Time Type Information}
     36\newabbreviation{fcfs}{FCFS}{First Come First Served}
     37\newabbreviation{toctou}{TOCTOU}{time-of-check to time-of-use}
    3838
    3939\newglossaryentry{actor}
  • doc/theses/colby_parsons_MMAth/style/style.tex

    r2cb8bf71 r8913de4  
    1515\newsavebox{\myboxB}
    1616
    17 \lstnewenvironment{Golang}[1][]
    18 {\lstset{language=Go,literate={<-}{\makebox[2ex][c]{\textless\raisebox{0.4ex}{\rule{0.8ex}{0.075ex}}}}2,
    19         moredelim=**[is][\protect\color{red}]{@}{@}}\lstset{#1}}
    20 {}
    21 
    2217\lstnewenvironment{java}[1][]
    2318{\lstset{language=java,moredelim=**[is][\protect\color{red}]{@}{@}}\lstset{#1}}
  • doc/theses/colby_parsons_MMAth/text/channels.tex

    r2cb8bf71 r8913de4  
    1717Additionally all channel operations in CSP are synchronous (no buffering).
    1818Advanced channels as a programming language feature has been popularized in recent years by the language Go~\cite{Go}, which encourages the use of channels as its fundamental concurrent feature.
    19 It was the popularity of Go channels that lead to their implementation in \CFA.
     19It was the popularity of Go channels that lead to their implemention in \CFA.
    2020Neither Go nor \CFA channels have the restrictions of the early channel-based concurrent systems.
    2121
     
    6161\section{Channel Implementation}
    6262Currently, only the Go programming language provides user-level threading where the primary communication mechanism is channels.
    63 Experiments were conducted that varied the producer-consumer algorithm and lock type used inside the channel.
     63Experiments were conducted that varied the producer-consumer problem algorithm and lock type used inside the channel.
    6464With the exception of non-\gls{fcfs} or non-FIFO 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.
    6565Performance of channels can be improved by sharding the underlying buffer \cite{Dice11}.
    66 However, the FIFO property is lost, which is undesirable for user-facing channels.
     66In doing so the FIFO property is lost, which is undesireable for user-facing channels.
    6767Therefore, the low-level channel implementation in \CFA is largely copied from the Go implementation, but adapted to the \CFA type and runtime systems.
    6868As such the research contributions added by \CFA's channel implementation lie in the realm of safety and productivity features.
    6969
    70 The Go channel implementation utilizes cooperation among threads to achieve good performance~\cite{go:chan}.
    71 This cooperation only occurs when producers or consumers need to block due to the buffer being full or empty.
    72 In these cases, a blocking thread stores their relevant data in a shared location and the signalling thread completes the blocking thread's operation before waking them;
    73 \ie the blocking thread has no work to perform after it unblocks because the signalling threads has done this work.
    74 This approach is similar to wait morphing for locks~\cite[p.~82]{Butenhof97} and improves performance in a few ways.
    75 First, each thread interacting with the channel only acquires and releases the internal channel lock once.
    76 As a result, contention on the internal lock is decreased, threads only compete for the lock upon entry, as unblocking threads do not reacquire the lock.
    77 The other advantage of Go's wait-morphing approach is that it eliminates the bottleneck of waiting for signalled threads to run.
    78 Note, the property of acquiring/releasing the lock only once can also be achieved with a different form of cooperation, called \Newterm{baton passing}.
    79 Baton passing occurs when one thread acquires a lock but does not release it, and instead signals a thread inside the critical section, conceptually ``passing'' the mutual exclusion from the signalling thread to the signalled thread.
    80 The baton-passing approach has threads cooperate to pass mutual exclusion without additional lock acquires or releases;
    81 the wait-morphing approach has threads cooperate by completing the signalled thread's operation, thus removing a signalled thread's need for mutual exclusion after unblocking.
    82 While baton passing is useful in some algorithms, it results in worse channel performance than the Go approach.
    83 In the baton-passing approach, all threads need to wait for the signalled thread to reach the front of the ready queue, context switch, and run before other operations on the channel can proceed, since the signalled thread holds mutual exclusion;
    84 in the wait-morphing approach, since the operation is completed before the signal, other threads can continue to operate on the channel without waiting for the signalled thread to run.
     70The Go channel implementation utilitizes cooperation between threads to achieve good performance~\cite{go:chan}.
     71The cooperation between threads only occurs when producers or consumers need to block due to the buffer being full or empty.
     72In these cases the blocking thread stores their relevant data in a shared location and the signalling thread will complete their operation before waking them.
     73This helps improve performance in a few ways.
     74First, each thread interacting with the channel with only acquire and release the internal channel lock exactly once.
     75This decreases contention on the internal lock, as only entering threads will compete for the lock since signalled threads never reacquire the lock.
     76The other advantage of the cooperation approach is that it eliminates the potential bottleneck of waiting for signalled threads.
     77The property of acquiring/releasing the lock only once can be achieved without cooperation by \Newterm{baton passing} the lock.
     78Baton passing is when one thread acquires a lock but does not release it, and instead signals a thread inside the critical section conceptually "passing" the mutual exclusion to the signalled thread.
     79While baton passing is useful in some algorithms, it results in worse performance than the cooperation approach in channel implementations since all entering threads then need to wait for the blocked thread to reach the front of the ready queue and run before other operations on the channel can proceed.
    8580
    8681In this work, all channel sizes \see{Sections~\ref{s:ChannelSize}} are implemented with bounded buffers.
     
    105100\subsection{Toggle-able Statistics}
    106101As discussed, a channel is a concurrent layer over a bounded buffer.
    107 To achieve efficient buffering, users should aim for as few blocking operations on a channel as possible.
    108 Mechanisms to reduce blocking are: change the buffer size, shard a channel into multiple channels, or tweak the number of producer and consumer threads.
    109 For users to be able to make informed decisions when tuning channel usage, toggle-able channel statistics are provided.
    110 The statistics are toggled on during the \CFA build by defining the @CHAN_STATS@ macro, which guarantees zero cost when not using this feature.
    111 When statistics are turned on, four counters are maintained per channel, two for inserting (producers) and two for removing (consumers).
     102To achieve efficient buffering users should aim for as few blocking operations on a channel as possible.
     103Often to achieve this users may change the buffer size, shard a channel into multiple channels, or tweak the number of producer and consumer threads.
     104Fo users to be able to make informed decisions when tuning channel usage, toggle-able channel statistics are provided.
     105The statistics are toggled at compile time via the @CHAN_STATS@ macro to ensure that they are entirely elided when not used.
     106When statistics are turned on, four counters are maintained per channel, two for producers and two for consumers.
    112107The two counters per type of operation track the number of blocking operations and total operations.
    113 In the channel destructor, the counters are printed out aggregated and also per type of operation.
    114 An example use case is noting that producer inserts are blocking often while consumer removes do not block often.
    115 This information can be used to increase the number of consumers to decrease the blocking producer operations, thus increasing the channel throughput.
    116 Whereas, increasing the channel size in this scenario is unlikely to produce a benefit because the consumers can never keep up with the producers.
     108In the channel destructor the counters are printed out aggregated and also per type of operation.
     109An example use case of the counters follows.
     110A user is buffering information between producer and consumer threads and wants to analyze channel performance.
     111Via the statistics they see that producers block for a large percentage of their operations while consumers do not block often.
     112They then can use this information to adjust their number of producers/consumers or channel size to achieve a larger percentage of non-blocking producer operations, thus increasing their channel throughput.
    117113
    118114\subsection{Deadlock Detection}
    119 The deadlock detection in the \CFA channels is fairly basic but detects a very common channel mistake during termination.
    120 That is, it detects the case where threads are blocked on the channel during channel deallocation.
    121 This case is guaranteed to deadlock since there are no producer threads to supply values needed by the waiting consumer threads.
    122 Only if a user maintained a separate reference to the consumer threads and manually unblocks them outside the channel could the deadlock be avoid.
    123 However, without special consumer semantics, this unblocking would generate other runtime errors where the consumer attempts to access non-existing channel data or even a deallocated channel.
    124 More robust deadlock detection needs to be implemented separate from channels since it requires knowledge about the threading system and other channel/thread state.
     115The deadlock detection in the \CFA channels is fairly basic.
     116It only detects the case where threads are blocked on the channel during deallocation.
     117This case is guaranteed to deadlock since the list holding the blocked thread is internal to the channel and will be deallocated.
     118If a user maintained a separate reference to a thread and unparked it outside the channel they could avoid the deadlock, but would run into other runtime errors since the thread would access channel data after waking that is now deallocated.
     119More robust deadlock detection surrounding channel usage would have to be implemented separate from the channel implementation since it would require knowledge about the threading system and other channel/thread state.
    125120
    126121\subsection{Program Shutdown}
    127122Terminating concurrent programs is often one of the most difficult parts of writing concurrent code, particularly if graceful termination is needed.
    128 The difficulty for graceful termination often arises from the usage of synchronization primitives that need to be handled carefully during shutdown.
     123The difficulty of graceful termination often arises from the usage of synchronization primitives that need to be handled carefully during shutdown.
    129124It is easy to deadlock during termination if threads are left behind on synchronization primitives.
    130125Additionally, 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.
    131126\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.
    132127Channels are a particularly hard synchronization primitive to terminate since both sending and receiving to/from a channel can block.
    133 Thus, improperly handled \gls{toctou} issues with channels often result in deadlocks as threads performing the termination may end up unexpectedly blocking in their attempt to help other threads exit the system.
    134 
    135 \paragraph{Go channels} provide a set of tools to help with concurrent shutdown~\cite{go:chan} using a @close@ operation in conjunction with the \Go{select} statement.
     128Thus, 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.
     129
     130\paragraph{Go channels} provide a set of tools to help with concurrent shutdown~\cite{go:chan}.
     131Channels in Go have a @close@ operation and a \Go{select} statement that both can be used to help threads terminate.
    136132The \Go{select} statement is discussed in \ref{s:waituntil}, where \CFA's @waituntil@ statement is compared with the Go \Go{select} statement.
    137133
     
    147143Note, panics in Go can be caught, but it is not the idiomatic way to write Go programs.
    148144
    149 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.
     145While 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.
    150146Since both closing and sending panic once a channel is closed, a user often has to synchronize the senders (producers) before the channel can be closed to avoid panics.
    151147However, in doing so it renders the @close@ operation nearly useless, as the only utilities it provides are the ability to ensure receivers no longer block on the channel and receive zero-valued elements.
    152148This functionality is only useful if the zero-typed element is recognized as a sentinel value, but if another sentinel value is necessary, then @close@ only provides the non-blocking feature.
    153149To 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.
    154 Hence, due to Go's asymmetric approach to channel shutdown, separate synchronization between producers and consumers of a channel has to occur during shutdown.
     150Due to Go's asymmetric approach to channel shutdown, separate synchronization between producers and consumers of a channel has to occur during shutdown.
    155151
    156152\paragraph{\CFA channels} have access to an extensive exception handling mechanism~\cite{Beach21}.
     
    165161When a channel in \CFA is closed, all subsequent calls to the channel raise a resumption exception at the caller.
    166162If the resumption is handled, the caller attempts to complete the channel operation.
    167 However, if the channel operation would block, a termination exception is thrown.
     163However, if channel operation would block, a termination exception is thrown.
    168164If the resumption is not handled, the exception is rethrown as a termination.
    169165These termination exceptions allow for non-local transfer that is used to great effect to eagerly and gracefully shut down a thread.
    170166When 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.
    171 The resumption exception, @channel_closed@, has internal fields to aid in handling the exception.
    172 The exception contains a pointer to the channel it is thrown from and a pointer to a buffer element.
    173 For exceptions thrown from @remove@, the buffer element pointer is null.
    174 For exceptions thrown from @insert@, the element pointer points to the buffer element that the thread attempted to insert.
     167The resumption exception, @channel_closed@, has a couple fields to aid in handling the exception.
     168The exception contains a pointer to the channel it was thrown from, and a pointer to an element.
     169In exceptions thrown from remove the element pointer will be null.
     170In the case of insert the element pointer points to the element that the thread attempted to insert.
    175171This 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.
    176 Furthermore, due to \CFA's powerful exception system, this data can be used to choose handlers based on which channel and operation failed.
    177 For example, exception handlers in \CFA have an optional predicate which can be used to trigger or skip handlers based on the content of the matching exception.
    178 It is worth mentioning that using exceptions for termination may incur a larger performance cost than the Go approach.
    179 However, this should not be an issue, since termination is rarely on the fast-path of an application.
    180 In contrast, ensuring termination can be easily implemented correctly is the aim of the exception approach.
     172Furthermore, due to \CFA's powerful exception system, this data can be used to choose handlers based which channel and operation failed.
     173Exception 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.
     174It is worth mentioning that the approach of exceptions for termination may incur a larger performance cost during termination that the approach used in Go.
     175This 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.
    181176
    182177\section{\CFA / Go channel Examples}
    183 To highlight the differences between \CFA's and Go's close semantics, three examples are presented.
     178To highlight the differences between \CFA's and Go's close semantics, three examples will be presented.
    184179The first example is a simple shutdown case, where there are producer threads and consumer threads operating on a channel for a fixed duration.
    185 Once the duration ends, producers and consumers terminate immediately leaving unprocessed elements in the channel.
    186 The second example extends the first by requiring the channel to be empty after shutdown.
     180Once the duration ends, producers and consumers terminate without worrying about any leftover values in the channel.
     181The second example extends the first example by requiring the channel to be empty upon shutdown.
    187182Both the first and second example are shown in Figure~\ref{f:ChannelTermination}.
     183
     184
     185First the Go solutions to these examples shown in Figure~\ref{l:go_chan_term} are discussed.
     186Since some of the elements being passed through the channel are zero-valued, closing the channel in Go does not aid in communicating shutdown.
     187Instead, a different mechanism to communicate with the consumers and producers needs to be used.
     188This 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.
     189In this example, a flag is used to communicate with producers and another flag is used for consumers.
     190Producers and consumers need separate avenues of communication both so that producers terminate before the channel is closed to avoid panicking, and to avoid the case where all the consumers terminate first, which can result in a deadlock for producers if the channel is full.
     191The producer flag is set first, then after producers terminate the consumer flag is set and the channel is closed.
     192In the second example where all values need to be consumed, the main thread iterates over the closed channel to process any remaining values.
     193
     194
     195In the \CFA solutions in Figure~\ref{l:cfa_chan_term}, shutdown is communicated directly to both producers and consumers via the @close@ call.
     196In the first example where all values do not need to be consumed, both producers and consumers do not handle the resumption and finish once they receive the termination exception.
     197The second \CFA example where all values must be consumed highlights how resumption is used with channel shutdown.
     198The @Producer@ thread-main knows to stop producing when the @insert@ call on a closed channel raises exception @channel_closed@.
     199The @Consumer@ thread-main knows to stop consuming after all elements of a closed channel are removed and the call to @remove@ would block.
     200Hence, the consumer knows the moment the channel closes because a resumption exception is raised, caught, and ignored, and then control returns to @remove@ to return another item from the buffer.
     201Only when the buffer is drained and the call to @remove@ would block, a termination exception is raised to stop consuming.
     202The \CFA semantics allow users to communicate channel shutdown directly through the channel, without having to share extra state between threads.
     203Additionally, when the channel needs to be drained, \CFA provides users with easy options for processing the leftover channel values in the main thread or in the consumer threads.
     204If one wishes to consume the leftover values in the consumer threads in Go, extra synchronization between the main thread and the consumer threads is needed.
    188205
    189206\begin{figure}
     
    191208
    192209\begin{lrbox}{\myboxA}
    193 \begin{Golang}[aboveskip=0pt,belowskip=0pt]
    194 var channel chan int = make( chan int, 128 )
    195 var prodJoin chan int = make( chan int, 4 )
    196 var consJoin chan int = make( chan int, 4 )
    197 var cons_done, prod_done bool = false, false;
    198 func producer() {
    199         for {
    200                 if prod_done { break }
    201                 channel <- 5
    202         }
    203         prodJoin <- 0 // synch with main thd
    204 }
    205 
    206 func consumer() {
    207         for {
    208                 if cons_done { break }
    209                 <- channel
    210         }
    211         consJoin <- 0 // synch with main thd
    212 }
    213 
    214 
    215 func main() {
    216         for j := 0; j < 4; j++ { go consumer() }
    217         for j := 0; j < 4; j++ { go producer() }
    218         time.Sleep( time.Second * 10 )
    219         prod_done = true
    220         for j := 0; j < 4 ; j++ { <- prodJoin }
    221         cons_done = true
    222         close(channel) // ensure no cons deadlock
    223         @for elem := range channel {@
    224                 // process leftover values
    225         @}@
    226         for j := 0; j < 4; j++ { <- consJoin }
    227 }
    228 \end{Golang}
     210\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     211channel( size_t ) Channel{ ChannelSize };
     212
     213thread Consumer {};
     214void main( Consumer & this ) {
     215    try {
     216        for ( ;; )
     217            remove( Channel );
     218    @} catchResume( channel_closed * ) { @
     219    // handled resume => consume from chan
     220    } catch( channel_closed * ) {
     221        // empty or unhandled resume
     222    }
     223}
     224
     225thread Producer {};
     226void main( Producer & this ) {
     227    size_t count = 0;
     228    try {
     229        for ( ;; )
     230            insert( Channel, count++ );
     231    } catch ( channel_closed * ) {
     232        // unhandled resume or full
     233    }
     234}
     235
     236int main( int argc, char * argv[] ) {
     237    Consumer c[Consumers];
     238    Producer p[Producers];
     239    sleep(Duration`s);
     240    close( Channel );
     241    return 0;
     242}
     243\end{cfa}
    229244\end{lrbox}
    230245
    231246\begin{lrbox}{\myboxB}
    232247\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    233 channel( size_t ) chan{ 128 };
    234 thread Consumer {};
    235 thread Producer {};
    236 
    237 void main( Producer & this ) {
    238         try {
    239                 for ()
    240                         insert( chan, 5 );
    241         } catch( channel_closed * ) {
    242                 // unhandled resume or full
    243         }
    244 }
    245 void main( Consumer & this ) {
    246         try {
    247                 for () { int i = remove( chan ); }
    248         @} catchResume( channel_closed * ) {@
    249                 // handled resume => consume from chan
    250         } catch( channel_closed * ) {
    251                 // empty or unhandled resume
    252         }
    253 }
    254 int main() {
    255         Consumer c[4];
    256         Producer p[4];
    257         sleep( 10`s );
    258         close( chan );
    259 }
    260 
    261 
    262 
    263 
    264 
    265 
    266 
     248var cons_done, prod_done bool = false, false;
     249var prodJoin chan int = make(chan int, Producers)
     250var consJoin chan int = make(chan int, Consumers)
     251
     252func consumer( channel chan uint64 ) {
     253    for {
     254        if cons_done { break }
     255        <-channel
     256    }
     257    consJoin <- 0 // synch with main thd
     258}
     259
     260func producer( channel chan uint64 ) {
     261    var count uint64 = 0
     262    for {
     263        if prod_done { break }
     264        channel <- count++
     265    }
     266    prodJoin <- 0 // synch with main thd
     267}
     268
     269func main() {
     270    channel = make(chan uint64, ChannelSize)
     271    for j := 0; j < Consumers; j++ {
     272        go consumer( channel )
     273    }
     274    for j := 0; j < Producers; j++ {
     275        go producer( channel )
     276    }
     277    time.Sleep(time.Second * Duration)
     278    prod_done = true
     279    for j := 0; j < Producers ; j++ {
     280        <-prodJoin // wait for prods
     281    }
     282    cons_done = true
     283    close(channel) // ensure no cons deadlock
     284    @for elem := range channel { @
     285        // process leftover values
     286    @}@
     287    for j := 0; j < Consumers; j++{
     288        <-consJoin // wait for cons
     289    }
     290}
    267291\end{cfa}
    268292\end{lrbox}
    269293
    270 \subfloat[Go style]{\label{l:go_chan_term}\usebox\myboxA}
     294\subfloat[\CFA style]{\label{l:cfa_chan_term}\usebox\myboxA}
    271295\hspace*{3pt}
    272296\vrule
    273297\hspace*{3pt}
    274 \subfloat[\CFA style]{\label{l:cfa_chan_term}\usebox\myboxB}
     298\subfloat[Go style]{\label{l:go_chan_term}\usebox\myboxB}
    275299\caption{Channel Termination Examples 1 and 2. Code specific to example 2 is highlighted.}
    276300\label{f:ChannelTermination}
    277301\end{figure}
    278302
    279 Figure~\ref{l:go_chan_term} shows the Go solution.
    280 Since some of the elements being passed through the channel are zero-valued, closing the channel in Go does not aid in communicating shutdown.
    281 Instead, a different mechanism to communicate with the consumers and producers needs to be used.
    282 Flag variables are common in Go-channel shutdown-code to avoid panics on a channel, meaning the channel shutdown has to be communicated with threads before it occurs.
    283 Hence, the two flags @cons_done@ and @prod_done@ are used to communicate with the producers and consumers, respectively.
    284 Furthermore, producers and consumers need separate shutdown channels so producers terminate before the channel is closed to avoid panicking, and to avoid the case where all the consumers terminate first, which can result in a deadlock for producers if the channel is full.
    285 The producer flag is set first;
    286 then after all producers terminate, the consumer flag is set and the channel is closed leaving elements in the buffer.
    287 To purge the buffer, a loop is added (red) that iterates over the closed channel to process any remaining values.
    288 
    289 Figure~\ref{l:cfa_chan_term} shows the \CFA solution.
    290 Here, shutdown is communicated directly to both producers and consumers via the @close@ call.
    291 A @Producer@ thread knows to stop producing when the @insert@ call on a closed channel raises exception @channel_closed@.
    292 If a @Consumer@ thread ignores the first resumption exception from the @close@, the exception is reraised as a termination exception and elements are left in the buffer.
    293 If a @Consumer@ thread handles the resumptions exceptions (red), control returns to complete the remove.
    294 A @Consumer@ thread knows to stop consuming after all elements of a closed channel are removed and the consumer would block, which causes a termination raise of @channel_closed@.
    295 The \CFA semantics allow users to communicate channel shutdown directly through the channel, without having to share extra state between threads.
    296 Additionally, when the channel needs to be drained, \CFA provides users with easy options for processing the leftover channel values in the main thread or in the consumer threads.
    297 
    298 Figure~\ref{f:ChannelBarrierTermination} shows a final shutdown example using channels to implement a barrier.
    299 A Go and \CFA style solution are presented but both are implemented using \CFA syntax so they can be easily compared.
    300 Implementing a barrier is interesting because threads are both producers and consumers on the barrier-internal channels, @entryWait@ and @barWait@.
    301 The outline for the barrier implementation starts by initially filling the @entryWait@ channel with $N$ tickets in the barrier constructor, allowing $N$ arriving threads to remove these values and enter the barrier.
    302 After @entryWait@ is empty, arriving threads block when removing from @entryWait@.
    303 However, the arriving threads that entered the barrier cannot leave the barrier until $N$ threads have arrived.
    304 Hence, the entering threads block on the @barWait@ channel until the $N$th arriving thread inserts $N-1$ elements into @barWait@ to unblock the $N-1$ threads calling @remove@ on the @barWait@ channel.
    305 The race between these arriving threads blocking on @barWait@ and the $N$th thread inserting values into @barWait@ does not affect correctness;
    306 \ie an arriving thread may or may not block on channel @barWait@ to get its value.
    307 
    308 Now, the two channels makes termination synchronization between producers and consumers difficult.
    309 Interestingly, the shutdown details for this problem are also applicable to other problems with threads producing and consuming from the same channel.
    310 The Go-style solution cannot use the Go @close@ call since all threads are both potentially producers and consumers, causing panics on close to be unavoidable without complex synchronization.
    311 As such in Figure \ref{l:go_chan_bar}, a flush routine is needed to insert a sentinel value, @-1@, to inform threads waiting in the buffer they need to leave the barrier.
    312 This sentinel value has to be checked at two points along the fast-path and sentinel values daisy-chained into the buffers.
     303The final shutdown example uses channels to implement a barrier.
     304It is shown in Figure~\ref{f:ChannelBarrierTermination}.
     305The problem of implementing a barrier is chosen since threads are both producers and consumers on the barrier-internal channels, which removes the ability to easily synchronize producers before consumers during shutdown.
     306As such, while the shutdown details will be discussed with this problem in mind, they are also applicable to other problems taht have individual threads both producing and consuming from channels.
     307Both of these examples are implemented using \CFA syntax so that they can be easily compared.
     308Figure~\ref{l:cfa_chan_bar} uses \CFA-style channel close semantics and Figure~\ref{l:go_chan_bar} uses Go-style close semantics.
     309In this example it is infeasible to use the Go @close@ call since all threads are both potentially producers and consumers, causing panics on close to be unavoidable without complex synchronization.
     310As such in Figure~\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.
     311This sentinel value has to be checked at two points.
    313312Furthermore, an additional flag @done@ is needed to communicate to threads once they have left the barrier that they are done.
     313
     314In 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.
     315This 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.
    314316Also 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.
    315 For The \CFA solution~\ref{l:cfa_chan_bar}, the barrier shutdown results in an exception being thrown at threads operating on it, to inform waiting threads they must leave the barrier.
    316 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.
    317317
    318318\begin{figure}
     
    320320
    321321\begin{lrbox}{\myboxA}
     322\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     323struct barrier {
     324        channel( int ) barWait, entryWait;
     325        int size;
     326};
     327void ?{}( barrier & this, int size ) with(this) {
     328        barWait{size};   entryWait{size};
     329        this.size = size;
     330        for ( i; size )
     331                insert( entryWait, i );
     332}
     333void wait( barrier & this ) with(this) {
     334        int ticket = remove( entryWait );
     335
     336        if ( ticket == size - 1 ) {
     337                for ( i; size - 1 )
     338                        insert( barWait, i );
     339                return;
     340        }
     341        ticket = remove( barWait );
     342
     343        if ( size == 1 || ticket == size - 2 ) { // last ?
     344                for ( i; size )
     345                        insert( entryWait, i );
     346        }
     347}
     348void flush(barrier & this) with(this) {
     349        @close( barWait );   close( entryWait );@
     350}
     351enum { Threads = 4 };
     352barrier b{Threads};
     353
     354thread Thread {};
     355void main( Thread & this ) {
     356        @try {@
     357                for ()
     358                        wait( b );
     359        @} catch ( channel_closed * ) {}@
     360}
     361int main() {
     362        Thread t[Threads];
     363        sleep(10`s);
     364
     365        flush( b );
     366} // wait for threads to terminate
     367\end{cfa}
     368\end{lrbox}
     369
     370\begin{lrbox}{\myboxB}
    322371\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    323372struct barrier {
     
    368417\end{lrbox}
    369418
    370 \begin{lrbox}{\myboxB}
    371 \begin{cfa}[aboveskip=0pt,belowskip=0pt]
    372 struct barrier {
    373         channel( int ) barWait, entryWait;
    374         int size;
    375 };
    376 void ?{}( barrier & this, int size ) with(this) {
    377         barWait{size};   entryWait{size};
    378         this.size = size;
    379         for ( i; size )
    380                 insert( entryWait, i );
    381 }
    382 void wait( barrier & this ) with(this) {
    383         int ticket = remove( entryWait );
    384 
    385         if ( ticket == size - 1 ) {
    386                 for ( i; size - 1 )
    387                         insert( barWait, i );
    388                 return;
    389         }
    390         ticket = remove( barWait );
    391 
    392         if ( size == 1 || ticket == size - 2 ) { // last ?
    393                 for ( i; size )
    394                         insert( entryWait, i );
    395         }
    396 }
    397 void flush(barrier & this) with(this) {
    398         @close( barWait );   close( entryWait );@
    399 }
    400 enum { Threads = 4 };
    401 barrier b{Threads};
    402 
    403 thread Thread {};
    404 void main( Thread & this ) {
    405         @try {@
    406                 for ()
    407                         wait( b );
    408         @} catch ( channel_closed * ) {}@
    409 }
    410 int main() {
    411         Thread t[Threads];
    412         sleep(10`s);
    413 
    414         flush( b );
    415 } // wait for threads to terminate
    416 \end{cfa}
    417 \end{lrbox}
    418 
    419 \subfloat[Go style]{\label{l:go_chan_bar}\usebox\myboxA}
     419\subfloat[\CFA style]{\label{l:cfa_chan_bar}\usebox\myboxA}
    420420\hspace*{3pt}
    421421\vrule
    422422\hspace*{3pt}
    423 \subfloat[\CFA style]{\label{l:cfa_chan_bar}\usebox\myboxB}
     423\subfloat[Go style]{\label{l:go_chan_bar}\usebox\myboxB}
    424424\caption{Channel Barrier Termination}
    425425\label{f:ChannelBarrierTermination}
  • doc/theses/colby_parsons_MMAth/thesis.tex

    r2cb8bf71 r8913de4  
    111111    colorlinks=true,        % false: boxed links; true: colored links
    112112    linkcolor=blue,         % color of internal links
    113     citecolor=blue,         % color of links to bibliography
     113    citecolor=blue,        % color of links to bibliography
    114114    filecolor=magenta,      % color of file links
    115     urlcolor=cyan,          % color of external links
    116     breaklinks=true
     115    urlcolor=cyan           % color of external links
    117116}
    118117\ifthenelse{\boolean{PrintVersion}}{   % for improved print quality, change some hyperref options
     
    127126% \usepackage[acronym]{glossaries}
    128127\usepackage[automake,toc,abbreviations]{glossaries-extra} % Exception to the rule of hyperref being the last add-on package
    129 \renewcommand*{\glstextformat}[1]{\textcolor{black}{#1}}
    130128% If glossaries-extra is not in your LaTeX distribution, get it from CTAN (http://ctan.org/pkg/glossaries-extra),
    131129% although it's supposed to be in both the TeX Live and MikTeX distributions. There are also documentation and
Note: See TracChangeset for help on using the changeset viewer.