Skip to content

Commit cf4eaab

Browse files
Merge pull request #3 from oxcaml/idiomatic
Update setup and slides
2 parents 08ae23f + 6856c29 commit cf4eaab

File tree

4 files changed

+8413
-4002
lines changed

4 files changed

+8413
-4002
lines changed

handson_activity/act02_gensym_atomics/gensym_atomics.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let gensym =
1111

1212
(* Here's an example of how to use fork_join2 *)
1313
let fork_join_demo par =
14-
let (l,r) =
14+
let #(l,r) =
1515
Parallel.fork_join2 par
1616
(fun _ -> "left")
1717
(fun _ -> "right")
@@ -33,8 +33,7 @@ let gensym_pair par =
3333
let run_parallel ~f =
3434
let module Scheduler = Parallel_scheduler_work_stealing in
3535
let scheduler = Scheduler.create () in
36-
let monitor = Parallel.Monitor.create_root () in
37-
let result = Scheduler.schedule scheduler ~monitor ~f in
36+
let result = Scheduler.parallel scheduler ~f in
3837
Scheduler.stop scheduler;
3938
result
4039

handson_activity/act03_gensym_capsules/gensym_capsules.ml

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22
stick with the non-atomic references in `gensym`. Use capsules to ensure that
33
gensym is safe for parallel access (and the code will compile). *)
44

5-
open Basement
5+
open Await
66

77
(* Here is an example snippet to show how to create and use capsules. *)
88
let safe_ref (init : int) =
9-
(* Create capsule and extract key to bind the type variable *)
10-
let (P key) = Capsule.create () in
9+
(* Create a capsule guarded by a mutex and extract the latter *)
10+
let (P mutex) = Capsule.Mutex.create () in
1111
(* Create encapsulated data bound to the same key type *)
1212
let r = Capsule.Data.create (fun () -> ref init) in
13-
(* Create mutex from key *)
14-
let mutex = Capsule.Mutex.create key in
1513

1614
(* Access with lock *)
17-
let read () =
18-
Capsule.Mutex.with_lock mutex ~f:(fun password ->
19-
Capsule.Data.extract r ~password ~f:(fun (r : int ref) -> !r))
15+
let read (w : Await.t) =
16+
Capsule.Mutex.with_lock w mutex ~f:(fun access ->
17+
let r = Capsule.Data.unwrap r ~access in !r)
2018
in
21-
let write (v : int) =
22-
Capsule.Mutex.with_lock mutex ~f:(fun password ->
23-
Capsule.Data.extract r ~password ~f:(fun r -> r := v))
19+
let write (w : Await.t) v =
20+
Capsule.Mutex.with_lock w mutex ~f:(fun access ->
21+
let r = Capsule.Data.unwrap r ~access in r := v)
2422
in
2523
(read, write)
2624

@@ -33,21 +31,22 @@ let gensym =
3331
let parallel_read_write par =
3432
let r = safe_ref 0 in
3533
let read, write = r in
36-
Parallel.fork_join2 par
37-
(fun _ ->
38-
for _ = 1 to 1000 do
39-
ignore (read ())
40-
done)
41-
(fun _ ->
42-
for i = 1 to 1000 do
43-
write i
44-
done)
45-
|> ignore
34+
let #((), ()) =
35+
Parallel.fork_join2 par
36+
(fun _ -> Await_blocking.with_await Terminator.never ~f:(fun w ->
37+
for _ = 1 to 1000 do
38+
ignore (read w)
39+
done))
40+
(fun _ -> Await_blocking.with_await Terminator.never ~f:(fun w ->
41+
for i = 1 to 1000 do
42+
write w i
43+
done))
44+
in ()
4645

4746
(* Test that gensym produces distinct symbols in parallel *)
4847

4948
let gensym_pair par =
50-
let s1, s2 =
49+
let #(s1, s2) =
5150
Parallel.fork_join2 par (fun _ -> gensym ()) (fun _ -> gensym ())
5251
in
5352
assert (s1 <> s2)
@@ -56,8 +55,7 @@ let gensym_pair par =
5655
let run_parallel ~f =
5756
let module Scheduler = Parallel_scheduler_work_stealing in
5857
let scheduler = Scheduler.create () in
59-
let monitor = Parallel.Monitor.create_root () in
60-
let result = Scheduler.schedule scheduler ~monitor ~f in
58+
let result = Scheduler.parallel scheduler ~f in
6159
Scheduler.stop scheduler;
6260
result
6361

slides/index.html

Lines changed: 8379 additions & 3959 deletions
Large diffs are not rendered by default.

slides/index.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -834,27 +834,21 @@ Associate mutable state with locks, ensuring exclusive access. Capsules use the
834834

835835
{pause #capsules}
836836

837-
⚠️ **A simpler interface is coming, the following may hurt your eyes** ⚠️
838-
839-
{pause}
840-
841837
```ocaml
842-
let gensym =
843-
(* 1. Create capsule and get key *)
844-
let (P key) = Capsule.create () in
838+
let gensym =
839+
(* Create a capsule guarded by a mutex and unpack to get the brand. *)
840+
let (P mutex) = Capsule.Mutex.create () in
845841
846-
(* 2. Create encapsulated data *)
842+
(* Create encapsulated data bound to the same key brand. *)
847843
let counter = Capsule.Data.create (fun () -> ref 0) in
848844
849-
let mutex = Capsule.Mutex.create key in (* 3. Create mutex from key *)
850-
851-
(* 4. Access with lock *)
852-
let fetch_and_incr () =
853-
Capsule.Mutex.with_lock mutex ~f:(fun password ->
854-
Capsule.Data.extract counter ~password ~f:(fun c ->
855-
c := !c + 1; !c))
845+
(* Access the data, requiring a capability to wait/block. *)
846+
let fetch_and_incr (w : Await.t) =
847+
Capsule.Mutex.with_lock w mutex ~f:(fun access ->
848+
let c = Capsule.Data.unwrap ~access counter in
849+
c := !c + 1; !c)
856850
in
857-
fun () -> "gsym_" ^ (Int.to_string (fetch_and_incr ()))
851+
fun w -> "gsym_" ^ Int.to_string (fetch_and_incr w)
858852
```
859853

860854
{pause up}

0 commit comments

Comments
 (0)