Index: doc/proposals/concurrency/text/concurrency.tex
===================================================================
--- doc/proposals/concurrency/text/concurrency.tex	(revision e0a653db8aa583cd75afd672597c8a9f188a1add)
+++ doc/proposals/concurrency/text/concurrency.tex	(revision 21a1efb16da9a4e2b7d1a53dcfcd1d812920d346)
@@ -300,5 +300,5 @@
 % ======================================================================
 % ======================================================================
-It easier to understand the problem of multi-monitor scheduling using a series of pseudo-code. Note that for simplicity in the following snippets of pseudo-code, waiting and signalling is done using an implicit condition variable, like Java built-in monitors.
+It is easier to understand the problem of multi-monitor scheduling using a series of pseudo-code. Note that for simplicity in the following snippets of pseudo-code, waiting and signalling is done using an implicit condition variable, like Java built-in monitors.
 
 \begin{multicols}{2}
@@ -397,5 +397,5 @@
 \end{center}
 
-It is particularly important to pay attention to code sections 8 and 3, which are where the existing semantics of internal scheduling need to be extended for multiple monitors. The root of the problem is that \gls{group-acquire} 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 A \& B" (line 16), it must actually transfer ownership of monitor B to the waiting thread. This ownership trasnfer is required in order to prevent barging. Since the signalling thread still needs the monitor A, simply waking up the waiting thread is not an option because it would violate mutual exclusion. There are three options:
+It is particularly important to pay attention to code sections 8 and 4, which are where the existing semantics of internal scheduling need to be extended for multiple monitors. The root of the problem is that \gls{group-acquire} 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 A \& B" (line 16), it must actually transfer ownership of monitor B to the waiting thread. This ownership trasnfer is required in order to prevent barging. Since the signalling thread still needs the monitor A, simply waking up the waiting thread is not an option because it would violate mutual exclusion. There are three options:
 
 \subsubsection{Delaying signals}
@@ -467,5 +467,5 @@
 Note that ordering is not determined by a race condition but by whether signalled threads are enqueued in FIFO or FILO order. However, regardless of the answer, users can move line 15 before line 11 and get the reverse effect.
 
-In both cases however, the threads need to be able to distinguish on a per monitor basis which ones need to be released and which ones need to be transferred. Which means monitors cannot be handled as a single homogenous group.
+In both cases, the threads need to be able to distinguish on a per monitor basis which ones need to be released and which ones need to be transferred. Which means monitors cannot be handled as a single homogenous group.
 
 \subsubsection{Dependency graphs}
@@ -497,5 +497,5 @@
 Resolving dependency graph being a complex and expensive endeavour, this solution is not the preffered one.
 
-\subsubsection{Partial signalling}
+\subsubsection{Partial signalling} \label{partial-sig}
 Finally, the solution that is chosen for \CFA is to use partial signalling. Consider the following case:
 
@@ -605,9 +605,57 @@
 % ======================================================================
 % ======================================================================
