Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Jul 23, 2024
1 parent 4855dc2 commit a8587a4
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,112 @@ for further information.

#### Understanding Cancelation

#### Concepts

<!--
``` ocaml
open Picos
```
-->

#### Trigger

- first order

```ocaml
module type Trigger = sig
type t
val create : unit -> t (* initial *)
val signal : t -> unit (* signaled *)
val await : t -> Exn_bt.t option (* awaiting *)
end
```

```ocaml
module type Ivar = sig
type 'a t
val create : unit -> 'a t
val try_fill : 'a t -> 'a -> bool
val read : 'a t -> 'a
end
```

```ocaml
module Ivar = struct
type 'a state =
| Filled of 'a
| Empty of Trigger.t list
type 'a t = 'a state Atomic.t
let create () = Atomic.make (Empty [])
let rec try_fill t value =
match Atomic.get t with
| Filled _ -> false
| Empty triggers as before ->
let after = Filled value in
if Atomic.compare_and_set t before after then begin
List.iter Trigger.signal triggers;
true
end
else
try_fill t value
let rec cleanup t trigger =
match Atomic.get t with
| Filled _ -> ()
| Empty triggers as before ->
let triggers = List.filter ((!=) trigger) triggers in
let after = Empty triggers in
if not (Atomic.compare_and_set t before after) then
cleanup t trigger
let rec read t =
match Atomic.get t with
| Filled value -> value
| Empty triggers as before ->
let trigger = Trigger.create () in
let after = Empty (trigger :: triggers) in
if Atomic.compare_and_set t before after then
match Trigger.await trigger with
| None -> read t
| Some exn_bt ->
cleanup t trigger;
Exn_bt.raise exn_bt
else
read t
end
```

#### Computation

```ocaml
module type Computation = sig
type 'a t
val create : unit -> 'a t (* running *)
val try_return : 'a t -> 'a -> bool (* completed *)
val try_cancel : 'a t -> Exn_bt.t -> bool (* completed *)
val try_attach : 'a t -> Trigger.t -> bool
val detach : 'a t -> Trigger.t -> unit
end
```

- notification of status change

- cleanup

```ocaml
module Ivar : Ivar = struct
type 'a t = 'a Computation.t
let create () = Computation.create ()
let try_fill = Computation.try_return
let read = Computation.await
end
```

#### Fiber

### Elements of Concurrency
Expand Down

0 comments on commit a8587a4

Please sign in to comment.