Changes in / [5da9d6a:d16d159]


Ignore:
Files:
5 added
3 deleted
33 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/concurrency/text/basics.tex

    r5da9d6a rd16d159  
    44% ======================================================================
    55% ======================================================================
    6 Before any detailed discussion of the concurrency and parallelism in \CFA, it is important to describe the basics of concurrency and how they are expressed in \CFA user code.
     6Before any detailed discussion of the concurrency and parallelism in \CFA, it is important to describe the basics of concurrency and how they are expressed in \CFA user-code.
    77
    88\section{Basics of concurrency}
     
    1111Execution with a single thread and multiple stacks where the thread is self-scheduling deterministically across the stacks is called coroutining. Execution with a single and multiple stacks but where the thread is scheduled by an oracle (non-deterministic from the thread perspective) across the stacks is called concurrency.
    1212
    13 Therefore, a minimal concurrency system can be achieved by creating coroutines, which instead of context-switching among each other, always ask an oracle where to context-switch next. While coroutines can execute on the caller?s stack-frame, stack-full coroutines allow full generality and are sufficient as the basis for concurrency. The aforementioned oracle is a scheduler and the whole system now follows a cooperative threading-model (aka non-preemptive scheduling). The oracle/scheduler can either be a stack-less or stack-full entity and correspondingly require one or two context-switches to run a different coroutine. In any case, a subset of concurrency related challenges start to appear. For the complete set of concurrency challenges to occur, the only feature missing is preemption.
    14 
    15 A scheduler introduces order of execution uncertainty, while preemption introduces uncertainty about where context switches occur. Mutual exclusion and synchronization are ways of limiting non-determinism in a concurrent system. Now it is important to understand that uncertainty is desirable; uncertainty can be used by runtime systems to significantly increase performance and is often the basis of giving a user the illusion that tasks are running in parallel. Optimal performance in concurrent applications is often obtained by having as much non-determinism as correctness allows.
    16 
    17 \section{\protect\CFA's Thread Building Blocks}
    18 One of the important features that are missing in C is threading. On modern architectures, a lack of threading is unacceptable~\cite{Sutter05, Sutter05b}, and therefore modern programming languages must have the proper tools to allow users to write efficient concurrent programs to take advantage of parallelism. As an extension of C, \CFA needs to express these concepts in a way that is as natural as possible to programmers familiar with imperative languages. And being a system-level language means programmers expect to choose precisely which features they need and which cost they are willing to pay.
    19 
    20 \section{Coroutines: A Stepping Stone}\label{coroutine}
    21 While the main focus of this proposal is concurrency and parallelism, it is important to address coroutines, which are actually a significant building block of a concurrency system. Coroutines need to deal with context switches and other context-management operations. Therefore, this proposal includes coroutines both as an intermediate step for the implementation of threads, and a first-class feature of \CFA. Furthermore, many design challenges of threads are at least partially present in designing coroutines, which makes the design effort that much more relevant. The core \acrshort{api} of coroutines revolves around two features: independent call-stacks and \code{suspend}/\code{resume}.
     13Therefore, a minimal concurrency system can be achieved by creating coroutines, which instead of context switching among each other, always ask an oracle where to context switch next. While coroutines can execute on the caller's stack-frame, stack-full coroutines allow full generality and are sufficient as the basis for concurrency. The aforementioned oracle is a scheduler and the whole system now follows a cooperative threading-model (a.k.a non-preemptive scheduling). The oracle/scheduler can either be a stack-less or stack-full entity and correspondingly require one or two context switches to run a different coroutine. In any case, a subset of concurrency related challenges start to appear. For the complete set of concurrency challenges to occur, the only feature missing is preemption.
     14
     15A scheduler introduces order of execution uncertainty, while preemption introduces uncertainty about where context-switches occur. Mutual-exclusion and synchronization are ways of limiting non-determinism in a concurrent system. Now it is important to understand that uncertainty is desirable; uncertainty can be used by runtime systems to significantly increase performance and is often the basis of giving a user the illusion that tasks are running in parallel. Optimal performance in concurrent applications is often obtained by having as much non-determinism as correctness allows.
     16
     17\section{\protect\CFA 's Thread Building Blocks}
     18One of the important features that is missing in C is threading. On modern architectures, a lack of threading is unacceptable~\cite{Sutter05, Sutter05b}, and therefore modern programming languages must have the proper tools to allow users to write performant concurrent programs to take advantage of parallelism. As an extension of C, \CFA needs to express these concepts in a way that is as natural as possible to programmers familiar with imperative languages. And being a system-level language means programmers expect to choose precisely which features they need and which cost they are willing to pay.
     19
     20\section{Coroutines: A stepping stone}\label{coroutine}
     21While the main focus of this proposal is concurrency and parallelism, it is important to address coroutines, which are actually a significant building block of a concurrency system. Coroutines need to deal with context-switches and other context-management operations. Therefore, this proposal includes coroutines both as an intermediate step for the implementation of threads, and a first class feature of \CFA. Furthermore, many design challenges of threads are at least partially present in designing coroutines, which makes the design effort that much more relevant. The core \acrshort{api} of coroutines revolve around two features: independent call stacks and \code{suspend}/\code{resume}.
    2222
    2323\begin{table}
     
    133133\end{table}
    134134
    135 A good example of a problem made easier with coroutines is generators, like the Fibonacci sequence. This problem comes with the challenge of decoupling how a sequence is generated and how it is used. Table \ref{lst:fibonacci-c} shows conventional approaches to writing generators in C. All three of these approach suffer from strong coupling. The left and centre approaches require that the generator have knowledge of how the sequence is used, while the rightmost approach requires holding internal state between calls on behalf of the generator and makes it much harder to handle corner cases like the Fibonacci seed.
     135A good example of a problem made easier with coroutines is generators, like the Fibonacci sequence. This problem comes with the challenge of decoupling how a sequence is generated and how it is used. Table \ref{lst:fibonacci-c} shows conventional approaches to writing generators in C. All three of these approach suffer from strong coupling. The left and center approaches require that the generator have knowledge of how the sequence is used, while the rightmost approach requires holding internal state between calls on behalf of the generator and makes it much harder to handle corner cases like the Fibonacci seed.
    136136
    137137Listing \ref{lst:fibonacci-cfa} is an example of a solution to the Fibonacci problem using \CFA coroutines, where the coroutine stack holds sufficient state for the next generation. This solution has the advantage of having very strong decoupling between how the sequence is generated and how it is used. Indeed, this version is as easy to use as the \code{fibonacci_state} solution, while the implementation is very similar to the \code{fibonacci_func} example.
     
    233233One important design challenge for implementing coroutines and threads (shown in section \ref{threads}) is that the runtime system needs to run code after the user-constructor runs to connect the fully constructed object into the system. In the case of coroutines, this challenge is simpler since there is no non-determinism from preemption or scheduling. However, the underlying challenge remains the same for coroutines and threads.
    234234
    235 The runtime system needs to create the coroutine?s stack and more importantly prepare it for the first resumption. The timing of the creation is non-trivial since users both expect to have fully constructed objects once execution enters the coroutine main and to be able to resume the coroutine from the constructor. There are several solutions to this problem but the chosen option effectively forces the design of the coroutine.
    236 
    237 Furthermore, \CFA faces an extra challenge as polymorphic routines create invisible thunks when cast to non-polymorphic routines and these thunks have function scope. For example, the following code, while looking benign, can run into undefined behaviour because of thunks:
     235The runtime system needs to create the coroutine's stack and more importantly prepare it for the first resumption. The timing of the creation is non-trivial since users both expect to have fully constructed objects once execution enters the coroutine main and to be able to resume the coroutine from the constructor. There are several solutions to this problem but the chosen options effectively forces the design of the coroutine.
     236
     237Furthermore, \CFA faces an extra challenge as polymorphic routines create invisible thunks when casted to non-polymorphic routines and these thunks have function scope. For example, the following code, while looking benign, can run into undefined behaviour because of thunks:
    238238
    239239\begin{cfacode}
     
    268268}
    269269\end{ccode}
    270 The problem in this example is a storage management issue, the function pointer \code{_thunk0} is only valid until the end of the block, which limits the viable solutions because storing the function pointer for too long causes Undefined Behaviour; i.e., the stack-based thunk being destroyed before it can be used. This challenge is an extension of challenges that come with second-class routines. Indeed, GCC nested routines also have the limitation that nested routine cannot be passed outside of the declaration scope. The case of coroutines and threads is simply an extension of this problem to multiple call stacks.
     270The problem in this example is a storage management issue, the function pointer \code{_thunk0} is only valid until the end of the block, which limits the viable solutions because storing the function pointer for too long causes Undefined Behavior; i.e., the stack-based thunk being destroyed before it can be used. This challenge is an extension of challenges that come with second-class routines. Indeed, GCC nested routines also have the limitation that nested routine cannot be passed outside of the declaration scope. The case of coroutines and threads is simply an extension of this problem to multiple call-stacks.
    271271
    272272\subsection{Alternative: Composition}
     
    310310symmetric_coroutine<>::yield_type
    311311\end{cfacode}
    312 Often, the canonical threading paradigm in languages is based on function pointers, pthread being one of the most well-known examples. The main problem of this approach is that the thread usage is limited to a generic handle that must otherwise be wrapped in a custom type. Since the custom type is simple to write in \CFA and solves several issues, added support for routine/lambda based coroutines adds very little.
     312Often, the canonical threading paradigm in languages is based on function pointers, pthread being one of the most well known examples. The main problem of this approach is that the thread usage is limited to a generic handle that must otherwise be wrapped in a custom type. Since the custom type is simple to write in \CFA and solves several issues, added support for routine/lambda based coroutines adds very little.
    313313
    314314A variation of this would be to use a simple function pointer in the same way pthread does for threads :
     
    327327This semantics is more common for thread interfaces but coroutines work equally well. As discussed in section \ref{threads}, this approach is superseded by static approaches in terms of expressivity.
    328328
    329 \subsection{Alternative: Trait-Based Coroutines}
    330 
    331 Finally, the underlying approach, which is the one closest to \CFA idioms, is to use trait-based lazy coroutines. This approach defines a coroutine as anything that satisfies the trait \code{is_coroutine} and is used as a coroutine.
     329\subsection{Alternative: Trait-based coroutines}
     330
     331Finally the underlying approach, which is the one closest to \CFA idioms, is to use trait-based lazy coroutines. This approach defines a coroutine as anything that satisfies the trait \code{is_coroutine} and is used as a coroutine.
    332332
    333333\begin{cfacode}
     
    369369
    370370\section{Thread Interface}\label{threads}
    371 The basic building blocks of multithreading in \CFA are \glspl{cfathread}. Both user and kernel threads are supported, where user threads are the concurrency mechanism and kernel threads are the parallel mechanism. User threads offer a flexible and lightweight interface. A thread can be declared using a struct declaration \code{thread} as follows:
     371The basic building blocks of multi-threading in \CFA are \glspl{cfathread}. Both user and kernel threads are supported, where user threads are the concurrency mechanism and kernel threads are the parallel mechanism. User threads offer a flexible and lightweight interface. A thread can be declared using a struct declaration \code{thread} as follows:
    372372
    373373\begin{cfacode}
     
    394394\end{cfacode}
    395395
    396 In this example, threads of type \code{foo} start execution in the \code{void main(foo &)} routine, which prints \code{"Hello World!".} While this thesis encourages this approach to enforce strongly typed programming, users may prefer to use the routine-based thread semantics for the sake of simplicity. With the static semantics it is trivial to write a thread type that takes a function pointer as a parameter and executes it on its stack asynchronously.
     396In this example, threads of type \code{foo} start execution in the \code{void main(foo &)} routine, which prints \code{"Hello World!"}. While this thesis encourages this approach to enforce strongly-typed programming, users may prefer to use the routine-based thread semantics for the sake of simplicity. With the static semantics it is trivial to write a thread type that takes a function pointer as a parameter and executes it on its stack asynchronously.
    397397\begin{cfacode}
    398398typedef void (*voidFunc)(int);
     
    419419int main() {
    420420        FuncRunner f = {hello, 42};
    421         return 0?
    422 }
    423 \end{cfacode}
    424 
    425 A consequence of the strongly typed approach to main is that memory layout of parameters and return values to/from a thread are now explicitly specified in the \acrshort{api}.
     421        return 0'
     422}
     423\end{cfacode}
     424
     425A consequence of the strongly-typed approach to main is that memory layout of parameters and return values to/from a thread are now explicitly specified in the \acrshort{api}.
    426426
    427427Of course for threads to be useful, it must be possible to start and stop threads and wait for them to complete execution. While using an \acrshort{api} such as \code{fork} and \code{join} is relatively common in the literature, such an interface is unnecessary. Indeed, the simplest approach is to use \acrshort{raii} principles and have threads \code{fork} after the constructor has completed and \code{join} before the destructor runs.
  • doc/proposals/concurrency/text/cforall.tex

    r5da9d6a rd16d159  
    77The following is a quick introduction to the \CFA language, specifically tailored to the features needed to support concurrency.
    88
    9 \CFA is an extension of ISO-C and therefore supports all of the same paradigms as C. It is a non-object-oriented system-language, meaning most of the major abstractions have either no runtime overhead or can be opted out easily. Like C, the basics of \CFA revolve around structures and routines, which are thin abstractions over machine code. The vast majority of the code produced by the \CFA translator respects memory layouts and calling conventions laid out by C. Interestingly, while \CFA is not an object-oriented language, lacking the concept of a receiver (e.g., {\tt this}), it does have some notion of objects\footnote{C defines the term objects as : ``region of data storage in the execution environment, the contents of which can represent
    10 values''~\cite[3.15]{C11}}, most importantly construction and destruction of objects. Most of the following code examples can be found on the \CFA website~\cite{www-cfa}.
     9\CFA is an extension of ISO-C and therefore supports all of the same paradigms as C. It is a non-object-oriented system-language, meaning most of the major abstractions have either no runtime overhead or can be opt-out easily. Like C, the basics of \CFA revolve around structures and routines, which are thin abstractions over machine code. The vast majority of the code produced by the \CFA translator respects memory-layouts and calling-conventions laid out by C. Interestingly, while \CFA is not an object-oriented language, lacking the concept of a receiver (e.g., {\tt this}), it does have some notion of objects\footnote{C defines the term objects as : ``region of data storage in the execution environment, the contents of which can represent
     10values''~\cite[3.15]{C11}}, most importantly construction and destruction of objects. Most of the following code examples can be found on the \CFA website~\cite{www-cfa}
    1111
    1212% ======================================================================
     
    7272% ======================================================================
    7373\section{Constructors/Destructors}
    74 Object lifetime is often a challenge in concurrency. \CFA uses the approach of giving concurrent meaning to object lifetime as a means of synchronization and/or mutual exclusion. Since \CFA relies heavily on the lifetime of objects, constructors and destructors is a core feature required for concurrency and parallelism. \CFA uses the following syntax for constructors and destructors :
     74Object life-time is often a challenge in concurrency. \CFA uses the approach of giving concurrent meaning to object life-time as a mean of synchronization and/or mutual exclusion. Since \CFA relies heavily on the life time of objects, constructors and destructors are a core feature required for concurrency and parallelism. \CFA uses the following syntax for constructors and destructors :
    7575\begin{cfacode}
    7676struct S {
     
    135135\end{cfacode}
    136136
    137 Note that the type use for assertions can be either an \code{otype} or a \code{dtype}. Types declared as \code{otype} refer to ``complete'' objects, i.e., objects with a size, a default constructor, a copy constructor, a destructor and an assignment operator. Using \code{dtype,} on the other hand, has none of these assumptions but is extremely restrictive, it only guarantees the object is addressable.
     137Note that the type use for assertions can be either an \code{otype} or a \code{dtype}. Types declares as \code{otype} refer to ``complete'' objects, i.e., objects with a size, a default constructor, a copy constructor, a destructor and an assignment operator. Using \code{dtype} on the other hand has none of these assumptions but is extremely restrictive, it only guarantees the object is addressable.
    138138
    139139% ======================================================================
  • doc/proposals/concurrency/text/concurrency.tex

    r5da9d6a rd16d159  
    44% ======================================================================
    55% ======================================================================
    6 Several tools 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 calls). This distinction in turn means that, in order to be effective, programmers need to learn two sets of design patterns. While this distinction can be hidden away in library code, effective use of the library still has to take both paradigms into account.
     6Several 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.
    77
    88Approaches 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}.
    99
    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 system languages, which is why it was rejected as the core paradigm for concurrency in \CFA.
    11 
    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.
     10An 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.
     11
     12One 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.
    1313
    1414\section{Basics}
     
    1919
    2020\subsection{Synchronization}
    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 mechanisms 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.
     21As 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.
    2222
    2323% ======================================================================
     
    2626% ======================================================================
    2727% ======================================================================
    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 requirement is the ability to declare a handle to a shared object and a set of routines that act on it :
     28A 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 :
    2929\begin{cfacode}
    3030typedef /*some monitor type*/ monitor;
     
    3939% ======================================================================
    4040% ======================================================================
    41 \subsection{Call Semantics} \label{call}
     41\subsection{Call semantics} \label{call}
    4242% ======================================================================
    4343% ======================================================================
     
    103103int f5(graph(monitor*) & mutex m);
    104104\end{cfacode}
    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 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:
     105The 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:
    106106\begin{cfacode}
    107107int f1(monitor& mutex m);   //Okay : recommended case
     
    137137The \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.
    138138
    139 However, such use leads to the lock acquiring order problems. 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 Behaviour. As shown~\cite{Lister77}, solving this problem requires:
     139However, 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:
    140140\begin{enumerate}
    141141        \item Dynamically tracking of the monitor-call order.
     
    155155}
    156156\end{cfacode}
    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.
     157This 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.
    158158
    159159\subsection{\code{mutex} statement} \label{mutex-stmt}
    160160
    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 work around 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.
     161The 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.
    162162
    163163\begin{table}
     
    196196% ======================================================================
    197197% ======================================================================
    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 shown in section \ref{call}:
     198Once 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}:
    199199\begin{cfacode}
    200200monitor counter_t {
     
    227227% ======================================================================
    228228% ======================================================================
    229 \section{Internal Scheduling} \label{intsched}
     229\section{Internal scheduling} \label{intsched}
    230230% ======================================================================
    231231% ======================================================================
    232232In 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.
    233233
    234 First, here is a simple example of internal scheduling :
     234First, here is a simple example of internal-scheduling :
    235235
    236236\begin{cfacode}
     
    253253}
    254254\end{cfacode}
    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, ensuring a basic ordering.
    256 
    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 and implementation of \CFA concurrency.
    258 
    259 % ======================================================================
    260 % ======================================================================
    261 \subsection{Internal Scheduling - Multi-Monitor}
    262 % ======================================================================
    263 % ======================================================================
    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 parameters 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.
     255There 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.
     256
     257An 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.
     258
     259% ======================================================================
     260% ======================================================================
     261\subsection{Internal Scheduling - multi monitor}
     262% ======================================================================
     263% ======================================================================
     264It 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.
    265265
    266266\begin{multicols}{2}
     
    297297\end{pseudo}
    298298\end{multicols}
    299 \noindent 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 a group of monitors. On the implementation side, handling multiple monitors does add a degree of complexity as the next few examples demonstrate.
    300 
    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 :
     299This 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.
     300
     301While 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 :
    302302\begin{multicols}{2}
    303303\begin{pseudo}
     
    319319\end{pseudo}
    320320\end{multicols}
    321 \noindent 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}.
     321The \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}.
    322322
    323323However, 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}.
     
    343343\end{multicols}
    344344
    345 \noindent However, this simple refactoring may not be possible, forcing more complex restructuring.
    346 
    347 % ======================================================================
    348 % ======================================================================
    349 \subsection{Internal Scheduling - In Depth}
    350 % ======================================================================
    351 % ======================================================================
    352 
    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} parameters, global variables, pointer parameters, or using locals with the \code{mutex}-statement.
     345This simple refactoring may not be possible, forcing more complex restructuring.
     346
     347% ======================================================================
     348% ======================================================================
     349\subsection{Internal Scheduling - in depth}
     350% ======================================================================
     351% ======================================================================
     352
     353A 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.
    354354
    355355\begin{figure}[!t]
     
    376376                |\label{line:signal1}|signal A & B
    377377                //Code Section 7
    378         |\label{line:releaseFirst}|release A & B
     378        release A & B
    379379        //Code Section 8
    380380|\label{line:lastRelease}|release A
     
    446446\end{figure}
    447447
    448 The complexity begins at code sections 4 and 8 in listing \ref{lst:int-bulk-pseudo}, which are where the existing semantics of internal scheduling needs 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:releaseFirst}), 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. This solution releases the 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 a group might never be released. A more interesting interpretation is to transfer the group until it can be disbanded, which means the group is not passed further and a thread can retain its locks.
    452 
    453 However, listing \ref{lst:int-secret} shows this solution can become much more complicated depending on what is executed while secretly holding B at line \ref{line:secret}, while avoiding the need to transfer ownership of a subset of the condition monitors. 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 :
     448The 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}
     451The 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}.
     452
     453However, 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}).
     454The 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 :
    454455
    455456\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.
     
    459460Note 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}.
    460461
    461 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 knowing when to dispand a group becomes complex and inefficient (see next section) and therefore effectively precludes this approach.
     462In 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.
    462463
    463464\subsubsection{Dependency graphs}
     
    501502\end{figure}
    502503
    503 In listing \ref{lst:int-bulk-pseudo}, 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.
     504In 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.
    504505\begin{figure}
    505506\begin{multicols}{2}
     
    530531\end{figure}
    531532
    532 Given the three threads example in listing \ref{lst:dependency}, 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.
    533 
    534 \subsubsection{Partial Signalling} \label{partial-sig}
    535 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 wake up 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 algorithms, which is why it was chosen. Furthermore, after being fully implemented, this solution does not appear to have any significant downsides.
    536 
    537 Using partial signalling, listing \ref{lst:dependency} can be solved easily :
     533Listing \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.
     534
     535\subsubsection{Partial signalling} \label{partial-sig}
     536Finally, 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
     538While listing \ref{lst:dependency} is a complicated problem for previous solutions, it can be solved easily with partial signalling :
    538539\begin{itemize}
    539540        \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}.
    540541        \item When thread $\gamma$ reaches line \ref{line:release-a}  it transfers monitor \code{A} to thread $\beta$  and wakes it up.
    541542        \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!
    542544\end{itemize}
    543545
     
    652654An 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.
    653655
    654 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.
     656The 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.
    655657
    656658% ======================================================================
     
    721723\end{tabular}
    722724\end{center}
    723 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 was 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.
    724 
    725 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 other routine than \code{V} can acquire the monitor.
    726 
    727 % ======================================================================
    728 % ======================================================================
    729 \subsection{Loose Object Definitions}
    730 % ======================================================================
    731 % ======================================================================
    732 In \uC, a monitor class declaration includes 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:
     725This 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.
     726
     727For 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.
     728
     729% ======================================================================
     730% ======================================================================
     731\subsection{Loose object definitions}
     732% ======================================================================
     733% ======================================================================
     734In \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:
    733735
    734736\begin{cfacode}
     
    746748\end{cfacode}
    747749
    748 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:
     750Furthermore, 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:
    749751\begin{center}
    750752\begin{tabular}{l}
     
    761763\end{tabular}
    762764\end{center}
    763 For the first two conditions, it is easy to implement a check that can evaluate the condition in a few instructions. 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:
     765For 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:
    764766
    765767\begin{figure}[H]
     
    770772\end{figure}
    771773
    772 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 unique dense 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 inheritance. 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.
     774There 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.
    773775
    774776The alternative is to alter the implementation like this:
     777
    775778\begin{center}
    776779{\resizebox{0.4\textwidth}{!}{\input{ext_monitor}}}
    777780\end{center}
    778 Here, the mutex routine called is associated with a thread on the entry queue while a list of acceptable routines is kept separate. 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 comparison 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.
     781
     782Here, 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.
    779783
    780784\begin{figure}
     
    793797\end{figure}
    794798
    795 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 discussed in the next section. These details are omitted from the picture for the sake of simplicity.
    796 
    797 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 problem than writing locks that are as flexible as external scheduling in \CFA.
    798 
    799 % ======================================================================
    800 % ======================================================================
    801 \subsection{Multi-Monitor Scheduling}
    802 % ======================================================================
    803 % ======================================================================
    804 
    805 External scheduling, like internal scheduling, becomes significantly more complex when introducing multi-monitor syntax. Even in the simplest possible case, some new semantics needs to be established:
     799Note 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.
     800
     801At 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.
     802
     803% ======================================================================
     804% ======================================================================
     805\subsection{Multi-monitor scheduling}
     806% ======================================================================
     807% ======================================================================
     808
     809External 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:
    806810\begin{cfacode}
    807811monitor M {};
     
    833837
    834838void g(M & mutex a, M & mutex b) {
    835         //wait for call to f with arguments a and b
     839        //wait for call to f with argument a and b
    836840        waitfor(f, a, b);
    837841}
     
    866870% ======================================================================
    867871% ======================================================================
    868 \subsection{\code{waitfor} Semantics}
    869 % ======================================================================
    870 % ======================================================================
    871 
    872 Syntactically, the \code{waitfor} statement takes a function identifier and a set of monitors. While the set of monitors can be any list of expressions, 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 usages 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.
     872\subsection{\code{waitfor} semantics}
     873% ======================================================================
     874% ======================================================================
     875
     876Syntactically, 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.
    873877\begin{figure}
    874878\begin{cfacode}[caption={Various correct and incorrect uses of the waitfor statement},label={lst:waitfor}]
     
    904908\end{figure}
    905909
    906 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 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.
     910Finally, 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.
    907911
    908912\begin{figure}
     
    969973% ======================================================================
    970974% ======================================================================
    971 \subsection{Waiting For The Destructor}
    972 % ======================================================================
    973 % ======================================================================
    974 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 ordering of execution 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.
     975\subsection{Waiting for the destructor}
     976% ======================================================================
     977% ======================================================================
     978An 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.
    975979\begin{figure}
    976980\begin{cfacode}[caption={Example of an executor which executes action in series until the destructor is called.},label={lst:dtor-order}]
  • doc/proposals/concurrency/text/frontpgs.tex

    r5da9d6a rd16d159  
    8080\begin{center}\textbf{Abstract}\end{center}
    8181
    82 \CFA is a modern, non-object-oriented extension of the C programming language. This thesis serves as a definition and an implementation for the concurrency and parallelism \CFA offers. These features are created from scratch due to the lack of concurrency in ISO C. Lightweight threads are introduced into the language. In addition, monitors are introduced as a high-level tool for control-flow based synchronization and mutual-exclusion. The main contributions of this thesis are two-fold: it extends the existing semantics of monitors introduce by~\cite{Hoare74} to handle monitors in groups and also details the engineering effort needed to introduce these features as core language features. Indeed, these features are added with respect to expectations of C programmers, and integrate with the \CFA type-system and other language features.
     82\CFA is a modern, non-object-oriented extension of the C programming language. This thesis serves as a definition and an implementation for the concurrency and parallelism \CFA offers. These features are created from scratch due to the lack of concurrency in ISO C. Monitors are introduced as a high-level tool for control-flow based concurrency. In addition, lightweight threads are also introduced into the language. Specifically, the contribution of this thesis is two-fold: it extends the existing semantics of monitors introduce by~\cite{Hoare74} to handle monitors in groups and also details the engineering effort needed to introduce these features as core language features. Indeed, these features are added in respect with expectations of C programmers and are backwards compatible as much as possible.
    8383
    8484
     
    9595I would like to thank Professors Martin Karsten and Gregor Richards, for reading my thesis and providing helpful feedback.
    9696
    97 Thanks to Aaron Moss, Rob Schluntz and Andrew Beach for their work on the \CFA project as well as all the discussions which have helped me concretize the ideas in this thesis.
     97Thanks to Aaron Moss, Rob Schluntz and Andrew Beach for their work on the \CFA project as well as all the discussions which have help me concretize the ideas in this thesis.
    9898
    99 Finally, I acknowledge that this has been possible thanks to the financial help offered by the David R. Cheriton School of Computer Science and the corporate partnership with Huawei Ltd.
     99Finally, I acknowledge that this as been possible thanks to the financial help offered by the David R. Cheriton School of Computer Science and the corperate partnership with Huawei Ltd.
    100100
    101101\cleardoublepage
  • doc/proposals/concurrency/text/future.tex

    r5da9d6a rd16d159  
    11
    22\chapter{Conclusion}
    3 This thesis has achieved a minimal concurrency \acrshort{api} that is simple, efficient and usable as the basis for higher-level features. The approach presented is based on a lightweight thread-system for parallelism, which sits on top of clusters of processors. This M:N model is judged to be both more efficient and allow more flexibility for users. Furthermore, this document introduces monitors as the main concurrency tool for users. This thesis also offers a novel approach allowing multiple monitors to be accessed simultaneously without running into the Nested Monitor Problem~\cite{Lister77}. It also offers a full implementation of the concurrency runtime written entirely in \CFA, effectively the largest \CFA code base to date.
     3As mentionned in the introduction, this thesis provides a minimal concurrency \acrshort{api} that is simple, efficient and usable as the basis for higher-level features. The approach presented is based on a lighweight thread system for parallelism which sits on top of clusters of processors. This M:N model is jugded to be both more efficient and allow more flexibility for users. Furthermore, this document introduces monitors as the main concurrency tool for users. This thesis also offers a novel approach which allows using multiple monitors at once without running into the Nested Monitor Problem~\cite{Lister77}. It also offers a full implmentation of the concurrency runtime wirtten enterily in \CFA, effectively the largest \CFA code base to date.
    44
    55
     
    1111
    1212\subsection{Performance} \label{futur:perf}
    13 This thesis presents a first implementation of the \CFA runtime. Therefore, there is still significant work to improve performance. Many of the data structures and algorithms may change in the future to more efficient versions. For example, the number of monitors in a single \gls{bulk-acq} is only bound by the stack size, this is probably unnecessarily generous. It may be possible that limiting the number helps increase performance. However, it is not obvious that the benefit would be significant.
     13This thesis presents a first implementation of the \CFA runtime. Therefore, there is still significant work to do to improve performance. Many of the data structures and algorithms will change in the future to more efficient versions. For example, \CFA the number of monitors in a single \gls{bulk-acq} is only bound by the stack size, this is probably unnecessarily generous. It may be possible that limiting the number help increase performance. However, it is not obvious that the benefit would be significant.
    1414
    1515\subsection{Flexible Scheduling} \label{futur:sched}
    16 An important part of concurrency is scheduling. Different scheduling algorithms can affect performance (both in terms of average and variation). However, no single scheduler is optimal for all workloads and therefore there is value in being able to change the scheduler for given programs. One solution is to offer various tweaking options to users, allowing the scheduler to be adjusted to the requirements of the workload. However, in order to be truly flexible, it would be interesting to allow users to add arbitrary data and arbitrary scheduling algorithms. For example, a web server could attach Type-of-Service information to threads and have a ``ToS aware'' scheduling algorithm tailored to this specific web server. This path of flexible schedulers will be explored for \CFA.
     16An important part of concurrency is scheduling. Different scheduling algorithm can affect performance (both in terms of average and variation). However, no single scheduler is optimal for all workloads and therefore there is value in being able to change the scheduler for given programs. One solution is to offer various tweaking options to users, allowing the scheduler to be adjusted to the requirements of the workload. However, in order to be truly flexible, it would be interesting to allow users to add arbitrary data and arbitrary scheduling algorithms to the scheduler. For example, a web server could attach Type-of-Service information to threads and have a ``ToS aware'' scheduling algorithm tailored to this specific web server. This path of flexible schedulers will be explored for \CFA.
    1717
    1818\subsection{Non-Blocking IO} \label{futur:nbio}
    19 While most of the parallelism tools are aimed at data parallelism and control-flow parallelism, many modern workloads are not bound on computation but on IO operations, a common case being web servers and XaaS (anything as a service). These types of workloads often require significant engineering around amortizing costs of blocking IO operations. At its core, Non-Blocking IO is an operating system level feature that allows queuing IO operations (e.g., network operations) and registering for notifications instead of waiting for requests to complete. In this context, the role of the language makes Non-Blocking IO easily available and with low overhead. The current trend is to use asynchronous programming using tools like callbacks and/or futures and promises, which can be seen in frameworks like Node.js~\cite{NodeJs} for JavaScript, Spring MVC~\cite{SpringMVC} for Java and Django~\cite{Django} for Python. However, while these are valid solutions, they lead to code that is harder to read and maintain because it is much less linear.
     19While most of the parallelism tools are aimed at data parallelism and control-flow parallelism, many modern workloads are not bound on computation but on IO operations, a common case being web-servers and XaaS (anything as a service). These type of workloads often require significant engineering around amortizing costs of blocking IO operations. At its core, Non-Blocking IO is a operating system level feature that allows queuing IO operations (e.g., network operations) and registering for notifications instead of waiting for requests to complete. In this context, the role of the language make Non-Blocking IO easily available and with low overhead. The current trend is to use asynchronous programming using tools like callbacks and/or futures and promises, which can be seen in frameworks like Node.js~\cite{NodeJs} for JavaScript, Spring MVC~\cite{SpringMVC} for Java and Django~\cite{Django} for Python. However, while these are valid solutions, they lead to code that is harder to read and maintain because it is much less linear.
    2020
    21 \subsection{Other Concurrency Tools} \label{futur:tools}
    22 While monitors offer a flexible and powerful concurrent core for \CFA, other concurrency tools are also necessary for a complete multi-paradigm concurrency package. Examples of such tools can include simple locks and condition variables, futures and promises~\cite{promises}, executors and actors. These additional features are useful when monitors offer a level of abstraction that is inadequate for certain tasks.
     21\subsection{Other concurrency tools} \label{futur:tools}
     22While monitors offer a flexible and powerful concurrent core for \CFA, other concurrency tools are also necessary for a complete multi-paradigm concurrency package. Example of such tools can include simple locks and condition variables, futures and promises~\cite{promises}, executors and actors. These additional features are useful when monitors offer a level of abstraction that is inadequate for certain tasks.
    2323
    24 \subsection{Implicit Threading} \label{futur:implcit}
    25 Simpler applications can benefit greatly from having implicit parallelism. That is, parallelism that does not rely on the user to write concurrency. This type of parallelism can be achieved both at the language level and at the library level. The canonical example of implicit parallelism is parallel for loops, which are the simplest example of a divide and conquer algorithms~\cite{uC++book}. Table \ref{lst:parfor} shows three different code examples that accomplish point-wise sums of large arrays. Note that none of these examples explicitly declare any concurrency or parallelism objects.
     24\subsection{Implicit threading} \label{futur:implcit}
     25Simpler applications can benefit greatly from having implicit parallelism. That is, parallelism that does not rely on the user to write concurrency. This type of parallelism can be achieved both at the language level and at the library level. The canonical example of implicit parallelism is parallel for loops, which are the simplest example of a divide and conquer algorithm~\cite{uC++book}. Table \ref{lst:parfor} shows three different code examples that accomplish point-wise sums of large arrays. Note that none of these examples explicitly declare any concurrency or parallelism objects.
    2626
    2727\begin{table}
     
    108108\end{table}
    109109
    110 Implicit parallelism is a restrictive solution and therefore has its limitations. However, it is a quick and simple approach to parallelism, which may very well be sufficient for smaller applications and reduces the amount of boilerplate needed to start benefiting from parallelism in modern CPUs.
     110Implicit parallelism is a restrictive solution and therefore has its limitations. However, it is a quick and simple approach to parallelism, which may very well be sufficient for smaller applications and reduces the amount of boiler-plate needed to start benefiting from parallelism in modern CPUs.
    111111
    112112
  • doc/proposals/concurrency/text/internals.tex

    r5da9d6a rd16d159  
    11
    2 \chapter{Behind the Scenes}
    3 There are several challenges specific to \CFA when implementing concurrency. These challenges are a direct result of \gls{bulk-acq} and loose object definitions. These two constraints are the root cause of most design decisions in the implementation. Furthermore, to avoid contention from dynamically allocating memory in a concurrent environment, the internal-scheduling design is (almost) entirely free of mallocs. This approach avoids the chicken and egg problem~\cite{Chicken} 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.
    4 
    5 The main memory concern for concurrency is queues. All blocking operations are made by parking threads onto queues and all queues are designed with intrusive nodes, where each node has pre-allocated link fields for chaining, to avoid the need for memory allocation. Since several concurrency operations can use an unbound amount of memory (depending on \gls{bulk-acq}), statically defining information in the intrusive fields of threads is insufficient.The only way to use a variable amount of memory without requiring memory allocation is to pre-allocate large buffers of memory eagerly and store the information in these buffers. Conveniently, the call stack fits that description and is easy to use, which is why it is used heavily in the implementation of internal scheduling, particularly variable-length arrays. Since stack allocation is based on scopes, 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 array. The threads and the condition both have a fixed amount of memory, while mute routines and blocking calls allow for an unbound amount, within the stack size.
     2\chapter{Behind the scene}
     3There are several challenges specific to \CFA when implementing concurrency. These challenges are a direct result of \gls{bulk-acq} and loose object-definitions. These two constraints are the root cause of most design decisions in the implementation. Furthermore, to avoid contention from dynamically allocating memory in a concurrent environment, the internal-scheduling design is (almost) entirely free of mallocs. This approach avoids the chicken and egg problem~\cite{Chicken} 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.
     4
     5The main memory concern for concurrency is queues. All blocking operations are made by parking threads onto queues and all queues are designed with intrusive nodes, where each not has pre-allocated link fields for chaining, to avoid the need for memory allocation. Since several concurrency operations can use an unbound amount of memory (depending on \gls{bulk-acq}), statically defining information in the intrusive fields of threads is insufficient.The only way to use a variable amount of memory without requiring memory allocation is to pre-allocate large buffers of memory eagerly and store the information in these buffers. Conveniently, the callstack fits that description and is easy to use, which is why it is used heavily in the implementation of internal scheduling, particularly variable-length arrays. 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 array. The threads and the condition both have a fixed amount of memory, while mutex-routines and the actual blocking call allow for an unbound amount, within the stack size.
    66
    77Note that since the major contributions of this thesis are extending monitor semantics to \gls{bulk-acq} and loose object definitions, any challenges that are not resulting of these characteristics of \CFA are considered as solved problems and therefore not discussed.
     
    99% ======================================================================
    1010% ======================================================================
    11 \section{Mutex Routines}
    12 % ======================================================================
    13 % ======================================================================
    14 
    15 The first step towards the monitor implementation is simple mute routines. In the single monitor case, mutual-exclusion is done using the entry/exit procedure in listing \ref{lst:entry1}. The entry/exit procedures do not have to be extended to support multiple monitors. Indeed it is sufficient to enter/leave monitors one-by-one as long as the order is correct to prevent deadlock~\cite{Havender68}. In \CFA, ordering of monitor acquisition relies on memory ordering. This approach is sufficient because all objects are guaranteed to have distinct non-overlapping memory layouts and mutual-exclusion for a monitor is only defined for its lifetime, meaning that destroying a monitor while it is acquired is Undefined Behaviour. When a mutex call is made, the concerned monitors are aggregated into a variable-length pointer array and sorted based on pointer values. This array persists for the entire duration of the mutual-exclusion and its ordering reused extensively.
     11\section{Mutex routines}
     12% ======================================================================
     13% ======================================================================
     14
     15The first step towards the monitor implementation is simple mutex-routines. In the single monitor case, mutual-exclusion is done using the entry/exit procedure in listing \ref{lst:entry1}. The entry/exit procedures do not have to be extended to support multiple monitors. Indeed it is sufficient to enter/leave monitors one-by-one as long as the order is correct to prevent deadlock~\cite{Havender68}. In \CFA, ordering of monitor acquisition relies on memory ordering. This approach is sufficient because all objects are guaranteed to have distinct non-overlapping memory layouts and mutual-exclusion for a monitor is only defined for its lifetime, meaning that destroying a monitor while it is acquired is Undefined Behavior. When a mutex call is made, the concerned monitors are aggregated into a variable-length pointer-array and sorted based on pointer values. This array persists for the entire duration of the mutual-exclusion and its ordering reused extensively.
    1616\begin{figure}
    1717\begin{multicols}{2}
     
    109109\end{cfacode}
    110110
    111 Both entry point and \gls{callsite-locking} are feasible implementations. The current \CFA implementation uses entry-point locking because it requires less work when using \gls{raii}, effectively transferring the burden of implementation to object construction/destruction. It is harder to use \gls{raii} for call-site locking, as it does not necessarily have an existing scope that matches exactly the scope of the mutual exclusion, i.e.: the function body. For example, the monitor call can appear in the middle of an expression. Furthermore, entry-point locking requires less code generation since any useful routine multiple times, but there is only one entry point for many call sites.
     111Both entry-point and \gls{callsite-locking} are feasible implementations. The current \CFA implementations uses entry-point locking because it requires less work when using \gls{raii}, effectively transferring the burden of implementation to object construction/destruction. It is harder to use \gls{raii} for call-site locking, as it does not necessarily have an existing scope that matches exactly the scope of the mutual exclusion, i.e.: the function body. For example, the monitor call can appear in the middle of an expression. Furthermore, entry-point locking requires less code generation since any useful routine multiple times, but there is only one entry-point for many call-sites.
    112112
    113113% ======================================================================
     
    117117% ======================================================================
    118118
    119 Figure \ref{fig:system1} shows a high-level picture if the \CFA runtime system in regards to concurrency. Each component of the picture is explained in detail in the flowing sections.
     119Figure \ref{fig:system1} shows a high-level picture if the \CFA runtime system in regards to concurrency. Each component of the picture is explained in details in the flowing sections.
    120120
    121121\begin{figure}
     
    128128
    129129\subsection{Context Switching}
    130 As mentioned in section \ref{coroutine}, coroutines are a stepping stone for implementing threading, because they share the same mechanism for context-switching between different stacks. To improve performance and simplicity, context-switching is implemented using the following assumption: all context-switches happen inside a specific function call. This assumption means that the context-switch only has to copy the callee-saved registers onto the stack and then switch the stack registers with the ones of the target coroutine/thread. Note that the instruction pointer can be left untouched since the context-switch is always inside the same function. Threads, however, do not context-switch between each other directly. They context-switch to the scheduler. This method is called a 2-step context-switch and has the advantage of having a clear distinction between user code and the kernel where scheduling and other system operations happen. Obviously, this doubles the context-switch cost because threads must context-switch to an intermediate stack. The alternative 1-step context-switch uses the stack of the ``from'' thread to schedule and then context-switches directly to the ``to'' thread. However, the performance of the 2-step context-switch is still superior to a \code{pthread_yield} (see section \ref{results}). Additionally, for users in need for optimal performance, it is important to note that having a 2-step context-switch as the default does not prevent \CFA from offering a 1-step context-switch (akin to the Microsoft \code{SwitchToFiber}~\cite{switchToWindows} routine). This option is not currently present in \CFA but the changes required to add it are strictly additive.
     130As mentioned in section \ref{coroutine}, coroutines are a stepping stone for implementing threading, because they share the same mechanism for context-switching between different stacks. To improve performance and simplicity, context-switching is implemented using the following assumption: all context-switches happen inside a specific function call. This assumption means that the context-switch only has to copy the callee-saved registers onto the stack and then switch the stack registers with the ones of the target coroutine/thread. Note that the instruction pointer can be left untouched since the context-switch is always inside the same function. Threads however do not context-switch between each other directly. They context-switch to the scheduler. This method is called a 2-step context-switch and has the advantage of having a clear distinction between user code and the kernel where scheduling and other system operation happen. Obviously, this doubles the context-switch cost because threads must context-switch to an intermediate stack. The alternative 1-step context-switch uses the stack of the ``from'' thread to schedule and then context-switches directly to the ``to'' thread. However, the performance of the 2-step context-switch is still superior to a \code{pthread_yield} (see section \ref{results}). Additionally, for users in need for optimal performance, it is important to note that having a 2-step context-switch as the default does not prevent \CFA from offering a 1-step context-switch (akin to the Microsoft \code{SwitchToFiber}~\cite{switchToWindows} routine). This option is not currently present in \CFA but the changes required to add it are strictly additive.
    131131
    132132\subsection{Processors}
    133 Parallelism in \CFA is built around using processors to specify how much parallelism is desired. \CFA processors are object wrappers around kernel threads, specifically pthreads in the current implementation of \CFA. Indeed, any parallelism must go through operating-system libraries. However, \glspl{uthread} are still the main source of concurrency, processors are simply the underlying source of parallelism. Indeed, processor \glspl{kthread} simply fetch a \gls{uthread} from the scheduler and run it; they are effectively executers for user-threads. The main benefit of this approach is that it offers a well-defined boundary between kernel code and user code, for example, kernel thread quiescing, scheduling and interrupt handling. Processors internally use coroutines to take advantage of the existing context-switching semantics.
    134 
    135 \subsection{Stack Management}
    136 One of the challenges of this system is to reduce the footprint as much as possible. Specifically, all pthreads created also have a stack created with them, which should be used as much as possible. Normally, coroutines also create there own stack to run on, however, in the case of the coroutines used for processors, these coroutines run directly on the \gls{kthread} stack, effectively stealing the processor stack. The exception to this rule is the Main Processor, i.e. the initial \gls{kthread} that is given to any program. In order to respect C user expectations, the stack of the initial kernel thread, the main stack of the program, is used by the main user thread rather than the main processor, which can grow very large.
     133Parallelism in \CFA is built around using processors to specify how much parallelism is desired. \CFA processors are object wrappers around kernel threads, specifically pthreads in the current implementation of \CFA. Indeed, any parallelism must go through operating-system libraries. However, \glspl{uthread} are still the main source of concurrency, processors are simply the underlying source of parallelism. Indeed, processor \glspl{kthread} simply fetch a \gls{uthread} from the scheduler and run it; they are effectively executers for user-threads. The main benefit of this approach is that it offers a well defined boundary between kernel code and user code, for example, kernel thread quiescing, scheduling and interrupt handling. Processors internally use coroutines to take advantage of the existing context-switching semantics.
     134
     135\subsection{Stack management}
     136One of the challenges of this system is to reduce the footprint as much as possible. Specifically, all pthreads created also have a stack created with them, which should be used as much as possible. Normally, coroutines also create there own stack to run on, however, in the case of the coroutines used for processors, these coroutines run directly on the \gls{kthread} stack, effectively stealing the processor stack. The exception to this rule is the Main Processor, i.e. the initial \gls{kthread} that is given to any program. In order to respect C user-expectations, the stack of the initial kernel thread, the main stack of the program, is used by the main user thread rather than the main processor, which can grow very large
    137137
    138138\subsection{Preemption} \label{preemption}
    139 Finally, an important aspect for any complete threading system is preemption. As mentioned in chapter \ref{basics}, preemption introduces an extra degree of uncertainty, which enables users to have multiple threads interleave transparently, rather than having to cooperate among threads for proper scheduling and CPU distribution. Indeed, preemption is desirable because it adds a degree of isolation among threads. In a fully cooperative system, any thread that runs a long loop can starve other threads, while in a preemptive system, starvation can still occur but it does not rely on every thread having to yield or block on a regular basis, which reduces significantly a programmer burden. Obviously, preemption is not optimal for every workload. However any preemptive system can become a cooperative system by making the time slices extremely large. Therefore, \CFA uses a preemptive threading system.
     139Finally, an important aspect for any complete threading system is preemption. As mentioned in chapter \ref{basics}, preemption introduces an extra degree of uncertainty, which enables users to have multiple threads interleave transparently, rather than having to cooperate among threads for proper scheduling and CPU distribution. Indeed, preemption is desirable because it adds a degree of isolation among threads. In a fully cooperative system, any thread that runs a long loop can starve other threads, while in a preemptive system, starvation can still occur but it does not rely on every thread having to yield or block on a regular basis, which reduces significantly a programmer burden. Obviously, preemption is not optimal for every workload, however any preemptive system can become a cooperative system by making the time-slices extremely large. Therefore, \CFA uses a preemptive threading system.
    140140
    141141Preemption in \CFA is based on kernel timers, which are used to run a discrete-event simulation. Every processor keeps track of the current time and registers an expiration time with the preemption system. When the preemption system receives a change in preemption, it inserts the time in a sorted order and sets a kernel timer for the closest one, effectively stepping through preemption events on each signal sent by the timer. These timers use the Linux signal {\tt SIGALRM}, which is delivered to the process rather than the kernel-thread. This results in an implementation problem, because when delivering signals to a process, the kernel can deliver the signal to any kernel thread for which the signal is not blocked, i.e. :
     
    146146For the sake of simplicity and in order to prevent the case of having two threads receiving alarms simultaneously, \CFA programs block the {\tt SIGALRM} signal on every kernel thread except one. Now because of how involuntary context-switches are handled, the kernel thread handling {\tt SIGALRM} cannot also be a processor thread.
    147147
    148 Involuntary context-switching is done by sending signal {\tt SIGUSER1} to the corresponding proces\-sor and having the thread yield from inside the signal handler. This approach effectively context-switches away from the signal handler back to the kernel and the signal-handler frame is eventually unwound when the thread is scheduled again. As a result, a signal-handler can start on one kernel thread and terminate on a second kernel thread (but the same user thread). It is important to note that signal-handlers save and restore signal masks because user-thread migration can cause a signal mask to migrate from one kernel thread to another. This behaviour is only a problem if all kernel threads, among which a user thread can migrate, differ in terms of signal masks\footnote{Sadly, official POSIX documentation is silent on what distinguishes ``async-signal-safe'' functions from other functions.}. However, since the kernel thread handling preemption requires a different signal mask, executing user threads on the kernel-alarm thread can cause deadlocks. For this reason, the alarm thread is in a tight loop around a system call to \code{sigwaitinfo}, requiring very little CPU time for preemption. One final detail about the alarm thread is how to wake it when additional communication is required (e.g., on thread termination). This unblocking is also done using {\tt SIGALRM}, but sent through the \code{pthread_sigqueue}. Indeed, \code{sigwait} can differentiate signals sent from \code{pthread_sigqueue} from signals sent from alarms or the kernel.
     148Involuntary context-switching is done by sending signal {\tt SIGUSER1} to the corresponding proces\-sor and having the thread yield from inside the signal handler. This approach effectively context-switches away from the signal-handler back to the kernel and the signal-handler frame is eventually unwound when the thread is scheduled again. As a result, a signal-handler can start on one kernel thread and terminate on a second kernel thread (but the same user thread). It is important to note that signal-handlers save and restore signal masks because user-thread migration can cause a signal mask to migrate from one kernel thread to another. This behaviour is only a problem if all kernel threads, among which a user thread can migrate, differ in terms of signal masks\footnote{Sadly, official POSIX documentation is silent on what distinguishes ``async-signal-safe'' functions from other functions.}. However, since the kernel thread handling preemption requires a different signal mask, executing user threads on the kernel-alarm thread can cause deadlocks. For this reason, the alarm thread is in a tight loop around a system call to \code{sigwaitinfo}, requiring very little CPU time for preemption. One final detail about the alarm thread is how to wake it when additional communication is required (e.g., on thread termination). This unblocking is also done using {\tt SIGALRM}, but sent through the \code{pthread_sigqueue}. Indeed, \code{sigwait} can differentiate signals sent from \code{pthread_sigqueue} from signals sent from alarms or the kernel.
    149149
    150150\subsection{Scheduler}
     
    153153% ======================================================================
    154154% ======================================================================
    155 \section{Internal Scheduling} \label{impl:intsched}
     155\section{Internal scheduling} \label{impl:intsched}
    156156% ======================================================================
    157157% ======================================================================
     
    165165\end{figure}
    166166
    167 This picture has several components, the two most important being the entry queue and the AS-stack. The entry queue is an (almost) FIFO list where threads waiting to enter are parked, while the acceptor/signaller (AS) stack is a FILO list used for threads that have been signalled or otherwise marked as running next.
     167This picture has several components, the two most important being the entry-queue and the AS-stack. The entry-queue is an (almost) FIFO list where threads waiting to enter are parked, while the acceptor-signaler (AS) stack is a FILO list used for threads that have been signalled or otherwise marked as running next.
    168168
    169169For \CFA, this picture does not have support for blocking multiple monitors on a single condition. To support \gls{bulk-acq} two changes to this picture are required. First, it is no longer helpful to attach the condition to \emph{a single} monitor. Secondly, the thread waiting on the condition has to be separated across multiple monitors, seen in figure \ref{fig:monitor_cfa}.
     
    173173{\resizebox{0.8\textwidth}{!}{\input{int_monitor}}}
    174174\end{center}
    175 \caption{Illustration of \CFA Monitor}
     175\caption{Illustration of \CFA monitor}
    176176\label{fig:monitor_cfa}
    177177\end{figure}
    178178
    179 This picture and the proper entry and leave algorithms (see listing \ref{lst:entry2}) is the fundamental implementation of internal scheduling. Note that when a thread is moved from the condition to the AS-stack, it is conceptually split the thread into N pieces, where N is the number of monitors specified in the parameter list. The thread is woken up when all the pieces have popped from the AS-stacks and made active. In this picture, the threads are split into halves but this is only because there are two monitors. For a specific signalling operation every monitor needs a piece of thread on its AS-stack.
     179This picture and the proper entry and leave algorithms (see listing \ref{lst:entry2}) is the fundamental implementation of internal scheduling. Note that when a thread is moved from the condition to the AS-stack, it is conceptually split the thread into N pieces, where N is the number of monitors specified in the parameter list. The thread is woken up when all the pieces have popped from the AS-stacks and made active. In this picture, the threads are split into halves but this is only because there are two monitors. For a specific signaling operation every monitor needs a piece of thread on its AS-stack.
    180180
    181181\begin{figure}[b]
     
    210210\end{figure}
    211211
    212 Some important things to notice about the exit routine. The solution discussed in \ref{intsched} can be seen in the exit routine of listing \ref{lst:entry2}. Basically, the solution boils down to having a separate 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 transferred ownership. This solution is deadlock safe as well as preventing any potential barging. The data structures used for the AS-stack are reused extensively for external scheduling, but in the case of internal scheduling, the data is allocated using variable-length arrays on the call stack of the \code{wait} and \code{signal_block} routines.
     212Some important things to notice about the exit routine. The solution discussed in \ref{intsched} can be seen in the exit routine of listing \ref{lst:entry2}. Basically, the solution boils down to having a separate 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 transferred ownership. This solution is deadlock safe as well as preventing any potential barging. The data structure used for the AS-stack are reused extensively for external scheduling, but in the case of internal scheduling, the data is allocated using variable-length arrays on the call-stack of the \code{wait} and \code{signal_block} routines.
    213213
    214214\begin{figure}[H]
     
    220220\end{figure}
    221221
    222 Figure \ref{fig:structs} shows a high-level representation of these data structures. The main idea behind them is that, a thread cannot contain an arbitrary number of intrusive stacks for linking onto monitor. The \code{condition node} is the data structure that is queued onto a condition variable and, when signalled, the condition queue is popped and each \code{condition criterion} is moved to the AS-stack. Once all the criteria have been popped from their respective AS-stacks, the thread is woken up, which is what is shown in listing \ref{lst:entry2}.
    223 
    224 % ======================================================================
    225 % ======================================================================
    226 \section{External Scheduling}
     222Figure \ref{fig:structs} shows a high-level representation of these data-structures. The main idea behind them is that, a thread cannot contain an arbitrary number of intrusive stacks for linking onto monitor. The \code{condition node} is the data structure that is queued onto a condition variable and, when signaled, the condition queue is popped and each \code{condition criterion} are moved to the AS-stack. Once all the criterion have be popped from their respective AS-stacks, the thread is woken-up, which is what is shown in listing \ref{lst:entry2}.
     223
     224% ======================================================================
     225% ======================================================================
     226\section{External scheduling}
    227227% ======================================================================
    228228% ======================================================================
     
    232232\begin{itemize}
    233233        \item The queue of the monitor with the lowest address is no longer a true FIFO queue because threads can be moved to the front of the queue. These queues need to contain a set of monitors for each of the waiting threads. Therefore, another thread whose set contains the same lowest address monitor but different lower priority monitors may arrive first but enter the critical section after a thread with the correct pairing.
    234         \item The queue of the lowest priority monitor is both required and potentially unused. Indeed, since it is not known at compile time which monitor is the monitor which has the lowest address, every monitor needs to have the correct queues even though it is possible that some queues go unused for the entire duration of the program, for example if a monitor is only used in a specific pair.
     234        \item The queue of the lowest priority monitor is both required and potentially unused. Indeed, since it is not known at compile time which monitor is the monitor with have the lowest address, every monitor needs to have the correct queues even though it is possible that some queues go unused for the entire duration of the program, for example if a monitor is only used in a specific pair.
    235235\end{itemize}
    236236Therefore, the following modifications need to be made to support external scheduling :
     
    241241\end{itemize}
    242242
    243 \subsection{External Scheduling - Destructors}
    244 Finally, to support the ordering inversion of destructors, the code generation needs to be modified to use a special entry routine. This routine is needed because of the storage requirements of the call order inversion. Indeed, when waiting for the destructors, storage is needed for the waiting context and the lifetime of said storage needs to outlive the waiting operation it is needed for. For regular \code{waitfor} statements, the call stack of the routine itself matches this requirement but it is no longer the case when waiting for the destructor since it is pushed on to the AS-stack for later. The waitfor semantics can then be adjusted correspondingly, as seen in listing \ref{lst:entry-dtor}
     243\subsection{External scheduling - destructors}
     244Finally, to support the ordering inversion of destructors, the code generation needs to be modified to use a special entry routine. This routine is needed because of the storage requirements of the call order inversion. Indeed, when waiting for the destructors, storage is need for the waiting context and the lifetime of said storage needs to outlive the waiting operation it is needed for. For regular \code{waitfor} statements, the call-stack of the routine itself matches this requirement but it is no longer the case when waiting for the destructor since it is pushed on to the AS-stack for later. The waitfor semantics can then be adjusted correspondingly, as seen in listing \ref{lst:entry-dtor}
    245245
    246246\begin{figure}
     
    253253        continue
    254254elif matches waitfor mask
    255         push criteria to AS-stack
     255        push criterions to AS-stack
    256256        continue
    257257else
  • doc/proposals/concurrency/text/parallelism.tex

    r5da9d6a rd16d159  
    1010
    1111\section{Paradigms}
    12 \subsection{User-Level Threads}
    13 A direct improvement on the \gls{kthread} approach is to use \glspl{uthread}. These threads offer most of the same features that the operating system already provides but can be used on a much larger scale. This approach is the most powerful solution as it allows all the features of multithreading, while removing several of the more expensive costs of kernel threads. The downside is that almost none of the low-level threading problems are hidden; users still have to think about data races, deadlocks and synchronization issues. These issues can be somewhat alleviated by a concurrency toolkit with strong guarantees but the parallelism toolkit offers very little to reduce complexity in itself.
     12\subsection{User-level threads}
     13A direct improvement on the \gls{kthread} approach is to use \glspl{uthread}. These threads offer most of the same features that the operating system already provide but can be used on a much larger scale. This approach is the most powerful solution as it allows all the features of multi-threading, while removing several of the more expensive costs of kernel threads. The down side is that almost none of the low-level threading problems are hidden; users still have to think about data races, deadlocks and synchronization issues. These issues can be somewhat alleviated by a concurrency toolkit with strong guarantees but the parallelism toolkit offers very little to reduce complexity in itself.
    1414
    1515Examples of languages that support \glspl{uthread} are Erlang~\cite{Erlang} and \uC~\cite{uC++book}.
    1616
    17 \subsection{Fibers : User-Level Threads Without Preemption} \label{fibers}
    18 A popular variant of \glspl{uthread} is what is often referred to as \glspl{fiber}. However, \glspl{fiber} do not present meaningful semantic differences with \glspl{uthread}. The significant difference between \glspl{uthread} and \glspl{fiber} is the lack of \gls{preemption} in the latter. Advocates of \glspl{fiber} list their high performance and ease of implementation as major strengths but the performance difference between \glspl{uthread} and \glspl{fiber} is controversial, and the ease of implementation, while true, is a weak argument in the context of language design. Therefore this proposal largely ignores fibers.
     17\subsection{Fibers : user-level threads without preemption} \label{fibers}
     18A popular variant of \glspl{uthread} is what is often referred to as \glspl{fiber}. However, \glspl{fiber} do not present meaningful semantical differences with \glspl{uthread}. The significant difference between \glspl{uthread} and \glspl{fiber} is the lack of \gls{preemption} in the latter. Advocates of \glspl{fiber} list their high performance and ease of implementation as majors strengths but the performance difference between \glspl{uthread} and \glspl{fiber} is controversial, and the ease of implementation, while true, is a weak argument in the context of language design. Therefore this proposal largely ignores fibers.
    1919
    2020An example of a language that uses fibers is Go~\cite{Go}
    2121
    22 \subsection{Jobs and Thread Pools}
    23 An approach on the opposite end of the spectrum is to base parallelism on \glspl{pool}. Indeed, \glspl{pool} offer limited flexibility but at the benefit of a simpler user interface. In \gls{pool} based systems, users express parallelism as units of work, called jobs, and a dependency graph (either explicit or implicit) that ties them together. This approach means users need not worry about concurrency but significantly limit the interaction that can occur among jobs. Indeed, any \gls{job} that blocks also block the underlying worker, which effectively means the CPU utilization, and therefore throughput, suffers noticeably. It can be argued that a solution to this problem is to use more workers than available cores. However, unless the number of jobs and the number of workers are comparable, having a significant number of blocked jobs always results in idles cores.
     22\subsection{Jobs and thread pools}
     23An approach on the opposite end of the spectrum is to base parallelism on \glspl{pool}. Indeed, \glspl{pool} offer limited flexibility but at the benefit of a simpler user interface. In \gls{pool} based systems, users express parallelism as units of work, called jobs, and a dependency graph (either explicit or implicit) that tie them together. This approach means users need not worry about concurrency but significantly limit the interaction that can occur among jobs. Indeed, any \gls{job} that blocks also blocks the underlying worker, which effectively means the CPU utilization, and therefore throughput, suffers noticeably. It can be argued that a solution to this problem is to use more workers than available cores. However, unless the number of jobs and the number of workers are comparable, having a significant amount of blocked jobs always results in idles cores.
    2424
    2525The gold standard of this implementation is Intel's TBB library~\cite{TBB}.
    2626
    27 \subsection{Paradigm Performance}
    28 While the choice between the three paradigms listed above may have significant performance implication, it is difficult to pin down the performance implications of choosing a model at the language level. Indeed, in many situations one of these paradigms may show better performance but it all strongly depends on the workload. Having a large amount of mostly independent units of work to execute almost guarantees that the \gls{pool}-based system has the best performance thanks to the lower memory overhead (i.e., no thread stack per job). However, interactions among jobs can easily exacerbate contention. User-level threads allow fine-grain context switching, which results in better resource utilization, but a context switch is more expensive and the extra control means users need to tweak more variables to get the desired performance. Finally, if the units of uninterrupted work are large enough the paradigm choice is largely amortized by the actual work done.
     27\subsection{Paradigm performance}
     28While the choice between the three paradigms listed above may have significant performance implication, it is difficult to pin-down the performance implications of choosing a model at the language level. Indeed, in many situations one of these paradigms may show better performance but it all strongly depends on the workload. Having a large amount of mostly independent units of work to execute almost guarantees that the \gls{pool} based system has the best performance thanks to the lower memory overhead (i.e., no thread stack per job). However, interactions among jobs can easily exacerbate contention. User-level threads allow fine-grain context switching, which results in better resource utilization, but a context switch is more expensive and the extra control means users need to tweak more variables to get the desired performance. Finally, if the units of uninterrupted work are large enough the paradigm choice is largely amortized by the actual work done.
    2929
    3030\section{The \protect\CFA\ Kernel : Processors, Clusters and Threads}\label{kernel}
     
    3333\Glspl{cfacluster} have not been fully implemented in the context of this thesis, currently \CFA only supports one \gls{cfacluster}, the initial one.
    3434
    35 \subsection{Future Work: Machine Setup}\label{machine}
     35\subsection{Future Work: Machine setup}\label{machine}
    3636While this was not done in the context of this thesis, another important aspect of clusters is affinity. While many common desktop and laptop PCs have homogeneous CPUs, other devices often have more heterogeneous setups. For example, a system using \acrshort{numa} configurations may benefit from users being able to tie clusters and\/or kernel threads to certain CPU cores. OS support for CPU affinity is now common~\cite{affinityLinux, affinityWindows, affinityFreebsd, affinityNetbsd, affinityMacosx} which means it is both possible and desirable for \CFA to offer an abstraction mechanism for portable CPU affinity.
    3737
    3838\subsection{Paradigms}\label{cfaparadigms}
    39 Given these building blocks, it is possible to reproduce all three of the popular paradigms. Indeed, \glspl{uthread} is the default paradigm in \CFA. However, disabling \gls{preemption} on the \gls{cfacluster} means \glspl{cfathread} effectively become \glspl{fiber}. Since several \glspl{cfacluster} with different scheduling policy can coexist in the same application, this allows \glspl{fiber} and \glspl{uthread} to coexist in the runtime of an application. Finally, it is possible to build executors for thread pools from \glspl{uthread} or \glspl{fiber}, which includes specialized jobs like actors~\cite{Actors}.
     39Given these building blocks, it is possible to reproduce all three of the popular paradigms. Indeed, \glspl{uthread} is the default paradigm in \CFA. However, disabling \gls{preemption} on the \gls{cfacluster} means \glspl{cfathread} effectively become \glspl{fiber}. Since several \glspl{cfacluster} with different scheduling policy can coexist in the same application, this allows \glspl{fiber} and \glspl{uthread} to coexist in the runtime of an application. Finally, it is possible to build executors for thread pools from \glspl{uthread} or \glspl{fiber}, which includes specialize jobs like actors~\cite{Actors}.
  • doc/proposals/concurrency/text/results.tex

    r5da9d6a rd16d159  
    11% ======================================================================
    22% ======================================================================
    3 \chapter{Performance Results} \label{results}
     3\chapter{Performance results} \label{results}
    44% ======================================================================
    55% ======================================================================
    6 \section{Machine Setup}
    7 Table \ref{tab:machine} shows the characteristics of the machine used to run the benchmarks. All tests were made on this machine.
     6\section{Machine setup}
     7Table \ref{tab:machine} shows the characteristics of the machine used to run the benchmarks. All tests where made on this machine.
    88\begin{table}[H]
    99\begin{center}
     
    3737\end{table}
    3838
    39 \section{Micro Benchmarks}
     39\section{Micro benchmarks}
    4040All benchmarks are run using the same harness to produce the results, seen as the \code{BENCH()} macro in the following examples. This macro uses the following logic to benchmark the code :
    4141\begin{pseudo}
     
    4646        result = (after - before) / N;
    4747\end{pseudo}
    48 The method used to get time is \code{clock_gettime(CLOCK_THREAD_CPUTIME_ID);}. Each benchmark is using many iterations of a simple call to measure the cost of the call. The specific number of iterations depends on the specific benchmark.
    49 
    50 \subsection{Context-Switching}
    51 The first interesting benchmark is to measure how long context-switches take. The simplest approach to do this is to yield on a thread, which executes a 2-step context switch. In order to make the comparison fair, coroutines also execute a 2-step context-switch (\gls{uthread} to \gls{kthread} then \gls{kthread} to \gls{uthread}), which is a resume/suspend cycle instead of a yield. Listing \ref{lst:ctx-switch} shows the code for coroutines and threads with the results in table \ref{tab:ctx-switch}. All omitted tests are functionally identical to one of these tests.
     48The method used to get time is \code{clock_gettime(CLOCK_THREAD_CPUTIME_ID);}. Each benchmark is using many iterations of a simple call to measure the cost of the call. The specific number of iteration depends on the specific benchmark.
     49
     50\subsection{Context-switching}
     51The first interesting benchmark is to measure how long context-switches take. The simplest approach to do this is to yield on a thread, which executes a 2-step context switch. In order to make the comparison fair, coroutines also execute a 2-step context-switch (\gls{uthread} to \gls{kthread} then \gls{kthread} to \gls{uthread}), which is a resume/suspend cycle instead of a yield. Listing \ref{lst:ctx-switch} shows the code for coroutines and threads whith the results in table \ref{tab:ctx-switch}. All omitted tests are functionally identical to one of these tests.
    5252\begin{figure}
    5353\begin{multicols}{2}
     
    114114\end{table}
    115115
    116 \subsection{Mutual-Exclusion}
    117 The next interesting benchmark is to measure the overhead to enter/leave a critical-section. For monitors, the simplest approach is to measure how long it takes to enter and leave a monitor routine. Listing \ref{lst:mutex} shows the code for \CFA. To put the results in context, the cost of entering a non-inline function and the cost of acquiring and releasing a pthread mutex lock is also measured. The results can be shown in table \ref{tab:mutex}.
     116\subsection{Mutual-exclusion}
     117The next interesting benchmark is to measure the overhead to enter/leave a critical-section. For monitors, the simplest approach is to measure how long it takes to enter and leave a monitor routine. Listing \ref{lst:mutex} shows the code for \CFA. To put the results in context, the cost of entering a non-inline function and the cost of acquiring and releasing a pthread mutex lock are also measured. The results can be shown in table \ref{tab:mutex}.
    118118
    119119\begin{figure}
     
    156156\end{table}
    157157
    158 \subsection{Internal Scheduling}
     158\subsection{Internal scheduling}
    159159The internal-scheduling benchmark measures the cost of waiting on and signalling a condition variable. Listing \ref{lst:int-sched} shows the code for \CFA, with results table \ref{tab:int-sched}. As with all other benchmarks, all omitted tests are functionally identical to one of these tests.
    160160
     
    211211\end{table}
    212212
    213 \subsection{External Scheduling}
     213\subsection{External scheduling}
    214214The Internal scheduling benchmark measures the cost of the \code{waitfor} statement (\code{_Accept} in \uC). Listing \ref{lst:ext-sched} shows the code for \CFA, with results in table \ref{tab:ext-sched}. As with all other benchmarks, all omitted tests are functionally identical to one of these tests.
    215215
     
    264264\end{table}
    265265
    266 \subsection{Object Creation}
    267 Finally, the last benchmark measures the cost of creation for concurrent objects. Listing \ref{lst:creation} shows the code for pthreads and \CFA threads, with results shown in table \ref{tab:creation}. As with all other benchmarks, all omitted tests are functionally identical to one of these tests. The only note here is that the call stacks of \CFA coroutines are lazily created, therefore without priming the coroutine, the creation cost is very low.
     266\subsection{Object creation}
     267Finally, the last benchmark measurs the cost of creation for concurrent objects. Listing \ref{lst:creation} shows the code for pthreads and \CFA threads, with results shown in table \ref{tab:creation}. As with all other benchmarks, all omitted tests are functionally identical to one of these tests. The only note here is that the call-stacks of \CFA coroutines are lazily created, therefore without priming the coroutine, the creation cost is very low.
    268268
    269269\begin{figure}
     
    327327\end{tabular}
    328328\end{center}
    329 \caption{Creation comparison. All numbers are in nanoseconds(\si{\nano\second}).}
     329\caption{Creation comparison. All numbers are in nanoseconds(\si{\nano\second})}
    330330\label{tab:creation}
    331331\end{table}
  • doc/proposals/concurrency/text/together.tex

    r5da9d6a rd16d159  
    11% ======================================================================
    22% ======================================================================
    3 \chapter{Putting It All Together}
     3\chapter{Putting it all together}
    44% ======================================================================
    55% ======================================================================
    66
    77
    8 \section{Threads As Monitors}
     8\section{Threads as monitors}
    99As it was subtly alluded in section \ref{threads}, \code{thread}s in \CFA are in fact monitors, which means that all monitor features are available when using threads. For example, here is a very simple two thread pipeline that could be used for a simulator of a game engine :
    1010\begin{figure}[H]
  • doc/proposals/concurrency/version

    r5da9d6a rd16d159  
    1 0.11.299
     10.11.280
  • src/ControlStruct/ExceptTranslate.cc

    r5da9d6a rd16d159  
    211211                        ThrowStmt *throwStmt ) {
    212212                // __throw_terminate( `throwStmt->get_name()` ); }
    213                 return create_given_throw( "__cfaabi_ehm__throw_terminate", throwStmt );
     213                return create_given_throw( "__cfaehm__throw_terminate", throwStmt );
    214214        }
    215215
     
    232232                        ) ) );
    233233                result->push_back( new ExprStmt(
    234                         new UntypedExpr( new NameExpr( "__cfaabi_ehm__rethrow_terminate" ) )
     234                        new UntypedExpr( new NameExpr( "__cfaehm__rethrow_terminate" ) )
    235235                        ) );
    236236                delete throwStmt;
     
    241241                        ThrowStmt *throwStmt ) {
    242242                // __throw_resume( `throwStmt->get_name` );
    243                 return create_given_throw( "__cfaabi_ehm__throw_resume", throwStmt );
     243                return create_given_throw( "__cfaehm__throw_resume", throwStmt );
    244244        }
    245245
     
    309309                        local_except->get_attributes().push_back( new Attribute(
    310310                                "cleanup",
    311                                 { new NameExpr( "__cfaabi_ehm__cleanup_terminate" ) }
     311                                { new NameExpr( "__cfaehm__cleanup_terminate" ) }
    312312                                ) );
    313313
     
    430430                        FunctionDecl * terminate_catch,
    431431                        FunctionDecl * terminate_match ) {
    432                 // { __cfaabi_ehm__try_terminate(`try`, `catch`, `match`); }
     432                // { __cfaehm__try_terminate(`try`, `catch`, `match`); }
    433433
    434434                UntypedExpr * caller = new UntypedExpr( new NameExpr(
    435                         "__cfaabi_ehm__try_terminate" ) );
     435                        "__cfaehm__try_terminate" ) );
    436436                std::list<Expression *>& args = caller->get_args();
    437437                args.push_back( nameOf( try_wrapper ) );
     
    487487
    488488                // struct __try_resume_node __resume_node
    489                 //      __attribute__((cleanup( __cfaabi_ehm__try_resume_cleanup )));
     489                //      __attribute__((cleanup( __cfaehm__try_resume_cleanup )));
    490490                // ** unwinding of the stack here could cause problems **
    491491                // ** however I don't think that can happen currently **
    492                 // __cfaabi_ehm__try_resume_setup( &__resume_node, resume_handler );
     492                // __cfaehm__try_resume_setup( &__resume_node, resume_handler );
    493493
    494494                std::list< Attribute * > attributes;
     
    496496                        std::list< Expression * > attr_params;
    497497                        attr_params.push_back( new NameExpr(
    498                                 "__cfaabi_ehm__try_resume_cleanup" ) );
     498                                "__cfaehm__try_resume_cleanup" ) );
    499499                        attributes.push_back( new Attribute( "cleanup", attr_params ) );
    500500                }
     
    515515
    516516                UntypedExpr *setup = new UntypedExpr( new NameExpr(
    517                         "__cfaabi_ehm__try_resume_setup" ) );
     517                        "__cfaehm__try_resume_setup" ) );
    518518                setup->get_args().push_back( new AddressExpr( nameOf( obj ) ) );
    519519                setup->get_args().push_back( nameOf( resume_handler ) );
     
    540540        ObjectDecl * ExceptionMutatorCore::create_finally_hook(
    541541                        FunctionDecl * finally_wrapper ) {
    542                 // struct __cfaabi_ehm__cleanup_hook __finally_hook
     542                // struct __cfaehm__cleanup_hook __finally_hook
    543543                //      __attribute__((cleanup( finally_wrapper )));
    544544
     
    594594                        // Skip children?
    595595                        return;
    596                 } else if ( structDecl->get_name() == "__cfaabi_ehm__base_exception_t" ) {
     596                } else if ( structDecl->get_name() == "__cfaehm__base_exception_t" ) {
    597597                        assert( nullptr == except_decl );
    598598                        except_decl = structDecl;
    599599                        init_func_types();
    600                 } else if ( structDecl->get_name() == "__cfaabi_ehm__try_resume_node" ) {
     600                } else if ( structDecl->get_name() == "__cfaehm__try_resume_node" ) {
    601601                        assert( nullptr == node_decl );
    602602                        node_decl = structDecl;
    603                 } else if ( structDecl->get_name() == "__cfaabi_ehm__cleanup_hook" ) {
     603                } else if ( structDecl->get_name() == "__cfaehm__cleanup_hook" ) {
    604604                        assert( nullptr == hook_decl );
    605605                        hook_decl = structDecl;
  • src/ResolvExpr/Resolver.cc

    r5da9d6a rd16d159  
    369369                if ( throwStmt->get_expr() ) {
    370370                        StructDecl * exception_decl =
    371                                 indexer.lookupStruct( "__cfaabi_ehm__base_exception_t" );
     371                                indexer.lookupStruct( "__cfaehm__base_exception_t" );
    372372                        assert( exception_decl );
    373373                        Type * exceptType = new PointerType( noQualifiers, new StructInstType( noQualifiers, exception_decl ) );
  • src/driver/cfa.cc

    r5da9d6a rd16d159  
    275275                args[nargs] = "-Xlinker";
    276276                nargs += 1;
    277                 args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
     277                args[nargs] = "--undefined=__lib_debug_write";
    278278                nargs += 1;
    279279
  • src/libcfa/Makefile.am

    r5da9d6a rd16d159  
    5555
    5656libobjs = ${headers:=.o}
    57 libsrc = libcfa-prelude.c interpose.c bits/debug.c ${headers:=.c} \
     57libsrc = libcfa-prelude.c interpose.c libhdr/libdebug.c ${headers:=.c} \
    5858         assert.c exception.c virtual.c
    5959
     
    100100        math                            \
    101101        gmp                             \
    102         bits/align.h            \
    103102        bits/containers.h               \
    104103        bits/defs.h             \
    105         bits/debug.h            \
    106104        bits/locks.h            \
    107         concurrency/invoke.h
     105        concurrency/invoke.h    \
     106        libhdr.h                        \
     107        libhdr/libalign.h       \
     108        libhdr/libdebug.h       \
     109        libhdr/libtools.h
    108110
    109111CLEANFILES = libcfa-prelude.c
  • src/libcfa/Makefile.in

    r5da9d6a rd16d159  
    149149libcfa_d_a_LIBADD =
    150150am__libcfa_d_a_SOURCES_DIST = libcfa-prelude.c interpose.c \
    151         bits/debug.c fstream.c iostream.c iterator.c limits.c \
     151        libhdr/libdebug.c fstream.c iostream.c iterator.c limits.c \
    152152        rational.c stdlib.c containers/maybe.c containers/pair.c \
    153153        containers/result.c containers/vector.c \
     
    175175@BUILD_CONCURRENCY_TRUE@        concurrency/libcfa_d_a-preemption.$(OBJEXT)
    176176am__objects_4 = libcfa_d_a-libcfa-prelude.$(OBJEXT) \
    177         libcfa_d_a-interpose.$(OBJEXT) bits/libcfa_d_a-debug.$(OBJEXT) \
    178         $(am__objects_2) libcfa_d_a-assert.$(OBJEXT) \
    179         libcfa_d_a-exception.$(OBJEXT) libcfa_d_a-virtual.$(OBJEXT) \
    180         $(am__objects_3)
     177        libcfa_d_a-interpose.$(OBJEXT) \
     178        libhdr/libcfa_d_a-libdebug.$(OBJEXT) $(am__objects_2) \
     179        libcfa_d_a-assert.$(OBJEXT) libcfa_d_a-exception.$(OBJEXT) \
     180        libcfa_d_a-virtual.$(OBJEXT) $(am__objects_3)
    181181am_libcfa_d_a_OBJECTS = $(am__objects_4)
    182182libcfa_d_a_OBJECTS = $(am_libcfa_d_a_OBJECTS)
    183183libcfa_a_AR = $(AR) $(ARFLAGS)
    184184libcfa_a_LIBADD =
    185 am__libcfa_a_SOURCES_DIST = libcfa-prelude.c interpose.c bits/debug.c \
    186         fstream.c iostream.c iterator.c limits.c rational.c stdlib.c \
    187         containers/maybe.c containers/pair.c containers/result.c \
    188         containers/vector.c concurrency/coroutine.c \
    189         concurrency/thread.c concurrency/kernel.c \
    190         concurrency/monitor.c assert.c exception.c virtual.c \
    191         concurrency/CtxSwitch-@MACHINE_TYPE@.S concurrency/alarm.c \
    192         concurrency/invoke.c concurrency/preemption.c
     185am__libcfa_a_SOURCES_DIST = libcfa-prelude.c interpose.c \
     186        libhdr/libdebug.c fstream.c iostream.c iterator.c limits.c \
     187        rational.c stdlib.c containers/maybe.c containers/pair.c \
     188        containers/result.c containers/vector.c \
     189        concurrency/coroutine.c concurrency/thread.c \
     190        concurrency/kernel.c concurrency/monitor.c assert.c \
     191        exception.c virtual.c concurrency/CtxSwitch-@MACHINE_TYPE@.S \
     192        concurrency/alarm.c concurrency/invoke.c \
     193        concurrency/preemption.c
    193194@BUILD_CONCURRENCY_TRUE@am__objects_5 = concurrency/libcfa_a-coroutine.$(OBJEXT) \
    194195@BUILD_CONCURRENCY_TRUE@        concurrency/libcfa_a-thread.$(OBJEXT) \
     
    207208@BUILD_CONCURRENCY_TRUE@        concurrency/libcfa_a-preemption.$(OBJEXT)
    208209am__objects_8 = libcfa_a-libcfa-prelude.$(OBJEXT) \
    209         libcfa_a-interpose.$(OBJEXT) bits/libcfa_a-debug.$(OBJEXT) \
    210         $(am__objects_6) libcfa_a-assert.$(OBJEXT) \
    211         libcfa_a-exception.$(OBJEXT) libcfa_a-virtual.$(OBJEXT) \
    212         $(am__objects_7)
     210        libcfa_a-interpose.$(OBJEXT) \
     211        libhdr/libcfa_a-libdebug.$(OBJEXT) $(am__objects_6) \
     212        libcfa_a-assert.$(OBJEXT) libcfa_a-exception.$(OBJEXT) \
     213        libcfa_a-virtual.$(OBJEXT) $(am__objects_7)
    213214am_libcfa_a_OBJECTS = $(am__objects_8)
    214215libcfa_a_OBJECTS = $(am_libcfa_a_OBJECTS)
     
    263264        containers/result containers/vector concurrency/coroutine \
    264265        concurrency/thread concurrency/kernel concurrency/monitor \
    265         ${shell echo stdhdr/*} math gmp bits/align.h bits/containers.h \
    266         bits/defs.h bits/debug.h bits/locks.h concurrency/invoke.h
     266        ${shell echo stdhdr/*} math gmp bits/containers.h bits/defs.h \
     267        bits/locks.h concurrency/invoke.h libhdr.h libhdr/libalign.h \
     268        libhdr/libdebug.h libhdr/libtools.h
    267269HEADERS = $(nobase_cfa_include_HEADERS)
    268270am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP)
     
    422424        containers/vector $(am__append_3)
    423425libobjs = ${headers:=.o}
    424 libsrc = libcfa-prelude.c interpose.c bits/debug.c ${headers:=.c} \
     426libsrc = libcfa-prelude.c interpose.c libhdr/libdebug.c ${headers:=.c} \
    425427        assert.c exception.c virtual.c $(am__append_4)
    426428libcfa_a_SOURCES = ${libsrc}
     
    435437        math                            \
    436438        gmp                             \
    437         bits/align.h            \
    438439        bits/containers.h               \
    439440        bits/defs.h             \
    440         bits/debug.h            \
    441441        bits/locks.h            \
    442         concurrency/invoke.h
     442        concurrency/invoke.h    \
     443        libhdr.h                        \
     444        libhdr/libalign.h       \
     445        libhdr/libdebug.h       \
     446        libhdr/libtools.h
    443447
    444448CLEANFILES = libcfa-prelude.c
     
    507511clean-libLIBRARIES:
    508512        -test -z "$(lib_LIBRARIES)" || rm -f $(lib_LIBRARIES)
    509 bits/$(am__dirstamp):
    510         @$(MKDIR_P) bits
    511         @: > bits/$(am__dirstamp)
    512 bits/$(DEPDIR)/$(am__dirstamp):
    513         @$(MKDIR_P) bits/$(DEPDIR)
    514         @: > bits/$(DEPDIR)/$(am__dirstamp)
    515 bits/libcfa_d_a-debug.$(OBJEXT): bits/$(am__dirstamp) \
    516         bits/$(DEPDIR)/$(am__dirstamp)
     513libhdr/$(am__dirstamp):
     514        @$(MKDIR_P) libhdr
     515        @: > libhdr/$(am__dirstamp)
     516libhdr/$(DEPDIR)/$(am__dirstamp):
     517        @$(MKDIR_P) libhdr/$(DEPDIR)
     518        @: > libhdr/$(DEPDIR)/$(am__dirstamp)
     519libhdr/libcfa_d_a-libdebug.$(OBJEXT): libhdr/$(am__dirstamp) \
     520        libhdr/$(DEPDIR)/$(am__dirstamp)
    517521containers/$(am__dirstamp):
    518522        @$(MKDIR_P) containers
     
    559563        $(AM_V_AR)$(libcfa_d_a_AR) libcfa-d.a $(libcfa_d_a_OBJECTS) $(libcfa_d_a_LIBADD)
    560564        $(AM_V_at)$(RANLIB) libcfa-d.a
    561 bits/libcfa_a-debug.$(OBJEXT): bits/$(am__dirstamp) \
    562         bits/$(DEPDIR)/$(am__dirstamp)
     565libhdr/libcfa_a-libdebug.$(OBJEXT): libhdr/$(am__dirstamp) \
     566        libhdr/$(DEPDIR)/$(am__dirstamp)
    563567containers/libcfa_a-maybe.$(OBJEXT): containers/$(am__dirstamp) \
    564568        containers/$(DEPDIR)/$(am__dirstamp)
     
    592596mostlyclean-compile:
    593597        -rm -f *.$(OBJEXT)
    594         -rm -f bits/*.$(OBJEXT)
    595598        -rm -f concurrency/*.$(OBJEXT)
    596599        -rm -f containers/*.$(OBJEXT)
     600        -rm -f libhdr/*.$(OBJEXT)
    597601
    598602distclean-compile:
     
    621625@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-stdlib.Po@am__quote@
    622626@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcfa_d_a-virtual.Po@am__quote@
    623 @AMDEP_TRUE@@am__include@ @am__quote@bits/$(DEPDIR)/libcfa_a-debug.Po@am__quote@
    624 @AMDEP_TRUE@@am__include@ @am__quote@bits/$(DEPDIR)/libcfa_d_a-debug.Po@am__quote@
    625627@AMDEP_TRUE@@am__include@ @am__quote@concurrency/$(DEPDIR)/CtxSwitch-@MACHINE_TYPE@.Po@am__quote@
    626628@AMDEP_TRUE@@am__include@ @am__quote@concurrency/$(DEPDIR)/libcfa_a-alarm.Po@am__quote@
     
    646648@AMDEP_TRUE@@am__include@ @am__quote@containers/$(DEPDIR)/libcfa_d_a-result.Po@am__quote@
    647649@AMDEP_TRUE@@am__include@ @am__quote@containers/$(DEPDIR)/libcfa_d_a-vector.Po@am__quote@
     650@AMDEP_TRUE@@am__include@ @am__quote@libhdr/$(DEPDIR)/libcfa_a-libdebug.Po@am__quote@
     651@AMDEP_TRUE@@am__include@ @am__quote@libhdr/$(DEPDIR)/libcfa_d_a-libdebug.Po@am__quote@
    648652
    649653.S.o:
     
    700704@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -c -o libcfa_d_a-interpose.obj `if test -f 'interpose.c'; then $(CYGPATH_W) 'interpose.c'; else $(CYGPATH_W) '$(srcdir)/interpose.c'; fi`
    701705
    702 bits/libcfa_d_a-debug.o: bits/debug.c
    703 @am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -MT bits/libcfa_d_a-debug.o -MD -MP -MF bits/$(DEPDIR)/libcfa_d_a-debug.Tpo -c -o bits/libcfa_d_a-debug.o `test -f 'bits/debug.c' || echo '$(srcdir)/'`bits/debug.c
    704 @am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) bits/$(DEPDIR)/libcfa_d_a-debug.Tpo bits/$(DEPDIR)/libcfa_d_a-debug.Po
    705 @AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='bits/debug.c' object='bits/libcfa_d_a-debug.o' libtool=no @AMDEPBACKSLASH@
    706 @AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
    707 @am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -c -o bits/libcfa_d_a-debug.o `test -f 'bits/debug.c' || echo '$(srcdir)/'`bits/debug.c
    708 
    709 bits/libcfa_d_a-debug.obj: bits/debug.c
    710 @am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -MT bits/libcfa_d_a-debug.obj -MD -MP -MF bits/$(DEPDIR)/libcfa_d_a-debug.Tpo -c -o bits/libcfa_d_a-debug.obj `if test -f 'bits/debug.c'; then $(CYGPATH_W) 'bits/debug.c'; else $(CYGPATH_W) '$(srcdir)/bits/debug.c'; fi`
    711 @am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) bits/$(DEPDIR)/libcfa_d_a-debug.Tpo bits/$(DEPDIR)/libcfa_d_a-debug.Po
    712 @AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='bits/debug.c' object='bits/libcfa_d_a-debug.obj' libtool=no @AMDEPBACKSLASH@
    713 @AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
    714 @am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -c -o bits/libcfa_d_a-debug.obj `if test -f 'bits/debug.c'; then $(CYGPATH_W) 'bits/debug.c'; else $(CYGPATH_W) '$(srcdir)/bits/debug.c'; fi`
     706libhdr/libcfa_d_a-libdebug.o: libhdr/libdebug.c
     707@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -MT libhdr/libcfa_d_a-libdebug.o -MD -MP -MF libhdr/$(DEPDIR)/libcfa_d_a-libdebug.Tpo -c -o libhdr/libcfa_d_a-libdebug.o `test -f 'libhdr/libdebug.c' || echo '$(srcdir)/'`libhdr/libdebug.c
     708@am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) libhdr/$(DEPDIR)/libcfa_d_a-libdebug.Tpo libhdr/$(DEPDIR)/libcfa_d_a-libdebug.Po
     709@AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='libhdr/libdebug.c' object='libhdr/libcfa_d_a-libdebug.o' libtool=no @AMDEPBACKSLASH@
     710@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     711@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -c -o libhdr/libcfa_d_a-libdebug.o `test -f 'libhdr/libdebug.c' || echo '$(srcdir)/'`libhdr/libdebug.c
     712
     713libhdr/libcfa_d_a-libdebug.obj: libhdr/libdebug.c
     714@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -MT libhdr/libcfa_d_a-libdebug.obj -MD -MP -MF libhdr/$(DEPDIR)/libcfa_d_a-libdebug.Tpo -c -o libhdr/libcfa_d_a-libdebug.obj `if test -f 'libhdr/libdebug.c'; then $(CYGPATH_W) 'libhdr/libdebug.c'; else $(CYGPATH_W) '$(srcdir)/libhdr/libdebug.c'; fi`
     715@am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) libhdr/$(DEPDIR)/libcfa_d_a-libdebug.Tpo libhdr/$(DEPDIR)/libcfa_d_a-libdebug.Po
     716@AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='libhdr/libdebug.c' object='libhdr/libcfa_d_a-libdebug.obj' libtool=no @AMDEPBACKSLASH@
     717@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     718@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_d_a_CFLAGS) $(CFLAGS) -c -o libhdr/libcfa_d_a-libdebug.obj `if test -f 'libhdr/libdebug.c'; then $(CYGPATH_W) 'libhdr/libdebug.c'; else $(CYGPATH_W) '$(srcdir)/libhdr/libdebug.c'; fi`
    715719
    716720libcfa_d_a-fstream.o: fstream.c
     
    994998@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -c -o libcfa_a-interpose.obj `if test -f 'interpose.c'; then $(CYGPATH_W) 'interpose.c'; else $(CYGPATH_W) '$(srcdir)/interpose.c'; fi`
    995999
    996 bits/libcfa_a-debug.o: bits/debug.c
    997 @am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -MT bits/libcfa_a-debug.o -MD -MP -MF bits/$(DEPDIR)/libcfa_a-debug.Tpo -c -o bits/libcfa_a-debug.o `test -f 'bits/debug.c' || echo '$(srcdir)/'`bits/debug.c
    998 @am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) bits/$(DEPDIR)/libcfa_a-debug.Tpo bits/$(DEPDIR)/libcfa_a-debug.Po
    999 @AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='bits/debug.c' object='bits/libcfa_a-debug.o' libtool=no @AMDEPBACKSLASH@
    1000 @AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
    1001 @am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -c -o bits/libcfa_a-debug.o `test -f 'bits/debug.c' || echo '$(srcdir)/'`bits/debug.c
    1002 
    1003 bits/libcfa_a-debug.obj: bits/debug.c
    1004 @am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -MT bits/libcfa_a-debug.obj -MD -MP -MF bits/$(DEPDIR)/libcfa_a-debug.Tpo -c -o bits/libcfa_a-debug.obj `if test -f 'bits/debug.c'; then $(CYGPATH_W) 'bits/debug.c'; else $(CYGPATH_W) '$(srcdir)/bits/debug.c'; fi`
    1005 @am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) bits/$(DEPDIR)/libcfa_a-debug.Tpo bits/$(DEPDIR)/libcfa_a-debug.Po
    1006 @AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='bits/debug.c' object='bits/libcfa_a-debug.obj' libtool=no @AMDEPBACKSLASH@
    1007 @AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
    1008 @am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -c -o bits/libcfa_a-debug.obj `if test -f 'bits/debug.c'; then $(CYGPATH_W) 'bits/debug.c'; else $(CYGPATH_W) '$(srcdir)/bits/debug.c'; fi`
     1000libhdr/libcfa_a-libdebug.o: libhdr/libdebug.c
     1001@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -MT libhdr/libcfa_a-libdebug.o -MD -MP -MF libhdr/$(DEPDIR)/libcfa_a-libdebug.Tpo -c -o libhdr/libcfa_a-libdebug.o `test -f 'libhdr/libdebug.c' || echo '$(srcdir)/'`libhdr/libdebug.c
     1002@am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) libhdr/$(DEPDIR)/libcfa_a-libdebug.Tpo libhdr/$(DEPDIR)/libcfa_a-libdebug.Po
     1003@AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='libhdr/libdebug.c' object='libhdr/libcfa_a-libdebug.o' libtool=no @AMDEPBACKSLASH@
     1004@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     1005@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -c -o libhdr/libcfa_a-libdebug.o `test -f 'libhdr/libdebug.c' || echo '$(srcdir)/'`libhdr/libdebug.c
     1006
     1007libhdr/libcfa_a-libdebug.obj: libhdr/libdebug.c
     1008@am__fastdepCC_TRUE@    $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -MT libhdr/libcfa_a-libdebug.obj -MD -MP -MF libhdr/$(DEPDIR)/libcfa_a-libdebug.Tpo -c -o libhdr/libcfa_a-libdebug.obj `if test -f 'libhdr/libdebug.c'; then $(CYGPATH_W) 'libhdr/libdebug.c'; else $(CYGPATH_W) '$(srcdir)/libhdr/libdebug.c'; fi`
     1009@am__fastdepCC_TRUE@    $(AM_V_at)$(am__mv) libhdr/$(DEPDIR)/libcfa_a-libdebug.Tpo libhdr/$(DEPDIR)/libcfa_a-libdebug.Po
     1010@AMDEP_TRUE@@am__fastdepCC_FALSE@       $(AM_V_CC)source='libhdr/libdebug.c' object='libhdr/libcfa_a-libdebug.obj' libtool=no @AMDEPBACKSLASH@
     1011@AMDEP_TRUE@@am__fastdepCC_FALSE@       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     1012@am__fastdepCC_FALSE@   $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libcfa_a_CFLAGS) $(CFLAGS) -c -o libhdr/libcfa_a-libdebug.obj `if test -f 'libhdr/libdebug.c'; then $(CYGPATH_W) 'libhdr/libdebug.c'; else $(CYGPATH_W) '$(srcdir)/libhdr/libdebug.c'; fi`
    10091013
    10101014libcfa_a-fstream.o: fstream.c
     
    14071411        -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
    14081412        -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)
    1409         -rm -f bits/$(DEPDIR)/$(am__dirstamp)
    1410         -rm -f bits/$(am__dirstamp)
    14111413        -rm -f concurrency/$(DEPDIR)/$(am__dirstamp)
    14121414        -rm -f concurrency/$(am__dirstamp)
    14131415        -rm -f containers/$(DEPDIR)/$(am__dirstamp)
    14141416        -rm -f containers/$(am__dirstamp)
     1417        -rm -f libhdr/$(DEPDIR)/$(am__dirstamp)
     1418        -rm -f libhdr/$(am__dirstamp)
    14151419
    14161420maintainer-clean-generic:
     
    14221426
    14231427distclean: distclean-am
    1424         -rm -rf ./$(DEPDIR) bits/$(DEPDIR) concurrency/$(DEPDIR) containers/$(DEPDIR)
     1428        -rm -rf ./$(DEPDIR) concurrency/$(DEPDIR) containers/$(DEPDIR) libhdr/$(DEPDIR)
    14251429        -rm -f Makefile
    14261430distclean-am: clean-am distclean-compile distclean-generic \
     
    14681472
    14691473maintainer-clean: maintainer-clean-am
    1470         -rm -rf ./$(DEPDIR) bits/$(DEPDIR) concurrency/$(DEPDIR) containers/$(DEPDIR)
     1474        -rm -rf ./$(DEPDIR) concurrency/$(DEPDIR) containers/$(DEPDIR) libhdr/$(DEPDIR)
    14711475        -rm -f Makefile
    14721476maintainer-clean-am: distclean-am maintainer-clean-generic \
  • src/libcfa/assert.c

    r5da9d6a rd16d159  
    1717#include <stdarg.h>                                                             // varargs
    1818#include <stdio.h>                                                              // fprintf
    19 #include "bits/debug.h"
     19#include "libhdr/libdebug.h"
    2020
    2121extern "C" {
     
    2626        // called by macro assert in assert.h
    2727        void __assert_fail( const char *assertion, const char *file, unsigned int line, const char *function ) {
    28                 __cfaabi_dbg_bits_print_safe( CFA_ASSERT_FMT ".\n", __progname, function, line, file );
     28                __lib_debug_print_safe( CFA_ASSERT_FMT ".\n", __progname, function, line, file );
    2929                abort();
    3030        }
     
    3232        // called by macro assertf
    3333        void __assert_fail_f( const char *assertion, const char *file, unsigned int line, const char *function, const char *fmt, ... ) {
    34                 __cfaabi_dbg_bits_acquire();
    35                 __cfaabi_dbg_bits_print_nolock( CFA_ASSERT_FMT ": ", __progname, function, line, file );
     34                __lib_debug_acquire();
     35                __lib_debug_print_nolock( CFA_ASSERT_FMT ": ", __progname, function, line, file );
    3636
    3737                va_list args;
    3838                va_start( args, fmt );
    39                 __cfaabi_dbg_bits_print_vararg( fmt, args );
     39                __lib_debug_print_vararg( fmt, args );
    4040                va_end( args );
    4141
    42                 __cfaabi_dbg_bits_print_nolock( "\n" );
    43                 __cfaabi_dbg_bits_release();
     42                __lib_debug_print_nolock( "\n" );
     43                __lib_debug_release();
    4444                abort();
    4545        }
  • src/libcfa/bits/containers.h

    r5da9d6a rd16d159  
    1515#pragma once
    1616
    17 #include "bits/align.h"
    1817#include "bits/defs.h"
     18#include "libhdr.h"
    1919
    2020//-----------------------------------------------------------------------------
  • src/libcfa/bits/defs.h

    r5da9d6a rd16d159  
    3232#define __cfa_anonymous_object __cfa_anonymous_object
    3333#endif
    34 
    35 #ifdef __cforall
    36 extern "C" {
    37 #endif
    38 void abortf( const char fmt[], ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__));
    39 #ifdef __cforall
    40 }
    41 #endif
  • src/libcfa/bits/locks.h

    r5da9d6a rd16d159  
    1616#pragma once
    1717
    18 #include "bits/debug.h"
    1918#include "bits/defs.h"
     19
     20#include "libhdr.h"
    2021
    2122// pause to prevent excess processor bus usage
     
    6465
    6566        // Lock the spinlock, return false if already acquired
    66         static inline _Bool try_lock  ( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
     67        static inline _Bool try_lock  ( __spinlock_t & this DEBUG_CTX_PARAM2 ) {
    6768                _Bool result = __lock_test_and_test_and_set( this.lock );
    68                 __cfaabi_dbg_debug_do(
     69                LIB_DEBUG_DO(
    6970                        if( result ) {
    7071                                this.prev_name = caller;
     
    7677
    7778        // Lock the spinlock, spin if already acquired
    78         static inline void lock( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
     79        static inline void lock( __spinlock_t & this DEBUG_CTX_PARAM2 ) {
    7980                #ifndef NOEXPBACK
    8081                        enum { SPIN_START = 4, SPIN_END = 64 * 1024, };
     
    9798                        #endif
    9899                }
    99                 __cfaabi_dbg_debug_do(
     100                LIB_DEBUG_DO(
    100101                        this.prev_name = caller;
    101102                        this.prev_thrd = this_thread;
     
    104105
    105106        // Lock the spinlock, spin if already acquired
    106         static inline void lock_yield( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
     107        static inline void lock_yield( __spinlock_t & this DEBUG_CTX_PARAM2 ) {
    107108                for ( unsigned int i = 1;; i += 1 ) {
    108109                        if ( __lock_test_and_test_and_set( this.lock ) ) break;
    109110                        yield( i );
    110111                }
    111                 __cfaabi_dbg_debug_do(
     112                LIB_DEBUG_DO(
    112113                        this.prev_name = caller;
    113114                        this.prev_thrd = this_thread;
  • src/libcfa/concurrency/alarm.c

    r5da9d6a rd16d159  
    2323}
    2424
     25#include "libhdr.h"
     26
    2527#include "alarm.h"
    2628#include "kernel_private.h"
     
    108110}
    109111
    110 __cfaabi_dbg_debug_do( bool validate( alarm_list_t * this ) {
     112LIB_DEBUG_DO( bool validate( alarm_list_t * this ) {
    111113        alarm_node_t ** it = &this->head;
    112114        while( (*it) ) {
     
    184186
    185187        disable_interrupts();
    186         lock( event_kernel->lock __cfaabi_dbg_ctx2 );
     188        lock( event_kernel->lock DEBUG_CTX2 );
    187189        {
    188190                verify( validate( alarms ) );
     
    196198        unlock( event_kernel->lock );
    197199        this->set = true;
    198         enable_interrupts( __cfaabi_dbg_ctx );
     200        enable_interrupts( DEBUG_CTX );
    199201}
    200202
    201203void unregister_self( alarm_node_t * this ) {
    202204        disable_interrupts();
    203         lock( event_kernel->lock __cfaabi_dbg_ctx2 );
     205        lock( event_kernel->lock DEBUG_CTX2 );
    204206        {
    205207                verify( validate( &event_kernel->alarms ) );
     
    207209        }
    208210        unlock( event_kernel->lock );
    209         enable_interrupts( __cfaabi_dbg_ctx );
     211        enable_interrupts( DEBUG_CTX );
    210212        this->set = false;
    211213}
  • src/libcfa/concurrency/coroutine.c

    r5da9d6a rd16d159  
    2929#define __CFA_INVOKE_PRIVATE__
    3030#include "invoke.h"
     31
    3132
    3233//-----------------------------------------------------------------------------
     
    7576void ^?{}(coStack_t & this) {
    7677        if ( ! this.userStack && this.storage ) {
    77                 __cfaabi_dbg_debug_do(
     78                LIB_DEBUG_DO(
    7879                        if ( mprotect( this.storage, pageSize, PROT_READ | PROT_WRITE ) == -1 ) {
    7980                                abortf( "(coStack_t *)%p.^?{}() : internal error, mprotect failure, error(%d) %s.", &this, errno, strerror( errno ) );
     
    130131
    131132                // assume malloc has 8 byte alignment so add 8 to allow rounding up to 16 byte alignment
    132                 __cfaabi_dbg_debug_do( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) );
    133                 __cfaabi_dbg_no_debug_do( this->storage = malloc( cxtSize + this->size + 8 ) );
     133                LIB_DEBUG_DO( this->storage = memalign( pageSize, cxtSize + this->size + pageSize ) );
     134                LIB_NO_DEBUG_DO( this->storage = malloc( cxtSize + this->size + 8 ) );
    134135
    135                 __cfaabi_dbg_debug_do(
     136                LIB_DEBUG_DO(
    136137                        if ( mprotect( this->storage, pageSize, PROT_NONE ) == -1 ) {
    137138                                abortf( "(uMachContext &)%p.createContext() : internal error, mprotect failure, error(%d) %s.", this, (int)errno, strerror( (int)errno ) );
     
    143144                } // if
    144145
    145                 __cfaabi_dbg_debug_do( this->limit = (char *)this->storage + pageSize );
    146                 __cfaabi_dbg_no_debug_do( this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ) ); // minimum alignment
     146                LIB_DEBUG_DO( this->limit = (char *)this->storage + pageSize );
     147                LIB_NO_DEBUG_DO( this->limit = (char *)libCeiling( (unsigned long)this->storage, 16 ) ); // minimum alignment
    147148
    148149        } else {
  • src/libcfa/concurrency/invoke.c

    r5da9d6a rd16d159  
    1818#include <stdio.h>
    1919
     20#include "libhdr.h"
    2021#include "invoke.h"
    2122
     
    3031extern void __leave_thread_monitor( struct thread_desc * this );
    3132extern void disable_interrupts();
    32 extern void enable_interrupts( __cfaabi_dbg_ctx_param );
     33extern void enable_interrupts( DEBUG_CTX_PARAM );
    3334
    3435void CtxInvokeCoroutine(
    35         void (*main)(void *),
    36         struct coroutine_desc *(*get_coroutine)(void *),
    37         void *this
     36      void (*main)(void *),
     37      struct coroutine_desc *(*get_coroutine)(void *),
     38      void *this
    3839) {
    39         struct coroutine_desc* cor = get_coroutine( this );
     40      // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
    4041
    41         if(cor->state == Primed) {
    42                 __suspend_internal();
    43         }
     42      struct coroutine_desc* cor = get_coroutine( this );
    4443
    45         cor->state = Active;
     44      if(cor->state == Primed) {
     45            __suspend_internal();
     46      }
    4647
    47         main( this );
     48      cor->state = Active;
    4849
    49         cor->state = Halted;
     50      main( this );
    5051
    51         //Final suspend, should never return
    52         __leave_coroutine();
    53         abortf("Resumed dead coroutine");
     52      cor->state = Halted;
     53
     54      //Final suspend, should never return
     55      __leave_coroutine();
     56      abortf("Resumed dead coroutine");
    5457}
    5558
    5659void CtxInvokeThread(
    57         void (*dtor)(void *),
    58         void (*main)(void *),
    59         struct thread_desc *(*get_thread)(void *),
    60         void *this
     60      void (*dtor)(void *),
     61      void (*main)(void *),
     62      struct thread_desc *(*get_thread)(void *),
     63      void *this
    6164) {
    62         // First suspend, once the thread arrives here,
    63         // the function pointer to main can be invalidated without risk
    64         __suspend_internal();
     65      // First suspend, once the thread arrives here,
     66      // the function pointer to main can be invalidated without risk
     67      __suspend_internal();
    6568
    66         // Fetch the thread handle from the user defined thread structure
    67         struct thread_desc* thrd = get_thread( this );
     69      // Fetch the thread handle from the user defined thread structure
     70      struct thread_desc* thrd = get_thread( this );
    6871
    69         // Officially start the thread by enabling preemption
    70         enable_interrupts( __cfaabi_dbg_ctx );
     72      // Officially start the thread by enabling preemption
     73      enable_interrupts( DEBUG_CTX );
    7174
    72         // Call the main of the thread
    73         main( this );
     75      // Call the main of the thread
     76      main( this );
    7477
    75         // To exit a thread we must :
    76         // 1 - Mark it as halted
    77         // 2 - Leave its monitor
    78         // 3 - Disable the interupts
    79         // 4 - Final suspend
    80         // The order of these 4 operations is very important
    81         //Final suspend, should never return
    82         __leave_thread_monitor( thrd );
    83         abortf("Resumed dead thread");
     78      // To exit a thread we must :
     79      // 1 - Mark it as halted
     80      // 2 - Leave its monitor
     81      // 3 - Disable the interupts
     82      // 4 - Final suspend
     83      // The order of these 4 operations is very important
     84      //Final suspend, should never return
     85      __leave_thread_monitor( thrd );
     86      abortf("Resumed dead thread");
    8487}
    8588
    8689
    8790void CtxStart(
    88         void (*main)(void *),
    89         struct coroutine_desc *(*get_coroutine)(void *),
    90         void *this,
    91         void (*invoke)(void *)
     91      void (*main)(void *),
     92      struct coroutine_desc *(*get_coroutine)(void *),
     93      void *this,
     94      void (*invoke)(void *)
    9295) {
    93         struct coStack_t* stack = &get_coroutine( this )->stack;
     96      // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
     97
     98      struct coStack_t* stack = &get_coroutine( this )->stack;
    9499
    95100#if defined( __i386__ )
     
    98103            void *fixedRegisters[3];                    // fixed registers ebx, edi, esi (popped on 1st uSwitch, values unimportant)
    99104            uint32_t mxcr;                        // SSE Status and Control bits (control bits are preserved across function calls)
    100             uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
     105          uint16_t fcw;                         // X97 FPU control word (preserved across function calls)
    101106            void *rturn;                          // where to go on return from uSwitch
    102107            void *dummyReturn;                          // fake return compiler would have pushed on call to uInvoke
     
    111116        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->argument[0] = this;     // argument to invoke
    112117        ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = invoke;
    113         ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    114         ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
     118      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
     119      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
    115120
    116121#elif defined( __x86_64__ )
    117122
    118         struct FakeStack {
    119                 void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
    120                 uint32_t mxcr;                      // SSE Status and Control bits (control bits are preserved across function calls)
    121                 uint16_t fcw;                       // X97 FPU control word (preserved across function calls)
    122                 void *rturn;                        // where to go on return from uSwitch
    123                 void *dummyReturn;                  // NULL return address to provide proper alignment
    124         };
     123      struct FakeStack {
     124            void *fixedRegisters[5];            // fixed registers rbx, r12, r13, r14, r15
     125            uint32_t mxcr;                      // SSE Status and Control bits (control bits are preserved across function calls)
     126            uint16_t fcw;                       // X97 FPU control word (preserved across function calls)
     127            void *rturn;                        // where to go on return from uSwitch
     128            void *dummyReturn;                  // NULL return address to provide proper alignment
     129      };
    125130
    126         ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
    127         ((struct machine_context_t *)stack->context)->FP = NULL;                // terminate stack with NULL fp
     131      ((struct machine_context_t *)stack->context)->SP = (char *)stack->base - sizeof( struct FakeStack );
     132      ((struct machine_context_t *)stack->context)->FP = NULL;          // terminate stack with NULL fp
    128133
    129         ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
    130         ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
    131         ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
    132         ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
    133         ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
    134         ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
     134      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->dummyReturn = NULL;
     135      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->rturn = CtxInvokeStub;
     136      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[0] = this;
     137      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fixedRegisters[1] = invoke;
     138      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->mxcr = 0x1F80; //Vol. 2A 3-520
     139      ((struct FakeStack *)(((struct machine_context_t *)stack->context)->SP))->fcw = 0x037F;  //Vol. 1 8-7
    135140#else
    136         #error Only __i386__ and __x86_64__ is supported for threads in cfa
     141      #error Only __i386__ and __x86_64__ is supported for threads in cfa
    137142#endif
    138143}
  • src/libcfa/concurrency/kernel.c

    r5da9d6a rd16d159  
    1414//
    1515
     16#include "libhdr.h"
     17
    1618//C Includes
    1719#include <stddef.h>
     
    148150
    149151        this.runner = &runner;
    150         __cfaabi_dbg_print_safe("Kernel : constructing main processor context %p\n", &runner);
     152        LIB_DEBUG_PRINT_SAFE("Kernel : constructing main processor context %p\n", &runner);
    151153        runner{ &this };
    152154}
     
    154156void ^?{}(processor & this) {
    155157        if( ! this.do_terminate ) {
    156                 __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
     158                LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", &this);
    157159                this.do_terminate = true;
    158160                P( this.terminated );
     
    179181        processor * this = runner.proc;
    180182
    181         __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
     183        LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this);
    182184
    183185        {
     
    185187                preemption_scope scope = { this };
    186188
    187                 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
     189                LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
    188190
    189191                thread_desc * readyThread = NULL;
     
    211213                }
    212214
    213                 __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
     215                LIB_DEBUG_PRINT_SAFE("Kernel : core %p stopping\n", this);
    214216        }
    215217
    216218        V( this->terminated );
    217219
    218         __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
     220        LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
    219221}
    220222
     
    290292        processorCtx_t proc_cor_storage = { proc, &info };
    291293
    292         __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
     294        LIB_DEBUG_PRINT_SAFE("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
    293295
    294296        //Set global state
     
    297299
    298300        //We now have a proper context from which to schedule threads
    299         __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
     301        LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
    300302
    301303        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
     
    308310
    309311        // Main routine of the core returned, the core is now fully terminated
    310         __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, proc->runner);
     312        LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
    311313
    312314        return NULL;
     
    314316
    315317void start(processor * this) {
    316         __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
     318        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
    317319
    318320        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
    319321
    320         __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
     322        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
    321323}
    322324
     
    332334        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    333335
    334         lock(   this_processor->cltr->ready_queue_lock __cfaabi_dbg_ctx2 );
     336        lock(   this_processor->cltr->ready_queue_lock DEBUG_CTX2 );
    335337        append( this_processor->cltr->ready_queue, thrd );
    336338        unlock( this_processor->cltr->ready_queue_lock );
     
    341343thread_desc * nextThread(cluster * this) {
    342344        verify( disable_preempt_count > 0 );
    343         lock( this->ready_queue_lock __cfaabi_dbg_ctx2 );
     345        lock( this->ready_queue_lock DEBUG_CTX2 );
    344346        thread_desc * head = pop_head( this->ready_queue );
    345347        unlock( this->ready_queue_lock );
     
    353355        suspend();
    354356        verify( disable_preempt_count > 0 );
    355         enable_interrupts( __cfaabi_dbg_ctx );
     357        enable_interrupts( DEBUG_CTX );
    356358}
    357359
     
    365367        verify( disable_preempt_count > 0 );
    366368
    367         enable_interrupts( __cfaabi_dbg_ctx );
     369        enable_interrupts( DEBUG_CTX );
    368370}
    369371
     
    379381        verify( disable_preempt_count > 0 );
    380382
    381         enable_interrupts( __cfaabi_dbg_ctx );
     383        enable_interrupts( DEBUG_CTX );
    382384}
    383385
     
    393395        verify( disable_preempt_count > 0 );
    394396
    395         enable_interrupts( __cfaabi_dbg_ctx );
     397        enable_interrupts( DEBUG_CTX );
    396398}
    397399
     
    406408        verify( disable_preempt_count > 0 );
    407409
    408         enable_interrupts( __cfaabi_dbg_ctx );
     410        enable_interrupts( DEBUG_CTX );
    409411}
    410412
     
    421423        verify( disable_preempt_count > 0 );
    422424
    423         enable_interrupts( __cfaabi_dbg_ctx );
     425        enable_interrupts( DEBUG_CTX );
    424426}
    425427
     
    439441// Kernel boot procedures
    440442void kernel_startup(void) {
    441         __cfaabi_dbg_print_safe("Kernel : Starting\n");
     443        LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");
    442444
    443445        // Start by initializing the main thread
     
    448450        (*mainThread){ &info };
    449451
    450         __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
     452        LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
    451453
    452454        // Initialize the main cluster
     
    454456        (*mainCluster){};
    455457
    456         __cfaabi_dbg_print_safe("Kernel : main cluster ready\n");
     458        LIB_DEBUG_PRINT_SAFE("Kernel : main cluster ready\n");
    457459
    458460        // Initialize the main processor and the main processor ctx
     
    481483
    482484        // THE SYSTEM IS NOW COMPLETELY RUNNING
    483         __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
    484 
    485         enable_interrupts( __cfaabi_dbg_ctx );
     485        LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
     486
     487        enable_interrupts( DEBUG_CTX );
    486488}
    487489
    488490void kernel_shutdown(void) {
    489         __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
     491        LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
    490492
    491493        disable_interrupts();
     
    511513        ^(mainThread){};
    512514
    513         __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
     515        LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");
    514516}
    515517
     
    521523        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
    522524        // the globalAbort flag is true.
    523         lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
     525        lock( kernel_abort_lock DEBUG_CTX2 );
    524526
    525527        // first task to abort ?
     
    546548
    547549        int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->self_cor.name, thrd );
    548         __cfaabi_dbg_bits_write( abort_text, len );
     550        __lib_debug_write( abort_text, len );
    549551
    550552        if ( thrd != this_coroutine ) {
    551553                len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
    552                 __cfaabi_dbg_bits_write( abort_text, len );
     554                __lib_debug_write( abort_text, len );
    553555        }
    554556        else {
    555                 __cfaabi_dbg_bits_write( ".\n", 2 );
     557                __lib_debug_write( ".\n", 2 );
    556558        }
    557559}
    558560
    559561extern "C" {
    560         void __cfaabi_dbg_bits_acquire() {
    561                 lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
    562         }
    563 
    564         void __cfaabi_dbg_bits_release() {
     562        void __lib_debug_acquire() {
     563                lock( kernel_debug_lock DEBUG_CTX2 );
     564        }
     565
     566        void __lib_debug_release() {
    565567                unlock( kernel_debug_lock );
    566568        }
     
    580582
    581583void P(semaphore & this) {
    582         lock( this.lock __cfaabi_dbg_ctx2 );
     584        lock( this.lock DEBUG_CTX2 );
    583585        this.count -= 1;
    584586        if ( this.count < 0 ) {
     
    596598void V(semaphore & this) {
    597599        thread_desc * thrd = NULL;
    598         lock( this.lock __cfaabi_dbg_ctx2 );
     600        lock( this.lock DEBUG_CTX2 );
    599601        this.count += 1;
    600602        if ( this.count <= 0 ) {
  • src/libcfa/concurrency/kernel_private.h

    r5da9d6a rd16d159  
    1616#pragma once
    1717
     18#include "libhdr.h"
     19
    1820#include "kernel"
    1921#include "thread"
     
    2830        void disable_interrupts();
    2931        void enable_interrupts_noPoll();
    30         void enable_interrupts( __cfaabi_dbg_ctx_param );
     32        void enable_interrupts( DEBUG_CTX_PARAM );
    3133}
    3234
     
    3739        disable_interrupts();
    3840        ScheduleThread( thrd );
    39         enable_interrupts( __cfaabi_dbg_ctx );
     41        enable_interrupts( DEBUG_CTX );
    4042}
    4143thread_desc * nextThread(cluster * this);
  • src/libcfa/concurrency/monitor.c

    r5da9d6a rd16d159  
    1919#include <inttypes.h>
    2020
     21#include "libhdr.h"
    2122#include "kernel_private.h"
    2223
     
    9091        static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
    9192                // Lock the monitor spinlock
    92                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     93                DO_LOCK( this->lock DEBUG_CTX2 );
    9394                thread_desc * thrd = this_thread;
    9495
    95                 __cfaabi_dbg_print_safe("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
     96                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
    9697
    9798                if( !this->owner ) {
     
    99100                        set_owner( this, thrd );
    100101
    101                         __cfaabi_dbg_print_safe("Kernel :  mon is free \n");
     102                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon is free \n");
    102103                }
    103104                else if( this->owner == thrd) {
     
    105106                        this->recursion += 1;
    106107
    107                         __cfaabi_dbg_print_safe("Kernel :  mon already owned \n");
     108                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon already owned \n");
    108109                }
    109110                else if( is_accepted( this, group) ) {
     
    114115                        reset_mask( this );
    115116
    116                         __cfaabi_dbg_print_safe("Kernel :  mon accepts \n");
     117                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon accepts \n");
    117118                }
    118119                else {
    119                         __cfaabi_dbg_print_safe("Kernel :  blocking \n");
     120                        LIB_DEBUG_PRINT_SAFE("Kernel :  blocking \n");
    120121
    121122                        // Some one else has the monitor, wait in line for it
     
    123124                        BlockInternal( &this->lock );
    124125
    125                         __cfaabi_dbg_print_safe("Kernel : %10p Entered  mon %p\n", thrd, this);
     126                        LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered  mon %p\n", thrd, this);
    126127
    127128                        // BlockInternal will unlock spinlock, no need to unlock ourselves
     
    129130                }
    130131
    131                 __cfaabi_dbg_print_safe("Kernel : %10p Entered  mon %p\n", thrd, this);
     132                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered  mon %p\n", thrd, this);
    132133
    133134                // Release the lock and leave
     
    138139        static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) {
    139140                // Lock the monitor spinlock
    140                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     141                DO_LOCK( this->lock DEBUG_CTX2 );
    141142                thread_desc * thrd = this_thread;
    142143
    143                 __cfaabi_dbg_print_safe("Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner);
     144                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner);
    144145
    145146
    146147                if( !this->owner ) {
    147                         __cfaabi_dbg_print_safe("Kernel : Destroying free mon %p\n", this);
     148                        LIB_DEBUG_PRINT_SAFE("Kernel : Destroying free mon %p\n", this);
    148149
    149150                        // No one has the monitor, just take it
     
    163164                __monitor_group_t group = { &this, 1, func };
    164165                if( is_accepted( this, group) ) {
    165                         __cfaabi_dbg_print_safe("Kernel :  mon accepts dtor, block and signal it \n");
     166                        LIB_DEBUG_PRINT_SAFE("Kernel :  mon accepts dtor, block and signal it \n");
    166167
    167168                        // Wake the thread that is waiting for this
     
    182183                }
    183184                else {
    184                         __cfaabi_dbg_print_safe("Kernel :  blocking \n");
     185                        LIB_DEBUG_PRINT_SAFE("Kernel :  blocking \n");
    185186
    186187                        wait_ctx( this_thread, 0 )
     
    195196                }
    196197
    197                 __cfaabi_dbg_print_safe("Kernel : Destroying %p\n", this);
     198                LIB_DEBUG_PRINT_SAFE("Kernel : Destroying %p\n", this);
    198199
    199200        }
     
    202203        void __leave_monitor_desc( monitor_desc * this ) {
    203204                // Lock the monitor spinlock, DO_LOCK to reduce contention
    204                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
    205 
    206                 __cfaabi_dbg_print_safe("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
     205                DO_LOCK( this->lock DEBUG_CTX2 );
     206
     207                LIB_DEBUG_PRINT_SAFE("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
    207208
    208209                verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", this_thread, this->owner, this->recursion, this );
     
    214215                // it means we don't need to do anything
    215216                if( this->recursion != 0) {
    216                         __cfaabi_dbg_print_safe("Kernel :  recursion still %d\n", this->recursion);
     217                        LIB_DEBUG_PRINT_SAFE("Kernel :  recursion still %d\n", this->recursion);
    217218                        unlock( this->lock );
    218219                        return;
     
    231232        // Leave single monitor for the last time
    232233        void __leave_dtor_monitor_desc( monitor_desc * this ) {
    233                 __cfaabi_dbg_debug_do(
     234                LIB_DEBUG_DO(
    234235                        if( this_thread != this->owner ) {
    235236                                abortf("Destroyed monitor %p has inconsistent owner, expected %p got %p.\n", this, this_thread, this->owner);
     
    248249
    249250                // Lock the monitor now
    250                 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );
     251                DO_LOCK( this->lock DEBUG_CTX2 );
    251252
    252253                disable_interrupts();
     
    307308        (this_thread->monitors){m, count, func};
    308309
    309         // __cfaabi_dbg_print_safe("MGUARD : enter %d\n", count);
     310        // LIB_DEBUG_PRINT_SAFE("MGUARD : enter %d\n", count);
    310311
    311312        // Enter the monitors in order
     
    313314        enter( group );
    314315
    315         // __cfaabi_dbg_print_safe("MGUARD : entered\n");
     316        // LIB_DEBUG_PRINT_SAFE("MGUARD : entered\n");
    316317}
    317318
     
    319320// Dtor for monitor guard
    320321void ^?{}( monitor_guard_t & this ) {
    321         // __cfaabi_dbg_print_safe("MGUARD : leaving %d\n", this.count);
     322        // LIB_DEBUG_PRINT_SAFE("MGUARD : leaving %d\n", this.count);
    322323
    323324        // Leave the monitors in order
    324325        leave( this.m, this.count );
    325326
    326         // __cfaabi_dbg_print_safe("MGUARD : left\n");
     327        // LIB_DEBUG_PRINT_SAFE("MGUARD : left\n");
    327328
    328329        // Restore thread context
     
    429430
    430431        //Some more checking in debug
    431         __cfaabi_dbg_debug_do(
     432        LIB_DEBUG_DO(
    432433                thread_desc * this_thrd = this_thread;
    433434                if ( this.monitor_count != this_thrd->monitors.size ) {
     
    486487        set_owner( monitors, count, signallee );
    487488
    488         __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
     489        LIB_DEBUG_PRINT_BUFFER_DECL( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
    489490
    490491        //Everything is ready to go to sleep
     
    495496
    496497
    497         __cfaabi_dbg_print_buffer_local( "Kernel :   signal_block returned\n" );
     498        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel :   signal_block returned\n" );
    498499
    499500        //We are back, restore the masks and recursions
     
    534535        __lock_size_t actual_count = aggregate( mon_storage, mask );
    535536
    536         __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %d (s: %d, m: %d)\n", actual_count, mask.size, (__lock_size_t)max);
     537        LIB_DEBUG_PRINT_BUFFER_DECL( "Kernel : waitfor %d (s: %d, m: %d)\n", actual_count, mask.size, (__lock_size_t)max);
    537538
    538539        if(actual_count == 0) return;
    539540
    540         __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n");
     541        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : waitfor internal proceeding\n");
    541542
    542543        // Create storage for monitor context
     
    555556                        __acceptable_t& accepted = mask[index];
    556557                        if( accepted.is_dtor ) {
    557                                 __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n");
     558                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : dtor already there\n");
    558559                                verifyf( accepted.size == 1,  "ERROR: Accepted dtor has more than 1 mutex parameter." );
    559560
     
    567568                        }
    568569                        else {
    569                                 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n");
     570                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : thread present, baton-passing\n");
    570571
    571572                                // Create the node specific to this wait operation
     
    575576                                monitor_save;
    576577
    577                                 __cfaabi_dbg_print_buffer_local( "Kernel :  baton of %d monitors : ", count );
     578                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel :  baton of %d monitors : ", count );
    578579                                #ifdef __CFA_DEBUG_PRINT__
    579580                                        for( int i = 0; i < count; i++) {
    580                                                 __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top );
     581                                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "%p %p ", monitors[i], monitors[i]->signal_stack.top );
    581582                                        }
    582583                                #endif
    583                                 __cfaabi_dbg_print_buffer_local( "\n");
     584                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "\n");
    584585
    585586                                // Set the owners to be the next thread
     
    592593                                monitor_restore;
    593594
    594                                 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n");
     595                                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : thread present, returned\n");
    595596                        }
    596597
    597                         __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
     598                        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted);
    598599                        return;
    599600                }
     
    602603
    603604        if( duration == 0 ) {
    604                 __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n");
     605                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : non-blocking, exiting\n");
    605606
    606607                unlock_all( locks, count );
    607608
    608                 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
     609                LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted);
    609610                return;
    610611        }
     
    613614        verifyf( duration < 0, "Timeout on waitfor statments not supported yet.");
    614615
    615         __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n");
     616        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : blocking waitfor\n");
    616617
    617618        // Create the node specific to this wait operation
     
    635636        monitor_restore;
    636637
    637         __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n");
    638 
    639         __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
     638        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : exiting\n");
     639
     640        LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted);
    640641}
    641642
     
    644645
    645646static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
    646         // __cfaabi_dbg_print_safe("Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
     647        // LIB_DEBUG_PRINT_SAFE("Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
    647648
    648649        //Pass the monitor appropriately
     
    676677static inline thread_desc * next_thread( monitor_desc * this ) {
    677678        //Check the signaller stack
    678         __cfaabi_dbg_print_safe("Kernel :  mon %p AS-stack top %p\n", this, this->signal_stack.top);
     679        LIB_DEBUG_PRINT_SAFE("Kernel :  mon %p AS-stack top %p\n", this, this->signal_stack.top);
    679680        __condition_criterion_t * urgent = pop( this->signal_stack );
    680681        if( urgent ) {
     
    728729        for( __lock_size_t i = 0; i < count; i++) {
    729730                (criteria[i]){ monitors[i], waiter };
    730                 __cfaabi_dbg_print_safe( "Kernel :  target %p = %p\n", criteria[i].target, &criteria[i] );
     731                LIB_DEBUG_PRINT_SAFE( "Kernel :  target %p = %p\n", criteria[i].target, &criteria[i] );
    731732                push( criteria[i].target->signal_stack, &criteria[i] );
    732733        }
     
    737738static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
    738739        for( __lock_size_t i = 0; i < count; i++ ) {
    739                 DO_LOCK( *locks[i] __cfaabi_dbg_ctx2 );
     740                DO_LOCK( *locks[i] DEBUG_CTX2 );
    740741        }
    741742}
     
    744745        for( __lock_size_t i = 0; i < count; i++ ) {
    745746                __spinlock_t * l = &source[i]->lock;
    746                 DO_LOCK( *l __cfaabi_dbg_ctx2 );
     747                DO_LOCK( *l DEBUG_CTX2 );
    747748                if(locks) locks[i] = l;
    748749        }
     
    802803        for(    int i = 0; i < count; i++ ) {
    803804
    804                 // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target );
     805                // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
    805806                if( &criteria[i] == target ) {
    806807                        criteria[i].ready = true;
    807                         // __cfaabi_dbg_print_safe( "True\n" );
     808                        // LIB_DEBUG_PRINT_SAFE( "True\n" );
    808809                }
    809810
     
    811812        }
    812813
    813         __cfaabi_dbg_print_safe( "Kernel :  Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL );
     814        LIB_DEBUG_PRINT_SAFE( "Kernel :  Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL );
    814815        return ready2run ? node->waiting_thread : NULL;
    815816}
     
    818819        thread_desc * thrd = this_thread;
    819820        if( !this.monitors ) {
    820                 // __cfaabi_dbg_print_safe("Branding\n");
     821                // LIB_DEBUG_PRINT_SAFE("Branding\n");
    821822                assertf( thrd->monitors.data != NULL, "No current monitor to brand condition %p", thrd->monitors.data );
    822823                this.monitor_count = thrd->monitors.size;
  • src/libcfa/concurrency/preemption.c

    r5da9d6a rd16d159  
    1414//
    1515
     16#include "libhdr.h"
    1617#include "preemption.h"
    1718
     
    147148//=============================================================================================
    148149
    149 __cfaabi_dbg_debug_do( static thread_local void * last_interrupt = 0; )
     150LIB_DEBUG_DO( static thread_local void * last_interrupt = 0; )
    150151
    151152extern "C" {
     
    158159        // Enable interrupts by decrementing the counter
    159160        // If counter reaches 0, execute any pending CtxSwitch
    160         void enable_interrupts( __cfaabi_dbg_ctx_param ) {
     161        void enable_interrupts( DEBUG_CTX_PARAM ) {
    161162                processor * proc   = this_processor;      // Cache the processor now since interrupts can start happening after the atomic add
    162163                thread_desc * thrd = this_thread;         // Cache the thread now since interrupts can start happening after the atomic add
     
    172173
    173174                // For debugging purposes : keep track of the last person to enable the interrupts
    174                 __cfaabi_dbg_debug_do( proc->last_enable = caller; )
     175                LIB_DEBUG_DO( proc->last_enable = caller; )
    175176        }
    176177
     
    232233// Called from kernel_startup
    233234void kernel_start_preemption() {
    234         __cfaabi_dbg_print_safe("Kernel : Starting preemption\n");
     235        LIB_DEBUG_PRINT_SAFE("Kernel : Starting preemption\n");
    235236
    236237        // Start with preemption disabled until ready
     
    254255// Called from kernel_shutdown
    255256void kernel_stop_preemption() {
    256         __cfaabi_dbg_print_safe("Kernel : Preemption stopping\n");
     257        LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopping\n");
    257258
    258259        // Block all signals since we are already shutting down
     
    270271        // Preemption is now fully stopped
    271272
    272         __cfaabi_dbg_print_safe("Kernel : Preemption stopped\n");
     273        LIB_DEBUG_PRINT_SAFE("Kernel : Preemption stopped\n");
    273274}
    274275
     
    296297// Receives SIGUSR1 signal and causes the current thread to yield
    297298void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
    298         __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); )
     299        LIB_DEBUG_DO( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); )
    299300
    300301        // Check if it is safe to preempt here
     
    345346                assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int);
    346347
    347                 // __cfaabi_dbg_print_safe("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
     348                // LIB_DEBUG_PRINT_SAFE("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
    348349                // Switch on the code (a.k.a. the sender) to
    349350                switch( info.si_code )
     
    353354                case SI_TIMER:
    354355                case SI_KERNEL:
    355                         // __cfaabi_dbg_print_safe("Kernel : Preemption thread tick\n");
    356                         lock( event_kernel->lock __cfaabi_dbg_ctx2 );
     356                        // LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread tick\n");
     357                        lock( event_kernel->lock DEBUG_CTX2 );
    357358                        tick_preemption();
    358359                        unlock( event_kernel->lock );
     
    367368
    368369EXIT:
    369         __cfaabi_dbg_print_safe("Kernel : Preemption thread stopping\n");
     370        LIB_DEBUG_PRINT_SAFE("Kernel : Preemption thread stopping\n");
    370371        return NULL;
    371372}
     
    379380
    380381        if ( sigaction( sig, &act, NULL ) == -1 ) {
    381                 __cfaabi_dbg_print_buffer_decl(
     382                LIB_DEBUG_PRINT_BUFFER_DECL(
    382383                        " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
    383384                        sig, handler, flags, errno, strerror( errno )
     
    396397
    397398        if ( sigaction( sig, &act, NULL ) == -1 ) {
    398                 __cfaabi_dbg_print_buffer_decl(
     399                LIB_DEBUG_PRINT_BUFFER_DECL(
    399400                        " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",
    400401                        sig, errno, strerror( errno )
     
    408409//=============================================================================================
    409410
    410 __cfaabi_dbg_debug_do(
     411LIB_DEBUG_DO(
    411412        static void __kernel_backtrace( int start ) {
    412413                // skip first N stack frames
     
    475476
    476477// void sigHandler_segv( __CFA_SIGPARMS__ ) {
    477 //      __cfaabi_dbg_debug_do(
     478//      LIB_DEBUG_DO(
    478479//              #ifdef __USE_STREAM__
    479480//              serr    | "*CFA runtime error* program cfa-cpp terminated with"
     
    492493// void sigHandler_abort( __CFA_SIGPARMS__ ) {
    493494//      // skip first 6 stack frames
    494 //      __cfaabi_dbg_debug_do( __kernel_backtrace( 6 ); )
     495//      LIB_DEBUG_DO( __kernel_backtrace( 6 ); )
    495496
    496497//      // reset default signal handler
  • src/libcfa/concurrency/thread.c

    r5da9d6a rd16d159  
    1717
    1818#include "kernel_private.h"
     19#include "libhdr.h"
    1920
    2021#define __CFA_INVOKE_PRIVATE__
     
    7172        thrd_c->last = this_coroutine;
    7273
    73         // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
     74        // LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
    7475
    7576        disable_interrupts();
     
    8182
    8283        ScheduleThread(thrd_h);
    83         enable_interrupts( __cfaabi_dbg_ctx );
     84        enable_interrupts( DEBUG_CTX );
    8485}
    8586
  • src/libcfa/exception.c

    r5da9d6a rd16d159  
    2323#include <stdio.h>
    2424#include <unwind.h>
    25 #include <bits/debug.h>
     25#include <libhdr/libdebug.h>
    2626
    2727// FIX ME: temporary hack to keep ARM build working
     
    3737
    3838// Base exception vtable is abstract, you should not have base exceptions.
    39 struct __cfaabi_ehm__base_exception_t_vtable
    40                 ___cfaabi_ehm__base_exception_t_vtable_instance = {
     39struct __cfaehm__base_exception_t_vtable
     40                ___cfaehm__base_exception_t_vtable_instance = {
    4141        .parent = NULL,
    4242        .size = 0,
     
    4949// Temperary global exception context. Does not work with concurency.
    5050struct exception_context_t {
    51     struct __cfaabi_ehm__try_resume_node * top_resume;
    52     struct __cfaabi_ehm__try_resume_node * current_resume;
     51    struct __cfaehm__try_resume_node * top_resume;
     52    struct __cfaehm__try_resume_node * current_resume;
    5353
    5454    exception * current_exception;
     
    7878// RESUMPTION ================================================================
    7979
    80 void __cfaabi_ehm__throw_resume(exception * except) {
    81 
    82         __cfaabi_dbg_print_safe("Throwing resumption exception\n");
    83 
    84         struct __cfaabi_ehm__try_resume_node * original_head = shared_stack.current_resume;
    85         struct __cfaabi_ehm__try_resume_node * current =
     80void __cfaehm__throw_resume(exception * except) {
     81
     82        LIB_DEBUG_PRINT_SAFE("Throwing resumption exception\n");
     83
     84        struct __cfaehm__try_resume_node * original_head = shared_stack.current_resume;
     85        struct __cfaehm__try_resume_node * current =
    8686                (original_head) ? original_head->next : shared_stack.top_resume;
    8787
     
    9494        }
    9595
    96         __cfaabi_dbg_print_safe("Unhandled exception\n");
     96        LIB_DEBUG_PRINT_SAFE("Unhandled exception\n");
    9797        shared_stack.current_resume = original_head;
    9898
    9999        // Fall back to termination:
    100         __cfaabi_ehm__throw_terminate(except);
     100        __cfaehm__throw_terminate(except);
    101101        // TODO: Default handler for resumption.
    102102}
     
    105105// hook has to be added after the node is built but before it is made the top node.
    106106
    107 void __cfaabi_ehm__try_resume_setup(struct __cfaabi_ehm__try_resume_node * node,
     107void __cfaehm__try_resume_setup(struct __cfaehm__try_resume_node * node,
    108108                        _Bool (*handler)(exception * except)) {
    109109        node->next = shared_stack.top_resume;
     
    112112}
    113113
    114 void __cfaabi_ehm__try_resume_cleanup(struct __cfaabi_ehm__try_resume_node * node) {
     114void __cfaehm__try_resume_cleanup(struct __cfaehm__try_resume_node * node) {
    115115        shared_stack.top_resume = node->next;
    116116}
     
    122122// May have to move to cfa for constructors and destructors (references).
    123123
    124 struct __cfaabi_ehm__node {
    125         struct __cfaabi_ehm__node * next;
     124struct __cfaehm__node {
     125        struct __cfaehm__node * next;
    126126};
    127127
    128128#define NODE_TO_EXCEPT(node) ((exception *)(1 + (node)))
    129 #define EXCEPT_TO_NODE(except) ((struct __cfaabi_ehm__node *)(except) - 1)
     129#define EXCEPT_TO_NODE(except) ((struct __cfaehm__node *)(except) - 1)
    130130
    131131// Creates a copy of the indicated exception and sets current_exception to it.
    132 static void __cfaabi_ehm__allocate_exception( exception * except ) {
     132static void __cfaehm__allocate_exception( exception * except ) {
    133133        struct exception_context_t * context = this_exception_context();
    134134
    135135        // Allocate memory for the exception.
    136         struct __cfaabi_ehm__node * store = malloc(
    137                 sizeof( struct __cfaabi_ehm__node ) + except->virtual_table->size );
     136        struct __cfaehm__node * store = malloc(
     137                sizeof( struct __cfaehm__node ) + except->virtual_table->size );
    138138
    139139        if ( ! store ) {
     
    151151
    152152// Delete the provided exception, unsetting current_exception if relivant.
    153 static void __cfaabi_ehm__delete_exception( exception * except ) {
     153static void __cfaehm__delete_exception( exception * except ) {
    154154        struct exception_context_t * context = this_exception_context();
    155155
    156         __cfaabi_dbg_print_safe("Deleting Exception\n");
     156        LIB_DEBUG_PRINT_SAFE("Deleting Exception\n");
    157157
    158158        // Remove the exception from the list.
    159         struct __cfaabi_ehm__node * to_free = EXCEPT_TO_NODE(except);
    160         struct __cfaabi_ehm__node * node;
     159        struct __cfaehm__node * to_free = EXCEPT_TO_NODE(except);
     160        struct __cfaehm__node * node;
    161161
    162162        if ( context->current_exception == except ) {
     
    178178
    179179// If this isn't a rethrow (*except==0), delete the provided exception.
    180 void __cfaabi_ehm__cleanup_terminate( void * except ) {
    181         if ( *(void**)except ) __cfaabi_ehm__delete_exception( *(exception**)except );
     180void __cfaehm__cleanup_terminate( void * except ) {
     181        if ( *(void**)except ) __cfaehm__delete_exception( *(exception**)except );
    182182}
    183183
     
    202202
    203203// The exception that is being thrown must already be stored.
    204 __attribute__((noreturn)) void __cfaabi_ehm__begin_unwind(void) {
     204__attribute__((noreturn)) void __cfaehm__begin_unwind(void) {
    205205        if ( ! this_exception_context()->current_exception ) {
    206206                printf("UNWIND ERROR missing exception in begin unwind\n");
     
    233233}
    234234
    235 void __cfaabi_ehm__throw_terminate( exception * val ) {
    236         __cfaabi_dbg_print_safe("Throwing termination exception\n");
    237 
    238         __cfaabi_ehm__allocate_exception( val );
    239         __cfaabi_ehm__begin_unwind();
    240 }
    241 
    242 void __cfaabi_ehm__rethrow_terminate(void) {
    243         __cfaabi_dbg_print_safe("Rethrowing termination exception\n");
    244 
    245         __cfaabi_ehm__begin_unwind();
     235void __cfaehm__throw_terminate( exception * val ) {
     236        LIB_DEBUG_PRINT_SAFE("Throwing termination exception\n");
     237
     238        __cfaehm__allocate_exception( val );
     239        __cfaehm__begin_unwind();
     240}
     241
     242void __cfaehm__rethrow_terminate(void) {
     243        LIB_DEBUG_PRINT_SAFE("Rethrowing termination exception\n");
     244
     245        __cfaehm__begin_unwind();
    246246}
    247247
     
    254254{
    255255
    256         //__cfaabi_dbg_print_safe("CFA: 0x%lx\n", _Unwind_GetCFA(context));
    257         __cfaabi_dbg_print_safe("Personality function (%d, %x, %llu, %p, %p):", version, actions, exceptionClass, unwind_exception, context);
     256        //LIB_DEBUG_PRINT_SAFE("CFA: 0x%lx\n", _Unwind_GetCFA(context));
     257        LIB_DEBUG_PRINT_SAFE("Personality function (%d, %x, %llu, %p, %p):", version, actions, exceptionClass, unwind_exception, context);
    258258
    259259        // If we've reached the end of the stack then there is nothing much we can do...
     
    261261
    262262        if (actions & _UA_SEARCH_PHASE) {
    263                 __cfaabi_dbg_print_safe(" lookup phase");
     263                LIB_DEBUG_PRINT_SAFE(" lookup phase");
    264264        }
    265265        else if (actions & _UA_CLEANUP_PHASE) {
    266                 __cfaabi_dbg_print_safe(" cleanup phase");
     266                LIB_DEBUG_PRINT_SAFE(" cleanup phase");
    267267        }
    268268        // Just in case, probably can't actually happen
     
    307307                        void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
    308308                        void * ip = (void*)instruction_ptr;
    309                         __cfaabi_dbg_print_safe("\nfound %p - %p (%p, %p, %p), looking for %p\n", bp, ep, ls, cs, cl, ip);
     309                        LIB_DEBUG_PRINT_SAFE("\nfound %p - %p (%p, %p, %p), looking for %p\n", bp, ep, ls, cs, cl, ip);
    310310#endif // __CFA_DEBUG_PRINT__
    311311                        continue;
     
    346346
    347347                                        // Get a function pointer from the relative offset and call it
    348                                         // _Unwind_Reason_Code (*matcher)() = (_Unwind_Reason_Code (*)())lsd_info.LPStart + imatcher;
     348                                        // _Unwind_Reason_Code (*matcher)() = (_Unwind_Reason_Code (*)())lsd_info.LPStart + imatcher;                                   
    349349
    350350                                        _Unwind_Reason_Code (*matcher)(exception *) =
     
    357357                                        // Based on the return value, check if we matched the exception
    358358                                        if( ret == _URC_HANDLER_FOUND) {
    359                                                 __cfaabi_dbg_print_safe(" handler found\n");
     359                                                LIB_DEBUG_PRINT_SAFE(" handler found\n");
    360360                                        } else {
    361                                                 __cfaabi_dbg_print_safe(" no handler\n");
     361                                                LIB_DEBUG_PRINT_SAFE(" no handler\n");
    362362                                        }
    363363                                        return ret;
     
    365365
    366366                                // This is only a cleanup handler, ignore it
    367                                 __cfaabi_dbg_print_safe(" no action");
     367                                LIB_DEBUG_PRINT_SAFE(" no action");
    368368                        }
    369369                        else if (actions & _UA_CLEANUP_PHASE) {
     
    385385                                _Unwind_SetIP( context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
    386386
    387                                 __cfaabi_dbg_print_safe(" action\n");
     387                                LIB_DEBUG_PRINT_SAFE(" action\n");
    388388
    389389                                // Return have some action to run
     
    393393
    394394                // Nothing to do, move along
    395                 __cfaabi_dbg_print_safe(" no landing pad");
     395                LIB_DEBUG_PRINT_SAFE(" no landing pad");
    396396        }
    397397        // No handling found
    398         __cfaabi_dbg_print_safe(" table end reached\n");
     398        LIB_DEBUG_PRINT_SAFE(" table end reached\n");
    399399
    400400        UNWIND:
    401         __cfaabi_dbg_print_safe(" unwind\n");
     401        LIB_DEBUG_PRINT_SAFE(" unwind\n");
    402402
    403403        // Keep unwinding the stack
     
    408408// libcfa but there is one problem left, see the exception table for details
    409409__attribute__((noinline))
    410 void __cfaabi_ehm__try_terminate(void (*try_block)(),
     410void __cfaehm__try_terminate(void (*try_block)(),
    411411                void (*catch_block)(int index, exception * except),
    412412                __attribute__((unused)) int (*match_block)(exception * except)) {
     
    466466        // Body uses language specific data and therefore could be modified arbitrarily
    467467        ".LLSDACSBCFA2:\n"                                              // BODY start
    468         "       .uleb128 .TRYSTART-__cfaabi_ehm__try_terminate\n"               // Handled area start  (relative to start of function)
     468        "       .uleb128 .TRYSTART-__cfaehm__try_terminate\n"           // Handled area start  (relative to start of function)
    469469        "       .uleb128 .TRYEND-.TRYSTART\n"                           // Handled area length
    470         "       .uleb128 .CATCH-__cfaabi_ehm__try_terminate\n"                          // Hanlder landing pad adress  (relative to start of function)
     470        "       .uleb128 .CATCH-__cfaehm__try_terminate\n"                              // Hanlder landing pad adress  (relative to start of function)
    471471        "       .uleb128 1\n"                                           // Action code, gcc seems to use always 0
    472472        ".LLSDACSECFA2:\n"                                              // BODY end
    473473        "       .text\n"                                                        // TABLE footer
    474         "       .size   __cfaabi_ehm__try_terminate, .-__cfaabi_ehm__try_terminate\n"
     474        "       .size   __cfaehm__try_terminate, .-__cfaehm__try_terminate\n"
    475475        "       .ident  \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n"
    476476//      "       .section        .note.GNU-stack,\"x\",@progbits\n"
  • src/libcfa/exception.h

    r5da9d6a rd16d159  
    2121#endif
    2222
    23 struct __cfaabi_ehm__base_exception_t;
    24 typedef struct __cfaabi_ehm__base_exception_t exception;
    25 struct __cfaabi_ehm__base_exception_t_vtable {
    26         const struct __cfaabi_ehm__base_exception_t_vtable * parent;
     23struct __cfaehm__base_exception_t;
     24typedef struct __cfaehm__base_exception_t exception;
     25struct __cfaehm__base_exception_t_vtable {
     26        const struct __cfaehm__base_exception_t_vtable * parent;
    2727        size_t size;
    28         void (*copy)(struct __cfaabi_ehm__base_exception_t *this,
    29                      struct __cfaabi_ehm__base_exception_t * other);
    30         void (*free)(struct __cfaabi_ehm__base_exception_t *this);
    31         const char * (*msg)(struct __cfaabi_ehm__base_exception_t *this);
     28        void (*copy)(struct __cfaehm__base_exception_t *this,
     29                     struct __cfaehm__base_exception_t * other);
     30        void (*free)(struct __cfaehm__base_exception_t *this);
     31        const char * (*msg)(struct __cfaehm__base_exception_t *this);
    3232};
    33 struct __cfaabi_ehm__base_exception_t {
    34         struct __cfaabi_ehm__base_exception_t_vtable const * virtual_table;
     33struct __cfaehm__base_exception_t {
     34        struct __cfaehm__base_exception_t_vtable const * virtual_table;
    3535};
    36 extern struct __cfaabi_ehm__base_exception_t_vtable
    37         ___cfaabi_ehm__base_exception_t_vtable_instance;
     36extern struct __cfaehm__base_exception_t_vtable
     37        ___cfaehm__base_exception_t_vtable_instance;
    3838
    3939
    4040// Used in throw statement translation.
    41 void __cfaabi_ehm__throw_terminate(exception * except) __attribute__((noreturn));
    42 void __cfaabi_ehm__rethrow_terminate() __attribute__((noreturn));
    43 void __cfaabi_ehm__throw_resume(exception * except);
     41void __cfaehm__throw_terminate(exception * except) __attribute__((noreturn));
     42void __cfaehm__rethrow_terminate() __attribute__((noreturn));
     43void __cfaehm__throw_resume(exception * except);
    4444
    4545// Function catches termination exceptions.
    46 void __cfaabi_ehm__try_terminate(
     46void __cfaehm__try_terminate(
    4747    void (*try_block)(),
    4848    void (*catch_block)(int index, exception * except),
     
    5050
    5151// Clean-up the exception in catch blocks.
    52 void __cfaabi_ehm__cleanup_terminate(void * except);
     52void __cfaehm__cleanup_terminate(void * except);
    5353
    5454// Data structure creates a list of resume handlers.
    55 struct __cfaabi_ehm__try_resume_node {
    56     struct __cfaabi_ehm__try_resume_node * next;
     55struct __cfaehm__try_resume_node {
     56    struct __cfaehm__try_resume_node * next;
    5757    _Bool (*handler)(exception * except);
    5858};
    5959
    6060// These act as constructor and destructor for the resume node.
    61 void __cfaabi_ehm__try_resume_setup(
    62     struct __cfaabi_ehm__try_resume_node * node,
     61void __cfaehm__try_resume_setup(
     62    struct __cfaehm__try_resume_node * node,
    6363    _Bool (*handler)(exception * except));
    64 void __cfaabi_ehm__try_resume_cleanup(
    65     struct __cfaabi_ehm__try_resume_node * node);
     64void __cfaehm__try_resume_cleanup(
     65    struct __cfaehm__try_resume_node * node);
    6666
    6767// Check for a standard way to call fake deconstructors.
    68 struct __cfaabi_ehm__cleanup_hook {};
     68struct __cfaehm__cleanup_hook {};
    6969
    7070#ifdef __cforall
  • src/libcfa/interpose.c

    r5da9d6a rd16d159  
    2424}
    2525
    26 #include "bits/debug.h"
    27 #include "bits/defs.h"
     26#include "libhdr/libdebug.h"
     27#include "libhdr/libtools.h"
    2828#include "startup.h"
    2929
     
    6969__typeof__( exit ) libc_exit __attribute__(( noreturn ));
    7070__typeof__( abort ) libc_abort __attribute__(( noreturn ));
     71
     72// #define INIT_REALRTN( x, ver ) libc_##x = (__typeof__(libc_##x))interpose_symbol( #x, ver )
    7173
    7274forall(dtype T)
     
    125127                        va_end( args );
    126128
    127                         __cfaabi_dbg_bits_write( abort_text, len );
    128                         __cfaabi_dbg_bits_write( "\n", 1 );
     129                        __lib_debug_write( abort_text, len );
     130                        __lib_debug_write( "\n", 1 );
    129131                }
    130132
    131133                len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld)\n", (long int)getpid() ); // use UNIX pid (versus getPid)
    132                 __cfaabi_dbg_bits_write( abort_text, len );
     134                __lib_debug_write( abort_text, len );
    133135
    134136
  • src/libcfa/stdhdr/assert.h

    r5da9d6a rd16d159  
    3030#endif
    3131
    32 #if !defined(NDEBUG) && (defined(__CFA_DEBUG__) || defined(__CFA_VERIFY__))
    33         #define verify(x) assert(x)
    34         #define verifyf(x, ...) assertf(x, __VA_ARGS__)
    35 #else
    36         #define verify(x)
    37         #define verifyf(x, ...)
    38 #endif
    39 
    4032#ifdef __cforall
    4133} // extern "C"
  • src/prelude/builtins.c

    r5da9d6a rd16d159  
    1616// exception implementation
    1717
    18 typedef unsigned long long __cfaabi_abi_exception_type_t;
     18typedef unsigned long long __cfaabi_exception_type_t;
    1919
    2020#include "../libcfa/virtual.h"
     
    8080} // ?\?
    8181
    82 // FIXME (x \ (unsigned long int)y) relies on X ?\?(T, unsigned long) a function that is neither
    83 // defined, nor passed as an assertion parameter. Without user-defined conversions, cannot specify
    84 // X as a type that casts to double, yet it doesn't make sense to write functions with that type
     82// FIXME (x \ (unsigned long int)y) relies on X ?\?(T, unsigned long) a function that is neither 
     83// defined, nor passed as an assertion parameter. Without user-defined conversions, cannot specify 
     84// X as a type that casts to double, yet it doesn't make sense to write functions with that type 
    8585// signature where X is double.
    8686
  • src/tests/except-mac.h

    r5da9d6a rd16d159  
    77
    88// The fully (perhaps overly) qualified name of the base exception type:
    9 #define BASE_EXCEPT __cfaabi_ehm__base_exception_t
     9#define BASE_EXCEPT __cfaehm__base_exception_t
    1010
    1111// Get the name of the vtable type and vtable instance for an exception type:
Note: See TracChangeset for help on using the changeset viewer.