| 1 | \chapter{\CFA Existing Features}
|
|---|
| 2 |
|
|---|
| 3 | \CFA (C-for-all)~\cite{Cforall} is an open-source project extending ISO C with
|
|---|
| 4 | modern safety and productivity features, while still ensuring backwards
|
|---|
| 5 | compatibility with C and its programmers. \CFA is designed to have an
|
|---|
| 6 | orthogonal feature-set based closely on the C programming paradigm
|
|---|
| 7 | (non-object-oriented) and these features can be added incrementally to an
|
|---|
| 8 | existing C code-base allowing programmers to learn \CFA on an as-needed basis.
|
|---|
| 9 |
|
|---|
| 10 | Only those \CFA features pertinent to this thesis are discussed. Many of the
|
|---|
| 11 | \CFA syntactic and semantic features used in the thesis should be fairly
|
|---|
| 12 | obvious to the reader.
|
|---|
| 13 |
|
|---|
| 14 | \section{Overloading and \lstinline{extern}}
|
|---|
| 15 | \CFA has extensive overloading, allowing multiple definitions of the same name
|
|---|
| 16 | to be defined~\cite{Moss18}.
|
|---|
| 17 | \begin{cfa}
|
|---|
| 18 | char i; int i; double i;
|
|---|
| 19 | int f(); double f();
|
|---|
| 20 | void g( int ); void g( double );
|
|---|
| 21 | \end{cfa}
|
|---|
| 22 | This feature requires name mangling so the assembly symbols are unique for
|
|---|
| 23 | different overloads. For compatibility with names in C, there is also a syntax
|
|---|
| 24 | to disable name mangling. These unmangled names cannot be overloaded but act as
|
|---|
| 25 | the interface between C and \CFA code. The syntax for disabling/enabling
|
|---|
| 26 | mangling is:
|
|---|
| 27 | \begin{cfa}
|
|---|
| 28 | // name mangling on by default
|
|---|
| 29 | int i; // _X1ii_1
|
|---|
| 30 | extern "C" { // disables name mangling
|
|---|
| 31 | int j; // j
|
|---|
| 32 | extern "Cforall" { // enables name mangling
|
|---|
| 33 | int k; // _X1ki_1
|
|---|
| 34 | }
|
|---|
| 35 | // revert to no name mangling
|
|---|
| 36 | }
|
|---|
| 37 | // revert to name mangling
|
|---|
| 38 | \end{cfa}
|
|---|
| 39 | Both forms of @extern@ affect all the declarations within their nested lexical
|
|---|
| 40 | scope and transition back to the previous mangling state when the lexical scope
|
|---|
| 41 | ends.
|
|---|
| 42 |
|
|---|
| 43 | \section{Reference Type}
|
|---|
| 44 | \CFA adds a reference type to C as an auto-dereferencing pointer.
|
|---|
| 45 | They work very similarly to pointers.
|
|---|
| 46 | Reference-types are written the same way as a pointer-type is but each
|
|---|
| 47 | asterisk (@*@) is replaced with a ampersand (@&@);
|
|---|
| 48 | this includes cv-qualifiers and multiple levels of reference.
|
|---|
| 49 |
|
|---|
| 50 | They are intended for cases where you would want to use pointers but would
|
|---|
| 51 | be dereferencing them (almost) every usage.
|
|---|
| 52 | In most cases a reference can just be thought of as a pointer that
|
|---|
| 53 | automatically puts a dereference infront of each of its uses (per-level of
|
|---|
| 54 | reference).
|
|---|
| 55 | The address-of operator (@&@) acts as an escape and removes one of the
|
|---|
| 56 | automatic dereference operations.
|
|---|
| 57 | Mutable references may be assigned to by converting them to a pointer
|
|---|
| 58 | with a @&@ and then assigning a pointer too them.
|
|---|
| 59 |
|
|---|
| 60 | \begin{minipage}{0,5\textwidth}
|
|---|
| 61 | With references:
|
|---|
| 62 | \begin{cfa}
|
|---|
| 63 | int i, j;
|
|---|
| 64 | int & ri = i;
|
|---|
| 65 | int && rri = ri;
|
|---|
| 66 | rri = 3;
|
|---|
| 67 | &ri = &j;
|
|---|
| 68 | ri = 5;
|
|---|
| 69 | \end{cfa}
|
|---|
| 70 | \end{minipage}
|
|---|
| 71 | \begin{minipage}{0,5\textwidth}
|
|---|
| 72 | With pointers:
|
|---|
| 73 | \begin{cfa}
|
|---|
| 74 | int i, j;
|
|---|
| 75 | int * pi = &i
|
|---|
| 76 | int ** ppi = π
|
|---|
| 77 | **ppi = 3;
|
|---|
| 78 | pi = &j;
|
|---|
| 79 | *pi = 5;
|
|---|
| 80 | \end{cfa}
|
|---|
| 81 | \end{minipage}
|
|---|
| 82 |
|
|---|
| 83 | \section{Constructors and Destructors}
|
|---|
| 84 |
|
|---|
| 85 | Both constructors and destructors are operators, which means they are
|
|---|
| 86 | functions with special operator names rather than type names in \Cpp. The
|
|---|
| 87 | special operator names may be used to call the functions explicitly (not
|
|---|
| 88 | allowed in \Cpp for constructors).
|
|---|
| 89 |
|
|---|
| 90 | In general, operator names in \CFA are constructed by bracketing an operator
|
|---|
| 91 | token with @?@, which indicates the position of the arguments. For example,
|
|---|
| 92 | infixed multiplication is @?*?@ while prefix dereference is @*?@.
|
|---|
| 93 | This syntax make it easy to tell the difference between prefix operations
|
|---|
| 94 | (such as @++?@) and post-fix operations (@?++@).
|
|---|
| 95 |
|
|---|
| 96 | The special name for a constructor is @?{}@, which comes from the
|
|---|
| 97 | initialization syntax in C. That initialation syntax is also the operator
|
|---|
| 98 | form. \CFA will generate a constructor call each time a variable is declared,
|
|---|
| 99 | passing the initialization arguments to the constructort.
|
|---|
| 100 | \begin{cfa}
|
|---|
| 101 | struct Example { ... };
|
|---|
| 102 | void ?{}(Example & this) { ... }
|
|---|
| 103 | {
|
|---|
| 104 | Example a;
|
|---|
| 105 | Example b = {};
|
|---|
| 106 | }
|
|---|
| 107 | void ?{}(Example & this, char first, int num) { ... }
|
|---|
| 108 | {
|
|---|
| 109 | Example c = {'a', 2};
|
|---|
| 110 | }
|
|---|
| 111 | \end{cfa}
|
|---|
| 112 | Both @a@ and @b@ will be initalized with the first constructor (there is no
|
|---|
| 113 | general way to skip initialation) while @c@ will be initalized with the
|
|---|
| 114 | second.
|
|---|
| 115 |
|
|---|
| 116 | % I don't like the \^{} symbol but $^\wedge$ isn't better.
|
|---|
| 117 | Similarly destructors use the special name @^?{}@ (the @^@ has no special
|
|---|
| 118 | meaning). They can be called explicatly as well but normally they are
|
|---|
| 119 | implicitly called on a variable when it goes out of scope.
|
|---|
| 120 | \begin{cfa}
|
|---|
| 121 | void ^?{}(Example & this) { ... }
|
|---|
| 122 | {
|
|---|
| 123 | Example d;
|
|---|
| 124 | } // <- implicit destructor call
|
|---|
| 125 | \end{cfa}
|
|---|
| 126 | No operator name is restricted in what function signatures they may be bound
|
|---|
| 127 | to although most of the forms cannot be called in operator form. Some
|
|---|
| 128 | ``near-misses" will generate warnings.
|
|---|
| 129 |
|
|---|
| 130 | Whenever a type is defined, \CFA will create a default zero-argument
|
|---|
| 131 | constructor, a copy constructor, a series of argument-per-field constructors
|
|---|
| 132 | and a destructor. All user constructors are defined after this.
|
|---|
| 133 | Because operators are never part of the type definition they may be added
|
|---|
| 134 | at any time, including on built-in types.
|
|---|
| 135 |
|
|---|
| 136 | \section{Polymorphism}
|
|---|
| 137 | \CFA uses parametric polymorphism to create functions and types that are
|
|---|
| 138 | defined over multiple types. \CFA polymorphic declarations serve the same role
|
|---|
| 139 | as \Cpp templates or Java generics. The ``parametric'' means the polymorphism is
|
|---|
| 140 | accomplished by passing argument operations to associate \emph{parameters} at
|
|---|
| 141 | the call site, and these parameters are used in the function to differentiate
|
|---|
| 142 | among the types the function operates on.
|
|---|
| 143 |
|
|---|
| 144 | Polymorphic declarations start with a universal @forall@ clause that goes
|
|---|
| 145 | before the standard (monomorphic) declaration. These declarations have the same
|
|---|
| 146 | syntax except they may use the universal type names introduced by the @forall@
|
|---|
| 147 | clause. For example, the following is a polymorphic identity function that
|
|---|
| 148 | works on any type @T@:
|
|---|
| 149 | \begin{cfa}
|
|---|
| 150 | forall( T ) T identity( T val ) { return val; }
|
|---|
| 151 | int forty_two = identity( 42 );
|
|---|
| 152 | char capital_a = identity( 'A' );
|
|---|
| 153 | \end{cfa}
|
|---|
| 154 | Each use of a polymorphic declaration will resolve its polymorphic parameters
|
|---|
| 155 | (in this case, just @T@) to concrete types (@int@ in the first use and @char@
|
|---|
| 156 | in the second).
|
|---|
| 157 |
|
|---|
| 158 | To allow a polymorphic function to be separately compiled, the type @T@ must be
|
|---|
| 159 | constrained by the operations used on @T@ in the function body. The @forall@
|
|---|
| 160 | clauses is augmented with a list of polymorphic variables (local type names)
|
|---|
| 161 | and assertions (constraints), which represent the required operations on those
|
|---|
| 162 | types used in a function, \eg:
|
|---|
| 163 | \begin{cfa}
|
|---|
| 164 | forall( T | { void do_once(T); })
|
|---|
| 165 | void do_twice(T value) {
|
|---|
| 166 | do_once(value);
|
|---|
| 167 | do_once(value);
|
|---|
| 168 | }
|
|---|
| 169 | \end{cfa}
|
|---|
| 170 |
|
|---|
| 171 | A polymorphic function can be used in the same way as a normal function. The
|
|---|
| 172 | polymorphic variables are filled in with concrete types and the assertions are
|
|---|
| 173 | checked. An assertion is checked by verifying each assertion operation (with
|
|---|
| 174 | all the variables replaced with the concrete types from the arguments) is
|
|---|
| 175 | defined at a call site.
|
|---|
| 176 | \begin{cfa}
|
|---|
| 177 | void do_once(int i) { ... }
|
|---|
| 178 | int i;
|
|---|
| 179 | do_twice(i);
|
|---|
| 180 | \end{cfa}
|
|---|
| 181 | Any object with a type fulfilling the assertion may be passed as an argument to
|
|---|
| 182 | a @do_twice@ call.
|
|---|
| 183 |
|
|---|
| 184 | Note, a function named @do_once@ is not required in the scope of @do_twice@ to
|
|---|
| 185 | compile it, unlike \Cpp template expansion. Furthermore, call-site inferencing
|
|---|
| 186 | allows local replacement of the most specific parametric functions needs for a
|
|---|
| 187 | call.
|
|---|
| 188 | \begin{cfa}
|
|---|
| 189 | void do_once(double y) { ... }
|
|---|
| 190 | int quadruple(int x) {
|
|---|
| 191 | void do_once(int y) { y = y * 2; }
|
|---|
| 192 | do_twice(x);
|
|---|
| 193 | return x;
|
|---|
| 194 | }
|
|---|
| 195 | \end{cfa}
|
|---|
| 196 | Specifically, the complier deduces that @do_twice@'s T is an integer from the
|
|---|
| 197 | argument @x@. It then looks for the most specific definition matching the
|
|---|
| 198 | assertion, which is the nested integral @do_once@ defined within the
|
|---|
| 199 | function. The matched assertion function is then passed as a function pointer
|
|---|
| 200 | to @do_twice@ and called within it.
|
|---|
| 201 | The global definition of @do_once@ is ignored.
|
|---|
| 202 |
|
|---|
| 203 | To avoid typing long lists of assertions, constraints can be collect into
|
|---|
| 204 | convenient packages called a @trait@, which can then be used in an assertion
|
|---|
| 205 | instead of the individual constraints.
|
|---|
| 206 | \begin{cfa}
|
|---|
| 207 | trait done_once(T) {
|
|---|
| 208 | void do_once(T);
|
|---|
| 209 | }
|
|---|
| 210 | \end{cfa}
|
|---|
| 211 | and the @forall@ list in the previous example is replaced with the trait.
|
|---|
| 212 | \begin{cfa}
|
|---|
| 213 | forall(dtype T | done_once(T))
|
|---|
| 214 | \end{cfa}
|
|---|
| 215 | In general, a trait can contain an arbitrary number of assertions, both
|
|---|
| 216 | functions and variables, and are usually used to create a shorthand for, and
|
|---|
| 217 | give descriptive names to, common groupings of assertions describing a certain
|
|---|
| 218 | functionality, like @sumable@, @listable@, \etc.
|
|---|
| 219 |
|
|---|
| 220 | Polymorphic structures and unions are defined by qualifying the aggregate type
|
|---|
| 221 | with @forall@. The type variables work the same except they are used in field
|
|---|
| 222 | declarations instead of parameters, returns, and local variable declarations.
|
|---|
| 223 | \begin{cfa}
|
|---|
| 224 | forall(dtype T)
|
|---|
| 225 | struct node {
|
|---|
| 226 | node(T) * next;
|
|---|
| 227 | T * data;
|
|---|
| 228 | }
|
|---|
| 229 | node(int) inode;
|
|---|
| 230 | \end{cfa}
|
|---|
| 231 | The generic type @node(T)@ is an example of a polymorphic type usage. Like \Cpp
|
|---|
| 232 | template usage, a polymorphic type usage must specify a type parameter.
|
|---|
| 233 |
|
|---|
| 234 | There are many other polymorphism features in \CFA but these are the ones used
|
|---|
| 235 | by the exception system.
|
|---|
| 236 |
|
|---|
| 237 | \section{Control Flow}
|
|---|
| 238 | \CFA has a number of advanced control-flow features: @generator@, @coroutine@, @monitor@, @mutex@ parameters, and @thread@.
|
|---|
| 239 | The two features that interact with
|
|---|
| 240 | the exception system are @coroutine@ and @thread@; they and their supporting
|
|---|
| 241 | constructs are described here.
|
|---|
| 242 |
|
|---|
| 243 | \subsection{Coroutine}
|
|---|
| 244 | A coroutine is a type with associated functions, where the functions are not
|
|---|
| 245 | required to finish execution when control is handed back to the caller. Instead
|
|---|
| 246 | they may suspend execution at any time and be resumed later at the point of
|
|---|
| 247 | last suspension. (Generators are stackless and coroutines are stackful.) These
|
|---|
| 248 | types are not concurrent but share some similarities along with common
|
|---|
| 249 | underpinnings, so they are combined with the \CFA threading library. Further
|
|---|
| 250 | discussion in this section only refers to the coroutine because generators are
|
|---|
| 251 | similar.
|
|---|
| 252 |
|
|---|
| 253 | In \CFA, a coroutine is created using the @coroutine@ keyword, which is an
|
|---|
| 254 | aggregate type like @struct,@ except the structure is implicitly modified by
|
|---|
| 255 | the compiler to satisfy the @is_coroutine@ trait; hence, a coroutine is
|
|---|
| 256 | restricted by the type system to types that provide this special trait. The
|
|---|
| 257 | coroutine structure acts as the interface between callers and the coroutine,
|
|---|
| 258 | and its fields are used to pass information in and out of coroutine interface
|
|---|
| 259 | functions.
|
|---|
| 260 |
|
|---|
| 261 | Here is a simple example where a single field is used to pass (communicate) the
|
|---|
| 262 | next number in a sequence.
|
|---|
| 263 | \begin{cfa}
|
|---|
| 264 | coroutine CountUp {
|
|---|
| 265 | unsigned int next;
|
|---|
| 266 | }
|
|---|
| 267 | CountUp countup;
|
|---|
| 268 | \end{cfa}
|
|---|
| 269 | Each coroutine has a @main@ function, which takes a reference to a coroutine
|
|---|
| 270 | object and returns @void@.
|
|---|
| 271 | \begin{cfa}
|
|---|
| 272 | void main(CountUp & this) {
|
|---|
| 273 | for (unsigned int next = 0 ; true ; ++next) {
|
|---|
| 274 | next = up;
|
|---|
| 275 | suspend;$\label{suspend}$
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 | \end{cfa}
|
|---|
| 279 | In this function, or functions called by this function (helper functions), the
|
|---|
| 280 | @suspend@ statement is used to return execution to the coroutine's caller
|
|---|
| 281 | without terminating the coroutine's function.
|
|---|
| 282 |
|
|---|
| 283 | A coroutine is resumed by calling the @resume@ function, \eg @resume(countup)@.
|
|---|
| 284 | The first resume calls the @main@ function at the top. Thereafter, resume calls
|
|---|
| 285 | continue a coroutine in the last suspended function after the @suspend@
|
|---|
| 286 | statement, in this case @main@ line~\ref{suspend}. The @resume@ function takes
|
|---|
| 287 | a reference to the coroutine structure and returns the same reference. The
|
|---|
| 288 | return value allows easy access to communication variables defined in the
|
|---|
| 289 | coroutine object. For example, the @next@ value for coroutine object @countup@
|
|---|
| 290 | is both generated and collected in the single expression:
|
|---|
| 291 | @resume(countup).next@.
|
|---|
| 292 |
|
|---|
| 293 | \subsection{Monitor and Mutex Parameter}
|
|---|
| 294 | Concurrency does not guarantee ordering; without ordering results are
|
|---|
| 295 | non-deterministic. To claw back ordering, \CFA uses monitors and @mutex@
|
|---|
| 296 | (mutual exclusion) parameters. A monitor is another kind of aggregate, where
|
|---|
| 297 | the compiler implicitly inserts a lock and instances are compatible with
|
|---|
| 298 | @mutex@ parameters.
|
|---|
| 299 |
|
|---|
| 300 | A function that requires deterministic (ordered) execution, acquires mutual
|
|---|
| 301 | exclusion on a monitor object by qualifying an object reference parameter with
|
|---|
| 302 | @mutex@.
|
|---|
| 303 | \begin{cfa}
|
|---|
| 304 | void example(MonitorA & mutex argA, MonitorB & mutex argB);
|
|---|
| 305 | \end{cfa}
|
|---|
| 306 | When the function is called, it implicitly acquires the monitor lock for all of
|
|---|
| 307 | the mutex parameters without deadlock. This semantics means all functions with
|
|---|
| 308 | the same mutex type(s) are part of a critical section for objects of that type
|
|---|
| 309 | and only one runs at a time.
|
|---|
| 310 |
|
|---|
| 311 | \subsection{Thread}
|
|---|
| 312 | Functions, generators, and coroutines are sequential so there is only a single
|
|---|
| 313 | (but potentially sophisticated) execution path in a program. Threads introduce
|
|---|
| 314 | multiple execution paths that continue independently.
|
|---|
| 315 |
|
|---|
| 316 | For threads to work safely with objects requires mutual exclusion using
|
|---|
| 317 | monitors and mutex parameters. For threads to work safely with other threads,
|
|---|
| 318 | also requires mutual exclusion in the form of a communication rendezvous, which
|
|---|
| 319 | also supports internal synchronization as for mutex objects. For exceptions,
|
|---|
| 320 | only two basic thread operations are important: fork and join.
|
|---|
| 321 |
|
|---|
| 322 | Threads are created like coroutines with an associated @main@ function:
|
|---|
| 323 | \begin{cfa}
|
|---|
| 324 | thread StringWorker {
|
|---|
| 325 | const char * input;
|
|---|
| 326 | int result;
|
|---|
| 327 | };
|
|---|
| 328 | void main(StringWorker & this) {
|
|---|
| 329 | const char * localCopy = this.input;
|
|---|
| 330 | // ... do some work, perhaps hashing the string ...
|
|---|
| 331 | this.result = result;
|
|---|
| 332 | }
|
|---|
| 333 | {
|
|---|
| 334 | StringWorker stringworker; // fork thread running in "main"
|
|---|
| 335 | } // <- implicitly join with thread / wait for completion
|
|---|
| 336 | \end{cfa}
|
|---|
| 337 | The thread main is where a new thread starts execution after a fork operation
|
|---|
| 338 | and then the thread continues executing until it is finished. If another thread
|
|---|
| 339 | joins with an executing thread, it waits until the executing main completes
|
|---|
| 340 | execution. In other words, everything a thread does is between a fork and join.
|
|---|
| 341 |
|
|---|
| 342 | From the outside, this behaviour is accomplished through creation and
|
|---|
| 343 | destruction of a thread object. Implicitly, fork happens after a thread
|
|---|
| 344 | object's constructor is run and join happens before the destructor runs. Join
|
|---|
| 345 | can also be specified explicitly using the @join@ function to wait for a
|
|---|
| 346 | thread's completion independently from its deallocation (\ie destructor
|
|---|
| 347 | call). If @join@ is called explicitly, the destructor does not implicitly join.
|
|---|