[27dde72] | 1 | % ====================================================================== |
---|
| 2 | % ====================================================================== |
---|
| 3 | \chapter{Concurrency} |
---|
| 4 | % ====================================================================== |
---|
| 5 | % ====================================================================== |
---|
[b9d0fb6] | 6 | Several tool can be used to solve concurrency challenges. Since many of these challenges 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 closely relate to networking concepts (channels\cite{CSP,Go} 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). This distinction in turn means that, in order to be effective, programmers need to learn two sets of designs patterns. While this distinction can be hidden away in library code, effective use of the librairy still has to take both paradigms into account. |
---|
[7c17511] | 7 | |
---|
[e0a653d] | 8 | Approaches based on shared memory are more closely related to non-concurrent paradigms since they often rely on basic constructs like routine calls and shared objects. At the lowest level, concurrent paradigms are implemented as atomic operations and locks. 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}. |
---|
[7c17511] | 9 | |
---|
[b9d0fb6] | 10 | An approach that is worth mentioning 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 the main concurrency paradigm for systems language, which is why it was rejected as the core paradigm for concurrency in \CFA. |
---|
[7c17511] | 11 | |
---|
[3364962] | 12 | 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. |
---|
[27dde72] | 13 | |
---|
| 14 | \section{Basics} |
---|
[3364962] | 15 | Non-determinism requires concurrent systems to offer support for mutual-exclusion and synchronisation. Mutual-exclusion is the concept that only a fixed number of threads can access a critical section at any given time, where a critical section is a group of instructions on an associated portion of data that requires the restricted access. On the other hand, synchronization enforces relative ordering of execution and synchronization tools provide numerous mechanisms to establish timing relationships among threads. |
---|
[27dde72] | 16 | |
---|
| 17 | \subsection{Mutual-Exclusion} |
---|
[3364962] | 18 | As mentionned above, mutual-exclusion is the guarantee that only a fix number of threads can enter a critical section at once. However, many solutions exist for mutual exclusion, which vary in terms of performance, flexibility and ease of use. Methods range from low-level locks, which are fast and flexible but require significant attention to be correct, to higher-level mutual-exclusion methods, which sacrifice some performance in order to improve ease of use. Ease of use comes by either guaranteeing some problems cannot occur (e.g., being deadlock free) or by offering a more explicit coupling between data and corresponding critical section. For example, the \CC \code{std::atomic<T>} offers an easy way to express mutual-exclusion on a restricted set of operations (e.g.: reading/writing large types atomically). Another challenge with low-level locks is composability. Locks have restricted composability because it takes careful organising for multiple locks to be used while preventing deadlocks. Easing composability is another feature higher-level mutual-exclusion mechanisms often offer. |
---|
[27dde72] | 19 | |
---|
| 20 | \subsection{Synchronization} |
---|
[b9d0fb6] | 21 | As for mutual-exclusion, low-level synchronisation primitives often offer good performance and good flexibility at the cost of ease of use. Again, higher-level mechanism often simplify usage by adding better coupling between synchronization and data, e.g.: message passing, or offering simpler solution to otherwise involved challenges. As mentioned above, synchronization can be expressed as guaranteeing that event \textit{X} always happens before \textit{Y}. Most of the time, synchronisation happens within a critical section, where threads must acquire mutual-exclusion in a certain order. However, it may also be desirable to guarantee that event \textit{Z} does not occur between \textit{X} and \textit{Y}. Not satisfying this property called barging. For example, where event \textit{X} tries to effect event \textit{Y} but another thread acquires the critical section and emits \textit{Z} before \textit{Y}. The classic exmaple is the thread that finishes using a ressource and unblocks a thread waiting to use the resource, but the unblocked thread must compete again to acquire the resource. Preventing or detecting barging is an involved challenge with low-level locks, which can be made much easier by higher-level constructs. This challenge is often split into two different methods, barging avoidance and barging prevention. Algorithms that use status flags and other flag variables to detect barging threads are said to be using barging avoidance while algorithms that baton-passing locks between threads instead of releasing the locks are said to be using barging prevention. |
---|
[27dde72] | 22 | |
---|
| 23 | % ====================================================================== |
---|
| 24 | % ====================================================================== |
---|
| 25 | \section{Monitors} |
---|
| 26 | % ====================================================================== |
---|
| 27 | % ====================================================================== |
---|
[7c17511] | 28 | 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 OO semantics. The only requirements is the ability to declare a handle to a shared object and a set of routines that act on it : |
---|
[27dde72] | 29 | \begin{cfacode} |
---|
[fb31cb8] | 30 | typedef /*some monitor type*/ monitor; |
---|
| 31 | int f(monitor & m); |
---|
[27dde72] | 32 | |
---|
[fb31cb8] | 33 | int main() { |
---|
| 34 | monitor m; //Handle m |
---|
| 35 | f(m); //Routine using handle |
---|
| 36 | } |
---|
[27dde72] | 37 | \end{cfacode} |
---|
| 38 | |
---|
| 39 | % ====================================================================== |
---|
| 40 | % ====================================================================== |
---|
| 41 | \subsection{Call semantics} \label{call} |
---|
| 42 | % ====================================================================== |
---|
| 43 | % ====================================================================== |
---|
[7c17511] | 44 | The above monitor example displays some of the intrinsic characteristics. First, 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 objects. |
---|
[27dde72] | 45 | |
---|
[7c17511] | 46 | 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 occur for generic helper routines (\code{swap}, \code{sort}, etc.) or specific helper routines like the following to implement an atomic counter : |
---|
[27dde72] | 47 | |
---|
| 48 | \begin{cfacode} |
---|
[fb31cb8] | 49 | monitor counter_t { /*...see section $\ref{data}$...*/ }; |
---|
[27dde72] | 50 | |
---|
[fb31cb8] | 51 | void ?{}(counter_t & nomutex this); //constructor |
---|
| 52 | size_t ++?(counter_t & mutex this); //increment |
---|
[27dde72] | 53 | |
---|
[fb31cb8] | 54 | //need for mutex is platform dependent |
---|
| 55 | void ?{}(size_t * this, counter_t & mutex cnt); //conversion |
---|
[27dde72] | 56 | \end{cfacode} |
---|
[3364962] | 57 | This counter is used as follows: |
---|
| 58 | \begin{center} |
---|
| 59 | \begin{tabular}{c @{\hskip 0.35in} c @{\hskip 0.35in} c} |
---|
| 60 | \begin{cfacode} |
---|
| 61 | //shared counter |
---|
| 62 | counter_t cnt1, cnt2; |
---|
| 63 | |
---|
| 64 | //multiple threads access counter |
---|
| 65 | thread 1 : cnt1++; cnt2++; |
---|
| 66 | thread 2 : cnt1++; cnt2++; |
---|
| 67 | thread 3 : cnt1++; cnt2++; |
---|
| 68 | ... |
---|
| 69 | thread N : cnt1++; cnt2++; |
---|
| 70 | \end{cfacode} |
---|
| 71 | \end{tabular} |
---|
| 72 | \end{center} |
---|
[b9d0fb6] | 73 | Notice how the counter is used without any explicit synchronisation and yet supports thread-safe semantics for both reading and writting, which is similar in usage to \CC \code{atomic} template. |
---|
[27dde72] | 74 | |
---|
[b9d0fb6] | 75 | 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 con\-structed 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} keyword depending on whether or not reading a \code{size_t} is an atomic operation. |
---|
[27dde72] | 76 | |
---|
[b9d0fb6] | 77 | For maximum usability, monitors use \gls{multi-acq} semantics, which means a single thread can acquire the same monitor multiple times without deadlock. For example, figure \ref{fig:search} uses recursion and \gls{multi-acq} to print values inside a binary tree. |
---|
[3364962] | 78 | \begin{figure} |
---|
| 79 | \label{fig:search} |
---|
| 80 | \begin{cfacode} |
---|
| 81 | monitor printer { ... }; |
---|
| 82 | struct tree { |
---|
| 83 | tree * left, right; |
---|
| 84 | char * value; |
---|
| 85 | }; |
---|
| 86 | void print(printer & mutex p, char * v); |
---|
[27dde72] | 87 | |
---|
[3364962] | 88 | void print(printer & mutex p, tree * t) { |
---|
| 89 | print(p, t->value); |
---|
| 90 | print(p, t->left ); |
---|
| 91 | print(p, t->right); |
---|
| 92 | } |
---|
| 93 | \end{cfacode} |
---|
| 94 | \caption{Recursive printing algorithm using \gls{multi-acq}.} |
---|
| 95 | \end{figure} |
---|
| 96 | |
---|
[b9d0fb6] | 97 | Having both \code{mutex} and \code{nomutex} keywords is redundant based on the meaning of a routine having neither of these keywords. For example, given a routine without qualifiers \code{void foo(counter_t & this)}, then it is reasonable that it should default to the safest option \code{mutex}, whereas assuming \code{nomutex} is unsafe and may cause subtle errors. In fact, \code{nomutex} is the ``normal'' parameter behaviour, with the \code{nomutex} keyword effectively stating explicitly that ``this routine is not special''. Another alternative is making exactly one of these keywords mandatory, which provides the same semantics but without the ambiguity of supporting routines with neither keyword. Mandatory keywords would also have the added benefit of being self-documented but at the cost of extra typing. While there are several benefits to mandatory keywords, they do bring a few challenges. Mandatory keywords in \CFA would imply that the compiler must know without doubt whether or not a parameter is a monitor or not. 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 and uses no keyword to mean \code{nomutex}. |
---|
[27dde72] | 98 | |
---|
| 99 | The next semantic decision is to establish when \code{mutex} may be used as a type qualifier. Consider the following declarations: |
---|
| 100 | \begin{cfacode} |
---|
| 101 | int f1(monitor & mutex m); |
---|
| 102 | int f2(const monitor & mutex m); |
---|
| 103 | int f3(monitor ** mutex m); |
---|
[7c17511] | 104 | int f4(monitor * mutex m []); |
---|
[27dde72] | 105 | int f5(graph(monitor*) & mutex m); |
---|
| 106 | \end{cfacode} |
---|
[3364962] | 107 | 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 objects are only acquired once becomes none-trivial. This problem can be extended to absurd limits like \code{f5}, which uses a graph of monitors. To make the issue tractable, this project imposes the requirement that a routine may only acquire one monitor per parameter and it must be the type of the parameter with at most 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: |
---|
[27dde72] | 108 | \begin{cfacode} |
---|
| 109 | int f1(monitor & mutex m); //Okay : recommanded case |
---|
| 110 | int f2(monitor * mutex m); //Okay : could be an array but probably not |
---|
[7c17511] | 111 | int f3(monitor mutex m []); //Not Okay : Array of unkown length |
---|
[27dde72] | 112 | int f4(monitor ** mutex m); //Not Okay : Could be an array |
---|
[7c17511] | 113 | int f5(monitor * mutex m []); //Not Okay : Array of unkown length |
---|
[27dde72] | 114 | \end{cfacode} |
---|
[b9d0fb6] | 115 | Note that not all array functions are actually distinct in the type system. However, even if the code generation could tell the difference, the extra information is still not sufficient to extend meaningfully the monitor call semantic. |
---|
[27dde72] | 116 | |
---|
[b9d0fb6] | 117 | Unlike object-oriented monitors, where calling a mutex member \emph{implicitly} acquires mutual-exclusion of the receiver object, \CFA uses an explicit mechanism to acquire mutual-exclusion. A consequence of this approach is that it extends naturally to multi-monitor calls. |
---|
[27dde72] | 118 | \begin{cfacode} |
---|
| 119 | int f(MonitorA & mutex a, MonitorB & mutex b); |
---|
| 120 | |
---|
| 121 | MonitorA a; |
---|
| 122 | MonitorB b; |
---|
| 123 | f(a,b); |
---|
| 124 | \end{cfacode} |
---|
[b9d0fb6] | 125 | While OO monitors could be extended with a mutex qualifier for multiple-monitor calls, no example of this feature could be found. The capacity to acquire multiple locks before entering a critical section is called \emph{\gls{bulk-acq}}. 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 different routines using the same monitors as arguments. This consistent ordering means acquiring multiple monitors in the way is safe from deadlock. However, users can still force the acquiring order. For example, notice which routines use \code{mutex}/\code{nomutex} and how this affects aquiring order: |
---|
[27dde72] | 126 | \begin{cfacode} |
---|
[fb31cb8] | 127 | void foo(A & mutex a, B & mutex b) { //acquire a & b |
---|
| 128 | ... |
---|
| 129 | } |
---|
[27dde72] | 130 | |
---|
[fb31cb8] | 131 | void bar(A & mutex a, B & /*nomutex*/ b) { //acquire a |
---|
| 132 | ... foo(a, b); ... //acquire b |
---|
| 133 | } |
---|
[27dde72] | 134 | |
---|
[fb31cb8] | 135 | void baz(A & /*nomutex*/ a, B & mutex b) { //acquire b |
---|
| 136 | ... foo(a, b); ... //acquire a |
---|
| 137 | } |
---|
[27dde72] | 138 | \end{cfacode} |
---|
[3364962] | 139 | The \gls{multi-acq} 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. |
---|
[27dde72] | 140 | |
---|
[b9d0fb6] | 141 | However, such use leads to 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\cit, solving this problem requires: |
---|
[27dde72] | 142 | \begin{enumerate} |
---|
| 143 | \item Dynamically tracking of the monitor-call order. |
---|
| 144 | \item Implement rollback semantics. |
---|
| 145 | \end{enumerate} |
---|
[b9d0fb6] | 146 | 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 or only use \gls{bulk-acq} of all the monitors. While \CFA provides only a partial solution, many system provide no solution and the \CFA partial solution handles many useful cases. |
---|
[27dde72] | 147 | |
---|
[b9d0fb6] | 148 | For example, \gls{multi-acq} and \gls{bulk-acq} can be used together in interesting ways: |
---|
[27dde72] | 149 | \begin{cfacode} |
---|
[3364962] | 150 | monitor bank { ... }; |
---|
[27dde72] | 151 | |
---|
[3364962] | 152 | void deposit( bank & mutex b, int deposit ); |
---|
[27dde72] | 153 | |
---|
| 154 | void transfer( bank & mutex mybank, bank & mutex yourbank, int me2you) { |
---|
| 155 | deposit( mybank, -me2you ); |
---|
| 156 | deposit( yourbank, me2you ); |
---|
| 157 | } |
---|
| 158 | \end{cfacode} |
---|
[b9d0fb6] | 159 | This example shows a trivial solution to the bank-account transfer-problem\cit. Without \gls{multi-acq} and \gls{bulk-acq}, the solution to this problem is much more involved and requires carefull engineering. |
---|
[27dde72] | 160 | |
---|
[b9d0fb6] | 161 | \subsection{\code{mutex} statement} \label{mutex-stmt} |
---|
[fb31cb8] | 162 | |
---|
| 163 | The call semantics discussed aboved have one software engineering issue, only a named routine can acquire the mutual-exclusion of a set of monitor. \CFA offers the \code{mutex} statement to workaround the need for unnecessary names, avoiding a major software engineering problem\cit. Listing \ref{lst:mutex-stmt} shows an example of the \code{mutex} statement, which introduces a new scope in which the mutual-exclusion of a set of monitor is acquired. Beyond naming, the \code{mutex} statement has no semantic difference from a routine call with \code{mutex} parameters. |
---|
| 164 | |
---|
| 165 | \begin{figure} |
---|
| 166 | \begin{center} |
---|
| 167 | \begin{tabular}{|c|c|} |
---|
| 168 | function call & \code{mutex} statement \\ |
---|
| 169 | \hline |
---|
| 170 | \begin{cfacode}[tabsize=3] |
---|
| 171 | monitor M {}; |
---|
| 172 | void foo( M & mutex m ) { |
---|
| 173 | //critical section |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | void bar( M & m ) { |
---|
| 177 | foo( m ); |
---|
| 178 | } |
---|
| 179 | \end{cfacode}&\begin{cfacode}[tabsize=3] |
---|
| 180 | monitor M {}; |
---|
| 181 | void bar( M & m ) { |
---|
| 182 | mutex(m) { |
---|
| 183 | //critical section |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | \end{cfacode} |
---|
| 189 | \end{tabular} |
---|
| 190 | \end{center} |
---|
| 191 | \caption{Regular call semantics vs. \code{mutex} statement} |
---|
| 192 | \label{lst:mutex-stmt} |
---|
| 193 | \end{figure} |
---|
| 194 | |
---|
[27dde72] | 195 | % ====================================================================== |
---|
| 196 | % ====================================================================== |
---|
| 197 | \subsection{Data semantics} \label{data} |
---|
| 198 | % ====================================================================== |
---|
| 199 | % ====================================================================== |
---|
| 200 | 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 contain 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}: |
---|
| 201 | \begin{cfacode} |
---|
| 202 | monitor counter_t { |
---|
| 203 | int value; |
---|
| 204 | }; |
---|
| 205 | |
---|
| 206 | void ?{}(counter_t & this) { |
---|
| 207 | this.cnt = 0; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | int ?++(counter_t & mutex this) { |
---|
| 211 | return ++this.value; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | //need for mutex is platform dependent here |
---|
| 215 | void ?{}(int * this, counter_t & mutex cnt) { |
---|
| 216 | *this = (int)cnt; |
---|
| 217 | } |
---|
| 218 | \end{cfacode} |
---|
| 219 | |
---|
[b9d0fb6] | 220 | Like threads and coroutines, monitors are defined in terms of traits with some additional language support in the form of the \code{monitor} keyword. The monitor trait is : |
---|
| 221 | \begin{cfacode} |
---|
| 222 | trait is_monitor(dtype T) { |
---|
| 223 | monitor_desc * get_monitor( T & ); |
---|
| 224 | void ^?{}( T & mutex ); |
---|
| 225 | }; |
---|
| 226 | \end{cfacode} |
---|
| 227 | Note that the destructor of a monitor must be a \code{mutex} routine. This requirement ensures that the destructor has mutual-exclusion. As with any object, any call to a monitor, using \code{mutex} or otherwise, is Undefined Behaviour after the destructor has run. |
---|
[27dde72] | 228 | |
---|
| 229 | % ====================================================================== |
---|
| 230 | % ====================================================================== |
---|
[64b272a] | 231 | \section{Internal scheduling} \label{intsched} |
---|
[27dde72] | 232 | % ====================================================================== |
---|
| 233 | % ====================================================================== |
---|
[3364962] | 234 | In addition to mutual exclusion, the monitors at the core of \CFA's concurrency can also be used to achieve synchronisation. With monitors, this capability is generally achieved with internal or external scheduling as in\cit. Since internal scheduling within a single monitor is mostly a solved problem, this thesis concentrates on extending internal scheduling to multiple monitors. Indeed, like the \gls{bulk-acq} semantics, internal scheduling extends to multiple monitors in a way that is natural to the user but requires additional complexity on the implementation side. |
---|
[27dde72] | 235 | |
---|
[e0a653d] | 236 | First, here is a simple example of such a technique: |
---|
[27dde72] | 237 | |
---|
| 238 | \begin{cfacode} |
---|
[fb31cb8] | 239 | monitor A { |
---|
| 240 | condition e; |
---|
| 241 | } |
---|
[27dde72] | 242 | |
---|
[fb31cb8] | 243 | void foo(A & mutex a) { |
---|
| 244 | ... |
---|
| 245 | //Wait for cooperation from bar() |
---|
| 246 | wait(a.e); |
---|
| 247 | ... |
---|
| 248 | } |
---|
[27dde72] | 249 | |
---|
[fb31cb8] | 250 | void bar(A & mutex a) { |
---|
| 251 | //Provide cooperation for foo() |
---|
| 252 | ... |
---|
| 253 | //Unblock foo |
---|
| 254 | signal(a.e); |
---|
| 255 | } |
---|
[27dde72] | 256 | \end{cfacode} |
---|
| 257 | |
---|
[b9d0fb6] | 258 | There are two details to note here. First, the \code{signal} is a delayed operation, it only unblocks the waiting thread when it reaches the end of the critical section. This semantic is needed to respect mutual-exclusion. The alternative is to return immediately after the call to \code{signal}, which is significantly more restrictive. Second, in \CFA, while it is common to store a \code{condition} as a field of the monitor, a \code{condition} variable can be stored/created independently of a monitor. Here routine \code{foo} waits for the \code{signal} from \code{bar} before making further progress, effectively ensuring a basic ordering. |
---|
[27dde72] | 259 | |
---|
[b9d0fb6] | 260 | An important aspect of the implementation is that \CFA does not allow barging, which means that once function \code{bar} releases the monitor, \code{foo} is guaranteed to resume immediately after (unless some other thread 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 barging prevention or barging avoidance is more involved without language support. Supporting barging prevention as well as extending internal scheduling to multiple monitors is the main source of complexity in the design of \CFA concurrency. |
---|
[27dde72] | 261 | |
---|
| 262 | % ====================================================================== |
---|
| 263 | % ====================================================================== |
---|
| 264 | \subsection{Internal Scheduling - multi monitor} |
---|
| 265 | % ====================================================================== |
---|
| 266 | % ====================================================================== |
---|
[b9d0fb6] | 267 | It is easier to understand the problem of multi-monitor scheduling using a series of pseudo-code. Note that for simplicity in the following snippets of pseudo-code, waiting and signalling is done using an implicit condition variable, like Java built-in monitors. Indeed, \code{wait} statements always use the implicit condition as paremeter and explicitly names the monitors (A and B) associated with the condition. Note that in \CFA, condition variables are tied to a set of monitors on first use (called branding) which means that using internal scheduling with distinct sets of monitors requires one condition variable per set of monitors. |
---|
[27dde72] | 268 | |
---|
| 269 | \begin{multicols}{2} |
---|
| 270 | thread 1 |
---|
| 271 | \begin{pseudo} |
---|
| 272 | acquire A |
---|
| 273 | wait A |
---|
| 274 | release A |
---|
| 275 | \end{pseudo} |
---|
| 276 | |
---|
| 277 | \columnbreak |
---|
| 278 | |
---|
| 279 | thread 2 |
---|
| 280 | \begin{pseudo} |
---|
| 281 | acquire A |
---|
| 282 | signal A |
---|
| 283 | release A |
---|
| 284 | \end{pseudo} |
---|
| 285 | \end{multicols} |
---|
[3364962] | 286 | The example shows the simple case of having two threads (one for each column) and a single monitor A. One thread acquires before waiting (atomically blocking and releasing A) and the other acquires before signalling. It is important to note here that both \code{wait} and \code{signal} must be called with the proper monitor(s) already acquired. This semantic is a logical requirement for barging prevention. |
---|
[27dde72] | 287 | |
---|
[3364962] | 288 | A direct extension of the previous example is a \gls{bulk-acq} version: |
---|
[27dde72] | 289 | |
---|
| 290 | \begin{multicols}{2} |
---|
| 291 | \begin{pseudo} |
---|
| 292 | acquire A & B |
---|
| 293 | wait A & B |
---|
| 294 | release A & B |
---|
| 295 | \end{pseudo} |
---|
| 296 | |
---|
| 297 | \columnbreak |
---|
| 298 | |
---|
| 299 | \begin{pseudo} |
---|
| 300 | acquire A & B |
---|
| 301 | signal A & B |
---|
| 302 | release A & B |
---|
| 303 | \end{pseudo} |
---|
| 304 | \end{multicols} |
---|
[b9d0fb6] | 305 | This version uses \gls{bulk-acq} (denoted using the {\sf\&} symbol), but the presence of multiple monitors does not add a particularly new meaning. Synchronization happens between the two threads in exactly the same way and order. The only difference is that mutual exclusion covers more monitors. On the implementation side, handling multiple monitors does add a degree of complexity as the next few examples demonstrate. |
---|
[27dde72] | 306 | |
---|
[b9d0fb6] | 307 | While deadlock issues can occur when nesting monitors, these issues are only a symptom of the fact that locks, and by extension monitors, are not perfectly composable. For monitors, a well known deadlock problem is the Nested Monitor Problem\cit, which occurs when a \code{wait} is made by a thread that holds more than one monitor. For example, the following pseudo-code runs into the nested-monitor problem : |
---|
[3364962] | 308 | \begin{multicols}{2} |
---|
| 309 | \begin{pseudo} |
---|
| 310 | acquire A |
---|
| 311 | acquire B |
---|
| 312 | wait B |
---|
| 313 | release B |
---|
| 314 | release A |
---|
| 315 | \end{pseudo} |
---|
| 316 | |
---|
| 317 | \columnbreak |
---|
| 318 | |
---|
| 319 | \begin{pseudo} |
---|
| 320 | acquire A |
---|
| 321 | acquire B |
---|
| 322 | signal B |
---|
| 323 | release B |
---|
| 324 | release A |
---|
| 325 | \end{pseudo} |
---|
| 326 | \end{multicols} |
---|
[b9d0fb6] | 327 | |
---|
| 328 | The \code{wait} only releases monitor \code{B} so the signalling thread cannot acquire monitor \code{A} to get to the \code{signal}. Attempting release of all acquired monitors at the \code{wait} results in another set of problems such as releasing monitor \code{C}, which has nothing to do with the \code{signal}. |
---|
| 329 | |
---|
[3364962] | 330 | However, for monitors as for locks, it is possible to write a program using nesting without encountering any problems if nesting is done correctly. For example, the next pseudo-code snippet acquires monitors {\sf A} then {\sf B} before waiting, while only acquiring {\sf B} when signalling, effectively avoiding the nested monitor problem. |
---|
[27dde72] | 331 | |
---|
| 332 | \begin{multicols}{2} |
---|
| 333 | \begin{pseudo} |
---|
| 334 | acquire A |
---|
| 335 | acquire B |
---|
| 336 | wait B |
---|
| 337 | release B |
---|
| 338 | release A |
---|
| 339 | \end{pseudo} |
---|
| 340 | |
---|
| 341 | \columnbreak |
---|
| 342 | |
---|
| 343 | \begin{pseudo} |
---|
| 344 | |
---|
| 345 | acquire B |
---|
| 346 | signal B |
---|
| 347 | release B |
---|
| 348 | |
---|
| 349 | \end{pseudo} |
---|
| 350 | \end{multicols} |
---|
| 351 | |
---|
[b9d0fb6] | 352 | % ====================================================================== |
---|
| 353 | % ====================================================================== |
---|
| 354 | \subsection{Internal Scheduling - in depth} |
---|
| 355 | % ====================================================================== |
---|
| 356 | % ====================================================================== |
---|
| 357 | |
---|
| 358 | A larger example is presented to show complex issuesfor \gls{bulk-acq} and all the implementation options are analyzed. Listing \ref{lst:int-bulk-pseudo} shows an example where \gls{bulk-acq} adds a significant layer of complexity to the internal signalling semantics, and listing \ref{lst:int-bulk-cfa} shows the corresponding \CFA code which implements the pseudo-code in listing \ref{lst:int-bulk-pseudo}. For the purpose of translating the given pseudo-code into \CFA-code any method of introducing monitor into context, other than a \code{mutex} parameter, is acceptable, e.g., global variables, pointer parameters or using locals with the \code{mutex}-statement. |
---|
[27dde72] | 359 | |
---|
[fb31cb8] | 360 | \begin{figure}[!b] |
---|
[27dde72] | 361 | \begin{multicols}{2} |
---|
| 362 | Waiting thread |
---|
| 363 | \begin{pseudo}[numbers=left] |
---|
| 364 | acquire A |
---|
[3364962] | 365 | //Code Section 1 |
---|
[27dde72] | 366 | acquire A & B |
---|
[3364962] | 367 | //Code Section 2 |
---|
[27dde72] | 368 | wait A & B |
---|
[3364962] | 369 | //Code Section 3 |
---|
[27dde72] | 370 | release A & B |
---|
[3364962] | 371 | //Code Section 4 |
---|
[27dde72] | 372 | release A |
---|
| 373 | \end{pseudo} |
---|
| 374 | |
---|
| 375 | \columnbreak |
---|
| 376 | |
---|
| 377 | Signalling thread |
---|
| 378 | \begin{pseudo}[numbers=left, firstnumber=10] |
---|
| 379 | acquire A |
---|
[3364962] | 380 | //Code Section 5 |
---|
[27dde72] | 381 | acquire A & B |
---|
[3364962] | 382 | //Code Section 6 |
---|
[27dde72] | 383 | signal A & B |
---|
[3364962] | 384 | //Code Section 7 |
---|
[27dde72] | 385 | release A & B |
---|
[3364962] | 386 | //Code Section 8 |
---|
[27dde72] | 387 | release A |
---|
| 388 | \end{pseudo} |
---|
| 389 | \end{multicols} |
---|
[fb31cb8] | 390 | \caption{Internal scheduling with \gls{bulk-acq}} |
---|
| 391 | \label{lst:int-bulk-pseudo} |
---|
| 392 | \end{figure} |
---|
| 393 | |
---|
| 394 | \begin{figure}[!b] |
---|
[b9d0fb6] | 395 | \begin{center} |
---|
| 396 | \begin{cfacode}[xleftmargin=.4\textwidth] |
---|
| 397 | monitor A a; |
---|
| 398 | monitor B b; |
---|
| 399 | condition c; |
---|
| 400 | \end{cfacode} |
---|
| 401 | \end{center} |
---|
[fb31cb8] | 402 | \begin{multicols}{2} |
---|
| 403 | Waiting thread |
---|
| 404 | \begin{cfacode} |
---|
[b9d0fb6] | 405 | mutex(a) { |
---|
[fb31cb8] | 406 | //Code Section 1 |
---|
| 407 | mutex(a, b) { |
---|
| 408 | //Code Section 2 |
---|
| 409 | wait(c); |
---|
| 410 | //Code Section 3 |
---|
| 411 | } |
---|
| 412 | //Code Section 4 |
---|
| 413 | } |
---|
| 414 | \end{cfacode} |
---|
| 415 | |
---|
| 416 | \columnbreak |
---|
| 417 | |
---|
| 418 | Signalling thread |
---|
| 419 | \begin{cfacode} |
---|
[b9d0fb6] | 420 | mutex(a) { |
---|
[fb31cb8] | 421 | //Code Section 5 |
---|
| 422 | mutex(a, b) { |
---|
| 423 | //Code Section 6 |
---|
| 424 | signal(c); |
---|
| 425 | //Code Section 7 |
---|
| 426 | } |
---|
| 427 | //Code Section 8 |
---|
| 428 | } |
---|
| 429 | \end{cfacode} |
---|
| 430 | \end{multicols} |
---|
| 431 | \caption{Equivalent \CFA code for listing \ref{lst:int-bulk-pseudo}} |
---|
| 432 | \label{lst:int-bulk-cfa} |
---|
| 433 | \end{figure} |
---|
[27dde72] | 434 | |
---|
[b9d0fb6] | 435 | The complexity begins at code sections 4 and 8, which are where the existing semantics of internal scheduling need to be extended for multiple monitors. The root of the problem is that \gls{bulk-acq} is used in a context where one of the monitors is already acquired and is why it is important to define the behaviour of the previous pseudo-code. When the signaller thread reaches the location where it should ``release \code{A & B}'' (line 16), it must actually transfer ownership of monitor \code{B} to the waiting thread. This ownership trasnfer is required in order to prevent barging. Since the signalling thread still needs monitor \code{A}, simply waking up the waiting thread is not an option because it violates mutual exclusion. There are three options. |
---|
[27dde72] | 436 | |
---|
| 437 | \subsubsection{Delaying signals} |
---|
[b9d0fb6] | 438 | The 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 when the last lock is no longer needed because this semantics fits most closely to the behaviour of single-monitor scheduling. This solution has the main benefit of transferring ownership of groups of monitors, which simplifies the semantics from mutiple objects to a single group of objects, effectively making the existing single-monitor semantic viable by simply changing monitors to monitor groups. |
---|
[27dde72] | 439 | \begin{multicols}{2} |
---|
| 440 | Waiter |
---|
| 441 | \begin{pseudo}[numbers=left] |
---|
| 442 | acquire A |
---|
| 443 | acquire A & B |
---|
| 444 | wait A & B |
---|
| 445 | release A & B |
---|
| 446 | release A |
---|
| 447 | \end{pseudo} |
---|
| 448 | |
---|
| 449 | \columnbreak |
---|
| 450 | |
---|
| 451 | Signaller |
---|
| 452 | \begin{pseudo}[numbers=left, firstnumber=6] |
---|
| 453 | acquire A |
---|
| 454 | acquire A & B |
---|
| 455 | signal A & B |
---|
| 456 | release A & B |
---|
| 457 | //Secretly keep B here |
---|
| 458 | release A |
---|
| 459 | //Wakeup waiter and transfer A & B |
---|
| 460 | \end{pseudo} |
---|
| 461 | \end{multicols} |
---|
[3364962] | 462 | However, this solution can become much more complicated depending on what is executed while secretly holding B (at line 10). Indeed, nothing prevents signalling monitor A on a different condition variable: |
---|
[b9d0fb6] | 463 | \begin{figure} |
---|
| 464 | \begin{multicols}{3} |
---|
| 465 | Thread $\alpha$ |
---|
[ff98952] | 466 | \begin{pseudo}[numbers=left, firstnumber=1] |
---|
[27dde72] | 467 | acquire A |
---|
| 468 | acquire A & B |
---|
| 469 | wait A & B |
---|
| 470 | release A & B |
---|
| 471 | release A |
---|
| 472 | \end{pseudo} |
---|
| 473 | |
---|
| 474 | \columnbreak |
---|
| 475 | |
---|
[b9d0fb6] | 476 | Thread $\gamma$ |
---|
| 477 | \begin{pseudo}[numbers=left, firstnumber=1] |
---|
[27dde72] | 478 | acquire A |
---|
| 479 | acquire A & B |
---|
| 480 | signal A & B |
---|
| 481 | release A & B |
---|
| 482 | signal A |
---|
| 483 | release A |
---|
| 484 | \end{pseudo} |
---|
[b9d0fb6] | 485 | |
---|
| 486 | \columnbreak |
---|
| 487 | |
---|
| 488 | Thread $\beta$ |
---|
| 489 | \begin{pseudo}[numbers=left, firstnumber=1] |
---|
| 490 | acquire A |
---|
| 491 | wait A |
---|
| 492 | release A |
---|
| 493 | \end{pseudo} |
---|
| 494 | |
---|
[27dde72] | 495 | \end{multicols} |
---|
[b9d0fb6] | 496 | \caption{Dependency graph} |
---|
| 497 | \label{lst:dependency} |
---|
| 498 | \end{figure} |
---|
[27dde72] | 499 | |
---|
[e0a653d] | 500 | The goal in this solution is to avoid the need to transfer ownership of a subset of the condition monitors. However, this goal is unreacheable in the previous example. Depending on the order of signals (line 12 and 15) two cases can happen. |
---|
[ff98952] | 501 | |
---|
[e0a653d] | 502 | \paragraph{Case 1: thread 1 goes first.} In this case, the problem is that monitor A needs to be passed to thread 2 when thread 1 is done with it. |
---|
| 503 | \paragraph{Case 2: thread 2 goes first.} In this case, the problem is that monitor B needs to be passed to thread 1, which can be done directly or using thread 2 as an intermediate. |
---|
[ff98952] | 504 | \\ |
---|
| 505 | |
---|
[e0a653d] | 506 | Note that ordering is not determined by a race condition but by whether signalled threads are enqueued in FIFO or FILO order. However, regardless of the answer, users can move line 15 before line 11 and get the reverse effect. |
---|
| 507 | |
---|
[b9d0fb6] | 508 | In both cases, the threads need to be able to distinguish, on a per monitor basis, which ones need to be released and which ones need to be transferred, which means monitors cannot be handled as a single homogenous group and therefore effectively precludes this approach. |
---|
[27dde72] | 509 | |
---|
| 510 | \subsubsection{Dependency graphs} |
---|
[b9d0fb6] | 511 | In the listing \ref{lst:int-bulk-pseudo} pseudo-code, there is a solution which statisfies both barging prevention and mutual exclusion. If ownership of both monitors is transferred to the waiter when the signaller releases \code{A & B} and then the waiter transfers back ownership of \code{A} when it releases it, then the problem is solved (\code{B} is no longer in use at this point). Dynamically finding the correct order is therefore the second possible 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: |
---|
[27dde72] | 512 | |
---|
| 513 | \begin{multicols}{2} |
---|
| 514 | \begin{pseudo} |
---|
| 515 | acquire A |
---|
| 516 | acquire B |
---|
| 517 | acquire C |
---|
| 518 | wait A & B & C |
---|
| 519 | release C |
---|
| 520 | release B |
---|
| 521 | release A |
---|
| 522 | \end{pseudo} |
---|
| 523 | |
---|
| 524 | \columnbreak |
---|
| 525 | |
---|
| 526 | \begin{pseudo} |
---|
| 527 | acquire A |
---|
| 528 | acquire B |
---|
| 529 | acquire C |
---|
| 530 | signal A & B & C |
---|
| 531 | release C |
---|
| 532 | release B |
---|
| 533 | release A |
---|
| 534 | \end{pseudo} |
---|
| 535 | \end{multicols} |
---|
[3364962] | 536 | |
---|
| 537 | \begin{figure} |
---|
| 538 | \begin{center} |
---|
| 539 | \input{dependency} |
---|
| 540 | \end{center} |
---|
[fb31cb8] | 541 | \caption{Dependency graph of the statements in listing \ref{lst:dependency}} |
---|
[b9d0fb6] | 542 | \label{fig:dependency} |
---|
[3364962] | 543 | \end{figure} |
---|
| 544 | |
---|
[b9d0fb6] | 545 | Listing \ref{lst:dependency} is the three thread example rewritten for dependency graphs. Figure \ref{fig:dependency} shows the corresponding dependency graph that results, where every node is a statement of one of the three threads, and the arrows the dependency of that statement (e.g., $\alpha1$ must happen before $\alpha2$). The extra challenge is that this dependency graph is effectively post-mortem, but the runtime system needs to be able to build and solve these graphs as the dependency unfolds. Resolving dependency graph being a complex and expensive endeavour, this solution is not the preffered one. |
---|
[27dde72] | 546 | |
---|
[21a1efb] | 547 | \subsubsection{Partial signalling} \label{partial-sig} |
---|
[b9d0fb6] | 548 | Finally, the solution that is chosen for \CFA is to use partial signalling. Again using listing \ref{lst:int-bulk-pseudo}, 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 into only two actions, passing monitors to the next owner when they should be release and conditionally waking threads if all conditions are met. This solution has a much simpler implementation than a dependency graph solving algorithm which is why it was chosen. Furthermore, after being fully implemented, this solution does not appear to have any downsides worth mentionning. |
---|
[27dde72] | 549 | |
---|
| 550 | % ====================================================================== |
---|
| 551 | % ====================================================================== |
---|
| 552 | \subsection{Signalling: Now or Later} |
---|
| 553 | % ====================================================================== |
---|
| 554 | % ====================================================================== |
---|
[3364962] | 555 | \begin{figure} |
---|
[27dde72] | 556 | \begin{tabular}{|c|c|} |
---|
| 557 | \code{signal} & \code{signal_block} \\ |
---|
| 558 | \hline |
---|
[3364962] | 559 | \begin{cfacode}[tabsize=3] |
---|
| 560 | monitor DatingService |
---|
| 561 | { |
---|
| 562 | //compatibility codes |
---|
| 563 | enum{ CCodes = 20 }; |
---|
[27dde72] | 564 | |
---|
[3364962] | 565 | int girlPhoneNo |
---|
| 566 | int boyPhoneNo; |
---|
| 567 | }; |
---|
[27dde72] | 568 | |
---|
[3364962] | 569 | condition girls[CCodes]; |
---|
| 570 | condition boys [CCodes]; |
---|
| 571 | condition exchange; |
---|
[27dde72] | 572 | |
---|
[3364962] | 573 | int girl(int phoneNo, int ccode) |
---|
| 574 | { |
---|
| 575 | //no compatible boy ? |
---|
| 576 | if(empty(boys[ccode])) |
---|
| 577 | { |
---|
| 578 | //wait for boy |
---|
| 579 | wait(girls[ccode]); |
---|
[27dde72] | 580 | |
---|
[3364962] | 581 | //make phone number available |
---|
| 582 | girlPhoneNo = phoneNo; |
---|
[27dde72] | 583 | |
---|
[b9d0fb6] | 584 | //wake boy from chair |
---|
[3364962] | 585 | signal(exchange); |
---|
| 586 | } |
---|
| 587 | else |
---|
| 588 | { |
---|
| 589 | //make phone number available |
---|
| 590 | girlPhoneNo = phoneNo; |
---|
[27dde72] | 591 | |
---|
[3364962] | 592 | //wake boy |
---|
| 593 | signal(boys[ccode]); |
---|
[27dde72] | 594 | |
---|
[3364962] | 595 | //sit in chair |
---|
| 596 | wait(exchange); |
---|
| 597 | } |
---|
| 598 | return boyPhoneNo; |
---|
[27dde72] | 599 | } |
---|
| 600 | |
---|
[3364962] | 601 | int boy(int phoneNo, int ccode) |
---|
| 602 | { |
---|
| 603 | //same as above |
---|
| 604 | //with boy/girl interchanged |
---|
[27dde72] | 605 | } |
---|
[3364962] | 606 | \end{cfacode}&\begin{cfacode}[tabsize=3] |
---|
| 607 | monitor DatingService |
---|
| 608 | { |
---|
| 609 | //compatibility codes |
---|
| 610 | enum{ CCodes = 20 }; |
---|
| 611 | |
---|
| 612 | int girlPhoneNo; |
---|
| 613 | int boyPhoneNo; |
---|
| 614 | }; |
---|
[21a1efb] | 615 | |
---|
[3364962] | 616 | condition girls[CCodes]; |
---|
| 617 | condition boys [CCodes]; |
---|
| 618 | //exchange is not needed |
---|
[21a1efb] | 619 | |
---|
[3364962] | 620 | int girl(int phoneNo, int ccode) |
---|
| 621 | { |
---|
| 622 | //no compatible boy ? |
---|
| 623 | if(empty(boys[ccode])) |
---|
| 624 | { |
---|
| 625 | //wait for boy |
---|
| 626 | wait(girls[ccode]); |
---|
[21a1efb] | 627 | |
---|
[3364962] | 628 | //make phone number available |
---|
| 629 | girlPhoneNo = phoneNo; |
---|
[21a1efb] | 630 | |
---|
[b9d0fb6] | 631 | //wake boy from chair |
---|
[3364962] | 632 | signal(exchange); |
---|
| 633 | } |
---|
| 634 | else |
---|
| 635 | { |
---|
| 636 | //make phone number available |
---|
| 637 | girlPhoneNo = phoneNo; |
---|
[21a1efb] | 638 | |
---|
[3364962] | 639 | //wake boy |
---|
| 640 | signal_block(boys[ccode]); |
---|
[21a1efb] | 641 | |
---|
[3364962] | 642 | //second handshake unnecessary |
---|
[21a1efb] | 643 | |
---|
[3364962] | 644 | } |
---|
| 645 | return boyPhoneNo; |
---|
| 646 | } |
---|
[21a1efb] | 647 | |
---|
[3364962] | 648 | int boy(int phoneNo, int ccode) |
---|
| 649 | { |
---|
| 650 | //same as above |
---|
| 651 | //with boy/girl interchanged |
---|
| 652 | } |
---|
| 653 | \end{cfacode} |
---|
| 654 | \end{tabular} |
---|
| 655 | \caption{Dating service example using \code{signal} and \code{signal_block}. } |
---|
| 656 | \label{lst:datingservice} |
---|
| 657 | \end{figure} |
---|
[b9d0fb6] | 658 | An important note is that, until now, signalling a monitor was 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. However, in some cases, it may be more convenient for users to immediately transfer ownership to the thread that is waiting for cooperation, which is achieved using the \code{signal_block} routine\footnote{name to be discussed}. |
---|
| 659 | |
---|
| 660 | The example in listing \ref{lst:datingservice} highlights the difference in behaviour. As mentioned, \code{signal} only transfers ownership once the current critical section exits, this behaviour requires additional synchronisation when a two-way handshake is needed. To avoid this extraneous synchronisation, the \code{condition} type offers the \code{signal_block} routine, which handles the two-way handshake as shown in the example. This removes the need for a second condition variables and simplifies programming. Like every other monitor semantic, \code{signal_block} uses barging prevention, which means mutual-exclusion is baton-passed both on the frond-end and the back-end of the call to \code{signal_block}, meaning no other thread can acquire the monitor neither before nor after the call. |
---|
[27dde72] | 661 | |
---|
| 662 | % ====================================================================== |
---|
| 663 | % ====================================================================== |
---|
| 664 | \section{External scheduling} \label{extsched} |
---|
| 665 | % ====================================================================== |
---|
| 666 | % ====================================================================== |
---|
[b9d0fb6] | 667 | An alternative to internal scheduling is external scheduling, e.g., in \uC. |
---|
[27dde72] | 668 | \begin{center} |
---|
[b9d0fb6] | 669 | \begin{tabular}{|c|c|c|} |
---|
| 670 | Internal Scheduling & External Scheduling & Go\\ |
---|
[27dde72] | 671 | \hline |
---|
[b9d0fb6] | 672 | \begin{ucppcode}[tabsize=3] |
---|
[27dde72] | 673 | _Monitor Semaphore { |
---|
| 674 | condition c; |
---|
| 675 | bool inUse; |
---|
| 676 | public: |
---|
[e0a653d] | 677 | void P() { |
---|
[b9d0fb6] | 678 | if(inUse) |
---|
| 679 | wait(c); |
---|
[27dde72] | 680 | inUse = true; |
---|
| 681 | } |
---|
[e0a653d] | 682 | void V() { |
---|
| 683 | inUse = false; |
---|
| 684 | signal(c); |
---|
[27dde72] | 685 | } |
---|
| 686 | } |
---|
[b9d0fb6] | 687 | \end{ucppcode}&\begin{ucppcode}[tabsize=3] |
---|
[27dde72] | 688 | _Monitor Semaphore { |
---|
| 689 | |
---|
| 690 | bool inUse; |
---|
| 691 | public: |
---|
[e0a653d] | 692 | void P() { |
---|
[b9d0fb6] | 693 | if(inUse) |
---|
| 694 | _Accept(V); |
---|
[27dde72] | 695 | inUse = true; |
---|
| 696 | } |
---|
[21a1efb] | 697 | void V() { |
---|
[27dde72] | 698 | inUse = false; |
---|
| 699 | |
---|
| 700 | } |
---|
| 701 | } |
---|
[b9d0fb6] | 702 | \end{ucppcode}&\begin{gocode}[tabsize=3] |
---|
| 703 | type MySem struct { |
---|
| 704 | inUse bool |
---|
| 705 | c chan bool |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | // acquire |
---|
| 709 | func (s MySem) P() { |
---|
| 710 | if s.inUse { |
---|
| 711 | select { |
---|
| 712 | case <-s.c: |
---|
| 713 | } |
---|
| 714 | } |
---|
| 715 | s.inUse = true |
---|
| 716 | } |
---|
| 717 | |
---|
| 718 | // release |
---|
| 719 | func (s MySem) V() { |
---|
| 720 | s.inUse = false |
---|
| 721 | |
---|
| 722 | //This actually deadlocks |
---|
| 723 | //when single thread |
---|
| 724 | s.c <- false |
---|
| 725 | } |
---|
| 726 | \end{gocode} |
---|
[27dde72] | 727 | \end{tabular} |
---|
| 728 | \end{center} |
---|
[b9d0fb6] | 729 | This method is more constrained and explicit, which helps 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 (e.g., \uC with \code{_Accept}) or in terms of data (e.g., Go with channels). Of course, both of these paradigms have their own strenghts and weaknesses but for this project control-flow semantics were 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 previous example shows a simple use \code{_Accept} versus \code{wait}/\code{signal} and its advantages. Note that while other languages often use \code{accept}/\code{select} as the core external scheduling keyword, \CFA uses \code{waitfor} to prevent name collisions with existing socket \acrshort{api}s. |
---|
[27dde72] | 730 | |
---|
[b9d0fb6] | 731 | For the \code{P} member above using internal scheduling, the call to \code{wait} only guarantees that \code{V} is the last routine to access the monitor, allowing a third routine, say \code{isInUse()}, acquire mutual exclusion several times while routine \code{P} is waiting. On the other hand, external scheduling guarantees that while routine \code{P} is waiting, no routine other than \code{V} can acquire the monitor. |
---|
[27dde72] | 732 | |
---|
| 733 | % ====================================================================== |
---|
| 734 | % ====================================================================== |
---|
| 735 | \subsection{Loose object definitions} |
---|
| 736 | % ====================================================================== |
---|
| 737 | % ====================================================================== |
---|
[b9d0fb6] | 738 | In \uC, monitor declarations include an exhaustive list of monitor operations. Since \CFA is not object oriented, monitors become both more difficult to implement and less clear for a user: |
---|
[27dde72] | 739 | |
---|
| 740 | \begin{cfacode} |
---|
[fb31cb8] | 741 | monitor A {}; |
---|
[27dde72] | 742 | |
---|
[fb31cb8] | 743 | void f(A & mutex a); |
---|
| 744 | void g(A & mutex a) { |
---|
| 745 | waitfor(f); //Obvious which f() to wait for |
---|
| 746 | } |
---|
[3364962] | 747 | |
---|
[fb31cb8] | 748 | void f(A & mutex a, int); //New different F added in scope |
---|
| 749 | void h(A & mutex a) { |
---|
| 750 | waitfor(f); //Less obvious which f() to wait for |
---|
| 751 | } |
---|
[27dde72] | 752 | \end{cfacode} |
---|
| 753 | |
---|
[21a1efb] | 754 | Furthermore, external scheduling is an example where implementation constraints become visible from the interface. Indeed, since 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: |
---|
[27dde72] | 755 | |
---|
| 756 | \begin{center} |
---|
| 757 | \begin{tabular}{l} |
---|
| 758 | \begin{pseudo} |
---|
| 759 | if monitor is free |
---|
| 760 | enter |
---|
[3364962] | 761 | elif already own the monitor |
---|
[21a1efb] | 762 | continue |
---|
[27dde72] | 763 | elif monitor accepts me |
---|
| 764 | enter |
---|
| 765 | else |
---|
| 766 | block |
---|
| 767 | \end{pseudo} |
---|
| 768 | \end{tabular} |
---|
| 769 | \end{center} |
---|
| 770 | |
---|
[3364962] | 771 | For the first two conditions, 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: |
---|
[27dde72] | 772 | |
---|
| 773 | \begin{center} |
---|
| 774 | {\resizebox{0.4\textwidth}{!}{\input{monitor}}} |
---|
| 775 | \end{center} |
---|
| 776 | |
---|
[b9d0fb6] | 777 | There are other alternatives to these pictures, but in the case of this picture, implementing a fast accept check is relatively easy. Restricted to a fixed number of mutex members, N, the accept check reduces to updating a bitmask when the acceptor queue changes, a check that executes in a single instruction even with a fairly large number (e.g., 128) of mutex members. This technique cannot be used in \CFA because it relies on the fact that the monitor type enumerates (declares) all the acceptable routines. For OO languages this does not compromise much since monitors already have an exhaustive list of member routines. However, for \CFA this is not the case; routines can be added to a type anywhere after its declaration. It is 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. |
---|
| 778 | The alternative is to alter the implementeation like this: |
---|
[27dde72] | 779 | |
---|
| 780 | \begin{center} |
---|
| 781 | {\resizebox{0.4\textwidth}{!}{\input{ext_monitor}}} |
---|
| 782 | \end{center} |
---|
| 783 | |
---|
[b9d0fb6] | 784 | Generating a mask dynamically means that the storage for the mask information can vary between calls to \code{waitfor}, allowing for more flexibility and extensions. Storing an array of accepted function-pointers replaces the single instruction bitmask compare with dereferencing a pointer followed by a linear search. Furthermore, supporting nested external scheduling (e.g., listing \ref{lst:nest-ext}) may now require additionnal searches on calls to \code{waitfor} statement to check if a routine is already queued in. |
---|
[27dde72] | 785 | |
---|
[b9d0fb6] | 786 | \begin{figure} |
---|
| 787 | \begin{cfacode} |
---|
| 788 | monitor M {}; |
---|
| 789 | void foo( M & mutex a ) {} |
---|
| 790 | void bar( M & mutex b ) { |
---|
| 791 | //Nested in the waitfor(bar, c) call |
---|
| 792 | waitfor(foo, b); |
---|
| 793 | } |
---|
| 794 | void baz( M & mutex c ) { |
---|
| 795 | waitfor(bar, c); |
---|
| 796 | } |
---|
[27dde72] | 797 | |
---|
[b9d0fb6] | 798 | \end{cfacode} |
---|
| 799 | \caption{Example of nested external scheduling} |
---|
| 800 | \label{lst:nest-ext} |
---|
| 801 | \end{figure} |
---|
| 802 | |
---|
| 803 | Note that in the second picture, tasks need to always keep track of which routine they are attempting to acquire the monitor and the routine mask needs to have both a function pointer and a set of monitors, as will be discussed in the next section. These details where omitted from the picture for the sake of simplifying the representation. |
---|
| 804 | |
---|
| 805 | At this point, a decision must be made 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. In the end, the most flexible approach has been chosen since it allows users to write programs that would otherwise be prohibitively hard to write. This decision is based on the assumption that writing fast but inflexible locks is closer to a solved problems than writing locks that are as flexible as external scheduling in \CFA. |
---|
[27dde72] | 806 | |
---|
| 807 | % ====================================================================== |
---|
| 808 | % ====================================================================== |
---|
| 809 | \subsection{Multi-monitor scheduling} |
---|
| 810 | % ====================================================================== |
---|
| 811 | % ====================================================================== |
---|
| 812 | |
---|
[3364962] | 813 | External scheduling, like internal scheduling, becomes significantly more complex when introducing multi-monitor syntax. Even in the simplest possible case, some new semantics need to be established: |
---|
[27dde72] | 814 | \begin{cfacode} |
---|
[fb31cb8] | 815 | monitor M {}; |
---|
[27dde72] | 816 | |
---|
[fb31cb8] | 817 | void f(M & mutex a); |
---|
[27dde72] | 818 | |
---|
[b9d0fb6] | 819 | void g(M & mutex b, M & mutex c) { |
---|
| 820 | waitfor(f); //two monitors M => unkown which to pass to f(M & mutex) |
---|
[fb31cb8] | 821 | } |
---|
[27dde72] | 822 | \end{cfacode} |
---|
| 823 | |
---|
| 824 | The obvious solution is to specify the correct monitor as follows: |
---|
| 825 | |
---|
| 826 | \begin{cfacode} |
---|
[fb31cb8] | 827 | monitor M {}; |
---|
[27dde72] | 828 | |
---|
[fb31cb8] | 829 | void f(M & mutex a); |
---|
[27dde72] | 830 | |
---|
[fb31cb8] | 831 | void g(M & mutex a, M & mutex b) { |
---|
| 832 | waitfor( f, b ); |
---|
| 833 | } |
---|
[27dde72] | 834 | \end{cfacode} |
---|
| 835 | |
---|
[b9d0fb6] | 836 | This syntax is unambiguous. Both locks are acquired and kept by \code{g}. When routine \code{f} is called, the lock for monitor \code{b} is temporarily transferred from \code{g} to \code{f} (while \code{g} still holds lock \code{a}). This behavior can be extended to multi-monitor \code{waitfor} statement as follows. |
---|
[27dde72] | 837 | |
---|
| 838 | \begin{cfacode} |
---|
[fb31cb8] | 839 | monitor M {}; |
---|
[27dde72] | 840 | |
---|
[fb31cb8] | 841 | void f(M & mutex a, M & mutex b); |
---|
[27dde72] | 842 | |
---|
[fb31cb8] | 843 | void g(M & mutex a, M & mutex b) { |
---|
| 844 | waitfor( f, a, b); |
---|
| 845 | } |
---|
[21a1efb] | 846 | \end{cfacode} |
---|
| 847 | |
---|
[3364962] | 848 | Note that the set of monitors passed to the \code{waitfor} statement must be entirely contained in the set of monitors already acquired in the routine. \code{waitfor} used in any other context is Undefined Behaviour. |
---|
[21a1efb] | 849 | |
---|
[b9d0fb6] | 850 | An important behavior to note is when a set of monitors only match partially : |
---|
[21a1efb] | 851 | |
---|
| 852 | \begin{cfacode} |
---|
[fb31cb8] | 853 | mutex struct A {}; |
---|
[21a1efb] | 854 | |
---|
[fb31cb8] | 855 | mutex struct B {}; |
---|
[21a1efb] | 856 | |
---|
[fb31cb8] | 857 | void g(A & mutex a, B & mutex b) { |
---|
| 858 | waitfor(f, a, b); |
---|
| 859 | } |
---|
[21a1efb] | 860 | |
---|
[fb31cb8] | 861 | A a1, a2; |
---|
| 862 | B b; |
---|
[21a1efb] | 863 | |
---|
[fb31cb8] | 864 | void foo() { |
---|
| 865 | g(a1, b); //block on accept |
---|
| 866 | } |
---|
[21a1efb] | 867 | |
---|
[fb31cb8] | 868 | void bar() { |
---|
| 869 | f(a2, b); //fufill cooperation |
---|
| 870 | } |
---|
[27dde72] | 871 | \end{cfacode} |
---|
| 872 | |
---|
[b9d0fb6] | 873 | While the equivalent can happen when using internal scheduling, the fact that conditions are specific to a set of monitors means that users have to use two different condition variables. In both cases, partially matching monitor sets does not wake-up the waiting thread. It is also important to note that in the case of external scheduling, as for routine calls, the order of parameters is irrelevant; \code{waitfor(f,a,b)} and \code{waitfor(f,b,a)} are indistinguishable waiting condition. |
---|
[27dde72] | 874 | |
---|
| 875 | % ====================================================================== |
---|
| 876 | % ====================================================================== |
---|
[fb31cb8] | 877 | \subsection{\code{waitfor} semantics} |
---|
| 878 | % ====================================================================== |
---|
[27dde72] | 879 | % ====================================================================== |
---|
[fb31cb8] | 880 | |
---|
[b9d0fb6] | 881 | Syntactically, the \code{waitfor} statement takes a function identifier and a set of monitors. While the set of monitors can be any list of expression, the function name is more restricted because the compiler validates at compile time the validity of the function type and the parameters used with the \code{waitfor} statement. It checks that the set of monitor passed in matches the requirements for a function call. Listing \ref{lst:waitfor} shows various usage of the waitfor statement and which are acceptable. The choice of the function type is made ignoring any non-\code{mutex} parameter. One limitation of the current implementation is that it does not handle overloading. |
---|
[fb31cb8] | 882 | \begin{figure} |
---|
| 883 | \begin{cfacode} |
---|
| 884 | monitor A{}; |
---|
| 885 | monitor B{}; |
---|
| 886 | |
---|
| 887 | void f1( A & mutex ); |
---|
| 888 | void f2( A & mutex, B & mutex ); |
---|
| 889 | void f3( A & mutex, int ); |
---|
| 890 | void f4( A & mutex, int ); |
---|
| 891 | void f4( A & mutex, double ); |
---|
| 892 | |
---|
| 893 | void foo( A & mutex a1, A & mutex a2, B & mutex b1, B & b2 ) { |
---|
| 894 | A * ap = & a1; |
---|
| 895 | void (*fp)( A & mutex ) = f1; |
---|
| 896 | |
---|
| 897 | waitfor(f1, a1); //Correct : 1 monitor case |
---|
| 898 | waitfor(f2, a1, b1); //Correct : 2 monitor case |
---|
| 899 | waitfor(f3, a1); //Correct : non-mutex arguments are ignored |
---|
| 900 | waitfor(f1, *ap); //Correct : expression as argument |
---|
| 901 | |
---|
| 902 | waitfor(f1, a1, b1); //Incorrect : Too many mutex arguments |
---|
| 903 | waitfor(f2, a1); //Incorrect : Too few mutex arguments |
---|
| 904 | waitfor(f2, a1, a2); //Incorrect : Mutex arguments don't match |
---|
| 905 | waitfor(f1, 1); //Incorrect : 1 not a mutex argument |
---|
[b9d0fb6] | 906 | waitfor(f9, a1); //Incorrect : f9 function does not exist |
---|
| 907 | waitfor(*fp, a1 ); //Incorrect : fp not an identifier |
---|
[fb31cb8] | 908 | waitfor(f4, a1); //Incorrect : f4 ambiguous |
---|
| 909 | |
---|
| 910 | waitfor(f2, a1, b2); //Undefined Behaviour : b2 may not acquired |
---|
| 911 | } |
---|
| 912 | \end{cfacode} |
---|
| 913 | \caption{Various correct and incorrect uses of the waitfor statement} |
---|
| 914 | \label{lst:waitfor} |
---|
| 915 | \end{figure} |
---|
| 916 | |
---|
[b9d0fb6] | 917 | Finally, for added flexibility, \CFA supports constructing complex \code{waitfor} mask using the \code{or}, \code{timeout} and \code{else}. Indeed, multiple \code{waitfor} can be chained together using \code{or}; this chain forms a single statement that uses baton-pass to any one function that fits one of the function+monitor set passed in. To eanble users to tell which accepted function is accepted, \code{waitfor}s are followed by a statement (including the null statement \code{;}) or a compound statement. When multiple \code{waitfor} are chained together, only the statement corresponding to the accepted function is executed. A \code{waitfor} chain can also be followed by a \code{timeout}, to signify an upper bound on the wait, or an \code{else}, to signify that the call should be non-blocking, that is only check of a matching function call already arrived and return immediately otherwise. Any and all of these clauses can be preceded by a \code{when} condition to dynamically construct the mask based on some current state. Listing \ref{lst:waitfor2}, demonstrates several complex masks and some incorrect ones. |
---|
[fb31cb8] | 918 | |
---|
| 919 | \begin{figure} |
---|
| 920 | \begin{cfacode} |
---|
| 921 | monitor A{}; |
---|
| 922 | |
---|
| 923 | void f1( A & mutex ); |
---|
| 924 | void f2( A & mutex ); |
---|
| 925 | |
---|
| 926 | void foo( A & mutex a, bool b, int t ) { |
---|
| 927 | //Correct : blocking case |
---|
| 928 | waitfor(f1, a); |
---|
| 929 | |
---|
| 930 | //Correct : block with statement |
---|
| 931 | waitfor(f1, a) { |
---|
| 932 | sout | "f1" | endl; |
---|
| 933 | } |
---|
| 934 | |
---|
| 935 | //Correct : block waiting for f1 or f2 |
---|
| 936 | waitfor(f1, a) { |
---|
| 937 | sout | "f1" | endl; |
---|
| 938 | } or waitfor(f2, a) { |
---|
| 939 | sout | "f2" | endl; |
---|
| 940 | } |
---|
| 941 | |
---|
| 942 | //Correct : non-blocking case |
---|
| 943 | waitfor(f1, a); or else; |
---|
| 944 | |
---|
| 945 | //Correct : non-blocking case |
---|
| 946 | waitfor(f1, a) { |
---|
| 947 | sout | "blocked" | endl; |
---|
| 948 | } or else { |
---|
| 949 | sout | "didn't block" | endl; |
---|
| 950 | } |
---|
| 951 | |
---|
| 952 | //Correct : block at most 10 seconds |
---|
| 953 | waitfor(f1, a) { |
---|
| 954 | sout | "blocked" | endl; |
---|
| 955 | } or timeout( 10`s) { |
---|
| 956 | sout | "didn't block" | endl; |
---|
| 957 | } |
---|
| 958 | |
---|
| 959 | //Correct : block only if b == true |
---|
| 960 | //if b == false, don't even make the call |
---|
| 961 | when(b) waitfor(f1, a); |
---|
| 962 | |
---|
| 963 | //Correct : block only if b == true |
---|
| 964 | //if b == false, make non-blocking call |
---|
| 965 | waitfor(f1, a); or when(!b) else; |
---|
| 966 | |
---|
| 967 | //Correct : block only of t > 1 |
---|
| 968 | waitfor(f1, a); or when(t > 1) timeout(t); or else; |
---|
| 969 | |
---|
| 970 | //Incorrect : timeout clause is dead code |
---|
| 971 | waitfor(f1, a); or timeout(t); or else; |
---|
| 972 | |
---|
| 973 | //Incorrect : order must be |
---|
| 974 | //waitfor [or waitfor... [or timeout] [or else]] |
---|
| 975 | timeout(t); or waitfor(f1, a); or else; |
---|
| 976 | } |
---|
| 977 | \end{cfacode} |
---|
| 978 | \caption{Various correct and incorrect uses of the or, else, and timeout clause around a waitfor statement} |
---|
| 979 | \label{lst:waitfor2} |
---|
[64b272a] | 980 | \end{figure} |
---|
| 981 | |
---|
| 982 | % ====================================================================== |
---|
| 983 | % ====================================================================== |
---|
| 984 | \subsection{Waiting for the destructor} |
---|
| 985 | % ====================================================================== |
---|
| 986 | % ====================================================================== |
---|
[b9d0fb6] | 987 | An interesting use for the \code{waitfor} statement is destructor semantics. Indeed, the \code{waitfor} statement can accept any \code{mutex} routine, which includes the destructor (see section \ref{data}). However, with the semantics discussed until now, waiting for the destructor does not make any sense since using an object after its destructor is called is undefined behaviour. The simplest approach is to disallow \code{waitfor} on a destructor. However, a more expressive approach is to flip execution ordering when waiting for the destructor, meaning that waiting for the destructor allows the destructor to run after the current \code{mutex} routine, similarly to how a condition is signalled. |
---|
[64b272a] | 988 | \begin{figure} |
---|
| 989 | \begin{cfacode} |
---|
| 990 | monitor Executer {}; |
---|
| 991 | struct Action; |
---|
| 992 | |
---|
| 993 | void ^?{} (Executer & mutex this); |
---|
| 994 | void execute(Executer & mutex this, const Action & ); |
---|
| 995 | void run (Executer & mutex this) { |
---|
| 996 | while(true) { |
---|
| 997 | waitfor(execute, this); |
---|
| 998 | or waitfor(^?{} , this) { |
---|
| 999 | break; |
---|
| 1000 | } |
---|
| 1001 | } |
---|
| 1002 | } |
---|
| 1003 | \end{cfacode} |
---|
| 1004 | \caption{Example of an executor which executes action in series until the destructor is called.} |
---|
| 1005 | \label{lst:dtor-order} |
---|
| 1006 | \end{figure} |
---|
[b9d0fb6] | 1007 | For example, listing \ref{lst:dtor-order} shows an example of an executor with an infinite loop, which waits for the destructor to break out of this loop. Switching the semantic meaning introduces an idiomatic way to terminate a task and/or wait for its termination via destruction. |
---|