Changeset dcfc4b35 for doc/proposals/concurrency
- Timestamp:
- Oct 3, 2017, 3:09:12 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 3628765
- Parents:
- e1ff775
- Location:
- doc/proposals/concurrency
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/concurrency/Makefile
re1ff775 rdcfc4b35 16 16 text/basics \ 17 17 text/concurrency \ 18 text/internals \ 18 19 text/parallelism \ 19 20 text/together \ -
doc/proposals/concurrency/style/cfa-format.tex
re1ff775 rdcfc4b35 133 133 belowskip=3pt, 134 134 keepspaces=true, 135 tabsize=4, 135 136 % frame=lines, 136 137 literate=, … … 150 151 keywordstyle=\bfseries\color{blue}, 151 152 keywordstyle=[2]\bfseries\color{Plum}, 152 commentstyle=\ itshape\color{OliveGreen},% green and italic comments153 commentstyle=\sf\itshape\color{OliveGreen}, % green and italic comments 153 154 identifierstyle=\color{identifierCol}, 154 155 stringstyle=\sf\color{Mahogany}, % use sanserif font … … 158 159 belowskip=3pt, 159 160 keepspaces=true, 161 tabsize=4, 160 162 % frame=lines, 161 163 literate=, -
doc/proposals/concurrency/text/basics.tex
re1ff775 rdcfc4b35 1 1 % ====================================================================== 2 2 % ====================================================================== 3 \chapter{ Basics}\label{basics}3 \chapter{Concurrency Basics}\label{basics} 4 4 % ====================================================================== 5 5 % ====================================================================== 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 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. 7 7 8 8 \section{Basics of concurrency} 9 At its core, concurrency is based on having call-stacks and potentially multiple threads of execution for these stacks. Concurrency without parallelism only requires having multiple call stacks (or contexts) for a single thread of execution, and switching between these call stacks on a regular basis. A minimal concurrency product can be achieved by creating coroutines, which instead of context switching between each other, always ask an oracle where to context switch next. While coroutines do not technically require a stack, stackfull coroutines are the closest abstraction to a practical "naked"" call stack. When writing concurrency in terms of coroutines, the oracle effectively becomes a scheduler and the whole system now follows a cooperative threading-model \cit. The oracle/scheduler can either be a stackless or stackfull 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. Indeed, concurrency challenges appear with non-determinism. Guaranteeing mutual-exclusion or synchronisation are simply ways of limiting the lack of determinism in a system. A scheduler introduces order of execution uncertainty, while preemption introduces incertainty about where context-switches occur. Now it is important to understand that uncertainty is not necessarily undesireable; uncertainty can often be used by 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\cit. 9 At its core, concurrency is based on having multiple call-stacks and scheduling among threads of execution executing on these stacks. Concurrency without parallelism only requires having multiple call stacks (or contexts) for a single thread of execution. 10 11 Indeed, while execution 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. 12 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, stackfull 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 \cit. The oracle/scheduler can either be a stackless or stackfull 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. Indeed, concurrency challenges appear with non-determinism. Using mutual-exclusion or synchronisation are ways of limiting the lack of determinism in a system. A scheduler introduces order of execution uncertainty, while preemption introduces uncertainty about where context-switches occur. Now it is important to understand that uncertainty is not undesireable; uncertainty can often be used by 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\cit. 10 14 11 15 \section{\protect\CFA 's Thread Building Blocks} 12 One of the important features that is missing in C is threading. On modern architectures, a lack of threading is becoming less and less forgivable\cite{Sutter05, Sutter05b}, and therefore modern programming languages must have the proper tools to allow users to write performant concurrent and/or parallel programs. As an extension of C, \CFA needs to express these concepts in a way that is as natural as possible to programmers used toimperative 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.16 One 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 and/or parallel programs. 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. 13 17 14 18 \section{Coroutines: A stepping stone}\label{coroutine} 15 While the main focus of this proposal is concurrency and parallelism, as mentionned above it is important to adress coroutines, which are actually a significant underlying aspect of a concurrency system. Indeed, while having nothing to do with parallelism and arguably little to do with concurrency, coroutines need to deal with context-switchs 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 APIof coroutines revolve around two features: independent call stacks and \code{suspend}/\code{resume}.19 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-switchs 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}. 16 20 17 21 Here is an example of a solution to the fibonnaci problem using \CFA coroutines: … … 59 63 60 64 \subsection{Construction} 61 One important design challenge for coroutines and threads (shown in section \ref{threads}) is that the runtime system needs to run code after the user-constructor runs . 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.62 63 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. Like for regular objects, constructors can stillleak coroutines before they are ready. There are several solutions to this problem but the chosen options effectively forces the design of the coroutine.65 One important design challenge for 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 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. 66 67 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. As regular objects, constructors can leak coroutines before they are ready. There are several solutions to this problem but the chosen options effectively forces the design of the coroutine. 64 68 65 69 Furthermore, \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: … … 95 99 } 96 100 \end{ccode} 97 The problem in this example is a race condition between the start of the execution of \code{noop} on the other thread and the stack frame of \code{bar} being destroyed. This extra challenge limits which solutions are viable because storing the function pointer for too long only increases the chances that the race will end inundefined behavior; i.e. the stack based thunk being destroyed before it was used. This challenge is an extension of challenges that come with second-class routines. Indeed, GCC nested routines also have the limitation that the routines cannot be passed outside of the scope of the functions these were declared in. The case of coroutines and threads is simply an extension of this problem to multiple call-stacks.101 The problem in this example is a storage management issue, the function pointer \code{_thunk0} is only valid until the end of the block. This extra challenge limits which solutions are viable because storing the function pointer for too long causes undefined behavior; i.e. the stack based thunk being destroyed before it was used. This challenge is an extension of challenges that come with second-class routines. Indeed, GCC nested routines also have the limitation that the routines cannot be passed outside of the scope of the functions these were declared in. The case of coroutines and threads is simply an extension of this problem to multiple call-stacks. 98 102 99 103 \subsection{Alternative: Composition} 100 One solution to this challenge would be to use composition/containement,104 One solution to this challenge is to use composition/containement, where uses add insert a coroutine field which contains the necessary information to manage the coroutine. 101 105 102 106 \begin{cfacode} 103 107 struct Fibonacci { 104 int fn; //used for communication105 108 int fn; //used for communication 109 coroutine c; //composition 106 110 }; 107 111 108 112 void ?{}(Fibonacci & this) { 109 110 (this.c){}; 111 } 112 \end{cfacode} 113 There are two downsides to this approach. The first, which is relatively minor, is that the base class needs to be made aware of the main routine pointer, regardless of whether a parameter or a virtual pointer is used, this means the coroutine data must be made larger to store a value that is actually a compile time constant (address of the main routine). The second problem, which is both subtle and significant, is that now users can get the initialisation order of there coroutines wrong. Indeed, every field of a \CFA struct is constructed but in declaration order, unless users explicitly write otherwise. This semantics means that users who forget to initialize a the coroutine may resume the coroutine with an uninitilized object. For coroutines, this is unlikely to be a problem, for threads however, this is a significant problem.113 this.fn = 0; 114 (this.c){}; //Call constructor to initialize coroutine 115 } 116 \end{cfacode} 117 There are two downsides to this approach. The first, which is relatively minor, is that the coroutine handle needs to be made aware of the main routine pointer. This requirement means the coroutine data must be made larger to store a value that is actually a compile time constant (address of the main routine). The second problem, which is both subtle and significant, is that now users can get the initialisation order of coroutines wrong. Indeed, every field of a \CFA struct is constructed but in declaration order, unless users explicitly write otherwise. This semantics means that users who forget to initialize the coroutine handle may resume the coroutine with an uninitilized object. For coroutines, this is unlikely to be a problem, for threads however, this is a significant problem. 114 118 115 119 \subsection{Alternative: Reserved keyword} … … 118 122 \begin{cfacode} 119 123 coroutine Fibonacci { 120 124 int fn; // used for communication 121 125 }; 122 126 \end{cfacode} 123 This mean the compiler can solve problems by injecting code where needed. The downside of this approach is that it makes coroutine a special case in the language. Users who would want to extend coroutines or build their own for various reasons can only do so in ways offered by the language. Furthermore, implementing coroutines without language supports also displays the power of \CFA. 124 While this is ultimately the option used for idiomatic \CFA code, coroutines and threads can both be constructed by users without using the language support. The reserved keywords are only present to improve ease of use for the common cases. 127 This mean the compiler can solve problems by injecting code where needed. The downside of this approach is that it makes coroutine a special case in the language. Users who would want to extend coroutines or build their own for various reasons can only do so in ways offered by the language. Furthermore, implementing coroutines without language supports also displays the power of the programming language used. While this is ultimately the option used for idiomatic \CFA code, coroutines and threads can both be constructed by users without using the language support. The reserved keywords are only present to improve ease of use for the common cases. 125 128 126 129 \subsection{Alternative: Lamda Objects} … … 159 162 coroutine_desc * get_coroutine(T & this); 160 163 }; 161 \end{cfacode} 162 This ensures an object is not a coroutine until \code{resume} (or \code{prime}) is called on the object. Correspondingly, any object that is passed to \code{resume} is a coroutine since it must satisfy the \code{is_coroutine} trait to compile. The advantage of this approach is that users can easily create different types of coroutines, for example, changing the memory foot print of a coroutine is trivial when implementing the \code{get_coroutine} routine. The \CFA keyword \code{coroutine} only has the effect of implementing the getter and forward declarations required for users to only have to implement the main routine. 164 165 forall( dtype T | is_coroutine(T) ) void suspend(T &); 166 forall( dtype T | is_coroutine(T) ) void resume (T &); 167 \end{cfacode} 168 This ensures an object is not a coroutine until \code{resume} is called on the object. Correspondingly, any object that is passed to \code{resume} is a coroutine since it must satisfy the \code{is_coroutine} trait to compile. The advantage of this approach is that users can easily create different types of coroutines, for example, changing the memory layout of a coroutine is trivial when implementing the \code{get_coroutine} routine. The \CFA keyword \code{coroutine} only has the effect of implementing the getter and forward declarations required for users to only have to implement the main routine. 163 169 164 170 \begin{center} … … 186 192 \end{center} 187 193 188 The combination of these two approaches allows users new to co ncurrency to have a easy and concise method while more advanced users can expose themselves to otherwise hidden pitfalls at the benefit oftighter control on memory layout and initialization.194 The combination of these two approaches allows users new to coroutinning and concurrency to have an easy and concise specification, while more advanced users have tighter control on memory layout and initialization. 189 195 190 196 \section{Thread Interface}\label{threads} … … 205 211 \end{cfacode} 206 212 207 Obviously, for this thread implementation to be usefull it must run some user code. Several other threading interfaces use a function-pointer representation as the interface of threads (for example \Csharp~\cite{Csharp} and Scala~\cite{Scala}). However, this proposal considers that statically tying a \code{main} routine to a thread superseeds this approach. Since the \code{main} routine is already a special routine in \CFA (where the program begins), it is possible naturally extendthe semantics using overloading to declare mains for different threads (the normal main being the main of the initial thread). As such the \code{main} routine of a thread can be defined as213 Obviously, for this thread implementation to be usefull it must run some user code. Several other threading interfaces use a function-pointer representation as the interface of threads (for example \Csharp~\cite{Csharp} and Scala~\cite{Scala}). However, this proposal considers that statically tying a \code{main} routine to a thread superseeds this approach. Since the \code{main} routine is already a special routine in \CFA (where the program begins), it is a natural extension of the semantics using overloading to declare mains for different threads (the normal main being the main of the initial thread). As such the \code{main} routine of a thread can be defined as 208 214 \begin{cfacode} 209 215 thread foo {}; … … 214 220 \end{cfacode} 215 221 216 In this example, threads of type \code{foo} start execution in the \code{void main(foo *)} routine which prints \code{"Hello World!"}. While this proposoal encourages this approach to enforce strongly-typed programming, users may prefer to use the routine based thread semantics for the sake of simplicity. With these semantics it is trivial to write a thread type that takes a function pointer asparameter and executes it on its stack asynchronously217 \begin{cfacode} 218 typedef void (*voidFunc)( void);222 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 these semantics it is trivial to write a thread type that takes a function pointer as a parameter and executes it on its stack asynchronously 223 \begin{cfacode} 224 typedef void (*voidFunc)(int); 219 225 220 226 thread FuncRunner { 221 227 voidFunc func; 228 int arg; 222 229 }; 223 230 224 //ctor 225 void ?{}(FuncRunner & this, voidFunc inFunc) { 231 void ?{}(FuncRunner & this, voidFunc inFunc, int arg) { 226 232 this.func = inFunc; 227 233 } 228 234 229 //main230 235 void main(FuncRunner & this) { 231 this.func( );232 } 233 \end{cfacode} 234 235 An advantage of the overloading approach to main is to clearly highlight where and what memory is required to pass parameters and return values to/from a thread.236 237 Of 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} oncethe constructor has completed and \code{join} before the destructor runs.236 this.func( this.arg ); 237 } 238 \end{cfacode} 239 240 An 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}. 241 242 Of 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. 238 243 \begin{cfacode} 239 244 thread World; … … 254 259 \end{cfacode} 255 260 256 This semantic has several advantages over explicit semantics typesafety is guaranteed, a thread is always started and stopped exaclty once and users cannot make any progamming errors. Another advantage of this semantic is that it naturally scaleto multiple threads meaning basic synchronisation is very simple261 This semantic has several advantages over explicit semantics: a thread is always started and stopped exaclty once and users cannot make any progamming errors and it naturally scales to multiple threads meaning basic synchronisation is very simple 257 262 258 263 \begin{cfacode} … … 276 281 \end{cfacode} 277 282 278 However, one of the apparent drawbacks of this system is that threads now always form a lattice, that is they are always destroyed in opposite order of construction because of block structure. However, storage allocation is not limited to blocks; dynamic allocation can create threads that outlive the scope in which the thread is createdmuch like dynamically allocating memory lets objects outlive the scope in which they are created283 However, one of the drawbacks of this approach is that threads now always form a lattice, that is they are always destroyed in opposite order of construction because of block structure. This restriction is relaxed by using dynamic allocation, so threads can outlive the scope in which they are created, much like dynamically allocating memory lets objects outlive the scope in which they are created 279 284 280 285 \begin{cfacode} … … 283 288 }; 284 289 285 //main286 290 void main(MyThread & this) { 287 291 //... … … 291 295 MyThread * long_lived; 292 296 { 297 //Start a thread at the beginning of the scope 293 298 MyThread short_lived; 294 //Start a thread at the beginning of the scope295 296 DoStuff();297 299 298 300 //create another thread that will outlive the thread in this scope 299 301 long_lived = new MyThread; 300 302 303 DoStuff(); 304 301 305 //Wait for the thread short_lived to finish 302 306 } 303 307 DoMoreStuff(); 304 308 305 //Now wait for the short_lived to finish309 //Now wait for the long_lived to finish 306 310 delete long_lived; 307 311 } -
doc/proposals/concurrency/text/cforall.tex
re1ff775 rdcfc4b35 5 5 % ====================================================================== 6 6 7 As mentionned in the introduction, the document presents the design for the concurrency features in \CFA. Since it is a new language here is a quick review of the languagespecifically tailored to the features needed to support concurrency.7 This thesis presents the design for a set of concurrency features in \CFA. Since it is a new dialect of C, the following is a quick introduction to the language, specifically tailored to the features needed to support concurrency. 8 8 9 \CFA is a extension of ISO C and therefore supports much of the same paradigms as C. It is a non-object oriented system level language, meaning it has very most of the major abstractions have either no runtime cost or can be opt-out easily. Like C, the basics of \CFA revolve around structures and routines, which are thin abstractions over assembly. The vast majority of the code produced by a \CFA compiler respects memory-layouts and calling-conventions laid out by C. However, while \CFA is not an object-oriented language according to a strict definition. It does have some notion of objects, most importantly construction and destruction of objects. Most of the following pieces of code can be found as is on the \CFA website :\cite{www-cfa}9 \CFA is a 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 received (e.g.: this), it does have some notion of objects\footnote{C defines the term objects as : [Where to I get the C11 reference manual?]}, most importantly construction and destruction of objects. Most of the following pieces of code can be found on the \CFA website \cite{www-cfa} 10 10 11 11 \section{References} 12 12 13 Like \CC, \CFA introduces references as an alternative to pointers. In regards to concurrency, the semantics difference between pointers and references are n't particularly relevant but since this document uses mostly references here is a quick overview of the semantics :13 Like \CC, \CFA introduces references as an alternative to pointers. In regards to concurrency, the semantics difference between pointers and references are not particularly relevant but since this document uses mostly references here is a quick overview of the semantics : 14 14 \begin{cfacode} 15 15 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 16 16 &r1 = x, &&r2 = r1, &&&r3 = r2; 17 ***p3 = 3; // change x 18 r3 = 3; // change x, ***r3 19 **p3 = ...; // change p1 20 &r3 = ...; // change r1, (&*)**r3 21 *p3 = ...; // change p2 22 &&r3 = ...; // change r2, (&(&*)*)*r3 23 &&&r3 = p3; // change r3 to p3, (&(&(&*)*)*)r3 24 int y, z, & ar[3] = { x, y, z }; // initialize array of references 25 &ar[1] = &z; // change reference array element 26 typeof( ar[1] ) p; // is int, i.e., the type of referenced object 27 typeof( &ar[1] ) q; // is int &, i.e., the type of reference 28 sizeof( ar[1] ) == sizeof( int ); // is true, i.e., the size of referenced object 29 sizeof( &ar[1] ) == sizeof( int *); // is true, i.e., the size of a reference 17 ***p3 = 3; //change x 18 r3 = 3; //change x, ***r3 19 **p3 = ...; //change p1 20 *p3 = ...; //change p2 21 int y, z, & ar[3] = {x, y, z}; //initialize array of references 22 typeof( ar[1]) p; //is int, i.e., the type of referenced object 23 typeof(&ar[1]) q; //is int &, i.e., the type of reference 24 sizeof( ar[1]) == sizeof(int); //is true, i.e., the size of referenced object 25 sizeof(&ar[1]) == sizeof(int *); //is true, i.e., the size of a reference 30 26 \end{cfacode} 31 27 The important thing to take away from this code snippet is that references offer a handle to an object much like pointers but which is automatically derefferenced when convinient. … … 33 29 \section{Overloading} 34 30 35 Another important feature \CFA has in common with \CC is function overloading :31 Another important feature of \CFA is function overloading as in Java and \CC, where routine with the same name are selected based on the numbers and type of the arguments. As well, \CFA uses the return type as part of the selection criteria, as in Ada\cite{Ada}. For routines with multiple parameters and returns, the selection is complex. 36 32 \begin{cfacode} 37 // 38 void f( void ); //(1)39 void f( char ); //(2)40 void f( int, double ); //(3)41 f(); // 42 f( 'a' ); //select (2)43 f( 3, 5.2 ); //select (3)33 //selection based on type and number of parameters 34 void f(void); //(1) 35 void f(char); //(2) 36 void f(int, double); //(3) 37 f(); //select (1) 38 f('a'); //select (2) 39 f(3, 5.2); //select (3) 44 40 45 // selection based on type and number of returns 46 char f( int ); // (1) 47 double f( int ); // (2) 48 [ int, double ] f( int ); // (3) 49 char c = f( 3 ); // select (1) 50 double d = f( 4 ); // select (2) 51 [ int, double ] t = f( 5 ); // select (3) 41 //selection based on type and number of returns 42 char f(int); //(1) 43 double f(int); //(2) 44 char c = f(3); //select (1) 45 double d = f(4); //select (2) 52 46 \end{cfacode} 53 This feature is particularly important for concurrency since the runtime system relies on creating different types do represent concurrency objects. Therefore, overloading is necessary to prevent the need for long prefixes and other naming conventions that prevent clashes. As seen in chapter \ref{basics}, the main is an example of routine that benefits from overloading when concurrency in introduced.47 This feature is particularly important for concurrency since the runtime system relies on creating different types to represent concurrency objects. Therefore, overloading is necessary to prevent the need for long prefixes and other naming conventions that prevent name clashes. As seen in chapter \ref{basics}, routines main is an example that benefits from overloading. 54 48 55 49 \section{Operators} 56 50 Overloading also extends to operators. The syntax for denoting operator-overloading is to name a routine with the symbol of the operator and question marks where the arguments of the operation would be, like so : 57 51 \begin{cfacode} 58 int ++? ( int op ); //unary prefix increment59 int ?++ ( int op ); //unary postfix increment60 int ?+? ( int op1, int op2 ); //binary plus61 int ?<=?( int op1, int op2 ); //binary less than62 int ?=? ( int & op1, int op2 ); //binary assignment63 int ?+=?( int & op1, int op2 ); //binary plus-assignment52 int ++? (int op); //unary prefix increment 53 int ?++ (int op); //unary postfix increment 54 int ?+? (int op1, int op2); //binary plus 55 int ?<=?(int op1, int op2); //binary less than 56 int ?=? (int & op1, int op2); //binary assignment 57 int ?+=?(int & op1, int op2); //binary plus-assignment 64 58 65 struct S { int i, j;};66 S ?+?( S op1, S op2 ) { //add two structures67 return (S){ op1.i + op2.i, op1.j + op2.j};59 struct S {int i, j;}; 60 S ?+?(S op1, S op2) { //add two structures 61 return (S){op1.i + op2.i, op1.j + op2.j}; 68 62 } 69 S s1 = { 1, 2 }, s2 = { 2, 3}, s3;70 s3 = s1 + s2; // compute sum: s3 == { 2, 5}63 S s1 = {1, 2}, s2 = {2, 3}, s3; 64 s3 = s1 + s2; //compute sum: s3 == {2, 5} 71 65 \end{cfacode} 72 73 Since concurrency does not use operator overloading, this feature is more important as an introduction for the syntax of constructors. 66 While concurrency does not use operator overloading directly, this feature is more important as an introduction for the syntax of constructors. 74 67 75 68 \section{Constructors/Destructors} 76 Object 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 \& Destructors are a the core of the featuresrequired for concurrency and parallelism. \CFA uses the following syntax for constructors and destructors :69 Object 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 : 77 70 \begin{cfacode} 78 71 struct S { … … 80 73 int * ia; 81 74 }; 82 void ?{}( S & s, int asize ) with s { //constructor operator83 s ize = asize; //initialize fields84 ia = calloc( size, sizeof( S ));75 void ?{}(S & s, int asize) { //constructor operator 76 s.size = asize; //initialize fields 77 s.ia = calloc(size, sizeof(S)); 85 78 } 86 void ^?{}( S & s ) with s { //destructor operator87 free( ia ); //de-initialization fields79 void ^?{}(S & s) { //destructor operator 80 free(ia); //de-initialization fields 88 81 } 89 82 int main() { 90 S x = { 10 }, y = { 100 }; // implict calls: ?{}( x, 10 ), ?{}( y, 100)91 ... //use x and y92 ^x{}; ^y{}; //explicit calls to de-initialize93 x{ 20 }; y{ 200 }; //explicit calls to reinitialize94 ... //reuse x and y95 } // implict calls: ^?{}( y ), ^?{}( x)83 S x = {10}, y = {100}; //implict calls: ?{}(x, 10), ?{}(y, 100) 84 ... //use x and y 85 ^x{}; ^y{}; //explicit calls to de-initialize 86 x{20}; y{200}; //explicit calls to reinitialize 87 ... //reuse x and y 88 } //implict calls: ^?{}(y), ^?{}(x) 96 89 \end{cfacode} 97 The language guarantees that every object and all their fields are constructed. Like \CC construction is automatically done on declaration and destruction done when the declared variables reach the end of its scope. 90 The language guarantees that every object and all their fields are constructed. Like \CC, construction of an object is automatically done on allocation and destruction of the object is done on deallocation. Allocation and deallocation can occur on the stack or on the heap. 91 \begin{cfacode} 92 { 93 struct S s = {10}; //allocation, call constructor 94 ... 95 } //deallocation, call destructor 96 struct S * s = new(); //allocation, call constructor 97 ... 98 delete(s); //deallocation, call destructor 99 \end{cfacode} 100 Note that like \CC, \CFA introduces \code{new} and \code{delete}, which behave like \code{malloc} and \code{free} in addition to constructing and destructing objects, after calling \code{malloc} and before calling \code{free} respectively. 98 101 99 For more information see \cite{cforall-ug,rob-thesis,www-cfa}.102 For more information on \CFA see \cite{cforall-ug,rob-thesis,www-cfa}. -
doc/proposals/concurrency/text/concurrency.tex
re1ff775 rdcfc4b35 700 700 \end{tabular} 701 701 \end{center} 702 This method is more constrained and explicit, which may help users tone down the undeterministic nature of concurrency. Indeed, as the following examples demonstrates, external scheduling allows users to wait for events from other threads without the concern of unrelated events occuring. External scheduling can generally be done either in terms of control flow (e.g., \uC) or in terms of data (e.g. Go). Of course, both of these paradigms have their own strenghts and weaknesses but for this project control-flow semantics were chosen to stay consistent with the rest of the languages semantics. Two challenges specific to \CFA arise when trying to add external scheduling with loose object definitions and multi-monitor routines. The previous example shows a simple use \code{_Accept} versus \code{wait}/\code{signal} and its advantages. Note that while other languages often use \code{accept} as the core external scheduling keyword, \CFA uses \code{waitfor} to prevent name collisions with existing socket APIs.702 This method is more constrained and explicit, which may help users tone down the undeterministic nature of concurrency. Indeed, as the following examples demonstrates, external scheduling allows users to wait for events from other threads without the concern of unrelated events occuring. External scheduling can generally be done either in terms of control flow (e.g., \uC) or in terms of data (e.g. Go). Of course, both of these paradigms have their own strenghts and weaknesses but for this project control-flow semantics were chosen to stay consistent with the rest of the languages semantics. Two challenges specific to \CFA arise when trying to add external scheduling with loose object definitions and multi-monitor routines. The previous example shows a simple use \code{_Accept} versus \code{wait}/\code{signal} and its advantages. Note that while other languages often use \code{accept} as the core external scheduling keyword, \CFA uses \code{waitfor} to prevent name collisions with existing socket \acrshort{api}s. 703 703 704 704 In the case of internal scheduling, the call to \code{wait} only guarantees that \code{V} is the last routine to access the monitor. This entails that the routine \code{V} may have acquired mutual exclusion several times while routine \code{P} was waiting. On the other hand, external scheduling guarantees that while routine \code{P} was waiting, no routine other than \code{V} could acquire the monitor. -
doc/proposals/concurrency/text/intro.tex
re1ff775 rdcfc4b35 3 3 % ====================================================================== 4 4 5 This proposal provides a minimal concurrency API that is simple, efficient and can be reused to build higher-level features. The simplest possible concurrency system is a thread and a lock but this low-level approach is hard to master. An easier approach for users is to support higher-level constructs as the basis of the concurrency, in \CFA. Indeed, for highly productive concurrent programming, high-level approaches are much more popular~\cite{HPP:Study}. Examples are task based, message passing and implicit threading. Therefore a high-level approach is adopted in \CFA5 This thesis provides a minimal concurrency \acrshort{api} that is simple, efficient and can be reused to build higher-level features. The simplest possible concurrency system is a thread and a lock but this low-level approach is hard to master. An easier approach for users is to support higher-level constructs as the basis of concurrency. Indeed, for highly productive concurrent programming, high-level approaches are much more popular~\cite{HPP:Study}. Examples are task based, message passing and implicit threading. The high-level approach and its minimal \acrshort{api} are tested in a dialect of C, call \CFA. [Is there value to say that this thesis is also an early definition of the \CFA language and library in regards to concurrency?] 6 6 7 There are actually two problems that need to be solved in the design of concurrency for a programming language: which concurrency and which parallelism tools are available to the programmer s. While these two concepts are often combined, they are in fact distinct, requiring different tools~\cite{Buhr05a}. Concurrency tools need to handle mutual exclusion and synchronization, while parallelism tools are about performance, cost and resource utilization.7 There are actually two problems that need to be solved in the design of concurrency for a programming language: which concurrency and which parallelism tools are available to the programmer. While these two concepts are often combined, they are in fact distinct, requiring different tools~\cite{Buhr05a}. Concurrency tools need to handle mutual exclusion and synchronization, while parallelism tools are about performance, cost and resource utilization. -
doc/proposals/concurrency/thesis.tex
re1ff775 rdcfc4b35 103 103 \input{parallelism} 104 104 105 \input{internals} 106 105 107 \input{together} 106 108 -
doc/proposals/concurrency/version
re1ff775 rdcfc4b35 1 0.10. 21 0.10.33
Note: See TracChangeset
for help on using the changeset viewer.