-\subsection{Internal scheduling: Implementation} \label{insched-impl}
-% ======================================================================
-% ======================================================================
-\TODO
-
+\subsection{Internal scheduling: Implementation} \label{inschedimpl}
+% ======================================================================
+% ======================================================================
+There are several challenges specific to \CFA when implementing internal scheduling. These challenges are direct results of \gls{group-acquire} and loose object definitions. These two constraints are to root cause of most design decisions in the implementation of internal scheduling. Furthermore, to avoid the head-aches of dynamically allocating memory in a concurrent environment, the internal-scheduling design is entirely free of mallocs and other dynamic memory allocation scheme. This is to avoid the chicken and egg problem of having a memory allocator that relies on the threading system and a threading system that relies on the runtime. This extra goal, means that memory management is a constant concern in the design of the system.
+
+The main memory concern for concurrency is queues. All blocking operations are made by parking threads onto queues. These queues need to be intrinsic\cit to avoid the need memory allocation. This entails that all the fields needed to keep track of all needed information. Since internal scheduling can use an unbound amount of memory (depending on \gls{group-acquire}) statically defining information information in the intrusive fields of threads is insufficient. The only variable sized container that does not require memory allocation is the callstack, which is heavily used in the implementation of internal scheduling. Particularly the GCC extension variable length arrays which is used extensively.
+
+Since stack allocation is based around scope, the first step of the implementation is to identify the scopes that are available to store the information, and which of these can have a variable length. In the case of external scheduling, the threads and the condition both allow a fixed amount of memory to be stored, while mutex-routines and the actual blocking call allow for an unbound amount (though adding too much to the mutex routine stack size can become expansive faster).
+
+The following figure is the traditionnal illustration of a monitor :
+
+\begin{center}
+{\resizebox{0.4\textwidth}{!}{\input{monitor}}}
+\end{center}
+
+For \CFA, the previous picture does not have support for blocking multiple monitors on a single condition. To support \gls{group-acquire} two changes to this picture are required. First, it doesn't make sense to tie the condition to a single monitor since blocking two monitors as one would require arbitrarily picking a monitor to hold the condition. Secondly, the object waiting on the conditions and AS-stack cannot simply contain the waiting thread since a single thread can potentially wait on multiple monitors. As mentionned in section \ref{inschedimpl}, the handling in multiple monitors is done by partially passing, which entails that each concerned monitor needs to have a node object. However, for waiting on the condition, since all threads need to wait together, a single object needs to be queued in the condition. Moving out the condition and updating the node types yields :
+
+\begin{center}
+{\resizebox{0.8\textwidth}{!}{\input{int_monitor}}}
+\end{center}
+
+\newpage
+
+This picture and the proper entry and leave algorithms is the fundamental implementation of internal scheduling.
+
+\begin{multicols}{2}
+Entry
+\begin{pseudo}[numbers=left]
+if monitor is free
+	enter
+elif I already own the monitor
+	continue
+else
+	block
+increment recursion
+
+\end{pseudo}
+\columnbreak
+Exit
+\begin{pseudo}[numbers=left, firstnumber=8]
+decrement recursion
+if recursion == 0
+	if signal_stack not empty
+		set_owner to thread
+		if all monitors ready
+			wake-up thread
+
+	if entry queue not empty
+		wake-up thread
+\end{pseudo}
+\end{multicols}
+
+Some important things to notice about the exit routine. The solution discussed in \ref{inschedimpl} can be seen on line 11 of the previous pseudo code. Basically, the solution boils down to having a seperate data structure for the condition queue and the AS-stack, and unconditionally transferring ownership of the monitors but only unblocking the thread when the last monitor has trasnferred ownership. This solution is safe as well as preventing any potential barging.
 
 % ======================================================================
@@ -644,5 +692,5 @@
 		inUse = true;
 	}
