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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 7b69174 was 7b69174, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

New proposal version for Internal Scheduling semantics written

  • Property mode set to 100644
File size: 27.8 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{listings}                                                                   % format program code
28\usepackage[flushmargin]{footmisc}                                              % support label/reference in footnote
29\usepackage{latexsym}                                   % \Box glyph
30\usepackage{mathptmx}                                   % better math font with "times"
31\usepackage[usenames]{color}
32\usepackage[pagewise]{lineno}
33\renewcommand{\linenumberfont}{\scriptsize\sffamily}
34\input{common}                                          % bespoke macros used in the document
35\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
36\usepackage{breakurl}
37\renewcommand{\UrlFont}{\small\sf}
38
39\setlength{\topmargin}{-0.45in}                                                 % move running title into header
40\setlength{\headsep}{0.25in}
41
42%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43
44% Names used in the document.
45
46\newcommand{\Version}{1.0.0}
47\newcommand{\CS}{C\raisebox{-0.9ex}{\large$^\sharp$}\xspace}
48
49\newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}}
50\newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}}
51\newcommand{\R}[1]{\Textbf{#1}}
52\newcommand{\B}[1]{{\Textbf[blue]{#1}}}
53\newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
54\newcommand{\uC}{$\mu$\CC}
55\newcommand{\cit}{\textsuperscript{[Citation Needed]}\xspace}
56
57
58\newsavebox{\LstBox}
59
60%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61
62\setcounter{secnumdepth}{3}                             % number subsubsections
63\setcounter{tocdepth}{3}                                % subsubsections in table of contents
64\makeindex
65
66%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67
68\begin{document}
69% \linenumbers
70
71\title{Concurrency in \CFA}
72\author{Thierry Delisle \\
73Dept. of Computer Science, University of Waterloo, \\ Waterloo, Ontario, Canada
74}
75
76\maketitle
77\section{Introduction}
78This 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.
79Indeed, for higly productive parallel programming high-level approaches are much more popular. Examples are task based parallelism, message passing, implicit threading.
80
81There are actually to 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. Concurrency tools need to handle mutual exclusion and synchronization while parallelism tools are more about performance, cost and ressource utilisation.
82
83\section{Concurrency}
84Several 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 states 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
85
86Finally, an approach that 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.
87
88\section{Monitors}
89A 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\cit 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 :
90\begin{lstlisting}
91        typedef \*some monitor type*\ monitor;
92        int f(monitor& m);
93
94        int main() {
95                monitor m;
96                f(m);
97        }
98\end{lstlisting}
99
100\subsection{Call semantics}
101The 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.
102
103Another 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 external helper routines (\texttt{swap}, \texttt{sort}, etc.) or internal helper routines like the following example :
104
105\begin{lstlisting}
106
107\end{lstlisting}
108
109Having both \texttt{mutex} and \texttt{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 \texttt{h} then one could argue that it should be to default to \texttt{mutex} to be safe by default. On the other hand, making one of these keywords mandatory would provide the same semantics but without the ambiguity of supporting routine \texttt{h}. Mandatory keywords would also have the added benefice of being more clearly self-documented. In any case, the option of having routine \texttt{h} mean \texttt{nomutex} should be rejected since it is unsafe by default and may easily cause subtle errors.
110
111Furthermore, it is important to establish when mutex/nomutex may be used depending on type parameters.
112\begin{lstlisting}
113        int f01(monitor& mutex m);
114        int f02(const monitor& mutex m);
115        int f03(monitor* mutex m);
116        int f04(monitor* mutex * m);
117        int f05(monitor** mutex m);
118        int f06(monitor[10] mutex m);
119        int f07(monitor[] mutex m);
120        int f08(vector(monitor)& mutex m);
121        int f09(list(monitor)& mutex m);
122        int f10([monitor*, int]& mutex m);
123        int f11(graph(monitor*)& mutex m);
124\end{lstlisting}
125
126For the first routines it seems to make sense to support the mutex keyword for such small variations. The difference between pointers and reference (\texttt{f01} vs \texttt{f03}) or const and non-const (\texttt{f01} vs \texttt{f02}) has no significance to mutual exclusion. It may not always make sense to acquire the monitor when extra dereferences (\texttt{f04}, \texttt{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(\texttt{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 (\texttt{f07}) or a higher-level container (\texttt{f08}, \texttt{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 \texttt{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. Where to draw the lines is up for debate but it seems reasonnable to consider \texttt{f03} as accepted and \texttt{f06} as rejected.
127
128\subsection{Data semantics}
129Once 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 :
130\begin{lstlisting}
131        mutex struct counter_t {
132                int value;
133        };
134
135        void ?{}(counter_t& mutex this) {
136                this.cnt = 0;
137        }
138
139        int ++?(counter_t& mutex this) {
140                return ++this->value;
141        }
142
143        void ?{}(int* this, counter_t& mutex cnt) {
144                *this = (int)cnt;
145        }
146\end{lstlisting}
147\begin{tabular}{ c c }
148Thread 1 & Thread 2 \\
149\begin{lstlisting}
150        void main(counter_t& mutex c) {
151                for(;;) {
152                        int count = c;
153                        sout | count | endl;
154                }
155        }
156\end{lstlisting}&\begin{lstlisting}
157        void main(counter_t& mutex c) {
158                for(;;) {
159                        ++c;
160                }
161        }
162
163\end{lstlisting}
164\end{tabular}
165\\
166
167
168This simple counter monitor offers an example of monitor usage. Notice how the counter is used without any explicit synchronisation and yet is perfectly safe reglardless of how many threads use it simultaneously. \\
169
170These simple mutual exclusion semantics also naturally expand to multi-monitor calls.
171\begin{lstlisting}
172        int f(MonitorA& mutex a, MonitorB& mutex b);
173
174        MonitorA a;
175        MonitorB b;
176        f(a,b);
177\end{lstlisting}
178This 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. The ability to acquire multiple monitors at the same time does incur a significant pitfall even without looking into scheduling. For example :
179\begin{lstlisting}
180        void foo(A& mutex a, B& mutex a) {
181                //...
182        }
183
184        void bar(A& mutex a, B& mutex a)
185                //...
186                foo(a, b);
187                //...
188        }
189\end{lstlisting}
190
191
192% Here, there is a language design choice that has to be made. It is impossible to protect the user from both barging and deadlocks and therefore this code has the potential to deadlock if some other threads try to acquire the locks in a different order (keep in mind that the lock ordering may be invisible or non-deterministic). The alternative is to allow the algorithm to release the lock on monitor \texttt{a}. This would effectively prevent the deadlock but could also mean that mutual exclusion may be dropped in the midle of routine \texttt{bar}.
193%
194% Indeed, there are two options for acquiring multiple locks while preventing deadlocks. The first option is to prescribe some arbitrary order of locking. If used consistently in the application this solution is both deadlock-free and barging-free. However, it also relies on the user to consistently follow the ordering when manually specifying the order. If the lock ordering is based on lock creation order or heap address ordering, it may be impossible for users to statically predict the correct lock acquiring order which means that deadlocks are a very real possibility. On the other hand, if the locking algorithm tries to dynamically find the correct lock ordering then it must release all locks after each wrong ordering attempts. This does not cause any significant issue in the context where a users tries to acquire multiple locks at once since the thread is not already in a critical section. However, if the thread was already holding a lock then releasing all locks on failed attempts may mean violating the mutual exclusion of the critical section. Notice that this is only an issue when nested mutex routines are used, in any other case monitors will behave consistently between both algorithms. Since releasing a lock in the middle of a critical section effectively violates mutual exclusion, it seems reasonnable to reject algorithms that dynamically guess the order of lock acquiring since users need to be very comfortable with multi-lock semantics before they can expect nested monitor calls to end-up releasing locks.
195
196
197\subsubsection{Internal scheduling}
198Monitors 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 :
199
200\begin{lstlisting}
201        mutex struct A {
202                condition e;
203        }
204
205        void foo(A& mutex a) {
206                //...
207                wait(a.e);
208                //...
209        }
210
211        void bar(A& mutex a) {
212                signal(a.e);
213        }
214\end{lstlisting}
215
216Here routine \texttt{foo} waits on the \texttt{signal} from \texttt{bar} before making further progress, effectively ensuring a basic ordering. This can easily be extended to multi-monitor calls by offering the same guarantee.
217
218\begin{tabular}{ c c }
219Thread 1 & Thread 2 \\
220\begin{lstlisting}
221void foo(monitor& mutex a, monitor& mutex b) {
222        //...
223        wait(a.e);
224        //...
225}
226
227foo(a, b);
228\end{lstlisting}&\begin{lstlisting}
229void bar(monitor& mutex a, monitor& mutex b) {
230        signal(a.e);
231}
232
233
234
235bar(a, b);
236\end{lstlisting}
237\end{tabular}
238\\
239
240A 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 :
241
242\begin{tabular}{|c|c|c|}
243Context 1 & Context 2 & Context 3 \\
244\hline
245\begin{lstlisting}
246void foo(monitor& mutex a,
247         monitor& mutex b) {
248        wait(a.e);
249}
250
251
252
253
254
255
256foo(a,b);
257\end{lstlisting}&\begin{lstlisting}
258void bar(monitor& mutex a,
259         monitor& nomutex b) {
260        foo(a,b);
261}
262
263void foo(monitor& mutex a,
264         monitor& mutex b) {
265        wait(a.e);
266}
267
268bar(a, b);
269\end{lstlisting}&\begin{lstlisting}
270void bar(monitor& mutex a,
271         monitor& nomutex b) {
272        foo(a,b);
273}
274
275void baz(monitor& nomutex a,
276         monitor& mutex b) {
277        wait(a.e);
278}
279
280bar(a, b);
281\end{lstlisting}
282\end{tabular}
283\\
284
285This can be interpreted in two different ways.
286\begin{enumerate}
287        \item \texttt{wait} atomically releases the monitors \underline{theoretically} acquired by the inner-most mutex routine.
288        \item \texttt{wait} atomically releases the monitors \underline{actually} acquired by the inner-most mutex routine.
289\end{enumerate}
290While the difference between these two is subtle, it has a significant impact. In the first case it means that the calls to \texttt{foo} would behave the same in Context 1 and 2. This semantic would also mean that the call to \texttt{wait} in routine \texttt{baz} would only release \texttt{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.
291
292\begin{center}
293\begin{tabular}{c c c}
294\begin{lstlisting}
295enterMonitor(a);
296enterMonitor(b);
297// do stuff
298leaveMonitor(b);
299leaveMonitor(a);
300\end{lstlisting}& != &\begin{lstlisting}
301enterMonitor(a);
302enterMonitor(a, b);
303// do stuff
304leaveMonitor(a, b);
305leaveMonitor(a);
306\end{lstlisting}
307\end{tabular}
308\end{center}
309
310This 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 \texttt{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 \texttt{foo} behave differently depending on from which context it is called (Context 1 or 2). Indeed in Context 2, routine \texttt{foo} will actually behave like routine \texttt{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.
311\\
312
313The following examples shows three alternatives of explicit wait semantics :
314\\
315
316\begin{tabular}{|c|c|c|}
317Case 1 & Case 2 & Case 3 \\
318Branding on construction & Explicit release list & Explicit ignore list \\
319\hline
320\begin{lstlisting}
321void foo(monitor& mutex a,
322         monitor& mutex b,
323           condition& c)
324{
325        // Releases monitors
326        // branded on construction
327        wait(c);
328}
329
330monitor a;
331monitor b;
332condition1 c1 = {a};
333condition2 c2 = {a, b};
334
335//Will release only a
336foo(a,b,c1);
337
338//Will release a and b
339foo(a,b,c2);
340\end{lstlisting}&\begin{lstlisting}
341void foo(monitor& mutex a,
342         monitor& mutex b,
343           condition& c)
344{
345        // Releases monitor a
346        // Holds monitor b
347        waitRelease(c, [a]);
348}
349
350monitor a;
351monitor b;
352condition c;
353
354
355
356foo(a,b,c);
357
358
359
360\end{lstlisting}&\begin{lstlisting}
361void foo(monitor& mutex a,
362         monitor& mutex b,
363           condition& c)
364{
365        // Releases monitor a
366        // Holds monitor b
367        waitHold(c, [b]);
368}
369
370monitor a;
371monitor b;
372condition c;
373
374
375
376foo(a,b,c);
377
378
379
380\end{lstlisting}
381\end{tabular}
382
383(Note : Case 2 and 3 use tuple semantics to pass a variable length list of elements.)
384\\
385
386All 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 \texttt{wait} routine instead of the \texttt{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 \texttt{wait} routine can be written as follows :
387\begin{lstlisting}
388void wait(condition& cond) {
389        waitHold(cond, []);
390}
391\end{lstlisting}
392This alternative offers nice and consistent behavior between \texttt{wait} and \texttt{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 :
393\begin{lstlisting}
394extern void doStuff();
395
396void foo(monitor& mutex m) {
397        //...
398        doStuff(); //warning can release monitor m
399        //...
400}
401\end{lstlisting}
402Indeed, if Case 2 or 3 are chosen it any code can violate the mutual exclusion of calling code by issuing calls to \texttt{wait} or \texttt{waitHold} in a nested monitor context. Case 2 can be salvaged by removing the \texttt{wait} routine from the API but Case 3 cannot prevent users from calling \texttt{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 :
403\begin{lstlisting}
404struct condition { /*...*/ };
405
406void wait(condition& cond, [...] monitorsToRelease); // Second argument is a variable length tuple.
407void signal(condition& cond);
408
409struct conditionN { /*...*/ };
410
411void ?{}(conditionN* this, /*list of N monitors to release*/);
412void wait(conditionN& cond);
413void signal(conditionN& cond);
414\end{lstlisting}
415
416Regardless 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 \texttt{signal} needs to be called from the same monitor(s) than the call to \texttt{wait}. Otherwise, mutual exclusion cannot be properly transferred back to the waiting monitor.
417
418\subsection{External scheduling}
419As 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.
420
421\subsubsection{Loose object definitions}
422In \uC monitor definitions include an exhaustive list of monitor operations :
423\begin{lstlisting}
424        _Monitor blarg {
425        public:
426                void f() { _Accept(g); }
427                void g();
428        private:
429        }
430\end{lstlisting}
431
432Since \CFA is not an object oriented it becomes much more difficult to implement but also much less clear for the user :
433
434\begin{lstlisting}
435        mutex struct A {};
436
437        void f(A& mutex a) { accept(g); }
438        void g(A& mutex a);
439\end{lstlisting}
440
441While this is the direct translation of the \uC code, at the time of compiling routine \texttt{f} the \CFA does not already have a declaration of \texttt{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 :
442\begin{lstlisting}
443        accept( void g(mutex struct A& mutex a) )
444        mutex struct A {};
445
446        void f(A& mutex a) { accept(g); }
447        void g(A& mutex a);
448\end{lstlisting}
449
450This syntax is the most consistent with the language since it somewhat mimics the \texttt{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 : \\
451\begin{tabular}[t]{l l}
452Alternative 1 & Alternative 2 \\
453\begin{lstlisting}
454mutex struct A
455accept( void g(A& mutex a) )
456{};
457\end{lstlisting}&\begin{lstlisting}
458mutex struct A {}
459accept( void g(A& mutex a) );
460
461\end{lstlisting} \\
462Alternative 3 & Alternative 4 \\
463\begin{lstlisting}
464mutex struct A {
465        accept( void g(A& mutex a) )
466};
467
468\end{lstlisting}&\begin{lstlisting}
469mutex struct A {
470        accept :
471                void g(A& mutex a) );
472};
473\end{lstlisting}
474\end{tabular}
475
476
477An 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.
478
479\subsubsection{Multi-monitor scheduling}
480
481External 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 :
482\begin{lstlisting}
483        accept( void f(mutex struct A& mutex this))
484        mutex struct A {};
485
486        mutex struct B {};
487
488        void g(A& mutex a, B& mutex b) {
489                accept(f); //ambiguous, which monitor
490        }
491\end{lstlisting}
492
493The obvious solution is to specify the correct monitor as follows :
494
495\begin{lstlisting}
496        accept( void f(mutex struct A& mutex this))
497        mutex struct A {};
498
499        mutex struct B {};
500
501        void g(A& mutex a, B& mutex b) {
502                accept( f, b );
503        }
504\end{lstlisting}
505
506This is unambiguous. The both locks will be acquired and kept, when routine \texttt{f} is called the lock for monitor \texttt{a} will be temporarily transferred from \texttt{g} to \texttt{f} (while \texttt{g} still holds lock \texttt{b}). This behavior can be extended to multi-monitor accept statment as follows.
507
508\begin{lstlisting}
509        accept( void f(mutex struct A& mutex, mutex struct A& mutex))
510        mutex struct A {};
511
512        mutex struct B {};
513
514        void g(A& mutex a, B& mutex b) {
515                accept( f, b, a );
516        }
517\end{lstlisting}
518
519Note that the set of monitors passed to the \texttt{accept} statement must be entirely contained in the set of monitor already acquired in the routine. \texttt{accept} used in any other context is Undefined Behaviour.
520
521\subsection{Implementation Details}
522\subsubsection{Interaction with polymorphism}
523At 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.
524
525First of all, interaction between \texttt{otype} polymorphism and monitors is impossible since monitors do not support copying. Therefore the main question is how to support \texttt{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 \texttt{dtype} polymorphism always handle incomplete types (by definition) no \texttt{dtype} polymorphic routine can access shared data since the data would require knowledge about the type. Therefore the only concern when combining \texttt{dtype} polymorphism and monitors is to protect access to routines. With callsite-locking, this would require significant amount of work since any \texttt{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.
526
527\subsubsection{External scheduling queues}
528To 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.
529
530\section{Parrallelism}
531
532\section{Tasks}
533
534
535\section{Naming}
536
537\section{Future work}
538
539\section*{Acknowledgements}
540
541
542
543\bibliographystyle{plain}
544\bibliography{citations}
545
546
547\end{document}
Note: See TracBrowser for help on using the repository browser.