Skip to content

Commit

Permalink
fixed documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
evanmwilliams committed Dec 11, 2023
1 parent 82f822b commit 45762ca
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bril-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,19 @@ pub enum ValueOps {
/// <https://capra.cs.cornell.edu/bril/lang/memory.html#operations>
#[cfg(feature = "memory")]
PtrAdd,
/// <<https://capra.cs.cornell.edu/bril/lang/async.html>>
#[cfg(feature = "async")]
Resolve,
/// <<https://capra.cs.cornell.edu/bril/lang/async.html>>
#[cfg(feature = "async")]
CompareAndSwap,
/// <<https://capra.cs.cornell.edu/bril/lang/async.html>>
#[cfg(feature = "async")]
LoadAtomic,
/// <<https://capra.cs.cornell.edu/bril/lang/async.html>>
#[cfg(feature = "async")]
SwapAtomic,
/// <<https://capra.cs.cornell.edu/bril/lang/async.md>>
#[cfg(feature = "async")]
NewAtomic,
}
Expand Down
81 changes: 81 additions & 0 deletions docs/lang/async.bril
Original file line number Diff line number Diff line change
@@ -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<T>`: allows you to define a *promise* with return type `T`. Upon
encountering a function defined with a `Promise<T>` 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<T>`
- `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<int> = call @mod size;
prom2: promise<bool> = call @mod2 size2;
x : int = resolve prom;
y : bool = resolve prom2;
print x;
print y;
}

@mod(r: int): promise<int> {
ret r;
}

@mod2(r: bool): promise<bool> {
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).

0 comments on commit 45762ca

Please sign in to comment.