-	void g() {
+	void V() {
 		inUse = false;
 
@@ -652,7 +700,7 @@
 \end{tabular}
 \end{center}
-This method is more constrained and explicit, which may help users tone down the undeterministic nature of concurrency. Indeed, as the following examples demonstrates, external scheduling allows users to wait for events from other threads without the concern of unrelated events occuring. External scheduling can generally be done either in terms of control flow (e.g. \uC) or in terms of data (e.g. Go). Of course, both of these paradigms have their own strenghts and weaknesses but for this project control flow semantics where chosen to stay consistent with the rest of the languages semantics. Two challenges specific to \CFA arise when trying to add external scheduling with loose object definitions and multi-monitor routines. The following example shows a simple use \code{accept} versus \code{wait}/\code{signal} and its advantages.
-
-In the case of internal scheduling, the call to \code{wait} only guarantees that \code{g} is the last routine to access the monitor. This entails that the routine \code{f} may have acquired mutual exclusion several times while routine \code{h} was waiting. On the other hand, external scheduling guarantees that while routine \code{h} was waiting, no routine other than \code{g} could acquire the monitor.
+This method is more constrained and explicit, which may help users tone down the undeterministic nature of concurrency. Indeed, as the following examples demonstrates, external scheduling allows users to wait for events from other threads without the concern of unrelated events occuring. External scheduling can generally be done either in terms of control flow (e.g., \uC) or in terms of data (e.g. Go). Of course, both of these paradigms have their own strenghts and weaknesses but for this project control-flow semantics were chosen to stay consistent with the rest of the languages semantics. Two challenges specific to \CFA arise when trying to add external scheduling with loose object definitions and multi-monitor routines. The previous example shows a simple use \code{_Accept} versus \code{wait}/\code{signal} and its advantages. Note that while other languages often use \code{accept} as the core external scheduling keyword, \CFA uses \code{waitfor} to prevent name collisions with existing socket APIs.
+
+In the case of internal scheduling, the call to \code{wait} only guarantees that \code{V} is the last routine to access the monitor. This entails that the routine \code{V} may have acquired mutual exclusion several times while routine \code{P} was waiting. On the other hand, external scheduling guarantees that while routine \code{P} was waiting, no routine other than \code{V} could acquire the monitor.
 
 % ======================================================================
@@ -667,8 +715,11 @@
 
 	void f(A & mutex a);
-	void g(A & mutex a) { accept(f); }
-\end{cfacode}
-
-However, external scheduling is an example where implementation constraints become visible from the interface. Indeed, since there is no hard limit to the number of threads trying to acquire a monitor concurrently, performance is a significant concern. Here is the pseudo code for the entering phase of a monitor:
+	void f(int a, float b);
+	void g(A & mutex a) {
+		waitfor(f); // Less obvious which f() to wait for
+	}
+\end{cfacode}
+
+Furthermore, external scheduling is an example where implementation constraints become visible from the interface. Indeed, since there is no hard limit to the number of threads trying to acquire a monitor concurrently, performance is a significant concern. Here is the pseudo code for the entering phase of a monitor:
 
 \begin{center}
@@ -677,4 +728,6 @@
 	if monitor is free
 		enter
+	elif I already own the monitor
+		continue
 	elif monitor accepts me
 		enter
@@ -685,5 +738,5 @@
 \end{center}
 
-For the \pscode{monitor is free} condition it is easy to implement a check that can evaluate the condition in a few instruction. However, a fast check for \pscode{monitor accepts me} is much harder to implement depending on the constraints put on the monitors. Indeed, monitors are often expressed as an entry queue and some acceptor queue as in the following figure:
+For the fist 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:
 
 \begin{center}
@@ -691,5 +744,5 @@
 \end{center}
 
-There are other alternatives to these pictures but in the case of this picture implementing a fast accept check is relatively easy. Indeed simply updating a bitmask when the acceptor queue changes is enough to have a check that executes in a single instruction, even with a fairly large number (e.g. 128) of mutex members. However, this relies on the fact that all the acceptable routines are declared with the monitor type. For OO languages this does not compromise much since monitors already have an exhaustive list of member routines. However, for \CFA this is not the case; routines can be added to a type anywhere after its declaration. Its important to note that the bitmask approach does not actually require an exhaustive list of routines, but it requires a dense unique ordering of routines with an upper-bound and that ordering must be consistent across translation units.
+There are other alternatives to these pictures but in the case of this picture implementing a fast accept check is relatively easy. Indeed simply updating a bitmask when the acceptor queue changes is enough to have a check that executes in a single instruction, even with a fairly large number (e.g. 128) of mutex members. This technique cannot be used in \CFA because it relies on the fact that the monitor type declares all the acceptable routines. For OO languages this does not compromise much since monitors already have an exhaustive list of member routines. However, for \CFA this is not the case; routines can be added to a type anywhere after its declaration. Its important to note that the bitmask approach does not actually require an exhaustive list of routines, but it requires a dense unique ordering of routines with an upper-bound and that ordering must be consistent across translation units.
 The alternative would be to have a picture more like this one:
 
@@ -698,55 +751,9 @@
 \end{center}
 
-Not storing the queues inside the monitor means that the storage can vary between routines, allowing for more flexibility and extensions. Storing an array of function-pointers would solve the issue of uniquely identifying acceptable routines. However, the single instruction bitmask compare has been replaced by dereferencing a pointer followed by a linear search. Furthermore, supporting nested external scheduling may now require additionnal searches on calls to accept to check if a routine is already queued in.
-
-At this point we must make a decision 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 either cases here are a few alternatives for the different syntaxes this syntax : \\
-\begin{center}
-{\renewcommand{\arraystretch}{1.5}
-\begin{tabular}[t]{l @{\hskip 0.35in} l}
-\hline
-\multicolumn{2}{ c }{\code{accept} on type}\\
-\hline
-Alternative 1 & Alternative 2 \\
-\begin{lstlisting}
-mutex struct A
-accept( void f(A & mutex a) )
-{};
-\end{lstlisting} &\begin{lstlisting}
-mutex struct A {}
-accept( void f(A & mutex a) );
-
-\end{lstlisting} \\
-Alternative 3 & Alternative 4 \\
-\begin{lstlisting}
-mutex struct A {
-	accept( void f(A & mutex a) )
-};
-
-\end{lstlisting} &\begin{lstlisting}
-mutex struct A {
-	accept :
-		void f(A & mutex a) );
-};
-\end{lstlisting}\\
-\hline
-\multicolumn{2}{ c }{\code{accept} on routine}\\
-\hline
-\begin{lstlisting}
-mutex struct A {};
-
-void f(A & mutex a)
-
-accept( void f(A & mutex a) )
-void g(A & mutex a) {
-	/*...*/
-}
-\end{lstlisting}&\\
-\end{tabular}
-}
-\end{center}
-
-Another 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.
+Not storing the queues inside the monitor means that the storage can vary between routines, allowing for more flexibility and extensions. Storing an array of function-pointers would solve the issue of uniquely identifying acceptable routines. However, the single instruction bitmask compare has been replaced by dereferencing a pointer followed by a linear search. Furthermore, supporting nested external scheduling may now require additionnal searches on calls to waitfor to check if a routine is already queued in.
+
+At this point we must make a decision between flexibility and performance. Many design decisions in \CFA achieve both flexibility and performance, for example polymorphic routines add significant flexibility but inlining them means the optimizer can easily remove any runtime cost. Here however, the cost of flexibility cannot be trivially removed. In the end, the most flexible approach has been chosen since it allows users to write programs that would otherwise be prohibitively hard to write. This 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.
+
+Another 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 are considered as distinct routines. However, this could easily be extended in the future.
 
 % ======================================================================
@@ -758,5 +765,4 @@
 External scheduling, like internal scheduling, becomes orders of magnitude more complex when we start introducing multi-monitor syntax. Even in the simplest possible case some new semantics need to be established:
 \begin{cfacode}
-	accept( void f(mutex struct A & mutex this))
 	mutex struct A {};
 
@@ -764,5 +770,5 @@
 
 	void g(A & mutex a, B & mutex b) {
-		accept(f); //ambiguous, which monitor
+		waitfor(f); //ambiguous, which monitor
 	}
 \end{cfacode}
@@ -771,5 +777,4 @@
 
 \begin{cfacode}
-	accept( void f(mutex struct A & mutex this))
 	mutex struct A {};
 
@@ -777,12 +782,11 @@
 
 	void g(A & mutex a, B & mutex b) {
-		accept( b, f );
-	}
-\end{cfacode}
-
-This is unambiguous. 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.
-
-\begin{cfacode}
-	accept( void f(mutex struct A & mutex, mutex struct A & mutex))
+		waitfor( f, b );
+	}
+\end{cfacode}
+
+This is unambiguous. Both locks will be acquired and kept, when routine \code{f} is called the lock for monitor \code{b} will be temporarily transferred from \code{g} to \code{f} (while \code{g} still holds lock \code{a}). This behavior can be extended to multi-monitor waitfor statment as follows.
+
+\begin{cfacode}
 	mutex struct A {};
 
