[0faacb8] | 1 | \chapter{Concurrency in \CFA}\label{s:cfa_concurrency} |
---|
| 2 | |
---|
[deeda09] | 3 | The groundwork for concurrency in \CFA was laid by Thierry Delisle in his Master's Thesis~\cite{Delisle18}. |
---|
| 4 | In that work, he introduced generators, coroutines, monitors, and user-level threading. |
---|
| 5 | Not listed in that work were basic concurrency features needed as building blocks, such as locks, futures, and condition variables, which he also added to \CFA. |
---|
[0faacb8] | 6 | |
---|
| 7 | \section{Threading Model}\label{s:threading} |
---|
[deeda09] | 8 | \CFA provides user-level threading and supports an $M$:$N$ threading model where $M$ user threads are scheduled on $N$ kernel threads, where both $M$ and $N$ can be explicitly set by the user. |
---|
[21d1c9c] | 9 | Kernel threads are created by declaring a @processor@ structure. |
---|
[deeda09] | 10 | User-thread types are defined by creating a @thread@ aggregate-type, \ie replace @struct@ with @thread@. |
---|
| 11 | For each thread type a corresponding @main@ routine must be defined, which is where the thread starts running once it is created. |
---|
| 12 | Examples of \CFA user thread and processor creation are shown in \VRef[Listing]{l:cfa_thd_init}. |
---|
[0faacb8] | 13 | |
---|
[deeda09] | 14 | \begin{cfa}[caption={Example of \CFA user thread and processor creation},label={l:cfa_thd_init}] |
---|
| 15 | @thread@ my_thread {...}; $\C{// user thread type}$ |
---|
| 16 | void @main@( my_thread & this ) { $\C{// thread start routine}$ |
---|
| 17 | sout | "Hello threading world"; |
---|
[0faacb8] | 18 | } |
---|
| 19 | |
---|
| 20 | int main() { |
---|
[4541b09] | 21 | @processor@ p[2]; $\C{// add 2 processors = 3 total with starting processor}$ |
---|
[deeda09] | 22 | { |
---|
| 23 | my_thread t[2], * t3 = new(); $\C{// create 3 user threads, running in main routine}$ |
---|
| 24 | ... // execute concurrently |
---|
| 25 | delete( t3 ); $\C{// wait for thread to end and deallocate}$ |
---|
[4541b09] | 26 | } // wait for threads to end and deallocate |
---|
[0faacb8] | 27 | } |
---|
[deeda09] | 28 | \end{cfa} |
---|
| 29 | |
---|
| 30 | When processors are added, they are added alongside the existing processor given to each program. |
---|
| 31 | Thus, for $N$ processors, allocate $N-1$ processors. |
---|
[2d831a1] | 32 | A thread is implicitly joined at deallocation, either implicitly at block exit for stack allocation or explicitly at @delete@ for heap allocation. |
---|
[deeda09] | 33 | The thread performing the deallocation must wait for the thread to terminate before the deallocation can occur. |
---|
| 34 | A thread terminates by returning from the main routine where it starts. |
---|
[0faacb8] | 35 | |
---|
[aae9c17] | 36 | \section{Existing Concurrency Features} |
---|
| 37 | \CFA currently provides a suite of concurrency features including futures, locks, condition variables, generators, coroutines, monitors. |
---|
| 38 | Examples of these features are omitted as most of them are the same as their counterparts in other languages. |
---|
| 39 | It is worthwhile to note that all concurrency features added to \CFA are made to be compatible each other. |
---|
| 40 | The laundry list of features above and the ones introduced in this thesis can be used in the same program without issue. |
---|
| 41 | |
---|
| 42 | Solving concurrent problems requires a diverse toolkit. |
---|
| 43 | This work aims to flesh out \CFA's concurrent toolkit to fill in gaps. |
---|
| 44 | Futures are used when a one-shot result needs to be safely delivered concurrently, and are especially useful when the receiver needs to block until the result is ready. |
---|
| 45 | When multiple values have to be sent, or more synchronization is needed, futures are not powerful enough, which introduces the need for channels. |
---|
| 46 | A close cousin of channels is actor systems, which take message passing a step further and go beyond channels to provide a level of abstraction that allows for easy scalability and separation of concerns. |
---|
| 47 | The @waituntil@ and @mutex@ statements provide utilities allowing for easier use of the existing features. |
---|
| 48 | All the contributions of this thesis provide the ability to solve concurrent problems that formerly would require a user to either implement a similar feature themselves or construct an ad-hoc solution. |
---|
| 49 | |
---|
[deeda09] | 50 | % Local Variables: % |
---|
| 51 | % tab-width: 4 % |
---|
| 52 | % End: % |
---|