diff --git a/docs/lang/AsyncBril.md b/docs/lang/AsyncBril.md new file mode 100644 index 000000000..b50575abf --- /dev/null +++ b/docs/lang/AsyncBril.md @@ -0,0 +1,81 @@ +Asynchronous Bril +======================== + +This extension gives Bril users access to +[promises](https://en.wikipedia.org/wiki/Futures_and_promises) and +[atomics](https://wiki.osdev.org/Atomic_operation). Read more about the +implementation in our blog. + +Install +------- + +The extension comes with the [Rust library](https://capra.cs.cornell.edu/bril/tools/rust.html) +for Bril. Feel free to walk through the setup and installation for Rust mentioned +in the documentation. + +Syntax +------- + +We have added a few new pices of syntax to the language: +- `Promise`: allows you to define a *promise* with return type `T`. Upon +encountering a function defined with a `Promise` return type, a thread is +immediately started. +- `Resolve`: allows you to resolve a promise. Will return a variable of type `T` +if the promise being resolved is of type `Promise` +- `atomicint`: allows you to create variables of type `atomicint`, which, +as the name suggests, represents an atomic integer. + +Examples +------- + +Here is an example of promises in Bril: +``` +@main { + size: int = const 4; + size2: bool = const false; + prom: promise = call @mod size; + prom2: promise = call @mod2 size2; + x : int = resolve prom; + y : bool = resolve prom2; + print x; + print y; +} + +@mod(r: int): promise { + ret r; +} + +@mod2(r: bool): promise { + ret r; +} +``` + +Note that the right-hand side of a promise type is a function call. Upon calling +`resolve`, we are able to assign the output of the function call to a normal +Bril variable (in this case we support `int` and `bool`). + +Here is an example of atomics in Bril: +``` +@main { + one : int = const 1; + size : atomicint = newatomic one; + expect : int = const 1; + upd : int = const 2; + res : int = cas size expect upd; + + three : int = const 3; + res1 : int = loadatomic size; + res2 : int = swapatomic size three; + res3 : int = loadatomic size; + print res; + print res1; + print res2; + print res3; +} +``` + +Note that to create an `atomicint`, we introduce a constructor `newatomic`. +The constructor takes in a normal Bril integer and uses it to produce the new +atomic variable. Further, the functions `loadatomic` and `swapatomic` can be +used to manipulate atomic variables, in addition to the function `cas` (which +stands for Compare-and-Swap). \ No newline at end of file