@@ -790,9 +794,34 @@
 
 	void g(A & mutex a, B & mutex b) {
-		accept( b, a, f);
-	}
-\end{cfacode}
-
-Note 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.
+		waitfor( f, a, b);
+	}
+\end{cfacode}
+
+Note that the set of monitors passed to the \code{waitfor} statement must be entirely contained in the set of monitor already acquired in the routine. \code{waitfor} used in any other context is Undefined Behaviour.
+
+An important behavior to note is that what happens when set of monitors only match partially :
+
+\begin{cfacode}
+	mutex struct A {};
+
+	mutex struct B {};
+
+	void g(A & mutex a, B & mutex b) {
+		waitfor(f, a, b);
+	}
+
+	A a1, a2;
+	B b;
+
+	void foo() {
+		g(a1, b);
+	}
+
+	void bar() {
+		f(a2, b);
+	}
+\end{cfacode}
+
+While the equivalent can happen when using internal scheduling, the fact that conditions are branded on first use means that users have to use two different condition variables. In both cases, partially matching monitor sets will not wake-up the waiting thread. It is also important to note that in the case of external scheduling, as for routine calls, the order of parameters is important; \code{waitfor(f,a,b)} and \code{waitfor(f,b,a)} are to distinct waiting condition.
 
 % ======================================================================
