1 | % requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended |
---|
2 | |
---|
3 | % inline code ©...© (copyright symbol) emacs: C-q M-) |
---|
4 | % red highlighting ®...® (registered trademark symbol) emacs: C-q M-. |
---|
5 | % blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_ |
---|
6 | % green highlighting ¢...¢ (cent symbol) emacs: C-q M-" |
---|
7 | % LaTex escape §...§ (section symbol) emacs: C-q M-' |
---|
8 | % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^ |
---|
9 | % math escape $...$ (dollar symbol) |
---|
10 | |
---|
11 | \documentclass[twoside,11pt]{article} |
---|
12 | |
---|
13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
14 | |
---|
15 | % Latex packages used in the document. |
---|
16 | \usepackage[T1]{fontenc} % allow Latin1 (extended ASCII) characters |
---|
17 | \usepackage{textcomp} |
---|
18 | \usepackage[latin1]{inputenc} |
---|
19 | \usepackage{fullpage,times,comment} |
---|
20 | \usepackage{epic,eepic} |
---|
21 | \usepackage{upquote} % switch curled `'" to straight |
---|
22 | \usepackage{calc} |
---|
23 | \usepackage{xspace} |
---|
24 | \usepackage{graphicx} |
---|
25 | \usepackage{tabularx} |
---|
26 | \usepackage[acronym]{glossaries} |
---|
27 | \usepackage{varioref} % extended references |
---|
28 | \usepackage{inconsolata} |
---|
29 | \usepackage{listings} % format program code |
---|
30 | \usepackage[flushmargin]{footmisc} % support label/reference in footnote |
---|
31 | \usepackage{latexsym} % \Box glyph |
---|
32 | \usepackage{mathptmx} % better math font with "times" |
---|
33 | \usepackage[usenames]{color} |
---|
34 | \usepackage[pagewise]{lineno} |
---|
35 | \usepackage{fancyhdr} |
---|
36 | \renewcommand{\linenumberfont}{\scriptsize\sffamily} |
---|
37 | \input{common} % bespoke macros used in the document |
---|
38 | \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref} |
---|
39 | \usepackage{breakurl} |
---|
40 | |
---|
41 | \usepackage{tikz} |
---|
42 | \def\checkmark{\tikz\fill[scale=0.4](0,.35) -- (.25,0) -- (1,.7) -- (.25,.15) -- cycle;} |
---|
43 | |
---|
44 | \renewcommand{\UrlFont}{\small\sf} |
---|
45 | |
---|
46 | \setlength{\topmargin}{-0.45in} % move running title into header |
---|
47 | \setlength{\headsep}{0.25in} |
---|
48 | |
---|
49 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
50 | |
---|
51 | % Names used in the document. |
---|
52 | |
---|
53 | \newcommand{\Version}{1.0.0} |
---|
54 | \newcommand{\CS}{C\raisebox{-0.9ex}{\large$^\sharp$}\xspace} |
---|
55 | |
---|
56 | \newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}} |
---|
57 | \newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}} |
---|
58 | \newcommand{\R}[1]{\Textbf{#1}} |
---|
59 | \newcommand{\B}[1]{{\Textbf[blue]{#1}}} |
---|
60 | \newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}} |
---|
61 | \newcommand{\uC}{$\mu$\CC} |
---|
62 | \newcommand{\cit}{\textsuperscript{[Citation Needed]}\xspace} |
---|
63 | \newcommand{\code}[1]{\lstinline{#1}} |
---|
64 | \newcommand{\pseudo}[1]{\lstinline[language=Pseudo]{#1}} |
---|
65 | |
---|
66 | \input{glossary} |
---|
67 | |
---|
68 | \newsavebox{\LstBox} |
---|
69 | |
---|
70 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
71 | |
---|
72 | \setcounter{secnumdepth}{3} % number subsubsections |
---|
73 | \setcounter{tocdepth}{3} % subsubsections in table of contents |
---|
74 | % \linenumbers % comment out to turn off line numbering |
---|
75 | \makeindex |
---|
76 | \pagestyle{fancy} |
---|
77 | \fancyhf{} |
---|
78 | \cfoot{\thepage} |
---|
79 | \rfoot{v\input{version}} |
---|
80 | |
---|
81 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
82 | |
---|
83 | \begin{document} |
---|
84 | % \linenumbers |
---|
85 | |
---|
86 | \title{Concurrency in \CFA} |
---|
87 | \author{Thierry Delisle \\ |
---|
88 | School of Computer Science, University of Waterloo, \\ Waterloo, Ontario, Canada |
---|
89 | } |
---|
90 | |
---|
91 | \maketitle |
---|
92 | |
---|
93 | % ### # # ####### ###### ####### |
---|
94 | % # ## # # # # # # |
---|
95 | % # # # # # # # # # |
---|
96 | % # # # # # ###### # # |
---|
97 | % # # # # # # # # # |
---|
98 | % # # ## # # # # # |
---|
99 | % ### # # # # # ####### |
---|
100 | |
---|
101 | \section{Introduction} |
---|
102 | This proposal provides a minimal core concurrency API that is both simple, efficient and can be reused to build higher-level features. The simplest possible concurrency core is a thread and a lock but this low-level approach is hard to master. An easier approach for users is to support higher-level constructs as the basis of the concurrency in \CFA. Indeed, for highly productive parallel programming, high-level approaches are much more popular~\cite{HPP:Study}. Examples are task based parallelism, message passing and implicit threading. |
---|
103 | |
---|
104 | There are actually two problems that need to be solved in the design of the concurrency for a programming language. Which concurrency tools are available to the users and which parallelism tools are available. While these two concepts are often seen together, they are in fact distinct concepts that require different sorts of tools~\cite{Buhr05a}. Concurrency tools need to handle mutual exclusion and synchronization, while parallelism tools are more about performance, cost and resource utilization. |
---|
105 | |
---|
106 | % ##### ####### # # ##### # # ###### ###### ####### # # ##### # # |
---|
107 | % # # # # ## # # # # # # # # # # ## # # # # # |
---|
108 | % # # # # # # # # # # # # # # # # # # # # |
---|
109 | % # # # # # # # # # ###### ###### ##### # # # # # |
---|
110 | % # # # # # # # # # # # # # # # # # # # |
---|
111 | % # # # # # ## # # # # # # # # # # ## # # # |
---|
112 | % ##### ####### # # ##### ##### # # # # ####### # # ##### # |
---|
113 | |
---|
114 | \section{Concurrency} |
---|
115 | Several tool can be used to solve concurrency challenges. Since these challenges always appear with the use of mutable shared state, some languages and libraries simply disallow mutable shared-state (Erlang~\cite{Erlang}, Haskell~\cite{Haskell}, Akka (Scala)~\cite{Akka}). In these paradigms, interaction among concurrent objects relies on message passing~\cite{Thoth,Harmony,V-Kernel} or other paradigms that closely relate to networking concepts. However, in languages that use routine calls as their core abstraction mechanism, these approaches force a clear distinction between concurrent and non-concurrent paradigms (i.e. message passing versus routine call). Which in turn means that, in order to be effective, programmers need to learn two sets of designs patterns. This distinction can be hidden away in library code, but effective use of the librairy will still have to take both paradigms into account. Approaches based on shared memory are more closely related to non-concurrent paradigms since they often rely on non-concurrent constructs like routine calls and objects. At a lower level these can be implemented as locks and atomic operations. Many such mechanisms have been proposed, including semaphores~\cite{Dijkstra68b} and path expressions~\cite{Campbell74}. However, for productivity reasons it is desireable to have a higher-level construct to be the core concurrency paradigm~\cite{HPP:Study}. An approach that is worth mentionning because it is gaining in popularity is transactionnal memory~\cite{Dice10}[Check citation]. While this approach is even pursued by system languages like \CC\cit, the performance and feature set is currently too restrictive to be possible to add such a paradigm to a language like C or \CC\cit, which is why it was rejected as the core paradigm for concurrency in \CFA. One of the most natural, elegant, and efficient mechanisms for synchronization and communication, especially for shared memory systems, is the \emph{monitor}. Monitors were first proposed by Brinch Hansen~\cite{Hansen73} and later described and extended by C.A.R.~Hoare~\cite{Hoare74}. Many programming languages---e.g., Concurrent Pascal~\cite{ConcurrentPascal}, Mesa~\cite{Mesa}, Modula~\cite{Modula-2}, Turing~\cite{Turing:old}, Modula-3~\cite{Modula-3}, NeWS~\cite{NeWS}, Emerald~\cite{Emerald}, \uC~\cite{Buhr92a} and Java~\cite{Java}---provide monitors as explicit language constructs. In addition, operating-system kernels and device drivers have a monitor-like structure, although they often use lower-level primitives such as semaphores or locks to simulate monitors. For these reasons, this project proposes Monitors as the core concurrency construct. |
---|
116 | |
---|
117 | % # # ####### # # ### ####### ####### ###### ##### |
---|
118 | % ## ## # # ## # # # # # # # # # |
---|
119 | % # # # # # # # # # # # # # # # # |
---|
120 | % # # # # # # # # # # # # ###### ##### |
---|
121 | % # # # # # # # # # # # # # # |
---|
122 | % # # # # # ## # # # # # # # # |
---|
123 | % # # ####### # # ### # ####### # # ##### |
---|
124 | |
---|
125 | \subsection{Monitors} |
---|
126 | A monitor is a set of routines that ensure mutual exclusion when accessing shared state. This concept is generally associated with Object-Oriented Languages like Java~\cite{Java} or \uC~\cite{uC++book} but does not strictly require OOP semantics. The only requirements is the ability to declare a handle to a shared object and a set of routines that act on it : |
---|
127 | \begin{lstlisting} |
---|
128 | typedef /*some monitor type*/ monitor; |
---|
129 | int f(monitor & m); |
---|
130 | |
---|
131 | int main() { |
---|
132 | monitor m; |
---|
133 | f(m); |
---|
134 | } |
---|
135 | \end{lstlisting} |
---|
136 | |
---|
137 | % ##### # # # |
---|
138 | % # # # # # # |
---|
139 | % # # # # # |
---|
140 | % # # # # # |
---|
141 | % # ####### # # |
---|
142 | % # # # # # # |
---|
143 | % ##### # # ####### ####### |
---|
144 | |
---|
145 | \subsubsection{Call semantics} \label{call} |
---|
146 | The above monitor example displays some of their intrinsic characteristics. Indeed, it is necessary to use pass-by-reference over pass-by-value for monitor routines. This semantics is important because at their core, monitors are implicit mutual-exclusion objects (locks), and these objects cannot be copied. Therefore, monitors are implicitly non-copyable. |
---|
147 | |
---|
148 | Another aspect to consider is when a monitor acquires its mutual exclusion. For example, a monitor may need to be passed through multiple helper routines that do not acquire the monitor mutual exclusion on entry. Pass through can be both generic helper routines (\code{swap}, \code{sort}, etc.) or specific helper routines like the following to implement an atomic large counter : |
---|
149 | |
---|
150 | \begin{lstlisting} |
---|
151 | mutex struct counter_t { /*...*/ }; |
---|
152 | |
---|
153 | void ?{}(counter_t & nomutex this); |
---|
154 | size_t ++?(counter_t & mutex this); |
---|
155 | void ?{}(size_t * this, counter_t & mutex cnt); //need for mutex is platform dependent here |
---|
156 | \end{lstlisting} |
---|
157 | *semantics of the declaration of \code{mutex struct counter_t} are discussed in details in section \ref{data} |
---|
158 | |
---|
159 | Here, the constructor(\code(?{})) uses the \code{nomutex} keyword to signify that it does not acquire the monitor mutual exclusion when constructing. This semantics is because object not yet constructed should never be shared and therefore do not require mutual exclusion. The prefix increment operator uses \code{mutex} to protect the incrementing process from race conditions. Finally, there is a conversion operator from \code{counter_t} to \code{size_t}. This conversion may or may not require the \code{mutex} key word depending on whether or not reading an \code{size_t} is an atomic operation or not. |
---|
160 | |
---|
161 | Having both \code{mutex} and \code{nomutex} keywords could be argued to be redundant based on the meaning of a routine having neither of these keywords. If there were a meaning to routine \code{void foo(counter_t & this)} then one could argue that it should default to the safest option : \code{mutex}. On the other hand, the option of having routine \code{void foo(counter_t & this)} mean \code{nomutex} is unsafe by default and may easily cause subtle errors. It can be argued that this is the more "normal" behavior, \code{nomutex} effectively stating explicitly that "this routine has nothing special". Another alternative is to make having exactly one of these keywords mandatory, which would provide the same semantics but without the ambiguity of supporting routine \code{void foo(counter_t & this)}. Mandatory keywords would also have the added benefice of being self-documented but at the cost of extra typing. In the end, which solution should be picked is still up for debate. For the reminder of this proposal, the explicit approach is used for clarity. |
---|
162 | |
---|
163 | Regardless of which keyword is kept, it is important to establish when mutex/nomutex may be used as a type qualifier. Consider : |
---|
164 | \begin{lstlisting} |
---|
165 | int f1(monitor & mutex m); |
---|
166 | int f2(const monitor & mutex m); |
---|
167 | int f3(monitor ** mutex m); |
---|
168 | int f4(monitor *[] mutex m); |
---|
169 | int f5(graph(monitor*) & mutex m); |
---|
170 | \end{lstlisting} |
---|
171 | |
---|
172 | The problem is to indentify which object(s) should be acquired. Furthermore, each object needs to be acquired only once. In case of simple routines like \code{f1} and \code{f2} it is easy to identify an exhaustive list of objects to acquire on entering. Adding indirections (\code{f3}) still allows the compiler and programmer to indentify which object is acquired. However, adding in arrays (\code{f4}) makes it much harder. Array lengths are not necessarily known in C and even then making sure we only acquire objects once becomes also none trivial. This can be extended to absurd limits like \code{f5}, which uses a graph of monitors. To keep everyone as sane as possible~\cite{Chicken}, this projects imposes the requirement that a routine may only acquire one monitor per parameter and it must be the type of the parameter (ignoring potential qualifiers and indirections). Also note that while routine \code{f3} can be supported, meaning that monitor \code{**m} will be acquired, passing an array to this routine would be type safe and result in undefined behavior. For this reason, it would also be reasonnable to disallow mutex in the context where arrays may be passed. |
---|
173 | |
---|
174 | % ###### # ####### # |
---|
175 | % # # # # # # # |
---|
176 | % # # # # # # # |
---|
177 | % # # # # # # # |
---|
178 | % # # ####### # ####### |
---|
179 | % # # # # # # # |
---|
180 | % ###### # # # # # |
---|
181 | |
---|
182 | \subsubsection{Data semantics} \label{data} |
---|
183 | Once the call semantics are established, the next step is to establish data semantics. Indeed, until now a monitor is used simply as a generic handle but in most cases monitors contian shared data. This data should be intrinsic to the monitor declaration to prevent any accidental use of data without its appripriate protection. For example here is a more fleshed-out version of the counter showed in \ref{call}: |
---|
184 | \begin{lstlisting} |
---|
185 | mutex struct counter_t { |
---|
186 | int value; |
---|
187 | }; |
---|
188 | |
---|
189 | void ?{}(counter_t & nomutex this) { |
---|
190 | this.cnt = 0; |
---|
191 | } |
---|
192 | |
---|
193 | int ++?(counter_t & mutex this) { |
---|
194 | return ++this.value; |
---|
195 | } |
---|
196 | |
---|
197 | void ?{}(int * this, counter_t & mutex cnt) { //need for mutex is platform dependent here |
---|
198 | *this = (int)cnt; |
---|
199 | } |
---|
200 | \end{lstlisting} |
---|
201 | |
---|
202 | This simple counter offers an example of monitor usage. Notice how the counter is used without any explicit synchronisation and yet supports thread-safe semantics for both reading and writting : |
---|
203 | \begin{center} |
---|
204 | \begin{tabular}{c @{\hskip 0.35in} c @{\hskip 0.35in} c} |
---|
205 | \begin{lstlisting} |
---|
206 | counter_t cnt; |
---|
207 | |
---|
208 | thread 1 : cnt++; |
---|
209 | thread 2 : cnt++; |
---|
210 | thread 3 : cnt++; |
---|
211 | ... |
---|
212 | thread N : cnt++; |
---|
213 | \end{lstlisting} |
---|
214 | \end{tabular} |
---|
215 | \end{center} |
---|
216 | |
---|
217 | These simple mutual exclusion semantics also naturally expand to multi-monitor calls. |
---|
218 | \begin{lstlisting} |
---|
219 | int f(MonitorA & mutex a, MonitorB & mutex b); |
---|
220 | |
---|
221 | MonitorA a; |
---|
222 | MonitorB b; |
---|
223 | f(a,b); |
---|
224 | \end{lstlisting} |
---|
225 | |
---|
226 | This code acquires both locks before entering the critical section. In practice, writing multi-locking routines that can not lead to deadlocks can be tricky. Having language support for such a feature is therefore a significant asset for \CFA. In the case presented above, \CFA guarantees that the order of aquisition will be consistent across calls to routines using the same monitors as arguments. However, since \CFA monitors use multi-acquiring locks users can effectively force the acquiring order. For example, notice which routines use \code{mutex}/\code{nomutex} and how this affects aquiring order : |
---|
227 | \begin{lstlisting} |
---|
228 | void foo(A & mutex a, B & mutex a) { |
---|
229 | //... |
---|
230 | } |
---|
231 | |
---|
232 | void bar(A & mutex a, B & nomutex a) |
---|
233 | //... |
---|
234 | foo(a, b); |
---|
235 | //... |
---|
236 | } |
---|
237 | |
---|
238 | void baz(A & nomutex a, B & mutex a) |
---|
239 | //... |
---|
240 | foo(a, b); |
---|
241 | //... |
---|
242 | } |
---|
243 | \end{lstlisting} |
---|
244 | |
---|
245 | Such a use will lead to nested monitor call problems~\cite{Lister77}, which are a specific implementation of the lock acquiring order problem. In the example above, the user uses implicit ordering in the case of function \code{foo} but explicit ordering in the case of \code{bar} and \code{baz}. This subtle mistake means that calling these routines concurrently may lead to deadlocks, depending on the implicit ordering matching the explicit ordering. As shown on several occasion\cit, solving this problems requires to : |
---|
246 | \begin{enumerate} |
---|
247 | \item Dynamically track the monitor call order. |
---|
248 | \item Implement rollback semantics. |
---|
249 | \end{enumerate} |
---|
250 | |
---|
251 | While the first requirement is already a significant constraint on the system, implementing a general rollback semantics in a C-like language is prohibitively complex \cit. In \CFA users simply need to be carefull when acquiring multiple monitors at the same time. |
---|
252 | |
---|
253 | % ###### ####### ####### # ### # ##### |
---|
254 | % # # # # # # # # # # |
---|
255 | % # # # # # # # # # |
---|
256 | % # # ##### # # # # # ##### |
---|
257 | % # # # # ####### # # # |
---|
258 | % # # # # # # # # # # |
---|
259 | % ###### ####### # # # ### ####### ##### |
---|
260 | % |
---|
261 | % ###### ####### # # # # # ####### ###### # # |
---|
262 | % # # # # # # # ## ## # # # # # # |
---|
263 | % # # # # # # # # # # # # # # # # # |
---|
264 | % ##### ###### # # # # # # # # # ###### ####### |
---|
265 | % # # # # # # # # # # # # # |
---|
266 | % # # # # # # # # # # # # # |
---|
267 | % # ####### ####### # # # ####### # # # # |
---|
268 | |
---|
269 | \subsubsection{Implementation Details: Interaction with polymorphism} |
---|
270 | At first glance, interaction between monitors and \CFA's concept of polymorphism seem complex to support. However, it can be reasoned that entry-point locking can solve most of the issues that could be present with polymorphism. |
---|
271 | |
---|
272 | First of all, interaction between \code{otype} polymorphism and monitors is impossible since monitors do not support copying. Therefore, the main question is how to support \code{dtype} polymorphism. Since a monitor's main purpose is to ensure mutual exclusion when accessing shared data, this implies that mutual exclusion is only required for routines that do in fact access shared data. However, since \code{dtype} polymorphism always handles incomplete types (by definition), no \code{dtype} polymorphic routine can access shared data since the data requires knowledge about the type. Therefore the only concern when combining \code{dtype} polymorphism and monitors is to protect access to routines. \Gls{callsite-locking}\footnotemark would require a significant amount of work, since any \code{dtype} routine may have to obtain some lock before calling a routine, depending on whether or not the type passed is a monitor. However, with \gls{entry-point-locking}\footnotemark[\value{footnote}] calling a monitor routine becomes exactly the same as calling it from anywhere else. |
---|
273 | \footnotetext{See glossary for a definition of \gls{callsite-locking} and \gls{entry-point-locking}} |
---|
274 | |
---|
275 | % ### # # ####### ##### ##### # # ####### ###### |
---|
276 | % # ## # # # # # # # # # # # |
---|
277 | % # # # # # # # # # # # # |
---|
278 | % # # # # # ##### # ####### ##### # # |
---|
279 | % # # # # # ### # # # # # # # |
---|
280 | % # # ## # ### # # # # # # # # # |
---|
281 | % ### # # # ### ##### ##### # # ####### ###### |
---|
282 | |
---|
283 | \subsection{Internal scheduling} \label{insched} |
---|
284 | Monitors should also be able to schedule what threads access it as a mean of synchronization. Internal scheduling is one of the simple examples of such a feature. It allows users to declare condition variables and wait for them to be signaled. Here is a simple example of such a technique : |
---|
285 | |
---|
286 | \begin{lstlisting} |
---|
287 | mutex struct A { |
---|
288 | condition e; |
---|
289 | } |
---|
290 | |
---|
291 | void foo(A & mutex a) { |
---|
292 | //... |
---|
293 | wait(a.e); |
---|
294 | //... |
---|
295 | } |
---|
296 | |
---|
297 | void bar(A & mutex a) { |
---|
298 | signal(a.e); |
---|
299 | } |
---|
300 | \end{lstlisting} |
---|
301 | |
---|
302 | Here routine \code{foo} waits on the \code{signal} from \code{bar} before making further progress, effectively ensuring a basic ordering. This semantic can easily be extended to multi-monitor calls by offering the same guarantee. |
---|
303 | |
---|
304 | \begin{center} |
---|
305 | \begin{tabular}{ c @{\hskip 0.65in} c } |
---|
306 | Thread 1 & Thread 2 \\ |
---|
307 | \begin{lstlisting} |
---|
308 | void foo(monitor & mutex a, |
---|
309 | monitor & mutex b) { |
---|
310 | //... |
---|
311 | wait(a.e); |
---|
312 | //... |
---|
313 | } |
---|
314 | |
---|
315 | foo(a, b); |
---|
316 | \end{lstlisting} &\begin{lstlisting} |
---|
317 | void bar(monitor & mutex a, |
---|
318 | monitor & mutex b) { |
---|
319 | signal(a.e); |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | |
---|
324 | bar(a, b); |
---|
325 | \end{lstlisting} |
---|
326 | \end{tabular} |
---|
327 | \end{center} |
---|
328 | |
---|
329 | A direct extension of the single monitor semantics would be to release all locks when waiting and transferring ownership of all locks when signalling. However, for the purpose of synchronization it may be usefull to only release some of the locks but keep others. On the technical side, partially releasing lock is feasible but from the user perspective a choice must be made for the syntax of this feature. It is possible to do without any extra syntax by relying on order of acquisition (Note that here the use of helper routines is irrelevant, only routines the acquire mutual exclusion have an impact on internal scheduling): |
---|
330 | |
---|
331 | \begin{center} |
---|
332 | \begin{tabular}{|c|c|c|} |
---|
333 | Context 1 & Context 2 & Context 3 \\ |
---|
334 | \hline |
---|
335 | \begin{lstlisting} |
---|
336 | condition e; |
---|
337 | |
---|
338 | void foo(monitor & mutex a, |
---|
339 | monitor & mutex b) { |
---|
340 | wait(e); |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | |
---|
345 | |
---|
346 | |
---|
347 | |
---|
348 | foo(a,b); |
---|
349 | \end{lstlisting} &\begin{lstlisting} |
---|
350 | condition e; |
---|
351 | |
---|
352 | void bar(monitor & mutex a, |
---|
353 | monitor & nomutex b) { |
---|
354 | foo(a,b); |
---|
355 | } |
---|
356 | |
---|
357 | void foo(monitor & mutex a, |
---|
358 | monitor & mutex b) { |
---|
359 | wait(e); |
---|
360 | } |
---|
361 | |
---|
362 | bar(a, b); |
---|
363 | \end{lstlisting} &\begin{lstlisting} |
---|
364 | condition e; |
---|
365 | |
---|
366 | void bar(monitor & mutex a, |
---|
367 | monitor & nomutex b) { |
---|
368 | foo(a,b); |
---|
369 | } |
---|
370 | |
---|
371 | void baz(monitor & nomutex a, |
---|
372 | monitor & mutex b) { |
---|
373 | wait(e); |
---|
374 | } |
---|
375 | |
---|
376 | bar(a, b); |
---|
377 | \end{lstlisting} |
---|
378 | \end{tabular} |
---|
379 | \end{center} |
---|
380 | |
---|
381 | This can be interpreted in two different ways : |
---|
382 | \begin{flushleft} |
---|
383 | \begin{enumerate} |
---|
384 | \item \code{wait} atomically releases the monitors acquired by the inner-most routine, \underline{ignoring} nested calls. |
---|
385 | \item \code{wait} atomically releases the monitors acquired by the inner-most routine, \underline{considering} nested calls. |
---|
386 | \end{enumerate} |
---|
387 | \end{flushleft} |
---|
388 | While the difference between these two is subtle, it has a significant impact. In the first case it means that the calls to \code{foo} would behave the same in Context 1 and 2. This semantic would also mean that the call to \code{wait} in routine \code{baz} would only release \code{monitor b}. While this may seem intuitive with these examples, it does have one significant implication, it creates a strong distinction between acquiring multiple monitors in sequence and acquiring the same monitors simulatenously, i.e. : |
---|
389 | |
---|
390 | \begin{center} |
---|
391 | \begin{tabular}{c @{\hskip 0.35in} c @{\hskip 0.35in} c} |
---|
392 | \begin{lstlisting} |
---|
393 | enterMonitor(a); |
---|
394 | enterMonitor(b); |
---|
395 | // do stuff |
---|
396 | leaveMonitor(b); |
---|
397 | leaveMonitor(a); |
---|
398 | \end{lstlisting} & != &\begin{lstlisting} |
---|
399 | enterMonitor(a); |
---|
400 | enterMonitor(a, b); |
---|
401 | // do stuff |
---|
402 | leaveMonitor(a, b); |
---|
403 | leaveMonitor(a); |
---|
404 | \end{lstlisting} |
---|
405 | \end{tabular} |
---|
406 | \end{center} |
---|
407 | |
---|
408 | This is not intuitive because even if both methods display the same monitors state both inside and outside the critical section respectively, the behavior is different. Furthermore, the actual acquiring order will be exaclty the same since acquiring a monitor from inside its mutual exclusion is a no-op. This means that even if the data and the actual control flow are the same using both methods, the behavior of the \code{wait} will be different. The alternative is option 2, that is releasing acquired monitors, \underline{considering} nesting. This solves the issue of having the two acquiring method differ at the cost of making routine \code{foo} behave differently depending on from which context it is called (Context 1 or 2). Indeed in Context 2, routine \code{foo} actually behaves like routine \code{baz} rather than having the same behavior than in Context 1. The fact that both implicit approaches can be unintuitive depending on the perspective may be a sign that the explicit approach is superior. For this reason this \CFA does not support implicit monitor releasing and uses explicit semantics. |
---|
409 | \\ |
---|
410 | |
---|
411 | The following examples shows three alternatives of explicit wait semantics : |
---|
412 | \\ |
---|
413 | |
---|
414 | \begin{center} |
---|
415 | \begin{tabular}{|c|c|c|} |
---|
416 | Case 1 & Case 2 & Case 3 \\ |
---|
417 | Branding on construction & Explicit release list & Explicit ignore list \\ |
---|
418 | \hline |
---|
419 | \begin{lstlisting} |
---|
420 | void foo(monitor & mutex a, |
---|
421 | monitor & mutex b, |
---|
422 | condition & c) |
---|
423 | { |
---|
424 | // Releases monitors |
---|
425 | // branded in ctor |
---|
426 | wait(c); |
---|
427 | } |
---|
428 | |
---|
429 | monitor a; |
---|
430 | monitor b; |
---|
431 | condition1 c1 = {a}; |
---|
432 | condition2 c2 = {a, b}; |
---|
433 | |
---|
434 | //Will release only a |
---|
435 | foo(a,b,c1); |
---|
436 | |
---|
437 | //Will release a and b |
---|
438 | foo(a,b,c2); |
---|
439 | \end{lstlisting} &\begin{lstlisting} |
---|
440 | void foo(monitor & mutex a, |
---|
441 | monitor & mutex b, |
---|
442 | condition & c) |
---|
443 | { |
---|
444 | // Releases monitor a |
---|
445 | // Holds monitor b |
---|
446 | waitRelease(c, [a]); |
---|
447 | } |
---|
448 | |
---|
449 | monitor a; |
---|
450 | monitor b; |
---|
451 | condition c; |
---|
452 | |
---|
453 | |
---|
454 | |
---|
455 | foo(a,b,c); |
---|
456 | |
---|
457 | |
---|
458 | |
---|
459 | \end{lstlisting} &\begin{lstlisting} |
---|
460 | void foo(monitor & mutex a, |
---|
461 | monitor & mutex b, |
---|
462 | condition & c) |
---|
463 | { |
---|
464 | // Releases monitor a |
---|
465 | // Holds monitor b |
---|
466 | waitHold(c, [b]); |
---|
467 | } |
---|
468 | |
---|
469 | monitor a; |
---|
470 | monitor b; |
---|
471 | condition c; |
---|
472 | |
---|
473 | |
---|
474 | |
---|
475 | foo(a,b,c); |
---|
476 | |
---|
477 | |
---|
478 | |
---|
479 | \end{lstlisting} |
---|
480 | \end{tabular} |
---|
481 | \end{center} |
---|
482 | (Note : Case 2 and 3 use tuple semantics to pass a variable length list of elements.) |
---|
483 | \\ |
---|
484 | |
---|
485 | All these cases have their pros and cons. Case 1 is more distinct because it means programmers need to be carefull about where the condition is initialized as well as where it is used. On the other hand, it is very clear and explicitly states which monitor is released and which monitor stays acquired. This is similar to Case 2, which releases only the monitors explictly listed. However, in Case 2, calling the \code{wait} routine instead of the \code{waitRelease} routine releases all the acquired monitor. The Case 3 is an improvement on that since it releases all the monitors except those specified. The result is that the \code{wait} routine can be written as follows : |
---|
486 | \begin{lstlisting} |
---|
487 | void wait(condition & cond) { |
---|
488 | waitHold(cond, []); |
---|
489 | } |
---|
490 | \end{lstlisting} |
---|
491 | This alternative offers nice and consistent behavior between \code{wait} and \code{waitHold}. However, one large pitfall is that mutual exclusion can now be violated by calls to library code. Indeed, even if the following example seems benign there is one significant problem : |
---|
492 | \begin{lstlisting} |
---|
493 | monitor global; |
---|
494 | |
---|
495 | extern void doStuff(); //uses global |
---|
496 | |
---|
497 | void foo(monitor & mutex m) { |
---|
498 | //... |
---|
499 | doStuff(); //warning can release monitor m |
---|
500 | //... |
---|
501 | } |
---|
502 | |
---|
503 | foo(global); |
---|
504 | \end{lstlisting} |
---|
505 | |
---|
506 | Indeed, if Case 2 or 3 are chosen it any code can violate the mutual exclusion of the calling code by issuing calls to \code{wait} or \code{waitHold} in a nested monitor context. Case 2 can be salvaged by removing the \code{wait} routine from the API but Case 3 cannot prevent users from calling \code{waitHold(someCondition, [])}. For this reason the syntax proposed in Case 3 is rejected. Note that the syntax proposed in case 1 and 2 are not exclusive. Indeed, by supporting two types of condition both cases can be supported : |
---|
507 | \begin{lstlisting} |
---|
508 | struct condition { /*...*/ }; |
---|
509 | |
---|
510 | // Second argument is a variable length tuple. |
---|
511 | void wait(condition & cond, [...] monitorsToRelease); |
---|
512 | void signal(condition & cond); |
---|
513 | |
---|
514 | struct conditionN { /*...*/ }; |
---|
515 | |
---|
516 | void ?{}(conditionN* this, /*list of N monitors to release*/); |
---|
517 | void wait(conditionN & cond); |
---|
518 | void signal(conditionN & cond); |
---|
519 | \end{lstlisting} |
---|
520 | |
---|
521 | Regardless of the option chosen for wait semantics, signal must be symmetrical. In all cases, signal only needs a single parameter, the condition variable that needs to be signalled. But \code{signal} needs to be called from the same monitor(s) that call to \code{wait}. Otherwise, mutual exclusion cannot be properly transferred back to the waiting monitor. |
---|
522 | |
---|
523 | Finally, an additionnal semantic which can be very usefull is the \code{signalBlock} routine. This routine behaves like signal for all of the semantics discussed above, but with the subtelty that mutual exclusion is transferred to the waiting task immediately rather than wating for the end of the critical section. |
---|
524 | \\ |
---|
525 | |
---|
526 | % ####### # # ####### ##### ##### # # ####### ###### |
---|
527 | % # # # # # # # # # # # # # |
---|
528 | % # # # # # # # # # # # |
---|
529 | % ##### # # ##### # ####### ##### # # |
---|
530 | % # # # # ### # # # # # # # |
---|
531 | % # # # # ### # # # # # # # # # |
---|
532 | % ####### # # # ### ##### ##### # # ####### ###### |
---|
533 | |
---|
534 | \subsection{External scheduling} \label{extsched} |
---|
535 | As one might expect, the alternative to Internal scheduling is to use External scheduling instead. This method is somewhat more robust to deadlocks since one of the threads keeps a relatively tight control on scheduling. Indeed, as the following examples will demonstrate, external scheduling allows users to wait for events from other threads without the concern of unrelated events occuring. External scheduling can generally be done either in terms of control flow (ex: \uC) or in terms of data (ex: Go). Of course, both of these paradigms have their own strenghts and weaknesses but for this project control flow semantics where chosen to stay consistent with the rest of the languages semantics. Two challenges specific to \CFA arise when trying to add external scheduling with loose object definitions and multi-monitor routines. The following example shows what a simple use \code{accept} versus \code{wait}/\code{signal} and its advantages. |
---|
536 | |
---|
537 | \begin{center} |
---|
538 | \begin{tabular}{|c|c|} |
---|
539 | Internal Scheduling & External Scheduling \\ |
---|
540 | \hline |
---|
541 | \begin{lstlisting} |
---|
542 | _Monitor blarg { |
---|
543 | condition c; |
---|
544 | public: |
---|
545 | void f() { signal(c)} |
---|
546 | void g() { wait(c); } |
---|
547 | private: |
---|
548 | } |
---|
549 | \end{lstlisting}&\begin{lstlisting} |
---|
550 | _Monitor blarg { |
---|
551 | |
---|
552 | public: |
---|
553 | void f(); |
---|
554 | void g() { _Accept(f); } |
---|
555 | private: |
---|
556 | } |
---|
557 | \end{lstlisting} |
---|
558 | \end{tabular} |
---|
559 | \end{center} |
---|
560 | |
---|
561 | In the case of internal scheduling, the call to \code{wait} only guarantees that \code{g} was the last routine to access the monitor. This intails that the routine \code{f} may have acquired mutual exclusion several times while routine \code{h} was waiting. On the other hand, external scheduling guarantees that while routine \code{h} was waiting, no routine other than \code{g} could acquire the monitor. |
---|
562 | \\ |
---|
563 | |
---|
564 | % # ####### ####### ##### ####### ####### ###### # ##### |
---|
565 | % # # # # # # # # # # # # # # # |
---|
566 | % # # # # # # # # # # # # # |
---|
567 | % # # # # # ##### ##### # # ###### # ##### |
---|
568 | % # # # # # # # # # # # # # # |
---|
569 | % # # # # # # # # # # # # # # # # |
---|
570 | % ####### ####### ####### ##### ####### ####### ###### ##### ##### |
---|
571 | |
---|
572 | \subsubsection{Loose object definitions} |
---|
573 | In \uC, monitor declarations include an exhaustive list of monitor operations. Since \CFA is not object oriented it becomes both more difficult to implement but also less clear for the user : |
---|
574 | |
---|
575 | \begin{lstlisting} |
---|
576 | mutex struct A {}; |
---|
577 | |
---|
578 | void f(A & mutex a); |
---|
579 | void g(A & mutex a) { accept(f); } |
---|
580 | \end{lstlisting} |
---|
581 | |
---|
582 | However, external scheduling is an example where implementation constraints become visible from the interface. Indeed, ince there is no hard limit to the number of threads trying to acquire a monitor concurrently, performance is a significant concern. Here is the pseudo code for the entering phase of a monitor : |
---|
583 | |
---|
584 | \begin{center} |
---|
585 | \begin{tabular}{l} |
---|
586 | \begin{lstlisting}[language=Pseudo] |
---|
587 | if monitor is free : |
---|
588 | enter |
---|
589 | elif monitor accepts me : |
---|
590 | enter |
---|
591 | else : |
---|
592 | block |
---|
593 | \end{lstlisting} |
---|
594 | \end{tabular} |
---|
595 | \end{center} |
---|
596 | |
---|
597 | For the \pseudo{monitor is free} condition it is easy to implement a check that can evaluate the condition in a few instruction. However, a fast check for \pseudo{monitor accepts me} is much harder to implement depending on the constraints put on the monitors. Indeed, monitors are often expressed as an entry queue and some acceptor queue as in the following figure : |
---|
598 | |
---|
599 | \begin{center} |
---|
600 | {\resizebox{0.4\textwidth}{!}{\input{monitor}}} |
---|
601 | \end{center} |
---|
602 | |
---|
603 | There are other alternatives to these pictures but in the case of this picture implementing a fast accept check is relatively easy. Indeed simply updating a bitmask when the acceptor queue changes is enough to have a check that executes in a single instruction, even with a fairly large number of acceptor. However, this relies on the fact that all the acceptable routines are declared with the monitor type. For OO languages this doesn't compromise much since monitors already have an exhaustive list of member routines. However, for \CFA this isn't the case, routines can be added to a type anywhere after its declaration. Its important to note that the bitmask approach does not actually require an exhaustive list of routines, but it requires a dense unique ordering of routines with an upper-bound and that ordering must be consistent across translation units. |
---|
604 | The alternative would be to have a picture more like this one: |
---|
605 | |
---|
606 | \begin{center} |
---|
607 | {\resizebox{0.4\textwidth}{!}{\input{ext_monitor}}} |
---|
608 | \end{center} |
---|
609 | |
---|
610 | Not storing the queues inside the monitor means that the storage can vary between routines, allowing for more flexibility and extensions. Storing an array of function-pointers would solve the issue of uniquely identifying acceptable routines. However, the single instruction bitmask compare has been replaced by dereferencing a pointer followed by a linear search. Furthermore, supporting nested external scheduling may now require additionnal searches on calls to accept to check if a routine is already queued in. |
---|
611 | |
---|
612 | At this point we must make a decision between flexibility and performance. Many design decisions in \CFA achieve both flexibility and performance, for example polymorphic routines add significant flexibility but inlining them means the optimizer can easily remove any runtime cost. Here however, the cost of flexibility cannot be trivially removed. |
---|
613 | |
---|
614 | In either cases here are a few alternatives for the different syntaxes this syntax : \\ |
---|
615 | \begin{center} |
---|
616 | {\renewcommand{\arraystretch}{1.5} |
---|
617 | \begin{tabular}[t]{l @{\hskip 0.35in} l} |
---|
618 | \hline |
---|
619 | \multicolumn{2}{ c }{\code{accept} on type}\\ |
---|
620 | \hline |
---|
621 | Alternative 1 & Alternative 2 \\ |
---|
622 | \begin{lstlisting} |
---|
623 | mutex struct A |
---|
624 | accept( void f(A & mutex a) ) |
---|
625 | {}; |
---|
626 | \end{lstlisting} &\begin{lstlisting} |
---|
627 | mutex struct A {} |
---|
628 | accept( void f(A & mutex a) ); |
---|
629 | |
---|
630 | \end{lstlisting} \\ |
---|
631 | Alternative 3 & Alternative 4 \\ |
---|
632 | \begin{lstlisting} |
---|
633 | mutex struct A { |
---|
634 | accept( void f(A & mutex a) ) |
---|
635 | }; |
---|
636 | |
---|
637 | \end{lstlisting} &\begin{lstlisting} |
---|
638 | mutex struct A { |
---|
639 | accept : |
---|
640 | void f(A & mutex a) ); |
---|
641 | }; |
---|
642 | \end{lstlisting}\\ |
---|
643 | \hline |
---|
644 | \multicolumn{2}{ c }{\code{accept} on routine}\\ |
---|
645 | \hline |
---|
646 | \begin{lstlisting} |
---|
647 | mutex struct A {}; |
---|
648 | |
---|
649 | void f(A & mutex a) |
---|
650 | |
---|
651 | accept( void f(A & mutex a) ) |
---|
652 | void g(A & mutex a) { |
---|
653 | /*...*/ |
---|
654 | } |
---|
655 | \end{lstlisting}&\\ |
---|
656 | \end{tabular} |
---|
657 | } |
---|
658 | \end{center} |
---|
659 | |
---|
660 | An other aspect to consider is what happens if multiple overloads of the same routine are used. For the time being it is assumed that multiple overloads of the same routine should be scheduled regardless of the overload used. However, this could easily be extended in the future. |
---|
661 | |
---|
662 | % # # # # # ####### ### # # ####### # # |
---|
663 | % ## ## # # # # # ## ## # # ## # |
---|
664 | % # # # # # # # # # # # # # # # # # # |
---|
665 | % # # # # # # # # # # # # # # # # |
---|
666 | % # # # # # # # # # # # # # # |
---|
667 | % # # # # # # # # # # # # ## |
---|
668 | % # # ##### ####### # ### # # ####### # # |
---|
669 | |
---|
670 | \subsubsection{Multi-monitor scheduling} |
---|
671 | |
---|
672 | External scheduling, like internal scheduling, becomes orders of magnitude more complex when we start introducing multi-monitor syntax. Even in the simplest possible case some new semantics need to be established : |
---|
673 | \begin{lstlisting} |
---|
674 | accept( void f(mutex struct A & mutex this)) |
---|
675 | mutex struct A {}; |
---|
676 | |
---|
677 | mutex struct B {}; |
---|
678 | |
---|
679 | void g(A & mutex a, B & mutex b) { |
---|
680 | accept(f); //ambiguous, which monitor |
---|
681 | } |
---|
682 | \end{lstlisting} |
---|
683 | |
---|
684 | The obvious solution is to specify the correct monitor as follows : |
---|
685 | |
---|
686 | \begin{lstlisting} |
---|
687 | accept( void f(mutex struct A & mutex this)) |
---|
688 | mutex struct A {}; |
---|
689 | |
---|
690 | mutex struct B {}; |
---|
691 | |
---|
692 | void g(A & mutex a, B & mutex b) { |
---|
693 | accept( f, b ); |
---|
694 | } |
---|
695 | \end{lstlisting} |
---|
696 | |
---|
697 | This is unambiguous. Both locks will be acquired and kept, when routine \code{f} is called the lock for monitor \code{a} will be temporarily transferred from \code{g} to \code{f} (while \code{g} still holds lock \code{b}). This behavior can be extended to multi-monitor accept statment as follows. |
---|
698 | |
---|
699 | \begin{lstlisting} |
---|
700 | accept( void f(mutex struct A & mutex, mutex struct A & mutex)) |
---|
701 | mutex struct A {}; |
---|
702 | |
---|
703 | mutex struct B {}; |
---|
704 | |
---|
705 | void g(A & mutex a, B & mutex b) { |
---|
706 | accept( f, b, a ); |
---|
707 | } |
---|
708 | \end{lstlisting} |
---|
709 | |
---|
710 | Note that the set of monitors passed to the \code{accept} statement must be entirely contained in the set of monitor already acquired in the routine. \code{accept} used in any other context is Undefined Behaviour. |
---|
711 | |
---|
712 | % ###### ####### ####### # ### # ##### |
---|
713 | % # # # # # # # # # # |
---|
714 | % # # # # # # # # # |
---|
715 | % # # ##### # # # # # ##### |
---|
716 | % # # # # ####### # # # |
---|
717 | % # # # # # # # # # # |
---|
718 | % ###### ####### # # # ### ####### ##### |
---|
719 | % |
---|
720 | % ##### # # ####### # # ####### ##### |
---|
721 | % # # # # # # # # # # |
---|
722 | % # # # # # # # # # |
---|
723 | % ##### # # # # ##### # # ##### ##### |
---|
724 | % # # # # # # # # # # |
---|
725 | % # # # # # # # # # # |
---|
726 | % #### # ##### ####### ##### ####### ##### |
---|
727 | |
---|
728 | |
---|
729 | \subsubsection{Implementation Details: External scheduling queues} |
---|
730 | To support multi-monitor external scheduling means that some kind of entry-queues must be used that is aware of both monitors. However, acceptable routines must be aware of the entry queues which means they must be stored inside at least one of the monitors that will be acquired. This in turn adds the requirement a systematic algorithm of disambiguating which queue is relavant regardless of user ordering. The proposed algorithm is to fall back on monitors lock ordering and specify that the monitor that is acquired first is the lock with the relevant entry queue. This assumes that the lock acquiring order is static for the lifetime of all concerned objects but that is a reasonnable constraint. This algorithm choice has two consequences, the entry queue of the highest priority monitor is no longer a true FIFO queue and the queue of the lowest priority monitor is both required and probably unused. The queue can no longer be a FIFO queue because instead of simply containing the waiting threads in order arrival, they also contain the second mutex. Therefore, another thread with the same highest priority monitor but a different lowest priority monitor may arrive first but enter the critical section after a thread with the correct pairing. Secondly, since it may not be known at compile time which monitor will be the lowest priority monitor, every monitor needs to have the correct queues even though it is probable that half the multi-monitor queues will go unused for the entire duration of the program. |
---|
731 | |
---|
732 | \subsection{Other concurrency tools} |
---|
733 | TO BE CONTINUED... |
---|
734 | |
---|
735 | \newpage |
---|
736 | % ###### # ###### # # # ####### # ### ##### # # |
---|
737 | % # # # # # # # # # # # # # # # ## ## |
---|
738 | % # # # # # # # # # # # # # # # # # # |
---|
739 | % ###### # # ###### # # # # ##### # # ##### # # # |
---|
740 | % # ####### # # ####### # # # # # # # # |
---|
741 | % # # # # # # # # # # # # # # # # |
---|
742 | % # # # # # # # ####### ####### ####### ####### ### ##### # # |
---|
743 | \section{Parallelism} |
---|
744 | Historically, computer performance was about processor speeds and instructions count. However, with heat dissipation being an ever growing challenge, parallelism has become the new source of greatest performance~\cite{Sutter05, Sutter05b}. In this decade, it is not longer reasonnable to create high-performance application without caring about parallelism. Indeed, parallelism is an important aspect of performance and more specifically throughput and hardware utilization. The lowest level approach of parallelism is to use \glspl{kthread}. However since these have significant costs and limitations \glspl{kthread} are now mostly used as an implementation tool rather than a user oriented one. There are several alternatives to solve these issues which all have strengths and weaknesses. |
---|
745 | |
---|
746 | \subsection{User-level threads} |
---|
747 | A direct improvement on the \gls{kthread} approach is to use \glspl{uthread}. These threads offer most of the same features that the operating system already provide but can be used on a much larger scale. This is the most powerfull solution as it allows all the features of multi-threading while removing several of the more expensives costs of using kernel threads. The down side is that almost none of the low-level threading complexities are hidden, users still have to think about data races, deadlocks and synchronization issues. This can be somewhat alleviated by a concurrency toolkit with strong garantees but the parallelism toolkit offers very little to reduce complexity in itself. |
---|
748 | |
---|
749 | Examples of languages that support are Java~\cite{Java}, Haskell~\cite{Haskell} and \uC~\cite{uC++book}. |
---|
750 | |
---|
751 | \subsection{Jobs and thread pools} |
---|
752 | The approach on the opposite end of the spectrum is to base parallelism on \glspl{job}. Indeed, \glspl{job} offer limited flexibility but at the benefit of a simpler user interface. In \gls{job} based systems users express parallelism as units of work and the dependency graph (either explicit or implicit) that tie them together. This means users need not to worry about concurrency but significantly limits the interaction that can occur between different jobs. Indeed, any \gls{job} that blocks also blocks the underlying \gls{kthread}, this effectively mean the CPU utilization, and therefore throughput, will suffer noticeably. |
---|
753 | The golden standard of this implementation is Intel's TBB library~\cite{TBB}. |
---|
754 | |
---|
755 | \subsection{Fibers : user-level threads without preemption} |
---|
756 | Finally, in the middle of the flexibility versus complexity spectrum lay \glspl{fiber} which offer \glspl{uthread} without the complexity of preemption. This means users don't have to worry about other \glspl{fiber} suddenly executing between two instructions which signficantly reduces complexity. However, any call to IO or other concurrency primitives can lead to context switches. Furthermore, users can also block \glspl{fiber} in the middle of their execution without blocking a full processor core. This means users still have to worry about mutual exclusion, deadlocks and race conditions in their code, raising the complexity significantly. |
---|
757 | An example of a language that uses fibers is Go~\cite{Go} |
---|
758 | |
---|
759 | \subsection{Paradigm performance} |
---|
760 | While the choice between the three paradigms listed above may have significant performance implication, it is difficult to pin the performance implications of chosing a model at the language level. Indeed, in many situations one of these paradigms will show better performance but it all strongly depends on the usage. Having mostly indepent units of work to execute almost guarantess that the \gls{job} based system will have the best performance. However, add interactions between jobs and the processor utilisation might suffer. User-level threads may allow maximum ressource utilisation but context switches will be more expansive and it is also harder for users to get perfect tunning. As with every example, fibers sit somewhat in the middle of the spectrum. Furthermore, if the units of uninterrupted work are large enough the paradigm choice will be largely amorticised by the actual work done. |
---|
761 | |
---|
762 | % ##### ####### # ####### ###### ###### |
---|
763 | % # # # # # # # # # # |
---|
764 | % # # # # # # # # # |
---|
765 | % # ##### # # ##### # ###### ###### |
---|
766 | % # # ####### # # # # # |
---|
767 | % # # # # # # # # # # |
---|
768 | % ##### # # # # ###### ###### |
---|
769 | |
---|
770 | \section{\CFA 's Thread Building Blocks} |
---|
771 | As a system level language, \CFA should offer both performance and flexibilty as its primary goals, simplicity and user-friendliness being a secondary concern. Therefore, the core of parallelism in \CFA should prioritize power and efficiency. With this said, it is possible to deconstruct the three paradigms details aboved in order to get simple building blocks. Here is a table showing the core caracteristics of the mentionned paradigms : |
---|
772 | \begin{center} |
---|
773 | \begin{tabular}[t]{| r | c | c |} |
---|
774 | \cline{2-3} |
---|
775 | \multicolumn{1}{ c| }{} & Has a stack & Preemptive \\ |
---|
776 | \hline |
---|
777 | \Glspl{job} & X & X \\ |
---|
778 | \hline |
---|
779 | \Glspl{fiber} & \checkmark & X \\ |
---|
780 | \hline |
---|
781 | \Glspl{uthread} & \checkmark & \checkmark \\ |
---|
782 | \hline |
---|
783 | \end{tabular} |
---|
784 | \end{center} |
---|
785 | |
---|
786 | As shown in section \ref{cfaparadigms} these different blocks being available in \CFA it is trivial to reproduce any of these paradigm. |
---|
787 | |
---|
788 | % ####### # # ###### ####### # ###### ##### |
---|
789 | % # # # # # # # # # # # # |
---|
790 | % # # # # # # # # # # # |
---|
791 | % # ####### ###### ##### # # # # ##### |
---|
792 | % # # # # # # ####### # # # |
---|
793 | % # # # # # # # # # # # # |
---|
794 | % # # # # # ####### # # ###### ##### |
---|
795 | |
---|
796 | \subsection{Thread Interface} |
---|
797 | The basic building blocks of \CFA are \glspl{cfathread}. By default these are implemented as \glspl{uthread} and as such offer a flexible and lightweight threading interface (lightweight comparatievely to \glspl{kthread}). A thread can be declared using a struct declaration prefix with the \code{thread} as follows : |
---|
798 | |
---|
799 | \begin{lstlisting} |
---|
800 | thread struct foo {}; |
---|
801 | \end{lstlisting} |
---|
802 | |
---|
803 | Obviously, for this thread implementation to be usefull it must run some user code. Several other threading interfaces use some function pointer representation as the interface of threads (for example : \Csharp~\cite{Csharp} and Scala~\cite{Scala}). However, we consider that statically tying a \code{main} routine to a thread superseeds this approach. Since the \code{main} routine is definetely a special routine in \CFA, we can reuse the existing syntax for declaring routines with unordinary name, i.e. operator overloading. As such the \code{main} routine of a thread can be defined as such : |
---|
804 | \begin{lstlisting} |
---|
805 | thread struct foo {}; |
---|
806 | |
---|
807 | void ?main(thread foo* this) { |
---|
808 | /*... Some useful code ...*/ |
---|
809 | } |
---|
810 | \end{lstlisting} |
---|
811 | |
---|
812 | With these semantics it is trivial to write a thread type that takes a function pointer as parameter and executes it on its stack asynchronously : |
---|
813 | \begin{lstlisting} |
---|
814 | typedef void (*voidFunc)(void); |
---|
815 | |
---|
816 | thread struct FuncRunner { |
---|
817 | voidFunc func; |
---|
818 | }; |
---|
819 | |
---|
820 | //ctor |
---|
821 | void ?{}(thread FuncRunner* this, voidFunc inFunc) { |
---|
822 | func = inFunc; |
---|
823 | } |
---|
824 | |
---|
825 | //main |
---|
826 | void ?main(thread FuncRunner* this) { |
---|
827 | this->func(); |
---|
828 | } |
---|
829 | \end{lstlisting} |
---|
830 | |
---|
831 | % In this example \code{func} is a function pointer stored in \acrfull{tls}, which is \CFA is both easy to use and completly typesafe. |
---|
832 | |
---|
833 | Of course for threads to be useful, it must be possible to start and stop threads and wait for them to complete execution. While using an \acrshort{api} such as \code{fork} and \code{join} is relatively common in the literature, such an interface is not needed. Indeed, the simplest approach is to use \acrshort{raii} principles and have threads \code{fork} once the constructor has completed and \code{join} before the destructor runs. |
---|
834 | \begin{lstlisting} |
---|
835 | thread struct FuncRunner; //FuncRunner declared above |
---|
836 | |
---|
837 | void world() { |
---|
838 | sout | "World!" | endl; |
---|
839 | } |
---|
840 | |
---|
841 | void main() { |
---|
842 | FuncRunner run = {world}; |
---|
843 | //Thread run forks here |
---|
844 | |
---|
845 | //Print to "Hello " and "World!" will be run concurrently |
---|
846 | sout | "Hello " | endl; |
---|
847 | |
---|
848 | //Implicit join at end of scope |
---|
849 | } |
---|
850 | \end{lstlisting} |
---|
851 | This semantic has several advantages over explicit semantics : typesafety is guaranteed, any thread will always be started and stopped exaclty once and users can't make any progamming errors. Furthermore it naturally follows the memory allocation semantics which means users don't need to learn multiple semantics. |
---|
852 | |
---|
853 | These semantics also naturally scale to multiple threads meaning basic synchronisation is very simple : |
---|
854 | \begin{lstlisting} |
---|
855 | thread struct MyThread { |
---|
856 | //... |
---|
857 | }; |
---|
858 | |
---|
859 | //ctor |
---|
860 | void ?{}(thread MyThread* this) {} |
---|
861 | |
---|
862 | //main |
---|
863 | void ?main(thread MyThread* this) { |
---|
864 | //... |
---|
865 | } |
---|
866 | |
---|
867 | void foo() { |
---|
868 | MyThread thrds[10]; |
---|
869 | //Start 10 threads at the beginning of the scope |
---|
870 | |
---|
871 | DoStuff(); |
---|
872 | |
---|
873 | //Wait for the 10 threads to finish |
---|
874 | } |
---|
875 | \end{lstlisting} |
---|
876 | |
---|
877 | \newpage |
---|
878 | \large{\textbf{WORK IN PROGRESS}} |
---|
879 | \subsection{The \CFA Kernel : Processors, Clusters and Threads}\label{kernel} |
---|
880 | |
---|
881 | |
---|
882 | \subsection{Paradigms}\label{cfaparadigms} |
---|
883 | Given these building blocks we can then reproduce the all three of the popular paradigms. Indeed, we get \glspl{uthread} as the default paradigm in \CFA. However, disabling \glspl{preemption} on the \gls{cfacluster} means \glspl{cfathread} effectively become \glspl{fiber}. Since several \glspl{cfacluster} with different scheduling policy can coexist in the same application, this allows \glspl{fiber} and \glspl{uthread} to coexist in the runtime of an application. |
---|
884 | |
---|
885 | % \subsection{High-level options}\label{tasks} |
---|
886 | % |
---|
887 | % \subsubsection{Thread interface} |
---|
888 | % constructors destructors |
---|
889 | % initializer lists |
---|
890 | % monitors |
---|
891 | % |
---|
892 | % \subsubsection{Futures} |
---|
893 | % |
---|
894 | % \subsubsection{Implicit threading} |
---|
895 | % Finally, simpler applications can benefit greatly from having implicit parallelism. That is, parallelism that does not rely on the user to write concurrency. This type of parallelism can be achieved both at the language level and at the system level. |
---|
896 | % |
---|
897 | % \begin{center} |
---|
898 | % \begin{tabular}[t]{|c|c|c|} |
---|
899 | % Sequential & System Parallel & Language Parallel \\ |
---|
900 | % \begin{lstlisting} |
---|
901 | % void big_sum(int* a, int* b, |
---|
902 | % int* out, |
---|
903 | % size_t length) |
---|
904 | % { |
---|
905 | % for(int i = 0; i < length; ++i ) { |
---|
906 | % out[i] = a[i] + b[i]; |
---|
907 | % } |
---|
908 | % } |
---|
909 | % |
---|
910 | % |
---|
911 | % |
---|
912 | % |
---|
913 | % |
---|
914 | % int* a[10000]; |
---|
915 | % int* b[10000]; |
---|
916 | % int* c[10000]; |
---|
917 | % //... fill in a and b ... |
---|
918 | % big_sum(a, b, c, 10000); |
---|
919 | % \end{lstlisting} &\begin{lstlisting} |
---|
920 | % void big_sum(int* a, int* b, |
---|
921 | % int* out, |
---|
922 | % size_t length) |
---|
923 | % { |
---|
924 | % range ar(a, a + length); |
---|
925 | % range br(b, b + length); |
---|
926 | % range or(out, out + length); |
---|
927 | % parfor( ai, bi, oi, |
---|
928 | % [](int* ai, int* bi, int* oi) { |
---|
929 | % oi = ai + bi; |
---|
930 | % }); |
---|
931 | % } |
---|
932 | % |
---|
933 | % int* a[10000]; |
---|
934 | % int* b[10000]; |
---|
935 | % int* c[10000]; |
---|
936 | % //... fill in a and b ... |
---|
937 | % big_sum(a, b, c, 10000); |
---|
938 | % \end{lstlisting}&\begin{lstlisting} |
---|
939 | % void big_sum(int* a, int* b, |
---|
940 | % int* out, |
---|
941 | % size_t length) |
---|
942 | % { |
---|
943 | % for (ai, bi, oi) in (a, b, out) { |
---|
944 | % oi = ai + bi; |
---|
945 | % } |
---|
946 | % } |
---|
947 | % |
---|
948 | % |
---|
949 | % |
---|
950 | % |
---|
951 | % |
---|
952 | % int* a[10000]; |
---|
953 | % int* b[10000]; |
---|
954 | % int* c[10000]; |
---|
955 | % //... fill in a and b ... |
---|
956 | % big_sum(a, b, c, 10000); |
---|
957 | % \end{lstlisting} |
---|
958 | % \end{tabular} |
---|
959 | % \end{center} |
---|
960 | % |
---|
961 | % \subsection{Machine setup}\label{machine} |
---|
962 | % Threads are all good and well but wee still some OS support to fully utilize available hardware. |
---|
963 | % |
---|
964 | % \textbf{\large{Work in progress...}} Do wee need something beyond specifying the number of kernel threads? |
---|
965 | |
---|
966 | % # # # |
---|
967 | % # # # # |
---|
968 | % # # # # |
---|
969 | % # # # # |
---|
970 | % ####### # # |
---|
971 | % # # # # |
---|
972 | % # # ####### ####### |
---|
973 | \section{Putting it all together} |
---|
974 | |
---|
975 | |
---|
976 | |
---|
977 | |
---|
978 | |
---|
979 | |
---|
980 | |
---|
981 | |
---|
982 | |
---|
983 | |
---|
984 | % ####### # # ####### # # ###### ####### |
---|
985 | % # # # # # # # # # |
---|
986 | % # # # # # # # # # |
---|
987 | % ##### # # # # # ###### ##### |
---|
988 | % # # # # # # # # # |
---|
989 | % # # # # # # # # # |
---|
990 | % # ##### # ##### # # ###### |
---|
991 | \section{Future work} |
---|
992 | Concurrency and parallelism is still a very active field that strongly benefits from hardware advances. As such certain features that aren't necessarily mature enough in their current state could become relevant in the lifetime of \CFA. |
---|
993 | \subsection{Transactions} |
---|
994 | |
---|
995 | % ####### # # ###### |
---|
996 | % # ## # # # |
---|
997 | % # # # # # # |
---|
998 | % ##### # # # # # |
---|
999 | % # # # # # # |
---|
1000 | % # # ## # # |
---|
1001 | % ####### # # ###### |
---|
1002 | \section*{Acknowledgements} |
---|
1003 | |
---|
1004 | \clearpage |
---|
1005 | \printglossary[type=\acronymtype] |
---|
1006 | \printglossary |
---|
1007 | |
---|
1008 | \clearpage |
---|
1009 | \bibliographystyle{plain} |
---|
1010 | \bibliography{cw92,distSharedMem,lfp92,mlw92,parallel,parallelIO,partheory,pl,pldi92,ps,realtime,techreportsPAB,visual,local} |
---|
1011 | |
---|
1012 | |
---|
1013 | \end{document} |
---|