source: doc/proposals/concurrency/concurrency.tex @ 7e10773

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 7e10773 was 7e10773, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

further progress on concurrency part

  • Property mode set to 100644
File size: 28.4 KB
Line 
1% requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
2
3% inline code ©...© (copyright symbol) emacs: C-q M-)
4% red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
5% blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_
6% green highlighting ¢...¢ (cent symbol) emacs: C-q M-"
7% LaTex escape §...§ (section symbol) emacs: C-q M-'
8% keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
9% math escape $...$ (dollar symbol)
10
11\documentclass[twoside,11pt]{article}
12
13%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14
15% Latex packages used in the document.
16\usepackage[T1]{fontenc}                                % allow Latin1 (extended ASCII) characters
17\usepackage{textcomp}
18\usepackage[latin1]{inputenc}
19\usepackage{fullpage,times,comment}
20\usepackage{epic,eepic}
21\usepackage{upquote}                                                                    % switch curled `'" to straight
22\usepackage{calc}
23\usepackage{xspace}
24\usepackage{graphicx}
25\usepackage{tabularx}
26\usepackage{varioref}                                                           % extended references
27\usepackage{inconsolata}
28\usepackage{listings}                                                                   % format program code
29\usepackage[flushmargin]{footmisc}                                              % support label/reference in footnote
30\usepackage{latexsym}                                   % \Box glyph
31\usepackage{mathptmx}                                   % better math font with "times"
32\usepackage[usenames]{color}
33\usepackage[pagewise]{lineno}
34\renewcommand{\linenumberfont}{\scriptsize\sffamily}
35\input{common}                                          % bespoke macros used in the document
36\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
37\usepackage{breakurl}
38\renewcommand{\UrlFont}{\small\sf}
39
40\setlength{\topmargin}{-0.45in}                                                 % move running title into header
41\setlength{\headsep}{0.25in}
42
43%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44
45% Names used in the document.
46
47\newcommand{\Version}{1.0.0}
48\newcommand{\CS}{C\raisebox{-0.9ex}{\large$^\sharp$}\xspace}
49
50\newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}}
51\newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}}
52\newcommand{\R}[1]{\Textbf{#1}}
53\newcommand{\B}[1]{{\Textbf[blue]{#1}}}
54\newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
55\newcommand{\uC}{$\mu$\CC}
56\newcommand{\cit}{\textsuperscript{[Citation Needed]}\xspace}
57\newcommand{\code}[1]{\lstinline{#1}}
58
59
60\newsavebox{\LstBox}
61
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63
64\setcounter{secnumdepth}{3}                             % number subsubsections
65\setcounter{tocdepth}{3}                                % subsubsections in table of contents
66\makeindex
67
68%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69
70\begin{document}
71% \linenumbers
72
73\title{Concurrency in \CFA}
74\author{Thierry Delisle \\
75Dept. of Computer Science, University of Waterloo, \\ Waterloo, Ontario, Canada
76}
77
78\maketitle
79\section{Introduction}
80This proposal provides a minimal core concurrency API that is both simple, efficient and can be reused to build "higher level" features. The simplest possible core is a thread and a lock but this low level approach is hard to master. An easier approach for users is be to support higher level construct as the basis of the concurrency in \CFA.
81Indeed, for higly productive parallel programming high-level approaches are much more popular\cite{HPP:Study}. Examples are task based parallelism, message passing, implicit threading.
82
83There are actually two problems that need to be solved in the design of the concurrency for a language. Which concurrency tools are available to the users and which parallelism tools are available. While these two concepts are often seen together, they are in fact distinct concepts that require different sorts of tools\cite{Myths}. Concurrency tools need to handle mutual exclusion and synchronization while parallelism tools are more about performance, cost and ressource utilisation.
84
85\section{Concurrency}
86Several tool can be used to solve concurrency challenges. Since these challenges always appear with the use of mutable shared state, some languages and libraries simply disallow mutable shared state completely (Erlang, Haskel, Akka (Scala))\cit. In the paradigms, interaction between concurrent objects rely on message passing or other paradigms that often closely relate to networking concepts. However, in imperative or OO languages these approaches entail a clear distinction between concurrent and non concurrent paradigms. Which in turns mean that programmers need to learn two sets of designs patterns in order to be effective at their jobs. Approaches based on shared memory are more closely related to non-concurrent paradigms since they often rely on non-concurrent constructs like routine calls and objects. At a lower level these can be implemented as locks and atomic operations. However for productivity reasons it is desireable to have a higher-level construct to be the core concurrency paradigm\cite{HPP:Study}. This paper proposes Monitors\cit as the core concurrency construct.
87
88Finally, an approach that is worth mentionning because it is gaining in popularity is transactionnal memory\cit. However, the performance and feature set is currently too restrictive to be possible to add such a paradigm to a language like C or \CC\cit, which is why it was rejected as the core paradigm for concurrency in \CFA.
89
90\section{Monitors}
91A 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\cit or \uC\cite{uCPP:Book} but does not strictly require OOP semantics. The only requirements is to be able to declare a handle to a shared object and a set of routines that act on it :
92\begin{lstlisting}
93        typedef /*some monitor type*/ monitor;
94        int f(monitor & m);
95
96        int main() {
97                monitor m;
98                f(m);
99        }
100\end{lstlisting}
101
102\subsection{Call semantics} \label{call}
103The above example of monitors already displays some of their intrinsic caracteristics. Indeed, it is necessary to use pass-by-reference over pass-by-value for monitor routines. This semantics is important because since at their core, monitors are simply implicit mutual exclusion objects (locks) and copying semantics of these is ill defined. Therefore, monitors are implicitly non-copyable.
104
105Another aspect to consider is when a monitor acquires its mutual exclusion. Indeed, a monitor may need to be passed to helper routines that do not acquire the monitor mutual exclusion on entry. Examples of this can be both generic helper routines (\code{swap}, \code{sort}, etc.) or specific helper routines like the following example :
106
107\begin{lstlisting}
108        mutex struct counter_t { /*...*/ };
109
110        void ?{}(counter_t & mutex this);
111        int ++?(counter_t & mutex this);
112        void ?{}(int * this, counter_t & mutex cnt);
113
114        bool is_zero(counter_t & nomutex this) {
115                int val = this;
116                return val == 0;
117        }
118\end{lstlisting}
119*semantics of the declaration of \code{mutex struct counter_t} will be discussed in details in \ref{data}
120
121This is an example of a monitor used as safe(ish) counter for concurrency. This API, which offers the prefix increment operator and a conversion operator to \code{int}, guarantees that reading the value (by converting it to \code{int}) and incrementing it are mutually exclusive. Note that the \code{is_zero} routine uses the \code{nomutex} keyword. Indeed, since reading the value is already atomic, there is no point in maintaining the mutual exclusion once the value is copied locally (in the variable \code{val} ).
122
123Having both \code{mutex} and \code{nomutex} keywords could be argued to be redundant based on the meaning of a routine having neither of these keywords. If there were a meaning to routine \code{void foo(counter_t & this)} then one could argue that it should be to default to the safest option : \code{mutex}. On the other hand, the option of having routine \code{void foo(counter_t & this)} mean \code{nomutex} is unsafe by default and may easily cause subtle errors. It can be argued that this is the more "normal" behavior, \code{nomutex} effectively stating explicitly that "this routine has nothing special". An other alternative is to make one of these keywords mandatory, which would provide the same semantics but without the ambiguity of supporting routine \code{void foo(counter_t & this)}. Mandatory keywords would also have the added benefice of being more clearly self-documented but at the cost of extra typing. In the end, which solution should be picked is still up for debate. For the reminder of this proposal, the explicit approach will be used for the sake of clarity.
124
125Regardless of which keyword is kept, it is important to establish when mutex/nomutex may be used depending on type parameters.
126\begin{lstlisting}
127        int f01(monitor & mutex m);
128        int f02(const monitor & mutex m);
129        int f03(monitor * mutex m);
130        int f04(monitor * mutex * m);
131        int f05(monitor ** mutex m);
132        int f06(monitor[10] mutex m);
133        int f07(monitor[] mutex m);
134        int f08(vector(monitor) & mutex m);
135        int f09(list(monitor) & mutex m);
136        int f10([monitor*, int] & mutex m);
137        int f11(graph(monitor*) & mutex m);
138\end{lstlisting}
139
140For the first few routines it seems to make sense to support the mutex keyword for such small variations. The difference between pointers and reference (\code{f01} vs \code{f03}) or const and non-const (\code{f01} vs \code{f02}) has no significance to mutual exclusion. It may not always make sense to acquire the monitor when extra dereferences (\code{f04}, \code{f05}) are added but it is still technically feasible and the present of the explicit mutex keywork does make it very clear of the user's intentions. Passing in a known-sized array(\code{f06}) is also technically feasible but is close to the limits. Indeed, the size of the array is not actually enforced by the compiler and if replaced by a variable-sized array (\code{f07}) or a higher-level container (\code{f08}, \code{f09}) it becomes much more complex to properly acquire all the locks needed for such a complex critical section. This implicit acquisition also poses the question of what qualifies as a container. If the mutex keyword is supported on monitors stored inside of other types it can quickly become complex and unclear which monitor should be acquired and when. The extreme example of this is \code{f11} which takes a possibly cyclic graph of pointers to monitors. With such a routine signature the intuition of which monitors will be acquired on entry is lost\cite{Chicken}. Where to draw the lines is up for debate but it seems reasonnable to consider \code{f03} as accepted and \code{f06} as rejected.
141
142\subsection{Data semantics} \label{data}
143Once the call semantics are established, the next step is to establish data semantics. Indeed, until now a monitor is used simply as a generic handle but in most cases monitors contian shared data. This data should be intrinsic to the monitor declaration to prevent any accidental use of data without its appripriate protection. For example here is a more fleshed-out version of the counter showed in \ref{call}:
144\begin{lstlisting}
145        mutex struct counter_t {
146                int value;
147        };
148
149        void ?{}(counter_t & mutex this) {
150                this.cnt = 0;
151        }
152
153        int ++?(counter_t & mutex this) {
154                return ++this->value;
155        }
156
157        void ?{}(int * this, counter_t & mutex cnt) {
158                *this = (int)cnt;
159        }
160\end{lstlisting}
161\begin{tabular}{ c c }
162Thread 1 & Thread 2 \\
163\begin{lstlisting}
164        void main(counter_t & mutex c) {
165                for(;;) {
166                        int count = c;
167                        sout | count | endl;
168                }
169        }
170\end{lstlisting} &\begin{lstlisting}
171        void main(counter_t & mutex c) {
172                for(;;) {
173                        ++c;
174                }
175        }
176
177\end{lstlisting}
178\end{tabular}
179\\
180
181
182This simple counter offers an example of monitor usage. Notice how the counter is used without any explicit synchronisation and yet supports thread-safe semantics for both reading and writting. \\
183
184These simple mutual exclusion semantics also naturally expand to multi-monitor calls.
185\begin{lstlisting}
186        int f(MonitorA & mutex a, MonitorB & mutex b);
187
188        MonitorA a;
189        MonitorB b;
190        f(a,b);
191\end{lstlisting}
192
193This code acquires both locks before entering the critical section. In practice, writing multi-locking routines that can lead to deadlocks can be very tricky. Having language level support for such feature is therefore a significant asset for \CFA. However, as the this proposal shows, this does have significant repercussions relating to scheduling (see \ref{insched} and \ref{extsched}). The ability to acquire multiple monitors at the same time does incur a significant pitfall even without looking into scheduling. For example :
194\begin{lstlisting}
195        void foo(A & mutex a, B & mutex a) {
196                //...
197        }
198
199        void bar(A & mutex a, B & nomutex a)
200                //...
201                foo(a, b);
202                //...
203        }
204
205        void baz(A & nomutex a, B & mutex a)
206                //...
207                foo(a, b);
208                //...
209        }
210\end{lstlisting}
211
212TODO: dig further into monitor order aquiring
213
214Thoughs : calls to \code{baz} and \code{bar} are definitely incompatible because they explicitly acquire locks in reverse order and therefore are explicitly asking for a deadlock. The best that can be done in this situatuin is to detect the deadlock. The case of implicit ordering is less clear because in the case of monitors the runtime system \textit{may} be smart enough to figure out that someone is waiting with explicit ordering... maybe.
215
216\subsubsection{Internal scheduling} \label{insched}
217Monitors should also be able to schedule what threads access it as a mean of synchronization. Internal scheduling is one of the simple examples of such a feature. It allows users to declare condition variables and wait for them to be signaled. Here is a simple example of such a technique :
218
219\begin{lstlisting}
220        mutex struct A {
221                condition e;
222        }
223
224        void foo(A & mutex a) {
225                //...
226                wait(a.e);
227                //...
228        }
229
230        void bar(A & mutex a) {
231                signal(a.e);
232        }
233\end{lstlisting}
234
235Here routine \code{foo} waits on the \code{signal} from \code{bar} before making further progress, effectively ensuring a basic ordering. This can easily be extended to multi-monitor calls by offering the same guarantee.
236
237\begin{center}
238\begin{tabular}{ c @{\hskip 0.65in} c }
239Thread 1 & Thread 2 \\
240\begin{lstlisting}
241void foo(monitor & mutex a,
242         monitor & mutex b) {
243        //...
244        wait(a.e);
245        //...
246}
247
248foo(a, b);
249\end{lstlisting} &\begin{lstlisting}
250void bar(monitor & mutex a,
251         monitor & mutex b) {
252        signal(a.e);
253}
254
255
256
257bar(a, b);
258\end{lstlisting}
259\end{tabular}
260\end{center}
261
262A direct extension of the single monitor semantics would be to release all locks when waiting and transferring ownership of all locks when signalling. However, for the purpose of synchronization it may be usefull to only release some of the locks but keep others. On the technical side, partially releasing lock is feasible but from the user perspective a choice must be made for the syntax of this feature. It is possible to do without any extra syntax by relying on order of acquisition :
263
264\begin{center}
265\begin{tabular}{|c|c|c|}
266Context 1 & Context 2 & Context 3 \\
267\hline
268\begin{lstlisting}
269void foo(monitor & mutex a,
270         monitor & mutex b) {
271        wait(a.e);
272}
273
274
275
276
277
278
279foo(a,b);
280\end{lstlisting} &\begin{lstlisting}
281void bar(monitor & mutex a,
282         monitor & nomutex b) {
283        foo(a,b);
284}
285
286void foo(monitor & mutex a,
287         monitor & mutex b) {
288        wait(a.e);
289}
290
291bar(a, b);
292\end{lstlisting} &\begin{lstlisting}
293void bar(monitor & mutex a,
294         monitor & nomutex b) {
295        foo(a,b);
296}
297
298void baz(monitor & nomutex a,
299         monitor & mutex b) {
300        wait(a.e);
301}
302
303bar(a, b);
304\end{lstlisting}
305\end{tabular}
306\end{center}
307
308This can be interpreted in two different ways :
309\begin{enumerate}
310        \item \code{wait} atomically releases the monitors \underline{theoretically} acquired by the inner-most mutex routine.
311        \item \code{wait} atomically releases the monitors \underline{actually} acquired by the inner-most mutex routine.
312\end{enumerate}
313While the difference between these two is subtle, it has a significant impact. In the first case it means that the calls to \code{foo} would behave the same in Context 1 and 2. This semantic would also mean that the call to \code{wait} in routine \code{baz} would only release \code{monitor b}. While this may seem intuitive with these examples, it does have one significant implication, it creates a strong distinction between acquiring multiple monitors in sequence and acquiring the same monitors simulatenously.
314
315\begin{center}
316\begin{tabular}{c @{\hskip 0.35in} c @{\hskip 0.35in} c}
317\begin{lstlisting}
318enterMonitor(a);
319enterMonitor(b);
320// do stuff
321leaveMonitor(b);
322leaveMonitor(a);
323\end{lstlisting} & != &\begin{lstlisting}
324enterMonitor(a);
325enterMonitor(a, b);
326// do stuff
327leaveMonitor(a, b);
328leaveMonitor(a);
329\end{lstlisting}
330\end{tabular}
331\end{center}
332
333This is not intuitive because even if both methods will display the same monitors state both inside and outside the critical section respectively, the behavior is different. Furthermore, the actual acquiring order will be exaclty the same since acquiring a monitor from inside its mutual exclusion is a no-op. This means that even if the data and the actual control flow are the same using both methods, the behavior of the \code{wait} will be different. The alternative is option 2, that is releasing \underline{actually} acquired monitors. This solves the issue of having the two acquiring method differ at the cost of making routine \code{foo} behave differently depending on from which context it is called (Context 1 or 2). Indeed in Context 2, routine \code{foo} will actually behave like routine \code{baz} rather than having the same behavior than in context 1. The fact that both implicit approaches can be unintuitive depending on the perspective may be a sign that the explicit approach is superior.
334\\
335
336The following examples shows three alternatives of explicit wait semantics :
337\\
338
339\begin{center}
340\begin{tabular}{|c|c|c|}
341Case 1 & Case 2 & Case 3 \\
342Branding on construction & Explicit release list & Explicit ignore list \\
343\hline
344\begin{lstlisting}
345void foo(monitor & mutex a,
346         monitor & mutex b,
347           condition & c)
348{
349        // Releases monitors
350        // branded in ctor
351        wait(c);
352}
353
354monitor a;
355monitor b;
356condition1 c1 = {a};
357condition2 c2 = {a, b};
358
359//Will release only a
360foo(a,b,c1);
361
362//Will release a and b
363foo(a,b,c2);
364\end{lstlisting} &\begin{lstlisting}
365void foo(monitor & mutex a,
366         monitor & mutex b,
367           condition & c)
368{
369        // Releases monitor a
370        // Holds monitor b
371        waitRelease(c, [a]);
372}
373
374monitor a;
375monitor b;
376condition c;
377
378
379
380foo(a,b,c);
381
382
383
384\end{lstlisting} &\begin{lstlisting}
385void foo(monitor & mutex a,
386         monitor & mutex b,
387           condition & c)
388{
389        // Releases monitor a
390        // Holds monitor b
391        waitHold(c, [b]);
392}
393
394monitor a;
395monitor b;
396condition c;
397
398
399
400foo(a,b,c);
401
402
403
404\end{lstlisting}
405\end{tabular}
406\end{center}
407(Note : Case 2 and 3 use tuple semantics to pass a variable length list of elements.)
408\\
409
410All these cases have there pros and cons. Case 1 is more distinct because it means programmers need to be carefull about where the condition was initialized as well as where it is used. On the other hand, it is very clear and explicit which monitor will be released and which monitor will stay acquired. This is similar to Case 2, which releases only the monitors explictly listed. However, in Case 2, calling the \code{wait} routine instead of the \code{waitRelease} routine will release all the acquired monitor. The Case 3 is an improvement on that since it releases all the monitors except those specified. The result is that the \code{wait} routine can be written as follows :
411\begin{lstlisting}
412void wait(condition & cond) {
413        waitHold(cond, []);
414}
415\end{lstlisting}
416This alternative offers nice and consistent behavior between \code{wait} and \code{waitHold}. However, one large pitfall is that mutual exclusion can now be violated by calls to library code. Indeed, even if the following example seems benign there is one significant problem :
417\begin{lstlisting}
418extern void doStuff();
419
420void foo(monitor & mutex m) {
421        //...
422        doStuff(); //warning can release monitor m
423        //...
424}
425\end{lstlisting}
426
427Indeed, if Case 2 or 3 are chosen it any code can violate the mutual exclusion of calling code by issuing calls to \code{wait} or \code{waitHold} in a nested monitor context. Case 2 can be salvaged by removing the \code{wait} routine from the API but Case 3 cannot prevent users from calling \code{waitHold(someCondition, [])}. For this reason the syntax proposed in Case 3 is rejected. Note that syntaxes proposed in case 1 and 2 are not exclusive. Indeed, by supporting two types of condition as follows both cases can be supported :
428\begin{lstlisting}
429struct condition { /*...*/ };
430
431// Second argument is a variable length tuple.
432void wait(condition & cond, [...] monitorsToRelease);
433void signal(condition & cond);
434
435struct conditionN { /*...*/ };
436
437void ?{}(conditionN* this, /*list of N monitors to release*/);
438void wait(conditionN & cond);
439void signal(conditionN & cond);
440\end{lstlisting}
441
442Regardless of the option chosen for wait semantics, signal must be symmetrical. In all cases, signal only needs a single parameter, the condition variable that needs to be signalled. But \code{signal} needs to be called from the same monitor(s) than the call to \code{wait}. Otherwise, mutual exclusion cannot be properly transferred back to the waiting monitor.
443
444\subsection{External scheduling} \label{extsched}
445\textbf{\large{Work in progress...}}
446As one might expect, the alternative to Internal scheduling is to use external scheduling instead. The goal of external scheduling is to be able to have the same scheduling power as internal scheduling without the requirement that any thread can acquire the monitor lock. This method is somewhat more robust to deadlocks since one of the threads keeps a relatively tight control on scheduling. External scheduling can generally be done either in terms of control flow (see \uC) or in terms of data (see Go). Of course, both of these paradigms have their own strenghts and weaknesses but for this project control flow semantics where chosen to stay consistent with the reset of the languages semantics. Two challenges specific to \CFA arise when trying to add external scheduling which is loose object definitions and multi-monitor routines.
447
448\subsubsection{Loose object definitions}
449In \uC monitor definitions include an exhaustive list of monitor operations :
450\begin{lstlisting}
451        _Monitor blarg {
452        public:
453                void f() { _Accept(g); }
454                void g();
455        private:
456        }
457\end{lstlisting}
458
459Since \CFA is not an object oriented it becomes much more difficult to implement but also much less clear for the user :
460
461\begin{lstlisting}
462        mutex struct A {};
463
464        void f(A & mutex a) { accept(g); }
465        void g(A & mutex a);
466\end{lstlisting}
467
468While this is the direct translation of the \uC code, at the time of compiling routine \code{f} the \CFA does not already have a declaration of \code{g} while the \uC compiler does. This means that either the compiler has to dynamically find which routines are "acceptable" or the language needs a way of statically listing "acceptable" routines. Since \CFA has no existing concept that resemble dynamic routine definitions or pattern matching, the static approach seems the more consistent with the current language paradigms. This approach leads to the \uC example being translated to :
469\begin{lstlisting}
470        accept( void g(mutex struct A & mutex a) )
471        mutex struct A {};
472
473        void f(A & mutex a) { accept(g); }
474        void g(A & mutex a);
475\end{lstlisting}
476
477This syntax is the most consistent with the language since it somewhat mimics the \code{forall} declarations. However, the fact that it comes before the struct declaration does means the type needs to be forward declared (done inline in the example). Here are a few alternatives to this syntax : \\
478\begin{tabular}[t]{l l}
479Alternative 1 & Alternative 2 \\
480\begin{lstlisting}
481mutex struct A
482accept( void g(A & mutex a) )
483{};
484\end{lstlisting} &\begin{lstlisting}
485mutex struct A {}
486accept( void g(A & mutex a) );
487
488\end{lstlisting} \\
489Alternative 3 & Alternative 4 \\
490\begin{lstlisting}
491mutex struct A {
492        accept( void g(A & mutex a) )
493};
494
495\end{lstlisting} &\begin{lstlisting}
496mutex struct A {
497        accept :
498                void g(A & mutex a) );
499};
500\end{lstlisting}
501\end{tabular}
502
503
504An other aspect to consider is what happens if multiple overloads of the same routine are used. For the time being it is assumed that multiple overloads of the same routine should be scheduled regardless of the overload used. However, this could easily be extended in the future.
505
506\subsubsection{Multi-monitor scheduling}
507
508External scheduling, like internal scheduling, becomes orders of magnitude more complex when we start introducing multi-monitor syntax. Even in the simplest possible case some new semantics need to be established :
509\begin{lstlisting}
510        accept( void f(mutex struct A & mutex this))
511        mutex struct A {};
512
513        mutex struct B {};
514
515        void g(A & mutex a, B & mutex b) {
516                accept(f); //ambiguous, which monitor
517        }
518\end{lstlisting}
519
520The obvious solution is to specify the correct monitor as follows :
521
522\begin{lstlisting}
523        accept( void f(mutex struct A & mutex this))
524        mutex struct A {};
525
526        mutex struct B {};
527
528        void g(A & mutex a, B & mutex b) {
529                accept( f, b );
530        }
531\end{lstlisting}
532
533This is unambiguous. The both locks will be acquired and kept, when routine \code{f} is called the lock for monitor \code{a} will be temporarily transferred from \code{g} to \code{f} (while \code{g} still holds lock \code{b}). This behavior can be extended to multi-monitor accept statment as follows.
534
535\begin{lstlisting}
536        accept( void f(mutex struct A & mutex, mutex struct A & mutex))
537        mutex struct A {};
538
539        mutex struct B {};
540
541        void g(A & mutex a, B & mutex b) {
542                accept( f, b, a );
543        }
544\end{lstlisting}
545
546Note that the set of monitors passed to the \code{accept} statement must be entirely contained in the set of monitor already acquired in the routine. \code{accept} used in any other context is Undefined Behaviour.
547
548\subsection{Implementation Details}
549\textbf{\large{Work in progress...}}
550\subsubsection{Interaction with polymorphism}
551At first glance, interaction between monitors and \CFA's concept of polymorphism seem complexe to support. However, it can be reasoned that entry-point locking can solve most of the issues that could be present with polymorphism.
552
553First of all, interaction between \code{otype} polymorphism and monitors is impossible since monitors do not support copying. Therefore the main question is how to support \code{dtype} polymorphism. We must remember that monitors' main purpose is to ensure mutual exclusion when accessing shared data. This implies that mutual exclusion is only required for routines that do in fact access shared data. However, since \code{dtype} polymorphism always handle incomplete types (by definition) no \code{dtype} polymorphic routine can access shared data since the data would require knowledge about the type. Therefore the only concern when combining \code{dtype} polymorphism and monitors is to protect access to routines. With callsite-locking, this would require significant amount of work since any \code{dtype} routine could have to obtain some lock before calling a routine. However, with entry-point-locking calling a monitor routine becomes exactly the same as calling it from anywhere else.
554
555\subsubsection{External scheduling queues}
556To support multi-monitor external scheduling means that some kind of entry-queues must be used that is aware of both monitors. However, acceptable routines must be aware of the entry queues which means they most be stored inside at least one of the monitors that will be acquired. This in turn adds the requirement a systematic algorithm of disambiguating which queue is relavant regardless of user ordering. The proposed algorithm is to fall back on monitors lock ordering and specify that the monitor that is acquired first is the lock with the relevant entry queue. This assumes that the lock acquiring order is static for the lifetime of all concerned objects gut that is a reasonnable contraint. This algorithm choice has two consequences, the ofthe highest priority monitor is no longer a true FIFO queue and the queue of the lowest priority monitor is both required and probably unused. The queue can no longer be a FIFO queue because instead of simply containing the waiting threads in order arrival, they also contain the second mutex. Therefore, another thread with the same highest priority monitor but a different lowest priority monitor may arrive first but enter the critical section after a thread with the correct pairing. Secondly, since it may not be known at compile time which monitor will be the lowest priority monitor, every monitor needs to have the correct queues even though it is probably that half the multi-monitor queues will go unused for the entire duration of the program.
557
558\section{Parrallelism}
559
560\section{Tasks}
561
562\section{Naming}
563
564\section{Future work}
565
566\section*{Acknowledgements}
567
568
569
570\bibliographystyle{plain}
571\bibliography{citations}
572
573
574\end{document}
Note: See TracBrowser for help on using the repository browser.