[27dde72] | 1 | % ====================================================================== |
---|
| 2 | % ====================================================================== |
---|
| 3 | \chapter{Concurrency} |
---|
| 4 | % ====================================================================== |
---|
| 5 | % ====================================================================== |
---|
[9f10d1f2] | 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 library still has to take both paradigms into account. |
---|
[7c17511] | 7 | |
---|
[07c1e595] | 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 desirable to have a higher-level construct be the core concurrency paradigm~\cite{HPP:Study}. |
---|
[7c17511] | 9 | |
---|
[cf966b5] | 10 | An approach that is worth mentioning because it is gaining in popularity is transactional memory~\cite{Herlihy93}. While this approach is even pursued by system languages like \CC~\cite{Cpp-Transactions}, 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} |
---|
[07c1e595] | 15 | Non-determinism requires concurrent systems to offer support for mutual-exclusion and synchronization. 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} |
---|
[07c1e595] | 18 | As mentioned 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 organizing 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} |
---|
[9f10d1f2] | 21 | As for mutual-exclusion, low-level synchronization 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 a 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, synchronization 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 is 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 example is the thread that finishes using a resource 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 flag variables to detect barging threads are said to be using barging avoidance, while algorithms that baton-pass locks~\cite{Andrews89} 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; |
---|
[9f10d1f2] | 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 | % ====================================================================== |
---|
[9f10d1f2] | 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-copy-able objects (\code{dtype}). |
---|
[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} |
---|
[07c1e595] | 73 | Notice how the counter is used without any explicit synchronization and yet supports thread-safe semantics for both reading and writing, which is similar in usage to \CC \code{atomic} template. |
---|
[27dde72] | 74 | |
---|
[cf966b5] | 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. Furthermore, it allows the implementation greater freedom when it initializes the monitor locking. 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 | |
---|
[cf966b5] | 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, listing \ref{fig:search} uses recursion and \gls{multi-acq} to print values inside a binary tree. |
---|
[3364962] | 78 | \begin{figure} |
---|
[cf966b5] | 79 | \begin{cfacode}[caption={Recursive printing algorithm using \gls{multi-acq}.},label={fig:search}] |
---|
[3364962] | 80 | monitor printer { ... }; |
---|
| 81 | struct tree { |
---|
| 82 | tree * left, right; |
---|
| 83 | char * value; |
---|
| 84 | }; |
---|
| 85 | void print(printer & mutex p, char * v); |
---|
[27dde72] | 86 | |
---|
[3364962] | 87 | void print(printer & mutex p, tree * t) { |
---|
| 88 | print(p, t->value); |
---|
| 89 | print(p, t->left ); |
---|
| 90 | print(p, t->right); |
---|
| 91 | } |
---|
| 92 | \end{cfacode} |
---|
| 93 | \end{figure} |
---|
| 94 | |
---|
[cf966b5] | 95 | Having both \code{mutex} and \code{nomutex} keywords is redundant based on the meaning of a routine having neither of these keywords. For example, it is reasonable that it should default to the safest option (\code{mutex}) when given a routine without qualifiers \code{void foo(counter_t & this)}, whereas assuming \code{nomutex} is unsafe and may cause subtle errors. On the other hand, \code{nomutex} is the ``normal'' parameter behaviour, it effectively states 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] | 96 | |
---|
| 97 | The next semantic decision is to establish when \code{mutex} may be used as a type qualifier. Consider the following declarations: |
---|
| 98 | \begin{cfacode} |
---|
[9f10d1f2] | 99 | int f1(monitor& mutex m); |
---|
[27dde72] | 100 | int f2(const monitor & mutex m); |
---|
[9f10d1f2] | 101 | int f3(monitor** mutex m); |
---|
| 102 | int f4(monitor* mutex m []); |
---|
[27dde72] | 103 | int f5(graph(monitor*) & mutex m); |
---|
| 104 | \end{cfacode} |
---|
[9f10d1f2] | 105 | The problem is to identify 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 identify 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 behaviour 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] | 106 | \begin{cfacode} |
---|
[9f10d1f2] | 107 | int f1(monitor& mutex m); //Okay : recommended case |
---|
| 108 | int f2(monitor* mutex m); //Okay : could be an array but probably not |
---|
[07c1e595] | 109 | int f3(monitor mutex m []); //Not Okay : Array of unknown length |
---|
[9f10d1f2] | 110 | int f4(monitor** mutex m); //Not Okay : Could be an array |
---|
| 111 | int f5(monitor* mutex m []); //Not Okay : Array of unknown length |
---|
[27dde72] | 112 | \end{cfacode} |
---|
[b9d0fb6] | 113 | 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] | 114 | |
---|
[b9d0fb6] | 115 | 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] | 116 | \begin{cfacode} |
---|
| 117 | int f(MonitorA & mutex a, MonitorB & mutex b); |
---|
| 118 | |
---|
| 119 | MonitorA a; |
---|
| 120 | MonitorB b; |
---|
| 121 | f(a,b); |
---|
| 122 | \end{cfacode} |
---|
[9f10d1f2] | 123 | While OO monitors could be extended with a mutex qualifier for multiple-monitor calls, no example of this feature could be found. The capability 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 acquisition is consistent across calls to different routines using the same monitors as arguments. This consistent ordering means acquiring multiple monitors is safe from deadlock when using \gls{bulk-acq}. However, users can still force the acquiring order. For example, notice which routines use \code{mutex}/\code{nomutex} and how this affects acquiring order: |
---|
[27dde72] | 124 | \begin{cfacode} |
---|
[9f10d1f2] | 125 | void foo(A& mutex a, B& mutex b) { //acquire a & b |
---|
[fb31cb8] | 126 | ... |
---|
| 127 | } |
---|
[27dde72] | 128 | |
---|
[9f10d1f2] | 129 | void bar(A& mutex a, B& /*nomutex*/ b) { //acquire a |
---|
[fb31cb8] | 130 | ... foo(a, b); ... //acquire b |
---|
| 131 | } |
---|
[27dde72] | 132 | |
---|
[9f10d1f2] | 133 | void baz(A& /*nomutex*/ a, B& mutex b) { //acquire b |
---|
[fb31cb8] | 134 | ... foo(a, b); ... //acquire a |
---|
| 135 | } |
---|
[27dde72] | 136 | \end{cfacode} |
---|
[3364962] | 137 | 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] | 138 | |
---|
[9f10d1f2] | 139 | 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 difference means that calling these routines concurrently may lead to deadlock and is therefore Undefined Behavior. As shown~\cite{Lister77}, solving this problem requires: |
---|
[27dde72] | 140 | \begin{enumerate} |
---|
| 141 | \item Dynamically tracking of the monitor-call order. |
---|
| 142 | \item Implement rollback semantics. |
---|
| 143 | \end{enumerate} |
---|
[cf966b5] | 144 | While the first requirement is already a significant constraint on the system, implementing a general rollback semantics in a C-like language is still prohibitively complex~\cite{Dice10}. In \CFA, users simply need to be careful 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, most systems provide no solution and the \CFA partial solution handles many useful cases. |
---|
[27dde72] | 145 | |
---|
[b9d0fb6] | 146 | For example, \gls{multi-acq} and \gls{bulk-acq} can be used together in interesting ways: |
---|
[27dde72] | 147 | \begin{cfacode} |
---|
[3364962] | 148 | monitor bank { ... }; |
---|
[27dde72] | 149 | |
---|
[3364962] | 150 | void deposit( bank & mutex b, int deposit ); |
---|
[27dde72] | 151 | |
---|
| 152 | void transfer( bank & mutex mybank, bank & mutex yourbank, int me2you) { |
---|
| 153 | deposit( mybank, -me2you ); |
---|
| 154 | deposit( yourbank, me2you ); |
---|
| 155 | } |
---|
| 156 | \end{cfacode} |
---|
[9f10d1f2] | 157 | This example shows a trivial solution to the bank-account transfer-problem~\cite{BankTransfer}. Without \gls{multi-acq} and \gls{bulk-acq}, the solution to this problem is much more involved and requires careful engineering. |
---|
[27dde72] | 158 | |
---|
[b9d0fb6] | 159 | \subsection{\code{mutex} statement} \label{mutex-stmt} |
---|
[fb31cb8] | 160 | |
---|
[cf966b5] | 161 | The call semantics discussed above 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~\cite{2FTwoHardThings}. Table \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. |
---|
[fb31cb8] | 162 | |
---|
[cf966b5] | 163 | \begin{table} |
---|
[fb31cb8] | 164 | \begin{center} |
---|
| 165 | \begin{tabular}{|c|c|} |
---|
| 166 | function call & \code{mutex} statement \\ |
---|
| 167 | \hline |
---|
| 168 | \begin{cfacode}[tabsize=3] |
---|
| 169 | monitor M {}; |
---|
[9f10d1f2] | 170 | void foo( M & mutex m1, M & mutex m2 ) { |
---|
[fb31cb8] | 171 | //critical section |
---|
| 172 | } |
---|
| 173 | |
---|
[9f10d1f2] | 174 | void bar( M & m1, M & m2 ) { |
---|
| 175 | foo( m1, m2 ); |
---|
[fb31cb8] | 176 | } |
---|
| 177 | \end{cfacode}&\begin{cfacode}[tabsize=3] |
---|
| 178 | monitor M {}; |
---|
[9f10d1f2] | 179 | void bar( M & m1, M & m2 ) { |
---|
| 180 | mutex(m1, m2) { |
---|
[fb31cb8] | 181 | //critical section |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | \end{cfacode} |
---|
| 187 | \end{tabular} |
---|
| 188 | \end{center} |
---|
| 189 | \caption{Regular call semantics vs. \code{mutex} statement} |
---|
| 190 | \label{lst:mutex-stmt} |
---|
[cf966b5] | 191 | \end{table} |
---|
[fb31cb8] | 192 | |
---|
[27dde72] | 193 | % ====================================================================== |
---|
| 194 | % ====================================================================== |
---|
| 195 | \subsection{Data semantics} \label{data} |
---|
| 196 | % ====================================================================== |
---|
| 197 | % ====================================================================== |
---|
| 198 | 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}: |
---|
| 199 | \begin{cfacode} |
---|
| 200 | monitor counter_t { |
---|
| 201 | int value; |
---|
| 202 | }; |
---|
| 203 | |
---|
| 204 | void ?{}(counter_t & this) { |
---|
| 205 | this.cnt = 0; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | int ?++(counter_t & mutex this) { |
---|
| 209 | return ++this.value; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | //need for mutex is platform dependent here |
---|
| 213 | void ?{}(int * this, counter_t & mutex cnt) { |
---|
| 214 | *this = (int)cnt; |
---|
| 215 | } |
---|
| 216 | \end{cfacode} |
---|
| 217 | |
---|
[b9d0fb6] | 218 | 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 : |
---|
| 219 | \begin{cfacode} |
---|
| 220 | trait is_monitor(dtype T) { |
---|
| 221 | monitor_desc * get_monitor( T & ); |
---|
| 222 | void ^?{}( T & mutex ); |
---|
| 223 | }; |
---|
| 224 | \end{cfacode} |
---|
[9f10d1f2] | 225 | Note that the destructor of a monitor must be a \code{mutex} routine to prevent deallocation while a thread is accessing the monitor. As with any object, calls to a monitor, using \code{mutex} or otherwise, is Undefined Behaviour after the destructor has run. |
---|
[27dde72] | 226 | |
---|
| 227 | % ====================================================================== |
---|
| 228 | % ====================================================================== |
---|
[64b272a] | 229 | \section{Internal scheduling} \label{intsched} |
---|
[27dde72] | 230 | % ====================================================================== |
---|
| 231 | % ====================================================================== |
---|
[9f10d1f2] | 232 | In addition to mutual exclusion, the monitors at the core of \CFA's concurrency can also be used to achieve synchronization. With monitors, this capability is generally achieved with internal or external scheduling as in~\cite{Hoare74}. 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] | 233 | |
---|
[9f10d1f2] | 234 | First, here is a simple example of internal-scheduling : |
---|
[27dde72] | 235 | |
---|
| 236 | \begin{cfacode} |
---|
[fb31cb8] | 237 | monitor A { |
---|
| 238 | condition e; |
---|
| 239 | } |
---|
[27dde72] | 240 | |
---|
[9f10d1f2] | 241 | void foo(A& mutex a1, A& mutex a2) { |
---|
[fb31cb8] | 242 | ... |
---|
| 243 | //Wait for cooperation from bar() |
---|
[9f10d1f2] | 244 | wait(a1.e); |
---|
[fb31cb8] | 245 | ... |
---|
| 246 | } |
---|
[27dde72] | 247 | |
---|
[9f10d1f2] | 248 | void bar(A& mutex a1, A& mutex a2) { |
---|
[fb31cb8] | 249 | //Provide cooperation for foo() |
---|
| 250 | ... |
---|
| 251 | //Unblock foo |
---|
[9f10d1f2] | 252 | signal(a1.e); |
---|
[fb31cb8] | 253 | } |
---|
[27dde72] | 254 | \end{cfacode} |
---|
[cf966b5] | 255 | 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, i.e., the signaller and signalled thread cannot be in the monitor simultaneously. 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] | 256 | |
---|
[cf966b5] | 257 | 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 guarantee offers the benefit of not having to loop around waits to recheck that a condition is 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] | 258 | |
---|
| 259 | % ====================================================================== |
---|
| 260 | % ====================================================================== |
---|
| 261 | \subsection{Internal Scheduling - multi monitor} |
---|
| 262 | % ====================================================================== |
---|
| 263 | % ====================================================================== |
---|
[cf966b5] | 264 | It is easier to understand the problem of multi-monitor scheduling using a series of pseudo-code examples. 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 variable as parameter and explicitly names the monitors (A and B) associated with the condition. Note that in \CFA, condition variables are tied to a \emph{group} 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. The example below shows the simple case of having two threads (one for each column) and a single monitor A. |
---|
[27dde72] | 265 | |
---|
| 266 | \begin{multicols}{2} |
---|
| 267 | thread 1 |
---|
| 268 | \begin{pseudo} |
---|
| 269 | acquire A |
---|
| 270 | wait A |
---|
| 271 | release A |
---|
| 272 | \end{pseudo} |
---|
| 273 | |
---|
| 274 | \columnbreak |
---|
| 275 | |
---|
| 276 | thread 2 |
---|
| 277 | \begin{pseudo} |
---|
| 278 | acquire A |
---|
| 279 | signal A |
---|
| 280 | release A |
---|
| 281 | \end{pseudo} |
---|
| 282 | \end{multicols} |
---|
[cf966b5] | 283 | 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] | 284 | |
---|
[3364962] | 285 | A direct extension of the previous example is a \gls{bulk-acq} version: |
---|
[27dde72] | 286 | \begin{multicols}{2} |
---|
| 287 | \begin{pseudo} |
---|
| 288 | acquire A & B |
---|
| 289 | wait A & B |
---|
| 290 | release A & B |
---|
| 291 | \end{pseudo} |
---|
| 292 | \columnbreak |
---|
| 293 | \begin{pseudo} |
---|
| 294 | acquire A & B |
---|
| 295 | signal A & B |
---|
| 296 | release A & B |
---|
| 297 | \end{pseudo} |
---|
| 298 | \end{multicols} |
---|
[b9d0fb6] | 299 | 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] | 300 | |
---|
[9f10d1f2] | 301 | 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~\cite{Lister77}, 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] | 302 | \begin{multicols}{2} |
---|
| 303 | \begin{pseudo} |
---|
| 304 | acquire A |
---|
| 305 | acquire B |
---|
| 306 | wait B |
---|
| 307 | release B |
---|
| 308 | release A |
---|
| 309 | \end{pseudo} |
---|
| 310 | |
---|
| 311 | \columnbreak |
---|
| 312 | |
---|
| 313 | \begin{pseudo} |
---|
| 314 | acquire A |
---|
| 315 | acquire B |
---|
| 316 | signal B |
---|
| 317 | release B |
---|
| 318 | release A |
---|
| 319 | \end{pseudo} |
---|
| 320 | \end{multicols} |
---|
[cf966b5] | 321 | 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} introduces a different set of problems, such as releasing monitor \code{C}, which has nothing to do with the \code{signal}. |
---|
[b9d0fb6] | 322 | |
---|
[cf966b5] | 323 | 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~\cite{Lister77}. |
---|
[27dde72] | 324 | |
---|
| 325 | \begin{multicols}{2} |
---|
| 326 | \begin{pseudo} |
---|
| 327 | acquire A |
---|
| 328 | acquire B |
---|
| 329 | wait B |
---|
| 330 | release B |
---|
| 331 | release A |
---|
| 332 | \end{pseudo} |
---|
| 333 | |
---|
| 334 | \columnbreak |
---|
| 335 | |
---|
| 336 | \begin{pseudo} |
---|
| 337 | |
---|
| 338 | acquire B |
---|
| 339 | signal B |
---|
| 340 | release B |
---|
| 341 | |
---|
| 342 | \end{pseudo} |
---|
| 343 | \end{multicols} |
---|
| 344 | |
---|
[cf966b5] | 345 | This simple refactoring may not be possible, forcing more complex restructuring. |
---|
[9f10d1f2] | 346 | |
---|
[b9d0fb6] | 347 | % ====================================================================== |
---|
| 348 | % ====================================================================== |
---|
| 349 | \subsection{Internal Scheduling - in depth} |
---|
| 350 | % ====================================================================== |
---|
| 351 | % ====================================================================== |
---|
| 352 | |
---|
[cf966b5] | 353 | A larger example is presented to show complex issues for \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 to implement 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 a monitor is acceptable, e.g., \code{mutex} parameter, global variables, pointer parameters or using locals with the \code{mutex}-statement. |
---|
[27dde72] | 354 | |
---|
[cf966b5] | 355 | \begin{figure}[!t] |
---|
[27dde72] | 356 | \begin{multicols}{2} |
---|
| 357 | Waiting thread |
---|
| 358 | \begin{pseudo}[numbers=left] |
---|
| 359 | acquire A |
---|
[3364962] | 360 | //Code Section 1 |
---|
[27dde72] | 361 | acquire A & B |
---|
[3364962] | 362 | //Code Section 2 |
---|
[27dde72] | 363 | wait A & B |
---|
[3364962] | 364 | //Code Section 3 |
---|
[27dde72] | 365 | release A & B |
---|
[3364962] | 366 | //Code Section 4 |
---|
[27dde72] | 367 | release A |
---|
| 368 | \end{pseudo} |
---|
| 369 | \columnbreak |
---|
| 370 | Signalling thread |
---|
[9f10d1f2] | 371 | \begin{pseudo}[numbers=left, firstnumber=10,escapechar=|] |
---|
[27dde72] | 372 | acquire A |
---|
[3364962] | 373 | //Code Section 5 |
---|
[27dde72] | 374 | acquire A & B |
---|
[3364962] | 375 | //Code Section 6 |
---|
[9f10d1f2] | 376 | |\label{line:signal1}|signal A & B |
---|
[3364962] | 377 | //Code Section 7 |
---|
[27dde72] | 378 | release A & B |
---|
[3364962] | 379 | //Code Section 8 |
---|
[cf966b5] | 380 | |\label{line:lastRelease}|release A |
---|
[27dde72] | 381 | \end{pseudo} |
---|
| 382 | \end{multicols} |
---|
[cf966b5] | 383 | \begin{cfacode}[caption={Internal scheduling with \gls{bulk-acq}},label={lst:int-bulk-pseudo}] |
---|
| 384 | \end{cfacode} |
---|
[b9d0fb6] | 385 | \begin{center} |
---|
| 386 | \begin{cfacode}[xleftmargin=.4\textwidth] |
---|
| 387 | monitor A a; |
---|
| 388 | monitor B b; |
---|
| 389 | condition c; |
---|
| 390 | \end{cfacode} |
---|
| 391 | \end{center} |
---|
[fb31cb8] | 392 | \begin{multicols}{2} |
---|
| 393 | Waiting thread |
---|
| 394 | \begin{cfacode} |
---|
[b9d0fb6] | 395 | mutex(a) { |
---|
[fb31cb8] | 396 | //Code Section 1 |
---|
| 397 | mutex(a, b) { |
---|
| 398 | //Code Section 2 |
---|
| 399 | wait(c); |
---|
| 400 | //Code Section 3 |
---|
| 401 | } |
---|
| 402 | //Code Section 4 |
---|
| 403 | } |
---|
| 404 | \end{cfacode} |
---|
| 405 | \columnbreak |
---|
| 406 | Signalling thread |
---|
| 407 | \begin{cfacode} |
---|
[b9d0fb6] | 408 | mutex(a) { |
---|
[fb31cb8] | 409 | //Code Section 5 |
---|
| 410 | mutex(a, b) { |
---|
| 411 | //Code Section 6 |
---|
| 412 | signal(c); |
---|
| 413 | //Code Section 7 |
---|
| 414 | } |
---|
| 415 | //Code Section 8 |
---|
| 416 | } |
---|
| 417 | \end{cfacode} |
---|
| 418 | \end{multicols} |
---|
[cf966b5] | 419 | \begin{cfacode}[caption={Equivalent \CFA code for listing \ref{lst:int-bulk-pseudo}},label={lst:int-bulk-cfa}] |
---|
| 420 | \end{cfacode} |
---|
[27dde72] | 421 | \begin{multicols}{2} |
---|
| 422 | Waiter |
---|
| 423 | \begin{pseudo}[numbers=left] |
---|
| 424 | acquire A |
---|
| 425 | acquire A & B |
---|
| 426 | wait A & B |
---|
| 427 | release A & B |
---|
| 428 | release A |
---|
| 429 | \end{pseudo} |
---|
| 430 | |
---|
| 431 | \columnbreak |
---|
| 432 | |
---|
| 433 | Signaller |
---|
[9f10d1f2] | 434 | \begin{pseudo}[numbers=left, firstnumber=6,escapechar=|] |
---|
[27dde72] | 435 | acquire A |
---|
| 436 | acquire A & B |
---|
| 437 | signal A & B |
---|
| 438 | release A & B |
---|
[9f10d1f2] | 439 | |\label{line:secret}|//Secretly keep B here |
---|
[27dde72] | 440 | release A |
---|
| 441 | //Wakeup waiter and transfer A & B |
---|
| 442 | \end{pseudo} |
---|
| 443 | \end{multicols} |
---|
[cf966b5] | 444 | \begin{cfacode}[caption={Listing \ref{lst:int-bulk-pseudo}, with delayed signalling comments},label={lst:int-secret}] |
---|
| 445 | \end{cfacode} |
---|
| 446 | \end{figure} |
---|
| 447 | |
---|
| 448 | 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}'' (listing \ref{lst:int-bulk-pseudo} line \ref{line:signal1}), it must actually transfer ownership of monitor \code{B} to the waiting thread. This ownership transfer is required in order to prevent barging into \code{B} by another thread, since both the signalling and signalled threads still need monitor \code{A}. There are three options. |
---|
| 449 | |
---|
| 450 | \subsubsection{Delaying signals} |
---|
| 451 | 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 multiple objects to a single group of objects, effectively making the existing single-monitor semantic viable by simply changing monitors to monitor groups. The naive approach to this solution is to only release monitors once every monitor in a group can be released. However, since some monitors are never released (i.e., the monitor of a thread), this interpretation means groups can grow but may never shrink. A more interesting interpretation is to only transfer groups as one but to recreate the groups on every operation, i.e., limit ownership transfer to one per \code{signal}/\code{release}. |
---|
[9f10d1f2] | 452 | |
---|
[cf966b5] | 453 | However, this solution can become much more complicated depending on what is executed while secretly holding B (listing \ref{lst:int-secret} line \ref{line:secret}). |
---|
| 454 | The goal in this solution is to avoid the need to transfer ownership of a subset of the condition monitors. However, listing \ref{lst:dependency} shows a slightly different example where a third thread is waiting on monitor \code{A}, using a different condition variable. Because the third thread is signalled when secretly holding \code{B}, the goal becomes unreachable. Depending on the order of signals (listing \ref{lst:dependency} line \ref{line:signal-ab} and \ref{line:signal-a}) two cases can happen : |
---|
| 455 | |
---|
| 456 | \paragraph{Case 1: thread $\alpha$ goes first.} In this case, the problem is that monitor \code{A} needs to be passed to thread $\beta$ when thread $\alpha$ is done with it. |
---|
| 457 | \paragraph{Case 2: thread $\beta$ goes first.} In this case, the problem is that monitor \code{B} needs to be retained and passed to thread $\alpha$ along with monitor \code{A}, which can be done directly or possibly using thread $\beta$ as an intermediate. |
---|
[9f10d1f2] | 458 | \\ |
---|
| 459 | |
---|
[cf966b5] | 460 | 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 \ref{line:signal-a} before line \ref{line:signal-ab} and get the reverse effect for listing \ref{lst:dependency}. |
---|
[9f10d1f2] | 461 | |
---|
| 462 | 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 homogeneous group and therefore effectively precludes this approach. |
---|
| 463 | |
---|
| 464 | \subsubsection{Dependency graphs} |
---|
| 465 | |
---|
| 466 | |
---|
[b9d0fb6] | 467 | \begin{figure} |
---|
| 468 | \begin{multicols}{3} |
---|
| 469 | Thread $\alpha$ |
---|
[ff98952] | 470 | \begin{pseudo}[numbers=left, firstnumber=1] |
---|
[27dde72] | 471 | acquire A |
---|
| 472 | acquire A & B |
---|
| 473 | wait A & B |
---|
| 474 | release A & B |
---|
| 475 | release A |
---|
| 476 | \end{pseudo} |
---|
| 477 | \columnbreak |
---|
[b9d0fb6] | 478 | Thread $\gamma$ |
---|
[9f10d1f2] | 479 | \begin{pseudo}[numbers=left, firstnumber=6, escapechar=|] |
---|
[27dde72] | 480 | acquire A |
---|
| 481 | acquire A & B |
---|
[9f10d1f2] | 482 | |\label{line:signal-ab}|signal A & B |
---|
[cf966b5] | 483 | |\label{line:release-ab}|release A & B |
---|
[9f10d1f2] | 484 | |\label{line:signal-a}|signal A |
---|
[cf966b5] | 485 | |\label{line:release-a}|release A |
---|
[27dde72] | 486 | \end{pseudo} |
---|
[b9d0fb6] | 487 | \columnbreak |
---|
| 488 | Thread $\beta$ |
---|
[cf966b5] | 489 | \begin{pseudo}[numbers=left, firstnumber=12, escapechar=|] |
---|
[b9d0fb6] | 490 | acquire A |
---|
| 491 | wait A |
---|
[cf966b5] | 492 | |\label{line:release-aa}|release A |
---|
[b9d0fb6] | 493 | \end{pseudo} |
---|
[27dde72] | 494 | \end{multicols} |
---|
[cf966b5] | 495 | \begin{cfacode}[caption={Pseudo-code for the three thread example.},label={lst:dependency}] |
---|
| 496 | \end{cfacode} |
---|
[9f10d1f2] | 497 | \begin{center} |
---|
| 498 | \input{dependency} |
---|
| 499 | \end{center} |
---|
| 500 | \caption{Dependency graph of the statements in listing \ref{lst:dependency}} |
---|
| 501 | \label{fig:dependency} |
---|
[b9d0fb6] | 502 | \end{figure} |
---|
[27dde72] | 503 | |
---|
[9f10d1f2] | 504 | In the listing \ref{lst:int-bulk-pseudo} pseudo-code, there is a solution that satisfies 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} back to the signaller 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 is effectively 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 close to polynomial. This complexity explosion can be seen in listing \ref{lst:explosion}, which is just a direct extension to three monitors, requires at least three ownership transfer and has multiple solutions. Furthermore, the presence of multiple solutions for ownership transfer can cause deadlock problems if a specific solution is not consistently picked; In the same way that multiple lock acquiring order can cause deadlocks. |
---|
| 505 | \begin{figure} |
---|
[27dde72] | 506 | \begin{multicols}{2} |
---|
| 507 | \begin{pseudo} |
---|
| 508 | acquire A |
---|
| 509 | acquire B |
---|
| 510 | acquire C |
---|
| 511 | wait A & B & C |
---|
| 512 | release C |
---|
| 513 | release B |
---|
| 514 | release A |
---|
| 515 | \end{pseudo} |
---|
| 516 | |
---|
| 517 | \columnbreak |
---|
| 518 | |
---|
| 519 | \begin{pseudo} |
---|
| 520 | acquire A |
---|
| 521 | acquire B |
---|
| 522 | acquire C |
---|
| 523 | signal A & B & C |
---|
| 524 | release C |
---|
| 525 | release B |
---|
| 526 | release A |
---|
| 527 | \end{pseudo} |
---|
| 528 | \end{multicols} |
---|
[cf966b5] | 529 | \begin{cfacode}[caption={Extension to three monitors of listing \ref{lst:int-bulk-pseudo}},label={lst:explosion}] |
---|
| 530 | \end{cfacode} |
---|
[3364962] | 531 | \end{figure} |
---|
| 532 | |
---|
[9f10d1f2] | 533 | Listing \ref{lst:dependency} is the three threads example used in the delayed signals solution. 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 graphs being a complex and expensive endeavour, this solution is not the preferred one. |
---|
[27dde72] | 534 | |
---|
[21a1efb] | 535 | \subsubsection{Partial signalling} \label{partial-sig} |
---|
[cf966b5] | 536 | 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 \code{B} at lines \ref{line:signal1} to the waiter but does not wake the waiting thread since it is still using monitor \code{A}. Only when it reaches line \ref{line:lastRelease} 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 released 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 significant downsides. |
---|
| 537 | |
---|
| 538 | While listing \ref{lst:dependency} is a complicated problem for previous solutions, it can be solved easily with partial signalling : |
---|
| 539 | \begin{itemize} |
---|
| 540 | \item When thread $\gamma$ reaches line \ref{line:release-ab} it transfers monitor \code{B} to thread $\alpha$ and continues to hold monitor \code{A}. |
---|
| 541 | \item When thread $\gamma$ reaches line \ref{line:release-a} it transfers monitor \code{A} to thread $\beta$ and wakes it up. |
---|
| 542 | \item When thread $\beta$ reaches line \ref{line:release-aa} it transfers monitor \code{A} to thread $\alpha$ and wakes it up. |
---|
| 543 | \item Problem solved! |
---|
| 544 | \end{itemize} |
---|
[27dde72] | 545 | |
---|
| 546 | % ====================================================================== |
---|
| 547 | % ====================================================================== |
---|
| 548 | \subsection{Signalling: Now or Later} |
---|
| 549 | % ====================================================================== |
---|
| 550 | % ====================================================================== |
---|
[cf966b5] | 551 | \begin{table} |
---|
[27dde72] | 552 | \begin{tabular}{|c|c|} |
---|
| 553 | \code{signal} & \code{signal_block} \\ |
---|
| 554 | \hline |
---|
[3364962] | 555 | \begin{cfacode}[tabsize=3] |
---|
| 556 | monitor DatingService |
---|
| 557 | { |
---|
| 558 | //compatibility codes |
---|
| 559 | enum{ CCodes = 20 }; |
---|
[27dde72] | 560 | |
---|
[3364962] | 561 | int girlPhoneNo |
---|
| 562 | int boyPhoneNo; |
---|
| 563 | }; |
---|
[27dde72] | 564 | |
---|
[3364962] | 565 | condition girls[CCodes]; |
---|
| 566 | condition boys [CCodes]; |
---|
| 567 | condition exchange; |
---|
[27dde72] | 568 | |
---|
[3364962] | 569 | int girl(int phoneNo, int ccode) |
---|
| 570 | { |
---|
| 571 | //no compatible boy ? |
---|
| 572 | if(empty(boys[ccode])) |
---|
| 573 | { |
---|
| 574 | //wait for boy |
---|
| 575 | wait(girls[ccode]); |
---|
[27dde72] | 576 | |
---|
[3364962] | 577 | //make phone number available |
---|
| 578 | girlPhoneNo = phoneNo; |
---|
[27dde72] | 579 | |
---|
[b9d0fb6] | 580 | //wake boy from chair |
---|
[3364962] | 581 | signal(exchange); |
---|
| 582 | } |
---|
| 583 | else |
---|
| 584 | { |
---|
| 585 | //make phone number available |
---|
| 586 | girlPhoneNo = phoneNo; |
---|
[27dde72] | 587 | |
---|
[3364962] | 588 | //wake boy |
---|
| 589 | signal(boys[ccode]); |
---|
[27dde72] | 590 | |
---|
[3364962] | 591 | //sit in chair |
---|
| 592 | wait(exchange); |
---|
| 593 | } |
---|
| 594 | return boyPhoneNo; |
---|
[27dde72] | 595 | } |
---|
| 596 | |
---|
[3364962] | 597 | int boy(int phoneNo, int ccode) |
---|
| 598 | { |
---|
| 599 | //same as above |
---|
| 600 | //with boy/girl interchanged |
---|
[27dde72] | 601 | } |
---|
[3364962] | 602 | \end{cfacode}&\begin{cfacode}[tabsize=3] |
---|
| 603 | monitor DatingService |
---|
| 604 | { |
---|
| 605 | //compatibility codes |
---|
| 606 | enum{ CCodes = 20 }; |
---|
| 607 | |
---|
| 608 | int girlPhoneNo; |
---|
| 609 | int boyPhoneNo; |
---|
| 610 | }; |
---|
[21a1efb] | 611 | |
---|
[3364962] | 612 | condition girls[CCodes]; |
---|
| 613 | condition boys [CCodes]; |
---|
| 614 | //exchange is not needed |
---|
[21a1efb] | 615 | |
---|
[3364962] | 616 | int girl(int phoneNo, int ccode) |
---|
| 617 | { |
---|
| 618 | //no compatible boy ? |
---|
| 619 | if(empty(boys[ccode])) |
---|
| 620 | { |
---|
| 621 | //wait for boy |
---|
| 622 | wait(girls[ccode]); |
---|
[21a1efb] | 623 | |
---|
[3364962] | 624 | //make phone number available |
---|
| 625 | girlPhoneNo = phoneNo; |
---|
[21a1efb] | 626 | |
---|
[b9d0fb6] | 627 | //wake boy from chair |
---|
[3364962] | 628 | signal(exchange); |
---|
| 629 | } |
---|
| 630 | else |
---|
| 631 | { |
---|
| 632 | //make phone number available |
---|
| 633 | girlPhoneNo = phoneNo; |
---|
[21a1efb] | 634 | |
---|
[3364962] | 635 | //wake boy |
---|
| 636 | signal_block(boys[ccode]); |
---|
[21a1efb] | 637 | |
---|
[3364962] | 638 | //second handshake unnecessary |
---|
[21a1efb] | 639 | |
---|
[3364962] | 640 | } |
---|
| 641 | return boyPhoneNo; |
---|
| 642 | } |
---|
[21a1efb] | 643 | |
---|
[3364962] | 644 | int boy(int phoneNo, int ccode) |
---|
| 645 | { |
---|
| 646 | //same as above |
---|
| 647 | //with boy/girl interchanged |
---|
| 648 | } |
---|
| 649 | \end{cfacode} |
---|
| 650 | \end{tabular} |
---|
| 651 | \caption{Dating service example using \code{signal} and \code{signal_block}. } |
---|
[cf966b5] | 652 | \label{tbl:datingservice} |
---|
| 653 | \end{table} |
---|
| 654 | 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. |
---|
[b9d0fb6] | 655 | |
---|
[cf966b5] | 656 | The example in table \ref{tbl:datingservice} highlights the difference in behaviour. As mentioned, \code{signal} only transfers ownership once the current critical section exits, this behaviour requires additional synchronization when a two-way handshake is needed. To avoid this explicit synchronization, the \code{condition} type offers the \code{signal_block} routine, which handles the two-way handshake as shown in the example. This feature 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 either before or after the call. |
---|
[27dde72] | 657 | |
---|
| 658 | % ====================================================================== |
---|
| 659 | % ====================================================================== |
---|
| 660 | \section{External scheduling} \label{extsched} |
---|
| 661 | % ====================================================================== |
---|
| 662 | % ====================================================================== |
---|
[b9d0fb6] | 663 | An alternative to internal scheduling is external scheduling, e.g., in \uC. |
---|
[27dde72] | 664 | \begin{center} |
---|
[b9d0fb6] | 665 | \begin{tabular}{|c|c|c|} |
---|
| 666 | Internal Scheduling & External Scheduling & Go\\ |
---|
[27dde72] | 667 | \hline |
---|
[b9d0fb6] | 668 | \begin{ucppcode}[tabsize=3] |
---|
[27dde72] | 669 | _Monitor Semaphore { |
---|
| 670 | condition c; |
---|
| 671 | bool inUse; |
---|
| 672 | public: |
---|
[e0a653d] | 673 | void P() { |
---|
[b9d0fb6] | 674 | if(inUse) |
---|
| 675 | wait(c); |
---|
[27dde72] | 676 | inUse = true; |
---|
| 677 | } |
---|
[e0a653d] | 678 | void V() { |
---|
| 679 | inUse = false; |
---|
| 680 | signal(c); |
---|
[27dde72] | 681 | } |
---|
| 682 | } |
---|
[b9d0fb6] | 683 | \end{ucppcode}&\begin{ucppcode}[tabsize=3] |
---|
[27dde72] | 684 | _Monitor Semaphore { |
---|
| 685 | |
---|
| 686 | bool inUse; |
---|
| 687 | public: |
---|
[e0a653d] | 688 | void P() { |
---|
[b9d0fb6] | 689 | if(inUse) |
---|
| 690 | _Accept(V); |
---|
[27dde72] | 691 | inUse = true; |
---|
| 692 | } |
---|
[21a1efb] | 693 | void V() { |
---|
[27dde72] | 694 | inUse = false; |
---|
| 695 | |
---|
| 696 | } |
---|
| 697 | } |
---|
[b9d0fb6] | 698 | \end{ucppcode}&\begin{gocode}[tabsize=3] |
---|
| 699 | type MySem struct { |
---|
| 700 | inUse bool |
---|
| 701 | c chan bool |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | // acquire |
---|
| 705 | func (s MySem) P() { |
---|
| 706 | if s.inUse { |
---|
| 707 | select { |
---|
| 708 | case <-s.c: |
---|
| 709 | } |
---|
| 710 | } |
---|
| 711 | s.inUse = true |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | // release |
---|
| 715 | func (s MySem) V() { |
---|
| 716 | s.inUse = false |
---|
| 717 | |
---|
| 718 | //This actually deadlocks |
---|
| 719 | //when single thread |
---|
| 720 | s.c <- false |
---|
| 721 | } |
---|
| 722 | \end{gocode} |
---|
[27dde72] | 723 | \end{tabular} |
---|
| 724 | \end{center} |
---|
[9f10d1f2] | 725 | This method is more constrained and explicit, which helps users reduce the non-deterministic 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 occurring. External scheduling can generally be done either in terms of control flow (e.g., Ada with \code{accept}, \uC with \code{_Accept}) or in terms of data (e.g., Go with channels). Of course, both of these paradigms have their own strengths 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 multiple-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] | 726 | |
---|
[b9d0fb6] | 727 | 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] | 728 | |
---|
| 729 | % ====================================================================== |
---|
| 730 | % ====================================================================== |
---|
| 731 | \subsection{Loose object definitions} |
---|
| 732 | % ====================================================================== |
---|
| 733 | % ====================================================================== |
---|
[cf966b5] | 734 | In \uC, a monitor class declaration includee 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] | 735 | |
---|
| 736 | \begin{cfacode} |
---|
[fb31cb8] | 737 | monitor A {}; |
---|
[27dde72] | 738 | |
---|
[fb31cb8] | 739 | void f(A & mutex a); |
---|
| 740 | void g(A & mutex a) { |
---|
| 741 | waitfor(f); //Obvious which f() to wait for |
---|
| 742 | } |
---|
[3364962] | 743 | |
---|
[fb31cb8] | 744 | void f(A & mutex a, int); //New different F added in scope |
---|
| 745 | void h(A & mutex a) { |
---|
| 746 | waitfor(f); //Less obvious which f() to wait for |
---|
| 747 | } |
---|
[27dde72] | 748 | \end{cfacode} |
---|
| 749 | |
---|
[cf966b5] | 750 | Furthermore, external scheduling is an example where implementation constraints become visible from the interface. Here is the pseudo code for the entering phase of a monitor: |
---|
[27dde72] | 751 | \begin{center} |
---|
| 752 | \begin{tabular}{l} |
---|
| 753 | \begin{pseudo} |
---|
| 754 | if monitor is free |
---|
| 755 | enter |
---|
[3364962] | 756 | elif already own the monitor |
---|
[21a1efb] | 757 | continue |
---|
[27dde72] | 758 | elif monitor accepts me |
---|
| 759 | enter |
---|
| 760 | else |
---|
| 761 | block |
---|
| 762 | \end{pseudo} |
---|
| 763 | \end{tabular} |
---|
| 764 | \end{center} |
---|
[3364962] | 765 | 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] | 766 | |
---|
[20ffcf3] | 767 | \begin{figure}[H] |
---|
[27dde72] | 768 | \begin{center} |
---|
| 769 | {\resizebox{0.4\textwidth}{!}{\input{monitor}}} |
---|
| 770 | \end{center} |
---|
[20ffcf3] | 771 | \label{fig:monitor} |
---|
| 772 | \end{figure} |
---|
[27dde72] | 773 | |
---|
[cf966b5] | 774 | 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 approach requires a dense unique ordering of routines with an upper-bound and that ordering must be consistent across translation units. For OO languages these constraints are common, since objects only offer adding member routines consistently across translation units via inheritence. However, in \CFA users can extend objects with mutex routines that are only visible in certain translation unit. This means that establishing a program-wide dense-ordering among mutex routines can only be done in the program linking phase, and still could have issues when using dynamically shared objects. |
---|
| 775 | |
---|
[07c1e595] | 776 | The alternative is to alter the implementation like this: |
---|
[27dde72] | 777 | |
---|
| 778 | \begin{center} |
---|
| 779 | {\resizebox{0.4\textwidth}{!}{\input{ext_monitor}}} |
---|
| 780 | \end{center} |
---|
| 781 | |
---|
[cf966b5] | 782 | Here, the mutex routine called is associated with a thread on the entry queue while a list of acceptable routines is kept seperately. 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 additional searches for the \code{waitfor} statement to check if a routine is already queued. |
---|
[27dde72] | 783 | |
---|
[b9d0fb6] | 784 | \begin{figure} |
---|
[cf966b5] | 785 | \begin{cfacode}[caption={Example of nested external scheduling},label={lst:nest-ext}] |
---|
[b9d0fb6] | 786 | monitor M {}; |
---|
| 787 | void foo( M & mutex a ) {} |
---|
| 788 | void bar( M & mutex b ) { |
---|
| 789 | //Nested in the waitfor(bar, c) call |
---|
| 790 | waitfor(foo, b); |
---|
| 791 | } |
---|
| 792 | void baz( M & mutex c ) { |
---|
| 793 | waitfor(bar, c); |
---|
| 794 | } |
---|
[27dde72] | 795 | |
---|
[b9d0fb6] | 796 | \end{cfacode} |
---|
| 797 | \end{figure} |
---|
| 798 | |
---|
[9f10d1f2] | 799 | Note that in the second picture, tasks need to always keep track of the monitors associated with mutex routines, and the routine mask needs to have both a function pointer and a set of monitors, as is be discussed in the next section. These details are omitted from the picture for the sake of simplicity. |
---|
[b9d0fb6] | 800 | |
---|
[9f10d1f2] | 801 | 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 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] | 802 | |
---|
| 803 | % ====================================================================== |
---|
| 804 | % ====================================================================== |
---|
| 805 | \subsection{Multi-monitor scheduling} |
---|
| 806 | % ====================================================================== |
---|
| 807 | % ====================================================================== |
---|
| 808 | |
---|
[3364962] | 809 | 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] | 810 | \begin{cfacode} |
---|
[fb31cb8] | 811 | monitor M {}; |
---|
[27dde72] | 812 | |
---|
[fb31cb8] | 813 | void f(M & mutex a); |
---|
[27dde72] | 814 | |
---|
[b9d0fb6] | 815 | void g(M & mutex b, M & mutex c) { |
---|
[07c1e595] | 816 | waitfor(f); //two monitors M => unknown which to pass to f(M & mutex) |
---|
[fb31cb8] | 817 | } |
---|
[27dde72] | 818 | \end{cfacode} |
---|
| 819 | The obvious solution is to specify the correct monitor as follows: |
---|
| 820 | |
---|
| 821 | \begin{cfacode} |
---|
[fb31cb8] | 822 | monitor M {}; |
---|
[27dde72] | 823 | |
---|
[fb31cb8] | 824 | void f(M & mutex a); |
---|
[27dde72] | 825 | |
---|
[fb31cb8] | 826 | void g(M & mutex a, M & mutex b) { |
---|
[9f10d1f2] | 827 | //wait for call to f with argument b |
---|
| 828 | waitfor(f, b); |
---|
[fb31cb8] | 829 | } |
---|
[27dde72] | 830 | \end{cfacode} |
---|
[9f10d1f2] | 831 | 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 behaviour can be extended to the multi-monitor \code{waitfor} statement as follows. |
---|
[27dde72] | 832 | |
---|
| 833 | \begin{cfacode} |
---|
[fb31cb8] | 834 | monitor M {}; |
---|
[27dde72] | 835 | |
---|
[fb31cb8] | 836 | void f(M & mutex a, M & mutex b); |
---|
[27dde72] | 837 | |
---|
[fb31cb8] | 838 | void g(M & mutex a, M & mutex b) { |
---|
[9f10d1f2] | 839 | //wait for call to f with argument a and b |
---|
| 840 | waitfor(f, a, b); |
---|
[fb31cb8] | 841 | } |
---|
[21a1efb] | 842 | \end{cfacode} |
---|
| 843 | |
---|
[3364962] | 844 | 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] | 845 | |
---|
[07c1e595] | 846 | An important behaviour to note is when a set of monitors only match partially : |
---|
[21a1efb] | 847 | |
---|
| 848 | \begin{cfacode} |
---|
[fb31cb8] | 849 | mutex struct A {}; |
---|
[21a1efb] | 850 | |
---|
[fb31cb8] | 851 | mutex struct B {}; |
---|
[21a1efb] | 852 | |
---|
[fb31cb8] | 853 | void g(A & mutex a, B & mutex b) { |
---|
| 854 | waitfor(f, a, b); |
---|
| 855 | } |
---|
[21a1efb] | 856 | |
---|
[fb31cb8] | 857 | A a1, a2; |
---|
| 858 | B b; |
---|
[21a1efb] | 859 | |
---|
[fb31cb8] | 860 | void foo() { |
---|
| 861 | g(a1, b); //block on accept |
---|
| 862 | } |
---|
[21a1efb] | 863 | |
---|
[fb31cb8] | 864 | void bar() { |
---|
[07c1e595] | 865 | f(a2, b); //fulfill cooperation |
---|
[fb31cb8] | 866 | } |
---|
[27dde72] | 867 | \end{cfacode} |
---|
[9f10d1f2] | 868 | 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 the order of parameters is irrelevant; \code{waitfor(f,a,b)} and \code{waitfor(f,b,a)} are indistinguishable waiting condition. |
---|
[27dde72] | 869 | |
---|
| 870 | % ====================================================================== |
---|
| 871 | % ====================================================================== |
---|
[fb31cb8] | 872 | \subsection{\code{waitfor} semantics} |
---|
| 873 | % ====================================================================== |
---|
[27dde72] | 874 | % ====================================================================== |
---|
[fb31cb8] | 875 | |
---|
[9f10d1f2] | 876 | 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 monitors 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 but overloading is possible. |
---|
[fb31cb8] | 877 | \begin{figure} |
---|
[cf966b5] | 878 | \begin{cfacode}[caption={Various correct and incorrect uses of the waitfor statement},label={lst:waitfor}] |
---|
[fb31cb8] | 879 | monitor A{}; |
---|
| 880 | monitor B{}; |
---|
| 881 | |
---|
| 882 | void f1( A & mutex ); |
---|
| 883 | void f2( A & mutex, B & mutex ); |
---|
| 884 | void f3( A & mutex, int ); |
---|
| 885 | void f4( A & mutex, int ); |
---|
| 886 | void f4( A & mutex, double ); |
---|
| 887 | |
---|
| 888 | void foo( A & mutex a1, A & mutex a2, B & mutex b1, B & b2 ) { |
---|
| 889 | A * ap = & a1; |
---|
| 890 | void (*fp)( A & mutex ) = f1; |
---|
| 891 | |
---|
| 892 | waitfor(f1, a1); //Correct : 1 monitor case |
---|
| 893 | waitfor(f2, a1, b1); //Correct : 2 monitor case |
---|
| 894 | waitfor(f3, a1); //Correct : non-mutex arguments are ignored |
---|
| 895 | waitfor(f1, *ap); //Correct : expression as argument |
---|
| 896 | |
---|
| 897 | waitfor(f1, a1, b1); //Incorrect : Too many mutex arguments |
---|
| 898 | waitfor(f2, a1); //Incorrect : Too few mutex arguments |
---|
| 899 | waitfor(f2, a1, a2); //Incorrect : Mutex arguments don't match |
---|
| 900 | waitfor(f1, 1); //Incorrect : 1 not a mutex argument |
---|
[b9d0fb6] | 901 | waitfor(f9, a1); //Incorrect : f9 function does not exist |
---|
| 902 | waitfor(*fp, a1 ); //Incorrect : fp not an identifier |
---|
[fb31cb8] | 903 | waitfor(f4, a1); //Incorrect : f4 ambiguous |
---|
| 904 | |
---|
[9f10d1f2] | 905 | waitfor(f2, a1, b2); //Undefined Behaviour : b2 not mutex |
---|
[fb31cb8] | 906 | } |
---|
| 907 | \end{cfacode} |
---|
| 908 | \end{figure} |
---|
| 909 | |
---|
[9f10d1f2] | 910 | Finally, for added flexibility, \CFA supports constructing a complex \code{waitfor} statement using the \code{or}, \code{timeout} and \code{else}. Indeed, multiple \code{waitfor} clauses 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 enable users to tell which accepted function executed, \code{waitfor}s are followed by a statement (including the null statement \code{;}) or a compound statement, which is executed after the clause is triggered. 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, which checks for a matching function call already arrived and otherwise continues. Any and all of these clauses can be preceded by a \code{when} condition to dynamically toggle the accept clauses on or off based on some current state. Listing \ref{lst:waitfor2}, demonstrates several complex masks and some incorrect ones. |
---|
[fb31cb8] | 911 | |
---|
| 912 | \begin{figure} |
---|
[cf966b5] | 913 | \begin{cfacode}[caption={Various correct and incorrect uses of the or, else, and timeout clause around a waitfor statement},label={lst:waitfor2}] |
---|
[fb31cb8] | 914 | monitor A{}; |
---|
| 915 | |
---|
| 916 | void f1( A & mutex ); |
---|
| 917 | void f2( A & mutex ); |
---|
| 918 | |
---|
| 919 | void foo( A & mutex a, bool b, int t ) { |
---|
| 920 | //Correct : blocking case |
---|
| 921 | waitfor(f1, a); |
---|
| 922 | |
---|
| 923 | //Correct : block with statement |
---|
| 924 | waitfor(f1, a) { |
---|
| 925 | sout | "f1" | endl; |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | //Correct : block waiting for f1 or f2 |
---|
| 929 | waitfor(f1, a) { |
---|
| 930 | sout | "f1" | endl; |
---|
| 931 | } or waitfor(f2, a) { |
---|
| 932 | sout | "f2" | endl; |
---|
| 933 | } |
---|
| 934 | |
---|
| 935 | //Correct : non-blocking case |
---|
| 936 | waitfor(f1, a); or else; |
---|
| 937 | |
---|
| 938 | //Correct : non-blocking case |
---|
| 939 | waitfor(f1, a) { |
---|
| 940 | sout | "blocked" | endl; |
---|
| 941 | } or else { |
---|
| 942 | sout | "didn't block" | endl; |
---|
| 943 | } |
---|
| 944 | |
---|
| 945 | //Correct : block at most 10 seconds |
---|
| 946 | waitfor(f1, a) { |
---|
| 947 | sout | "blocked" | endl; |
---|
| 948 | } or timeout( 10`s) { |
---|
| 949 | sout | "didn't block" | endl; |
---|
| 950 | } |
---|
| 951 | |
---|
| 952 | //Correct : block only if b == true |
---|
| 953 | //if b == false, don't even make the call |
---|
| 954 | when(b) waitfor(f1, a); |
---|
| 955 | |
---|
| 956 | //Correct : block only if b == true |
---|
| 957 | //if b == false, make non-blocking call |
---|
| 958 | waitfor(f1, a); or when(!b) else; |
---|
| 959 | |
---|
| 960 | //Correct : block only of t > 1 |
---|
| 961 | waitfor(f1, a); or when(t > 1) timeout(t); or else; |
---|
| 962 | |
---|
| 963 | //Incorrect : timeout clause is dead code |
---|
| 964 | waitfor(f1, a); or timeout(t); or else; |
---|
| 965 | |
---|
| 966 | //Incorrect : order must be |
---|
| 967 | //waitfor [or waitfor... [or timeout] [or else]] |
---|
| 968 | timeout(t); or waitfor(f1, a); or else; |
---|
| 969 | } |
---|
| 970 | \end{cfacode} |
---|
[64b272a] | 971 | \end{figure} |
---|
| 972 | |
---|
| 973 | % ====================================================================== |
---|
| 974 | % ====================================================================== |
---|
| 975 | \subsection{Waiting for the destructor} |
---|
| 976 | % ====================================================================== |
---|
| 977 | % ====================================================================== |
---|
[b9d0fb6] | 978 | 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] | 979 | \begin{figure} |
---|
[cf966b5] | 980 | \begin{cfacode}[caption={Example of an executor which executes action in series until the destructor is called.},label={lst:dtor-order}] |
---|
[64b272a] | 981 | monitor Executer {}; |
---|
| 982 | struct Action; |
---|
| 983 | |
---|
| 984 | void ^?{} (Executer & mutex this); |
---|
| 985 | void execute(Executer & mutex this, const Action & ); |
---|
| 986 | void run (Executer & mutex this) { |
---|
| 987 | while(true) { |
---|
| 988 | waitfor(execute, this); |
---|
| 989 | or waitfor(^?{} , this) { |
---|
| 990 | break; |
---|
| 991 | } |
---|
| 992 | } |
---|
| 993 | } |
---|
| 994 | \end{cfacode} |
---|
| 995 | \end{figure} |
---|
[cf966b5] | 996 | 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. |
---|