Changes in / [8514fe19:c850687]
- Location:
- doc/proposals/concurrency
- Files:
-
- 1 deleted
- 4 edited
-
Makefile (modified) (1 diff)
-
cfa-format.tex (deleted)
-
concurrency.tex (modified) (32 diffs)
-
style.tex (modified) (1 diff)
-
version (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/concurrency/Makefile
r8514fe19 rc850687 10 10 concurrency \ 11 11 style \ 12 cfa-format \13 12 glossary \ 14 13 } -
doc/proposals/concurrency/concurrency.tex
r8514fe19 rc850687 9 9 % math escape $...$ (dollar symbol) 10 10 11 \documentclass[ letterpaper,12pt,titlepage,oneside,final]{book}11 \documentclass[twoside,11pt]{article} 12 12 13 13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% … … 24 24 \usepackage{graphicx} 25 25 \usepackage{tabularx} 26 \usepackage{multicol}27 26 \usepackage[acronym]{glossaries} 28 \usepackage{varioref} 27 \usepackage{varioref} % extended references 28 \usepackage{inconsolata} 29 29 \usepackage{listings} % format program code 30 30 \usepackage[flushmargin]{footmisc} % support label/reference in footnote … … 62 62 \newcommand{\cit}{\textsuperscript{[Citation Needed]}\xspace} 63 63 \newcommand{\code}[1]{\lstinline[language=CFA]{#1}} 64 \newcommand{\pscode}[1]{\lstinline[language=pseudo]{#1}} 65 \newcommand{\TODO}{{\Textbf{TODO}}} 64 \newcommand{\pseudo}[1]{\lstinline[language=Pseudo]{#1}} 66 65 67 66 \input{glossary} … … 100 99 % ### # # # # # ####### 101 100 102 \ chapter{Introduction}101 \section{Introduction} 103 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, message passing and implicit threading. 104 103 … … 113 112 % ##### ####### # # ##### ##### # # # # ####### # # ##### # 114 113 115 \ chapter{Concurrency}114 \section{Concurrency} 116 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 (channels\cit for example). 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 still has to take both paradigms into account. Approaches based on shared memory are more closely related to non-concurrent paradigms since they often rely on basic 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 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 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. 117 116 … … 124 123 % # # ####### # # ### # ####### # # ##### 125 124 126 \s ection{Monitors}125 \subsection{Monitors} 127 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 : 128 \begin{ cfacode}127 \begin{lstlisting} 129 128 typedef /*some monitor type*/ monitor; 130 129 int f(monitor & m); … … 134 133 f(m); 135 134 } 136 \end{ cfacode}135 \end{lstlisting} 137 136 138 137 % ##### # # # … … 144 143 % ##### # # ####### ####### 145 144 146 \subs ection{Call semantics} \label{call}145 \subsubsection{Call semantics} \label{call} 147 146 The above monitor example displays some of the 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. 148 147 149 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 counter : 150 149 151 \begin{ cfacode}152 m onitor counter_t { /*...see section $\ref{data}$...*/ };150 \begin{lstlisting} 151 mutex struct counter_t { /*...see section §\ref{data}§...*/ }; 153 152 154 153 void ?{}(counter_t & nomutex this); //constructor … … 157 156 //need for mutex is platform dependent here 158 157 void ?{}(size_t * this, counter_t & mutex cnt); //conversion 159 \end{ cfacode}158 \end{lstlisting} 160 159 161 160 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 an object not yet constructed should never be shared and therefore does 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. 162 161 163 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. For example, given a routine without quualifiers \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 \code{nomutex} is the more "normal" behaviour, the \code{nomutex} keyword 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. However, since \CFA relies heavily on traits as an abstraction mechanism, the distinction between a type that is a monitor and a type that looks like a monitor can become blurred. For this reason, \CFA only has the \code{mutex} keyword. 164 165 166 The next semantic decision is to establish when \code{mutex} may be used as a type qualifier. Consider the following declarations: 167 \begin{cfacode} 162 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. For example, given a routine without quualifiers \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 \code{nomutex} is the more "normal" behaviour, the \code{nomutex} keyword 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. 163 164 The next semantic decision is to establish when mutex/nomutex may be used as a type qualifier. Consider the following declarations: 165 \begin{lstlisting} 168 166 int f1(monitor & mutex m); 169 167 int f2(const monitor & mutex m); … … 171 169 int f4(monitor *[] mutex m); 172 170 int f5(graph(monitor*) & mutex m); 173 \end{cfacode} 174 The problem is to indentify which object(s) should be acquired. Furthermore, each object needs to be acquired only once. In the case of simple routines like \code{f1} and \code{f2} it is easy to identify an exhaustive list of objects to acquire on entry. 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 with one level of indirection (ignoring potential qualifiers). Also note that while routine \code{f3} can be supported, meaning that monitor \code{**m} is be acquired, passing an array to this routine would be type safe and yet result in undefined behavior because only the first element of the array is acquired. However, this ambiguity is part of the C type system with respects to arrays. For this reason, \code{mutex} is disallowed in the context where arrays may be passed. 175 176 Finally, for convenience, monitors support multiple acquireing, that is acquireing a monitor while already holding it does not cause a deadlock. It simply increments an internal counter which is then used to release the monitor after the number of acquires and releases match up. 171 \end{lstlisting} 172 The problem is to indentify which object(s) should be acquired. Furthermore, each object needs to be acquired only once. In the case of simple routines like \code{f1} and \code{f2} it is easy to identify an exhaustive list of objects to acquire on entry. 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} is be acquired, passing an array to this routine would be type safe and yet result in undefined behavior because only the first element of the array is acquired. However, this ambiguity is part of the C type system with respects to arrays. For this reason, it would also be reasonnable to disallow mutex in the context where arrays may be passed. 177 173 178 174 % ###### # ####### # … … 184 180 % ###### # # # # # 185 181 186 \subs ection{Data semantics} \label{data}182 \subsubsection{Data semantics} \label{data} 187 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 appropriate protection. For example, here is a complete version of the counter showed in section \ref{call}: 188 \begin{ cfacode}189 m onitorcounter_t {184 \begin{lstlisting} 185 mutex struct counter_t { 190 186 int value; 191 187 }; 192 188 193 void ?{}(counter_t & this) {189 void ?{}(counter_t & nomutex this) { 194 190 this.cnt = 0; 195 191 } … … 203 199 *this = (int)cnt; 204 200 } 205 \end{ cfacode}201 \end{lstlisting} 206 202 207 203 This simple counter is used as follows: 208 204 \begin{center} 209 205 \begin{tabular}{c @{\hskip 0.35in} c @{\hskip 0.35in} c} 210 \begin{ cfacode}206 \begin{lstlisting} 211 207 //shared counter 212 208 counter_t cnt; … … 218 214 ... 219 215 thread N : cnt++; 220 \end{ cfacode}216 \end{lstlisting} 221 217 \end{tabular} 222 218 \end{center} 223 219 224 220 Notice how the counter is used without any explicit synchronisation and yet supports thread-safe semantics for both reading and writting. Unlike object-oriented monitors, where calling a mutex member \emph{implicitly} acquires mutual-exclusion, \CFA uses an explicit mechanism to acquire mutual-exclusion. A consequence of this approach is that it extends to multi-monitor calls. 225 \begin{ cfacode}221 \begin{lstlisting} 226 222 int f(MonitorA & mutex a, MonitorB & mutex b); 227 223 … … 229 225 MonitorB b; 230 226 f(a,b); 231 \end{ cfacode}227 \end{lstlisting} 232 228 This code acquires both locks before entering the critical section, called \emph{\gls{group-acquire}}. In practice, writing multi-locking routines that do not lead to deadlocks is 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 is consistent across calls to routines using the same monitors as arguments. However, since \CFA monitors use multi-acquisition locks, users can effectively force the acquiring order. For example, notice which routines use \code{mutex}/\code{nomutex} and how this affects aquiring order : 233 \begin{ cfacode}229 \begin{lstlisting} 234 230 void foo(A & mutex a, B & mutex b) { //acquire a & b 235 231 //... 236 232 } 237 233 238 void bar(A & mutex a, B & /*nomutex*/b) { //acquire a234 void bar(A & mutex a, B & nomutex b) { //acquire a 239 235 //... 240 236 foo(a, b); //acquire b … … 242 238 } 243 239 244 void baz(A & /*nomutex*/a, B & mutex b) { //acquire b240 void baz(A & nomutex a, B & mutex b) { //acquire b 245 241 //... 246 242 foo(a, b); //acquire a 247 243 //... 248 244 } 249 \end{ cfacode}250 251 The multi-acquisition monitor lock allows a monitor lock to be acquired by both \code{bar} or \code{baz} and acquired again in \code{foo}. In the calls to \code{bar} and \code{baz} the monitors are acquired in opposite order. such use leads to nested monitor call problems~\cite{Lister77}, which is a more specific variation 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 deadlock and is therefore undefined behavior. As shown on several occasion\cit, solving this problem requires :245 \end{lstlisting} 246 247 The multi-acquisition monitor lock allows a monitor lock to be acquired by both \code{bar} or \code{baz} and acquired again in \code{foo}. In the calls to \code{bar} and \code{baz} the monitors are acquired in opposite order. such use leads to nested monitor call problems~\cite{Lister77}, which is 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 deadlock and is therefore undefined behavior. As shown on several occasion\cit, solving this problem requires : 252 248 \begin{enumerate} 253 249 \item Dynamically tracking of the monitor-call order. … … 273 269 % # ####### ####### # # # ####### # # # # 274 270 275 \subs ection{Implementation Details: Interaction with polymorphism}271 \subsubsection{Implementation Details: Interaction with polymorphism} 276 272 At first glance, interaction between monitors and \CFA's concept of polymorphism seems complex to support. However, it is shown that entry-point locking can solve most of the issues. 277 273 … … 283 279 \CFA & pseudo-code & pseudo-code \\ 284 280 \hline 285 \begin{ cfacode}[tabsize=3]286 void foo(monitor & mutex a){281 \begin{lstlisting} 282 void foo(monitor & mutex a) { 287 283 288 284 … … 301 297 302 298 } 303 \end{ cfacode} & \begin{pseudo}[tabsize=3]299 \end{lstlisting} &\begin{lstlisting} 304 300 foo(& a) { 305 301 … … 319 315 release(a); 320 316 } 321 \end{ pseudo} & \begin{pseudo}[tabsize=3]317 \end{lstlisting} &\begin{lstlisting} 322 318 foo(& a) { 323 319 //called routine … … 337 333 338 334 } 339 \end{ pseudo}335 \end{lstlisting} 340 336 \end{tabular} 341 337 \end{center} 342 338 343 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} 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} calling a monitor routine becomes exactly the same as calling it from anywhere else. Note that the \code{mutex} keyword relies on the resolver, which mean that in cases where generic monitor routines is actually desired, writing mutex routine is possible with the proper trait. 339 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} 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} calling a monitor routine becomes exactly the same as calling it from anywhere else. 340 344 341 345 342 … … 352 349 % ### # # # ### ##### ##### # # ####### ###### 353 350 354 \section{Internal scheduling} \label{insched} 355 In addition to mutual exclusion, the monitors at the core of \CFA's concurrency can also be used to achieve synchronisation. With monitors, this is generally achieved with internal or external scheduling as in\cit. Since internal scheduling of single monitors is mostly a solved problem, this proposal concentraits on extending internal scheduling to multiple monitors at once. Indeed, like the \gls{group-acquire} semantics, internal scheduling extends to multiple monitors at once in a way that is natural to the user but requires additional complexity on the implementation side. 356 357 First, Here is a simple example of such a technique : 358 359 \begin{cfacode} 360 monitor A { 361 condition e; 362 } 363 364 void foo(A & mutex a) { 365 // ... 366 // We need someone else to do something now 367 wait(a.e); 368 // ... 369 } 370 371 void bar(A & mutex a) { 372 // Do the thing foo is waiting on 373 // ... 374 // Signal foo it's done 375 signal(a.e); 376 } 377 \end{cfacode} 378 379 Note that in \CFA, \code{condition} have no particular need to be stored inside a monitor, beyond any software engineering reasons. Here routine \code{foo} waits for the \code{signal} from \code{bar} before making further progress, effectively ensuring a basic ordering. An important aspect to take into account here is that \CFA does not allow barging, which means that once function \code{bar} releases the monitor, foo is guaranteed to resume immediately after (unless some other function waited on the same condition). This guarantees offers the benefit of not having to loop arount waits in order to guarantee that a condition is still met. The main reason \CFA offers this guarantee is that users can easily introduce barging if it becomes a necessity but adding a barging prevention or barging avoidance is more involved without language support. 380 381 Supporting barging prevention as well as extending internal scheduling to multiple monitors is the main source of complexity in the design of \CFA concurrency. 382 383 \subsection{Internal Scheduling - multi monitor} 384 It easier to understand the problem of multi monitor scheduling using a series of pseudo code though experiment. Note that in the following snippets of pseudo-code waiting and signalling is done without the use of a condition variable. While \CFA requires condition variables to use signalling, the variable itself only really holds the data needed for the implementation of internal schedulling. Some languages like JAVA\cit simply define an implicit condition variable for every monitor while other languages like \uC use explicit condition variables. Since the following pseudo-codes are simple and focused experiments, all condition variables are implicit. 385 386 \begin{multicols}{2} 387 \begin{pseudo} 351 \subsection{Internal scheduling} \label{insched} 352 353 \begin{center} 354 \begin{tabular}{ c @{\hskip 0.65in} c } 355 \begin{lstlisting}[language=Pseudo] 388 356 acquire A 389 357 wait A 390 358 release A 391 \end{pseudo} 392 393 \columnbreak 394 395 \begin{pseudo} 359 \end{lstlisting}&\begin{lstlisting}[language=Pseudo] 396 360 acquire A 397 361 signal A 398 362 release A 399 \end{pseudo} 400 \end{multicols} 401 402 The previous example shows the simple case of having two threads (one for each column) and a single monitor A. One thread acquires before waiting and the other acquires before signalling. There are a few important things to note here. First, both \code{wait} and \code{signal} must be called with the proper monitor(s) already acquired. This can be hidden on the user side but is a logical requirement for barging prevention. Secondly, as stated above, while it is argued that not all problems regarding single monitors are solved, this paper only regards challenges of \gls{group-acquire} and considers other problems related to monitors as solved. 403 404 An important note about this example is that signalling a monitor is a delayed operation. The ownership of the monitor is transferred only when the monitor would have otherwise been released, not at the point of the \code{signal} statement. 405 406 A direct extension of the previous example is the \gls{group-acquire} version : 407 408 \begin{multicols}{2} 409 \begin{pseudo} 410 acquire A & B 411 wait A & B 412 release A & B 413 \end{pseudo} 414 415 \columnbreak 416 417 \begin{pseudo} 418 acquire A & B 419 signal A & B 420 release A & B 421 \end{pseudo} 422 \end{multicols} 423 424 This version uses \gls{group-acquire} (denoted using the \& symbol), but the presence of multiple monitors does not add a particularly new meaning. Synchronization will happen between the two threads in exactly the same way and order. The only difference is that mutual exclusion will cover more monitors. On the implementation side, handling multiple monitors at once does add a degree of complexity but it is not significant compared to the next few examples. 425 426 For the sake of completeness, here is another example of the single-monitor case, this time with nesting. 427 428 \begin{multicols}{2} 429 \begin{pseudo} 363 \end{lstlisting} 364 \end{tabular} 365 \end{center} 366 367 Easy : like uC++ 368 369 \begin{center} 370 \begin{tabular}{ c @{\hskip 0.65in} c } 371 \begin{lstlisting}[language=Pseudo] 430 372 acquire A 431 373 acquire B … … 433 375 release B 434 376 release A 435 \end{pseudo} 436 437 \columnbreak 438 439 \begin{pseudo} 440 441 acquire B 442 signal B 443 release B 444 445 \end{pseudo} 446 \end{multicols} 447 448 While these cases can cause some deadlock issues, we consider that these issues are only a symptom of the fact that locks, and by extension monitors, are not perfectly composable. However, for monitors as for locks, it is possible to write program that using nesting without encountering any problems if they are nested carefully. 449 450 The next example is where \gls{group-acquire} adds a significant layer of complexity to the internal signalling semantics. 451 452 \begin{multicols}{2} 453 \begin{pseudo} 377 \end{lstlisting}&\begin{lstlisting}[language=Pseudo] 378 acquire A 379 acquire B 380 signal B 381 release B 382 release A 383 \end{lstlisting} 384 \end{tabular} 385 \end{center} 386 387 Also easy : like uC++ 388 389 \begin{center} 390 \begin{tabular}{ c @{\hskip 0.65in} c } 391 \begin{lstlisting}[language=Pseudo] 392 acquire A & B 393 wait A & B 394 release A & B 395 \end{lstlisting}&\begin{lstlisting}[language=Pseudo] 396 acquire A & B 397 signal A & B 398 release A & B 399 \end{lstlisting} 400 \end{tabular} 401 \end{center} 402 403 Simplest extension : can be made like uC++ by tying B to A 404 405 \begin{center} 406 \begin{tabular}{ c @{\hskip 0.65in} c } 407 \begin{lstlisting}[language=Pseudo] 454 408 acquire A 455 409 // Code Section 1 456 acquire A &B410 acquire B 457 411 // Code Section 2 458 412 wait A & B 459 413 // Code Section 3 460 release A &B414 release B 461 415 // Code Section 4 462 416 release A 463 \end{pseudo} 464 465 \columnbreak 466 467 \begin{pseudo} 417 \end{lstlisting}&\begin{lstlisting}[language=Pseudo] 468 418 acquire A 469 419 // Code Section 5 470 acquire A &B420 acquire B 471 421 // Code Section 6 472 422 signal A & B 473 423 // Code Section 7 474 release A &B424 release B 475 425 // Code Section 8 476 426 release A 477 \end{pseudo} 478 \end{multicols} 479 480 It is particularly important to pay attention to code sections 8 and 3 which are where the existing semantics of internal scheduling are undefined. The root of the problem is that \gls{group-acquire} is used in a context where one of the monitors is already acquired. As mentionned in previous sections, monitors support multiple acquiring which means the that nesting \gls{group-acquire} can be done safely. However, in the context of internal scheduling it is important to define the behaviour of the previous pseudo-code. When the signaller thread reaches the location where it should "release A \& B", it actually only needs to release the monitor B. Since the other thread is waiting on monitor B, the signaller thread cannot simply release the monitor into the wild. This would mean that the waiting thread would have to reacquire the monitor and would therefore open the door to barging threads. Since the signalling thread still needs the monitor A, simply transferring ownership to the waiting thread is not an option because it would pottentially violate mutual exclusion. We are therefore left with three options : 481 482 \subsubsection{Delaying signals} 483 The first more obvious solution to solve the problem of multi-monitor scheduling is to keep ownership of all locks until the last lock is ready to be transferred. It can be argued that that moment is the correct time to transfer ownership when the last lock is no longer needed is what fits most closely to the behaviour of single monitor scheduling. However, this solution can become much more complicated depending on the content of the code section 8. Indeed, nothing prevents a user from signalling monitor A on a different condition variable. In that case, if monitor B is transferred with monitor A, then it means the system needs to handle threads having ownership on more monitors than expected and how to tie monitors together. On the other hand if the signalling thread only transfers monitor A then somehow both monitors A and B have to be transferred to the waiting thread from two different threads. While this solution may work, it was not fully explored because there is no apparent upper bound on the complexity of ownership transfer. 484 485 \subsubsection{Dependency graphs} 486 In the previous pseudo-code, there is a solution which would statisfy both barging prevention and mutual exclusion. If ownership of both monitors is transferred to the waiter when the signaller releases A and then the waiter transfers back ownership of A when it releases it then the problem is solved. This is the second solution. The problem it encounters is that it effectively boils down to resolving a dependency graph of ownership requirements. Here even the simplest of code snippets requires two transfers and it seems to increase in a manner closer to polynomial. For example the following code which is just a direct extension to three monitors requires at least three ownership transfer and has multiple solutions. 487 488 \begin{multicols}{2} 489 \begin{pseudo} 427 \end{lstlisting} 428 \end{tabular} 429 \end{center} 430 431 Hard extension : 432 433 Incorrect options for the signal : 434 435 \begin{description} 436 \item[-] Release B and baton pass after Code Section 8 : Passing b without having it 437 \item[-] Keep B during Code Section 8 : Can lead to deadlocks since we secretly keep a lock longer than specified by the user 438 \item[-] Instead of release B transfer A and B to waiter then try to reacquire A before running Code Section 8 : This allows barging 439 \end{description} 440 441 Since we don't want barging we need to pass A \& B and somehow block and get A back. 442 443 \begin{center} 444 \begin{tabular}{ c @{\hskip 0.65in} c } 445 \begin{lstlisting}[language=Pseudo] 490 446 acquire A 491 447 acquire B 492 448 acquire C 493 449 wait A & B & C 494 release C 495 release B 496 release A 497 \end{pseudo} 498 499 \columnbreak 500 501 \begin{pseudo} 450 1: release C 451 2: release B 452 3: release A 453 \end{lstlisting}&\begin{lstlisting}[language=Pseudo] 502 454 acquire A 503 455 acquire B 504 456 acquire C 505 457 signal A & B & C 506 release C 507 release B 508 release A 509 \end{pseudo} 510 \end{multicols} 511 512 \subsubsection{Partial signalling} 513 Finally, the solution that was chosen for \CFA is to use partial signalling. Consider the following case : 514 515 \begin{multicols}{2} 516 \begin{pseudo}[numbers=left] 458 4: release C 459 5: release B 460 6: release A 461 \end{lstlisting} 462 \end{tabular} 463 \end{center} 464 465 To prevent barging : 466 467 \begin{description} 468 \item[-] When the signaller hits 4 : pass A, B, C to waiter 469 \item[-] When the waiter hits 2 : pass A, B to signaller 470 \item[-] When the signaller hits 5 : pass A to waiter 471 \end{description} 472 473 474 \begin{center} 475 \begin{tabular}{ c @{\hskip 0.65in} c } 476 \begin{lstlisting}[language=Pseudo] 517 477 acquire A 518 acquire A & B 519 wait A & B 520 release A & B 521 release A 522 \end{pseudo} 523 524 \columnbreak 525 526 \begin{pseudo}[numbers=left, firstnumber=6] 527 acquire A 528 acquire A & B 529 signal A & B 530 release A & B 531 // ... More code 532 release A 533 \end{pseudo} 534 \end{multicols} 535 536 The partial signalling solution transfers ownership of monitor B at lines 10 but does not wake the waiting thread since it is still using monitor A. Only when it reaches line 11 does it actually wakeup the waiting thread. This solution has the benefit that complexity is encapsulated in to only two actions, passing monitors to the next owner when they should be release and conditionnaly waking threads if all conditions are met. Contrary to the other solutions, this solution quickly hits an upper bound on complexity of implementation. 537 538 % Hard extension : 539 540 % Incorrect options for the signal : 541 542 % \begin{description} 543 % \item[-] Release B and baton pass after Code Section 8 : Passing b without having it 544 % \item[-] Keep B during Code Section 8 : Can lead to deadlocks since we secretly keep a lock longer than specified by the user 545 % \item[-] Instead of release B transfer A and B to waiter then try to reacquire A before running Code Section 8 : This allows barging 546 % \end{description} 547 548 % Since we don't want barging we need to pass A \& B and somehow block and get A back. 549 550 % \begin{center} 551 % \begin{tabular}{ c @{\hskip 0.65in} c } 552 % \begin{lstlisting}[language=Pseudo] 553 % acquire A 554 % acquire B 555 % acquire C 556 % wait A & B & C 557 % 1: release C 558 % 2: release B 559 % 3: release A 560 % \end{lstlisting}&\begin{lstlisting}[language=Pseudo] 561 % acquire A 562 % acquire B 563 % acquire C 564 % signal A & B & C 565 % 4: release C 566 % 5: release B 567 % 6: release A 568 % \end{lstlisting} 569 % \end{tabular} 570 % \end{center} 571 572 % To prevent barging : 573 574 % \begin{description} 575 % \item[-] When the signaller hits 4 : pass A, B, C to waiter 576 % \item[-] When the waiter hits 2 : pass A, B to signaller 577 % \item[-] When the signaller hits 5 : pass A to waiter 578 % \end{description} 579 580 581 % \begin{center} 582 % \begin{tabular}{ c @{\hskip 0.65in} c } 583 % \begin{lstlisting}[language=Pseudo] 584 % acquire A 585 % acquire C 586 % acquire B 587 % wait A & B & C 588 % 1: release B 589 % 2: release C 590 % 3: release A 591 % \end{lstlisting}&\begin{lstlisting}[language=Pseudo] 592 % acquire B 593 % acquire A 594 % acquire C 595 % signal A & B & C 596 % 4: release C 597 % 5: release A 598 % 6: release B 599 % \end{lstlisting} 600 % \end{tabular} 601 % \end{center} 602 603 % To prevent barging : When the signaller hits 4 : pass A, B, C to waiter. When the waiter hits 1 it must release B, 604 605 % \begin{description} 606 % \item[-] 607 % \item[-] When the waiter hits 1 : pass A, B to signaller 608 % \item[-] When the signaller hits 5 : pass A, B to waiter 609 % \item[-] When the waiter hits 2 : pass A to signaller 610 % \end{description} 478 acquire C 479 acquire B 480 wait A & B & C 481 1: release B 482 2: release C 483 3: release A 484 \end{lstlisting}&\begin{lstlisting}[language=Pseudo] 485 acquire B 486 acquire A 487 acquire C 488 signal A & B & C 489 4: release C 490 5: release A 491 6: release B 492 \end{lstlisting} 493 \end{tabular} 494 \end{center} 495 496 To prevent barging : When the signaller hits 4 : pass A, B, C to waiter. When the waiter hits 1 it must release B, 497 498 \begin{description} 499 \item[-] 500 \item[-] When the waiter hits 1 : pass A, B to signaller 501 \item[-] When the signaller hits 5 : pass A, B to waiter 502 \item[-] When the waiter hits 2 : pass A to signaller 503 \end{description} 611 504 612 505 % Monitors also need to schedule waiting threads internally 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 have threads wait and signaled from them. Here is a simple example of such a technique : … … 1027 920 % # # # # ### # # # # # # # # # 1028 921 % ####### # # # ### ##### ##### # # ####### ###### 1029 \section{External scheduling} \label{extsched} 922 \newpage 923 \subsection{External scheduling} \label{extsched} 1030 924 An alternative to internal scheduling is to use external scheduling instead. This method is more constrained and explicit which may help users tone down the undeterministic nature of concurrency. Indeed, as the following examples demonstrates, 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 a simple use \code{accept} versus \code{wait}/\code{signal} and its advantages. 1031 925 … … 1065 959 % ####### ####### ####### ##### ####### ####### ###### ##### ##### 1066 960 1067 \subs ection{Loose object definitions}961 \subsubsection{Loose object definitions} 1068 962 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 : 1069 963 … … 1090 984 \end{center} 1091 985 1092 For the \ps code{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 \pscode{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 :986 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 : 1093 987 1094 988 \begin{center} … … 1163 1057 % # # ##### ####### # ### # # ####### # # 1164 1058 1165 \subs ection{Multi-monitor scheduling}1059 \subsubsection{Multi-monitor scheduling} 1166 1060 1167 1061 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 : … … 1222 1116 1223 1117 1224 \subs ection{Implementation Details: External scheduling queues}1118 \subsubsection{Implementation Details: External scheduling queues} 1225 1119 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. 1226 1120 1227 \s ection{Other concurrency tools}1121 \subsection{Other concurrency tools} 1228 1122 TO BE CONTINUED... 1229 1123 … … 1237 1131 1238 1132 1133 \newpage 1239 1134 % ###### # ###### # # # ####### # ### ##### # # 1240 1135 % # # # # # # # # # # # # # # # ## ## … … 1244 1139 % # # # # # # # # # # # # # # # # 1245 1140 % # # # # # # # ####### ####### ####### ####### ### ##### # # 1246 \ chapter{Parallelism}1141 \section{Parallelism} 1247 1142 Historically, computer performance was about processor speeds and instructions count. However, with heat dissipation being a direct consequence of speed increase, parallelism has become the new source for increased performance~\cite{Sutter05, Sutter05b}. In this decade, it is not longer reasonnable to create a 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} in combination with semantics like \code{fork}, \code{join}, etc. 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 that all have strengths and weaknesses. While there are many variations of the presented paradigms, most of these variations do not actually change the guarantees or the semantics, they simply move costs in order to achieve better performance for certain workloads. 1248 1143 1249 \section{Paradigm}1250 1144 \subsection{User-level threads} 1251 1145 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 approach 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 problems are hidden, users still have to think about data races, deadlocks and synchronization issues. These issues can be somewhat alleviated by a concurrency toolkit with strong garantees but the parallelism toolkit offers very little to reduce complexity in itself. … … 1253 1147 Examples of languages that support \glspl{uthread} are Erlang~\cite{Erlang} and \uC~\cite{uC++book}. 1254 1148 1255 \subs ection{Fibers : user-level threads without preemption}1149 \subsubsection{Fibers : user-level threads without preemption} 1256 1150 A popular varient of \glspl{uthread} is what is often reffered to as \glspl{fiber}. However, \glspl{fiber} do not present meaningful semantical differences with \glspl{uthread}. Advocates of \glspl{fiber} list their high performance and ease of implementation as majors strenghts of \glspl{fiber} but the performance difference between \glspl{uthread} and \glspl{fiber} is controversial and the ease of implementation, while true, is a weak argument in the context of language design. Therefore this proposal largely ignore fibers. 1257 1151 … … 1600 1494 % # # # # 1601 1495 % # # ####### ####### 1602 \chapter{Putting it all together} 1603 1604 1605 1606 1607 1608 \chapter{Conclusion} 1496 \section{Putting it all together} 1497 1498 1499 1500 1609 1501 1610 1502 … … 1620 1512 % # # # # # # # # # 1621 1513 % # ##### # ##### # # ###### 1622 \ chapter{Future work}1514 \section{Future work} 1623 1515 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. 1624 1516 \subsection{Transactions} -
doc/proposals/concurrency/style.tex
r8514fe19 rc850687 1 1 \input{common} % bespoke macros used in the document 2 \input{cfa-format}3 2 4 3 % \CFADefaultStyle -
doc/proposals/concurrency/version
r8514fe19 rc850687 1 0. 8.21 0.7.141
Note:
See TracChangeset
for help on using the changeset viewer.