Changeset 0aef549 for doc


Ignore:
Timestamp:
Jun 1, 2023, 12:01:05 PM (12 months ago)
Author:
caparsons <caparson@…>
Branches:
ast-experimental, master
Children:
4897ffa
Parents:
ac5d22f (diff), 8421d3f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
doc
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • doc/bibliography/pl.bib

    rac5d22f r0aef549  
    12091209    year        = 2018,
    12101210    pages       = {2111-2146},
    1211     note        = {\href{http://dx.doi.org/10.1002/spe.2624}{http://\-dx.doi.org/\-10.1002/\-spe.2624}},
     1211    optnote     = {\href{http://dx.doi.org/10.1002/spe.2624}{http://\-dx.doi.org/\-10.1002/\-spe.2624}},
    12121212}
    12131213
     
    18701870    month       = sep,
    18711871    year        = 2020,
    1872     note        = {\href{https://plg.uwaterloo.ca/~usystem/pub/uSystem/uC++.pdf}{https://\-plg.uwaterloo.ca/\-$\sim$usystem/\-pub/\-uSystem/uC++.pdf}},
     1872    note        = {\url{https://plg.uwaterloo.ca/~usystem/pub/uSystem/uC++.pdf}},
    18731873}
    18741874
     
    20042004    number      = 5,
    20052005    pages       = {1005-1042},
    2006     note        = {\href{https://onlinelibrary.wiley.com/doi/10.1002/spe.2925}{https://\-onlinelibrary.wiley.com/\-doi/\-10.1002/\-spe.2925}},
     2006    optnote     = {\href{https://onlinelibrary.wiley.com/doi/10.1002/spe.2925}{https://\-onlinelibrary.wiley.com/\-doi/\-10.1002/\-spe.2925}},
    20072007}
    20082008
     
    42234223    title       = {Implementing Lock-Free Queues},
    42244224    booktitle   = {Seventh International Conference on Parallel and Distributed Computing Systems},
     4225    organization= {International Society for Computers and Their Applications},
    42254226    address     = {Las Vegas, Nevada, U.S.A.},
    42264227    year        = {1994},
     
    50865087}
    50875088
    5088 @manual{MMTk,
     5089@misc{MMTk,
    50895090    keywords    = {Java memory management},
    50905091    contributer = {pabuhr@plg},
     
    50935094    month       = sep,
    50945095    year        = 2006,
    5095     note        = {\href{http://cs.anu.edu.au/~Robin.Garner/mmtk-guide.pdf}
    5096                   {http://cs.anu.edu.au/\-$\sim$Robin.Garner/\-mmtk-guide.pdf}},
     5096    howpublished= {\url{http://cs.anu.edu.au/~Robin.Garner/mmtk-guide.pdf}},
    50975097}
    50985098
     
    74027402}
    74037403
     7404@misc{rpmalloc,
     7405    author      = {Mattias Jansson},
     7406    title       = {rpmalloc version 1.4.1},
     7407    month       = apr,
     7408    year        = 2022,
     7409    howpublished= {\href{https://github.com/mjansson/rpmalloc}{https://\-github.com/\-mjansson/\-rpmalloc}},
     7410}
     7411
    74047412@manual{Rust,
    74057413    keywords    = {Rust programming language},
     
    74567464    booktitle   = {PLDI '04: Proceedings of the ACM SIGPLAN 2004 Conference on Programming Language Design and Implementation},
    74577465    location    = {Washington DC, USA},
    7458     publisher   = {ACM},
     7466    organization= {ACM},
    74597467    address     = {New York, NY, USA},
    74607468    volume      = 39,
  • doc/theses/colby_parsons_MMAth/Makefile

    rac5d22f r0aef549  
    9898
    9999${BASE}.dvi : Makefile ${GRAPHS} ${PROGRAMS} ${PICTURES} ${FIGURES} ${SOURCES} ${DATA} \
    100                 style/style.tex ${Macros}/common.tex ${Macros}/indexstyle local.bib ../../bibliography/pl.bib | ${Build}
     100                glossary.tex 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

    rac5d22f r0aef549  
    3232% Examples from template above
    3333
    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}
     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}}
    3838
    3939\newglossaryentry{actor}
  • doc/theses/colby_parsons_MMAth/style/style.tex

    rac5d22f r0aef549  
    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
    1722\lstnewenvironment{java}[1][]
    1823{\lstset{language=java,moredelim=**[is][\protect\color{red}]{@}{@}}\lstset{#1}}
  • doc/theses/colby_parsons_MMAth/text/channels.tex

    rac5d22f r0aef549  
    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 implemention in \CFA.
     19It was the popularity of Go channels that lead to their implementation in \CFA.
    2020Neither Go nor \CFA channels have the restrictions of the early channel-based concurrent systems.
    2121
     
    6767\section{Channel Implementation}
    6868Currently, only the Go programming language provides user-level threading where the primary communication mechanism is channels.
    69 Experiments were conducted that varied the producer-consumer problem algorithm and lock type used inside the channel.
     69Experiments were conducted that varied the producer-consumer algorithm and lock type used inside the channel.
    7070With 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.
    7171Performance of channels can be improved by sharding the underlying buffer \cite{Dice11}.
    72 In doing so the FIFO property is lost, which is undesireable for user-facing channels.
     72However, the FIFO property is lost, which is undesirable for user-facing channels.
    7373Therefore, the low-level channel implementation in \CFA is largely copied from the Go implementation, but adapted to the \CFA type and runtime systems.
    7474As such the research contributions added by \CFA's channel implementation lie in the realm of safety and productivity features.
    7575
    76 The Go channel implementation utilitizes cooperation between threads to achieve good performance~\cite{go:chan}.
    77 The cooperation between threads only occurs when producers or consumers need to block due to the buffer being full or empty.
    78 In these cases the blocking thread stores their relevant data in a shared location and the signalling thread will complete their operation before waking them.
    79 This helps improve performance in a few ways.
    80 First, each thread interacting with the channel with only acquire and release the internal channel lock exactly once.
    81 This decreases contention on the internal lock, as only entering threads will compete for the lock since signalled threads never reacquire the lock.
    82 The other advantage of the cooperation approach is that it eliminates the potential bottleneck of waiting for signalled threads.
    83 The property of acquiring/releasing the lock only once can be achieved without cooperation by \Newterm{baton passing} the lock.
    84 Baton 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.
    85 While 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.
     76The Go channel implementation utilizes cooperation among threads to achieve good performance~\cite{go:chan}.
     77This cooperation only occurs when producers or consumers need to block due to the buffer being full or empty.
     78In 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;
     79\ie the blocking thread has no work to perform after it unblocks because the signalling threads has done this work.
     80This approach is similar to wait morphing for locks~\cite[p.~82]{Butenhof97} and improves performance in a few ways.
     81First, each thread interacting with the channel only acquires and releases the internal channel lock once.
     82As a result, contention on the internal lock is decreased, as only entering threads compete for the lock as unblocking threads do not reacquire the lock.
     83The other advantage of Go's wait-morphing approach is that it eliminates the bottleneck of waiting for signalled threads to run.
     84Note, the property of acquiring/releasing the lock only once can also be achieved with a different form of cooperation, called \Newterm{baton passing}.
     85Baton 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.
     86The baton-passing approach has threads cooperate to pass mutual exclusion without additional lock acquires or releases;
     87the 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.
     88While baton passing is useful in some algorithms, it results in worse channel performance than the Go approach.
     89In 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;
     90in 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.
    8691
    8792In this work, all channel sizes \see{Sections~\ref{s:ChannelSize}} are implemented with bounded buffers.
     
    106111\subsection{Toggle-able Statistics}
    107112As discussed, a channel is a concurrent layer over a bounded buffer.
    108 To achieve efficient buffering users should aim for as few blocking operations on a channel as possible.
    109 Often to achieve this users may change the buffer size, shard a channel into multiple channels, or tweak the number of producer and consumer threads.
    110 Fo users to be able to make informed decisions when tuning channel usage, toggle-able channel statistics are provided.
    111 The statistics are toggled at compile time via the @CHAN_STATS@ macro to ensure that they are entirely elided when not used.
    112 When statistics are turned on, four counters are maintained per channel, two for producers and two for consumers.
     113To achieve efficient buffering, users should aim for as few blocking operations on a channel as possible.
     114Mechanisms to reduce blocking are: change the buffer size, shard a channel into multiple channels, or tweak the number of producer and consumer threads.
     115For users to be able to make informed decisions when tuning channel usage, toggle-able channel statistics are provided.
     116The statistics are toggled on during the \CFA build by defining the @CHAN_STATS@ macro, which guarantees zero cost when not using this feature.
     117When statistics are turned on, four counters are maintained per channel, two for inserting (producers) and two for removing (consumers).
    113118The two counters per type of operation track the number of blocking operations and total operations.
    114 In the channel destructor the counters are printed out aggregated and also per type of operation.
    115 An example use case of the counters follows.
    116 A user is buffering information between producer and consumer threads and wants to analyze channel performance.
    117 Via the statistics they see that producers block for a large percentage of their operations while consumers do not block often.
    118 They 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.
     119In the channel destructor, the counters are printed out aggregated and also per type of operation.
     120An example use case is noting that producer inserts are blocking often while consumer removes do not block often.
     121This information can be used to increase the number of consumers to decrease the blocking producer operations, thus increasing the channel throughput.
     122Whereas, increasing the channel size in this scenario is unlikely to produce a benefit because the consumers can never keep up with the producers.
    119123
    120124\subsection{Deadlock Detection}
    121 The deadlock detection in the \CFA channels is fairly basic.
    122 It only detects the case where threads are blocked on the channel during deallocation.
    123 This case is guaranteed to deadlock since the list holding the blocked thread is internal to the channel and will be deallocated.
    124 If 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.
    125 More 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.
     125The deadlock detection in the \CFA channels is fairly basic but detects a very common channel mistake during termination.
     126That is, it detects the case where threads are blocked on the channel during channel deallocation.
     127This case is guaranteed to deadlock since there are no producer threads to supply values needed by the waiting consumer threads.
     128Only if a user maintained a separate reference to the consumer threads and manually unblocks them outside the channel could the deadlock be avoid.
     129However, 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.
     130More robust deadlock detection needs to be implemented separate from channels since it requires knowledge about the threading system and other channel/thread state.
    126131
    127132\subsection{Program Shutdown}
    128133Terminating concurrent programs is often one of the most difficult parts of writing concurrent code, particularly if graceful termination is needed.
    129 The difficulty of graceful termination often arises from the usage of synchronization primitives that need to be handled carefully during shutdown.
     134The difficulty for graceful termination often arises from the usage of synchronization primitives that need to be handled carefully during shutdown.
    130135It is easy to deadlock during termination if threads are left behind on synchronization primitives.
    131136Additionally, 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.
    132137\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.
    133138Channels are a particularly hard synchronization primitive to terminate since both sending and receiving to/from a channel can block.
    134 Thus, 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.
    135 
    136 \paragraph{Go channels} provide a set of tools to help with concurrent shutdown~\cite{go:chan}.
    137 Channels in Go have a @close@ operation and a \Go{select} statement that both can be used to help threads terminate.
    138 The \Go{select} statement is discussed in Chapter~\ref{s:waituntil}, where \CFA's @waituntil@ statement is compared with the Go \Go{select} statement.
     139Thus, 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.
     140
     141\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.
     142The \Go{select} statement is discussed in \ref{s:waituntil}, where \CFA's @waituntil@ statement is compared with the Go \Go{select} statement.
    139143
    140144The @close@ operation on a channel in Go changes the state of the channel.
     
    149153Note, panics in Go can be caught, but it is not the idiomatic way to write Go programs.
    150154
    151 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.
     155While 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.
    152156Since 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.
    153157However, 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.
    154158This 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.
    155159To 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.
    156 Due to Go's asymmetric approach to channel shutdown, separate synchronization between producers and consumers of a channel has to occur during shutdown.
     160Hence, due to Go's asymmetric approach to channel shutdown, separate synchronization between producers and consumers of a channel has to occur during shutdown.
    157161
    158162\paragraph{\CFA channels} have access to an extensive exception handling mechanism~\cite{Beach21}.
     
    167171When a channel in \CFA is closed, all subsequent calls to the channel raise a resumption exception at the caller.
    168172If the resumption is handled, the caller attempts to complete the channel operation.
    169 However, if channel operation would block, a termination exception is thrown.
     173However, if the channel operation would block, a termination exception is thrown.
    170174If the resumption is not handled, the exception is rethrown as a termination.
    171175These termination exceptions allow for non-local transfer that is used to great effect to eagerly and gracefully shut down a thread.
    172176When 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.
    173 The resumption exception, @channel_closed@, has a couple fields to aid in handling the exception.
    174 The exception contains a pointer to the channel it was thrown from, and a pointer to an element.
    175 In exceptions thrown from remove the element pointer will be null.
    176 In the case of insert the element pointer points to the element that the thread attempted to insert.
     177The resumption exception, @channel_closed@, has internal fields to aid in handling the exception.
     178The exception contains a pointer to the channel it is thrown from and a pointer to a buffer element.
     179For exceptions thrown from @remove@, the buffer element pointer is null.
     180For exceptions thrown from @insert@, the element pointer points to the buffer element that the thread attempted to insert.
    177181This 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.
    178 Furthermore, due to \CFA's powerful exception system, this data can be used to choose handlers based which channel and operation failed.
    179 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.
    180 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.
    181 This 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.
     182Furthermore, due to \CFA's powerful exception system, this data can be used to choose handlers based on which channel and operation failed.
     183For 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.
     184It is worth mentioning that using exceptions for termination may incur a larger performance cost than the Go approach.
     185However, this should not be an issue, since termination is rarely on the fast-path of an application.
     186In contrast, ensuring termination can be easily implemented correctly is the aim of the exception approach.
    182187
    183188\section{\CFA / Go channel Examples}
    184 To highlight the differences between \CFA's and Go's close semantics, three examples will be presented.
     189To highlight the differences between \CFA's and Go's close semantics, three examples are presented.
    185190The first example is a simple shutdown case, where there are producer threads and consumer threads operating on a channel for a fixed duration.
    186 Once the duration ends, producers and consumers terminate without worrying about any leftover values in the channel.
    187 The second example extends the first example by requiring the channel to be empty upon shutdown.
     191Once the duration ends, producers and consumers terminate immediately leaving unprocessed elements in the channel.
     192The second example extends the first by requiring the channel to be empty after shutdown.
    188193Both the first and second example are shown in Figure~\ref{f:ChannelTermination}.
    189 
    190 
    191 First the Go solutions to these examples shown in Figure~\ref{l:go_chan_term} are discussed.
    192 Since some of the elements being passed through the channel are zero-valued, closing the channel in Go does not aid in communicating shutdown.
    193 Instead, a different mechanism to communicate with the consumers and producers needs to be used.
    194 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.
    195 In this example, a flag is used to communicate with producers and another flag is used for consumers.
    196 Producers 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.
    197 The producer flag is set first, then after producers terminate the consumer flag is set and the channel is closed.
    198 In the second example where all values need to be consumed, the main thread iterates over the closed channel to process any remaining values.
    199 
    200 
    201 In the \CFA solutions in Figure~\ref{l:cfa_chan_term}, shutdown is communicated directly to both producers and consumers via the @close@ call.
    202 In 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.
    203 The second \CFA example where all values must be consumed highlights how resumption is used with channel shutdown.
    204 The @Producer@ thread-main knows to stop producing when the @insert@ call on a closed channel raises exception @channel_closed@.
    205 The @Consumer@ thread-main knows to stop consuming after all elements of a closed channel are removed and the call to @remove@ would block.
    206 Hence, 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.
    207 Only when the buffer is drained and the call to @remove@ would block, a termination exception is raised to stop consuming.
    208 The \CFA semantics allow users to communicate channel shutdown directly through the channel, without having to share extra state between threads.
    209 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.
    210 If 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.
    211194
    212195\begin{figure}
     
    214197
    215198\begin{lrbox}{\myboxA}
    216 \begin{cfa}[aboveskip=0pt,belowskip=0pt]
    217 channel( size_t ) Channel{ ChannelSize };
    218 
    219 thread Consumer {};
    220 void main( Consumer & this ) {
    221     try {
    222         for ( ;; )
    223             remove( Channel );
    224     @} catchResume( channel_closed * ) { @
    225     // handled resume => consume from chan
    226     } catch( channel_closed * ) {
    227         // empty or unhandled resume
    228     }
    229 }
    230 
    231 thread Producer {};
    232 void main( Producer & this ) {
    233     size_t count = 0;
    234     try {
    235         for ( ;; )
    236             insert( Channel, count++ );
    237     } catch ( channel_closed * ) {
    238         // unhandled resume or full
    239     }
    240 }
    241 
    242 int main( int argc, char * argv[] ) {
    243     Consumer c[Consumers];
    244     Producer p[Producers];
    245     sleep(Duration`s);
    246     close( Channel );
    247     return 0;
    248 }
    249 \end{cfa}
     199\begin{Golang}[aboveskip=0pt,belowskip=0pt]
     200var channel chan int = make( chan int, 128 )
     201var prodJoin chan int = make( chan int, 4 )
     202var consJoin chan int = make( chan int, 4 )
     203var cons_done, prod_done bool = false, false;
     204func producer() {
     205        for {
     206                if prod_done { break }
     207                channel <- 5
     208        }
     209        prodJoin <- 0 // synch with main thd
     210}
     211
     212func consumer() {
     213        for {
     214                if cons_done { break }
     215                <- channel
     216        }
     217        consJoin <- 0 // synch with main thd
     218}
     219
     220
     221func main() {
     222        for j := 0; j < 4; j++ { go consumer() }
     223        for j := 0; j < 4; j++ { go producer() }
     224        time.Sleep( time.Second * 10 )
     225        prod_done = true
     226        for j := 0; j < 4 ; j++ { <- prodJoin }
     227        cons_done = true
     228        close(channel) // ensure no cons deadlock
     229        @for elem := range channel {@
     230                // process leftover values
     231        @}@
     232        for j := 0; j < 4; j++ { <- consJoin }
     233}
     234\end{Golang}
    250235\end{lrbox}
    251236
    252237\begin{lrbox}{\myboxB}
    253238\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     239<<<<<<< HEAD
    254240var cons_done, prod_done bool = false, false;
    255241var prodJoin chan int = make(chan int)
     
    263249    consJoin <- 0 // synch with main thd
    264250}
    265 
    266 func producer( channel chan uint64 ) {
    267     var count uint64 = 0
    268     for {
    269         if prod_done { break }
    270         channel <- count++
    271     }
    272     prodJoin <- 0 // synch with main thd
    273 }
    274 
    275 func main() {
    276     channel = make(chan uint64, ChannelSize)
    277     for j := 0; j < Consumers; j++ {
    278         go consumer( channel )
    279     }
    280     for j := 0; j < Producers; j++ {
    281         go producer( channel )
    282     }
    283     time.Sleep(time.Second * Duration)
    284     prod_done = true
    285     for j := 0; j < Producers ; j++ {
    286         <-prodJoin // wait for prods
    287     }
    288     cons_done = true
    289     close(channel) // ensure no cons deadlock
    290     @for elem := range channel { @
    291         // process leftover values
    292     @}@
    293     for j := 0; j < Consumers; j++{
    294         <-consJoin // wait for cons
    295     }
    296 }
     251=======
     252channel( size_t ) chan{ 128 };
     253thread Consumer {};
     254thread Producer {};
     255>>>>>>> 8421d3fad6379460563c3a8922889a72c4d868fe
     256
     257void main( Producer & this ) {
     258        try {
     259                for ()
     260                        insert( chan, 5 );
     261        } catch( channel_closed * ) {
     262                // unhandled resume or full
     263        }
     264}
     265void main( Consumer & this ) {
     266        try {
     267                for () { int i = remove( chan ); }
     268        @} catchResume( channel_closed * ) {@
     269                // handled resume => consume from chan
     270        } catch( channel_closed * ) {
     271                // empty or unhandled resume
     272        }
     273}
     274int main() {
     275        Consumer c[4];
     276        Producer p[4];
     277        sleep( 10`s );
     278        close( chan );
     279}
     280
     281
     282
     283
     284
     285
     286
    297287\end{cfa}
    298288\end{lrbox}
    299289
    300 \subfloat[\CFA style]{\label{l:cfa_chan_term}\usebox\myboxA}
     290\subfloat[Go style]{\label{l:go_chan_term}\usebox\myboxA}
    301291\hspace*{3pt}
    302292\vrule
    303293\hspace*{3pt}
    304 \subfloat[Go style]{\label{l:go_chan_term}\usebox\myboxB}
     294\subfloat[\CFA style]{\label{l:cfa_chan_term}\usebox\myboxB}
    305295\caption{Channel Termination Examples 1 and 2. Code specific to example 2 is highlighted.}
    306296\label{f:ChannelTermination}
    307297\end{figure}
    308298
    309 The final shutdown example uses channels to implement a barrier.
    310 It is shown in Figure~\ref{f:ChannelBarrierTermination}.
    311 The 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.
    312 As 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.
    313 Both of these examples are implemented using \CFA syntax so that they can be easily compared.
    314 Figure~\ref{l:cfa_chan_bar} uses \CFA-style channel close semantics and Figure~\ref{l:go_chan_bar} uses Go-style close semantics.
    315 In 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.
    316 As 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.
    317 This sentinel value has to be checked at two points.
     299Figure~\ref{l:go_chan_term} shows the Go solution.
     300Since some of the elements being passed through the channel are zero-valued, closing the channel in Go does not aid in communicating shutdown.
     301Instead, a different mechanism to communicate with the consumers and producers needs to be used.
     302Flag 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.
     303Hence, the two flags @cons_done@ and @prod_done@ are used to communicate with the producers and consumers, respectively.
     304Furthermore, 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.
     305The producer flag is set first;
     306then after all producers terminate, the consumer flag is set and the channel is closed leaving elements in the buffer.
     307To purge the buffer, a loop is added (red) that iterates over the closed channel to process any remaining values.
     308
     309Figure~\ref{l:cfa_chan_term} shows the \CFA solution.
     310Here, shutdown is communicated directly to both producers and consumers via the @close@ call.
     311A @Producer@ thread knows to stop producing when the @insert@ call on a closed channel raises exception @channel_closed@.
     312If 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.
     313If a @Consumer@ thread handles the resumptions exceptions (red), control returns to complete the remove.
     314A @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@.
     315The \CFA semantics allow users to communicate channel shutdown directly through the channel, without having to share extra state between threads.
     316Additionally, 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.
     317
     318Figure~\ref{f:ChannelBarrierTermination} shows a final shutdown example using channels to implement a barrier.
     319A Go and \CFA style solution are presented but both are implemented using \CFA syntax so they can be easily compared.
     320Implementing a barrier is interesting because threads are both producers and consumers on the barrier-internal channels, @entryWait@ and @barWait@.
     321The 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.
     322After @entryWait@ is empty, arriving threads block when removing.
     323However, the arriving threads that entered the barrier cannot leave the barrier until $N$ threads have arrived.
     324Hence, the entering threads block on the empty @barWait@ channel until the $N$th arriving thread inserts $N-1$ elements into @barWait@ to unblock the $N-1$ threads calling @remove@.
     325The race between these arriving threads blocking on @barWait@ and the $N$th thread inserting values into @barWait@ does not affect correctness;
     326\ie an arriving thread may or may not block on channel @barWait@ to get its value.
     327Finally, the last thread to remove from @barWait@ with ticket $N-2$, refills channel @entryWait@ with $N$ values to start the next group into the barrier.
     328
     329Now, the two channels makes termination synchronization between producers and consumers difficult.
     330Interestingly, the shutdown details for this problem are also applicable to other problems with threads producing and consuming from the same channel.
     331The 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.
     332As 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.
     333This sentinel value has to be checked at two points along the fast-path and sentinel values daisy-chained into the buffers.
    318334Furthermore, an additional flag @done@ is needed to communicate to threads once they have left the barrier that they are done.
    319 
    320 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.
     335Also 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.
     336For 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.
    321337This 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.
    322 Also 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.
    323338
    324339\begin{figure}
     
    326341
    327342\begin{lrbox}{\myboxA}
     343\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     344struct barrier {
     345        channel( int ) barWait, entryWait;
     346        int size;
     347};
     348void ?{}( barrier & this, int size ) with(this) {
     349        barWait{size + 1};   entryWait{size + 1};
     350        this.size = size;
     351        for ( i; size )
     352                insert( entryWait, i );
     353}
     354void wait( barrier & this ) with(this) {
     355        int ticket = remove( entryWait );
     356        @if ( ticket == -1 ) { insert( entryWait, -1 ); return; }@
     357        if ( ticket == size - 1 ) {
     358                for ( i; size - 1 )
     359                        insert( barWait, i );
     360                return;
     361        }
     362        ticket = remove( barWait );
     363        @if ( ticket == -1 ) { insert( barWait, -1 ); return; }@
     364        if ( size == 1 || ticket == size - 2 ) { // last ?
     365                for ( i; size )
     366                        insert( entryWait, i );
     367        }
     368}
     369void flush(barrier & this) with(this) {
     370        @insert( entryWait, -1 );   insert( barWait, -1 );@
     371}
     372enum { Threads = 4 };
     373barrier b{Threads};
     374@bool done = false;@
     375thread Thread {};
     376void main( Thread & this ) {
     377        for () {
     378          @if ( done ) break;@
     379                wait( b );
     380        }
     381}
     382int main() {
     383        Thread t[Threads];
     384        sleep(10`s);
     385        done = true;
     386        flush( b );
     387} // wait for threads to terminate
     388\end{cfa}
     389\end{lrbox}
     390
     391\begin{lrbox}{\myboxB}
    328392\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    329393struct barrier {
     
    374438\end{lrbox}
    375439
    376 \begin{lrbox}{\myboxB}
    377 \begin{cfa}[aboveskip=0pt,belowskip=0pt]
    378 struct barrier {
    379         channel( int ) barWait, entryWait;
    380         int size;
    381 };
    382 void ?{}( barrier & this, int size ) with(this) {
    383         barWait{size + 1};   entryWait{size + 1};
    384         this.size = size;
    385         for ( i; size )
    386                 insert( entryWait, i );
    387 }
    388 void wait( barrier & this ) with(this) {
    389         int ticket = remove( entryWait );
    390         @if ( ticket == -1 ) { insert( entryWait, -1 ); return; }@
    391         if ( ticket == size - 1 ) {
    392                 for ( i; size - 1 )
    393                         insert( barWait, i );
    394                 return;
    395         }
    396         ticket = remove( barWait );
    397         @if ( ticket == -1 ) { insert( barWait, -1 ); return; }@
    398         if ( size == 1 || ticket == size - 2 ) { // last ?
    399                 for ( i; size )
    400                         insert( entryWait, i );
    401         }
    402 }
    403 void flush(barrier & this) with(this) {
    404         @insert( entryWait, -1 );   insert( barWait, -1 );@
    405 }
    406 enum { Threads = 4 };
    407 barrier b{Threads};
    408 @bool done = false;@
    409 thread Thread {};
    410 void main( Thread & this ) {
    411         for () {
    412           @if ( done ) break;@
    413                 wait( b );
    414         }
    415 }
    416 int main() {
    417         Thread t[Threads];
    418         sleep(10`s);
    419         done = true;
    420         flush( b );
    421 } // wait for threads to terminate
    422 \end{cfa}
    423 \end{lrbox}
    424 
    425 \subfloat[\CFA style]{\label{l:cfa_chan_bar}\usebox\myboxA}
     440\subfloat[Go style]{\label{l:go_chan_bar}\usebox\myboxA}
    426441\hspace*{3pt}
    427442\vrule
    428443\hspace*{3pt}
    429 \subfloat[Go style]{\label{l:go_chan_bar}\usebox\myboxB}
     444\subfloat[\CFA style]{\label{l:cfa_chan_bar}\usebox\myboxB}
    430445\caption{Channel Barrier Termination}
    431446\label{f:ChannelBarrierTermination}
  • doc/theses/colby_parsons_MMAth/thesis.tex

    rac5d22f r0aef549  
    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
     115    urlcolor=cyan,          % color of external links
     116    breaklinks=true
    116117}
    117118\ifthenelse{\boolean{PrintVersion}}{   % for improved print quality, change some hyperref options
     
    126127% \usepackage[acronym]{glossaries}
    127128\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}}
    128130% If glossaries-extra is not in your LaTeX distribution, get it from CTAN (http://ctan.org/pkg/glossaries-extra),
    129131% 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.