Skip to content

Commit fe7be98

Browse files
committed
WIP: Change to use Picos for scheduler interop
1 parent 4c16f6b commit fe7be98

File tree

13 files changed

+98
-222
lines changed

13 files changed

+98
-222
lines changed

.ocamlformat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
profile = default
2-
version = 0.26.0
2+
version = 0.26.1
33

44
exp-grouping=preserve

README.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -613,33 +613,6 @@ argument for potentially blocking operations. For example, to perform a blocking
613613
pop with a timeout, one can simply explicitly pass the desired timeout in
614614
seconds:
615615

616-
```ocaml
617-
# let an_empty_stack = stack () in
618-
Xt.commit ~timeoutf:0.1 { tx = pop an_empty_stack }
619-
Exception: Failure "Domain_local_timeout.set_timeoutf not implemented".
620-
```
621-
622-
Oops! What happened above is that the
623-
[_domain local timeout_](https://github.com/ocaml-multicore/domain-local-timeout)
624-
mechanism used by **Kcas** was not implemented on the current domain. The idea
625-
is that, in the future, concurrent schedulers provide the mechanism out of the
626-
box, but there is also a default implementation using the Stdlib `Thread` and
627-
`Unix` modules that works on most platforms. However, to avoid direct
628-
dependencies to `Thread` and `Unix`, we need to explicitly tell the library that
629-
it can use those modules:
630-
631-
```ocaml
632-
# Domain_local_timeout.set_system (module Thread) (module Unix)
633-
- : unit = ()
634-
```
635-
636-
This initialization, if needed, should be done by application code rather than
637-
by libraries.
638-
639-
If we now retry the previous example we will get a
640-
[`Timeout`](https://ocaml-multicore.github.io/kcas/doc/kcas/Kcas/Timeout/index.html#exception-Timeout)
641-
exception as expected:
642-
643616
```ocaml
644617
# let an_empty_stack = stack () in
645618
Xt.commit ~timeoutf:0.1 { tx = pop an_empty_stack }

bench/bench.ml

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,11 @@ module Times = struct
3030
results.(domain_i)
3131
done
3232
in
33-
let prepare_for_await () =
34-
let open struct
35-
type state = Init | Released | Awaiting of { mutable released : bool }
36-
end in
37-
let state = Atomic.make Init in
38-
let release () =
39-
if Multicore_magic.fenceless_get state != Released then
40-
match Atomic.exchange state Released with
41-
| Awaiting r -> r.released <- true
42-
| _ -> ()
43-
in
44-
let await () =
45-
if Multicore_magic.fenceless_get state != Released then
46-
let awaiting = Awaiting { released = false } in
47-
if Atomic.compare_and_set state Init awaiting then
48-
match awaiting with
49-
| Awaiting r ->
50-
(* Avoid sleeping *)
51-
while not r.released do
52-
Domain.cpu_relax ()
53-
done
54-
| _ -> ()
55-
in
56-
Domain_local_await.{ release; await }
33+
let domains =
34+
Array.init n_domains @@ fun domain_i ->
35+
Domain.spawn @@ fun () -> main domain_i
5736
in
58-
Domain_local_await.using ~prepare_for_await ~while_running:(fun () ->
59-
let domains =
60-
Array.init n_domains @@ fun domain_i ->
61-
Domain.spawn @@ fun () -> main domain_i
62-
in
63-
Array.iter Domain.join domains);
37+
Array.iter Domain.join domains;
6438
let n = Stack.length results.(0) in
6539
let times = Array.create_float n in
6640
for run_i = 0 to n - 1 do

bench/dune

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
(package kcas_data)
44
(libraries
55
kcas_data
6-
domain-local-await
76
multicore-magic
87
yojson
98
domain_shims

doc/scheduler-interop.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ implementations that are conveniently provided by
2121
```ocaml
2222
# #thread
2323
# #require "kcas_data"
24+
# #require "picos"
25+
# open Picos
2426
# open Kcas_data
2527
# open Kcas
2628
```
@@ -36,45 +38,29 @@ module Scheduler : sig
3638
val fiber : t -> (unit -> 'a) -> 'a Promise.t
3739
end = struct
3840
open Effect.Deep
39-
type _ Effect.t +=
40-
| Suspend : (('a, unit) continuation -> unit) -> 'a Effect.t
4141
type t = {
4242
queue: (unit -> unit) Queue.t;
4343
domain: unit Domain.t
4444
}
4545
let spawn () =
46-
let queue = Queue.create () in
46+
let queue: (unit -> unit) Queue.t = Queue.create () in
4747
let rec scheduler work =
4848
let effc (type a) : a Effect.t -> _ = function
49-
| Suspend ef -> Some ef
50-
| _ -> None in
49+
| Trigger.Await release ->
50+
Some (fun (k: (a, _) continuation) ->
51+
if not (Trigger.on_signal release () () @@ fun _ () () ->
52+
Queue.add (fun () -> continue k None) queue) then
53+
continue k None)
54+
| _ ->
55+
None in
5156
try_with work () { effc };
5257
match Queue.take_opt queue with
5358
| Some work -> scheduler work
5459
| None -> () in
55-
let prepare_for_await _ =
56-
let state = Atomic.make `Init in
57-
let release () =
58-
if Atomic.get state != `Released then
59-
match Atomic.exchange state `Released with
60-
| `Awaiting k ->
61-
Queue.add (continue k) queue
62-
| _ -> () in
63-
let await () =
64-
if Atomic.get state != `Released then
65-
Effect.perform @@ Suspend (fun k ->
66-
if not (Atomic.compare_and_set state `Init
67-
(`Awaiting k)) then
68-
continue k ())
69-
in
70-
Domain_local_await.{ release; await } in
7160
let domain = Domain.spawn @@ fun () ->
7261
try
7362
while true do
74-
let work = Queue.take_blocking queue in
75-
Domain_local_await.using
76-
~prepare_for_await
77-
~while_running:(fun () -> scheduler work)
63+
scheduler (Queue.take_blocking queue)
7864
done
7965
with Exit -> () in
8066
{ queue; domain }

dune-project

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
(depends
1414
(ocaml (>= 4.13.0))
1515
(backoff (>= 0.1.0))
16-
(domain-local-await (>= 1.0.0))
17-
(domain-local-timeout (>= 1.0.0))
16+
picos
1817
(multicore-magic (>= 2.0.0))
1918
(domain_shims (and (>= 0.1.0) :with-test))
2019
(alcotest (and (>= 1.7.0) :with-test))
@@ -26,7 +25,7 @@
2625
(depends
2726
(kcas (= :version))
2827
(multicore-magic (>= 2.0.0))
29-
(domain-local-await (and (>= 1.0.0) :with-test))
28+
(picos :with-test)
3029
(domain_shims (and (>= 0.1.0) :with-test))
3130
(mtime (and (>= 2.0.0) :with-test))
3231
(alcotest (and (>= 1.7.0) :with-test))

kcas.opam

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ depends: [
1616
"dune" {>= "3.8"}
1717
"ocaml" {>= "4.13.0"}
1818
"backoff" {>= "0.1.0"}
19-
"domain-local-await" {>= "1.0.0"}
20-
"domain-local-timeout" {>= "1.0.0"}
19+
"picos"
2120
"multicore-magic" {>= "2.0.0"}
2221
"domain_shims" {>= "0.1.0" & with-test}
2322
"alcotest" {>= "1.7.0" & with-test}
@@ -40,3 +39,6 @@ build: [
4039
]
4140
dev-repo: "git+https://github.com/ocaml-multicore/kcas.git"
4241
doc: "https://ocaml-multicore.github.io/kcas/doc/kcas/Kcas/"
42+
pin-depends: [
43+
[ "picos.dev" "git+https://github.com/ocaml-multicore/picos#c74125ad631e8e8a2e115ffeca3210478d656e22" ]
44+
]

kcas.opam.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
doc: "https://ocaml-multicore.github.io/kcas/doc/kcas/Kcas/"
2+
pin-depends: [
3+
[ "picos.dev" "git+https://github.com/ocaml-multicore/picos#f8cf35c48a9894a0f21347f1036e242c791f846f" ]
4+
]

kcas_data.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ depends: [
1616
"dune" {>= "3.8"}
1717
"kcas" {= version}
1818
"multicore-magic" {>= "2.0.0"}
19-
"domain-local-await" {>= "1.0.0" & with-test}
19+
"picos" {with-test}
2020
"domain_shims" {>= "0.1.0" & with-test}
2121
"mtime" {>= "2.0.0" & with-test}
2222
"alcotest" {>= "1.7.0" & with-test}

src/kcas/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(library
22
(name kcas)
33
(public_name kcas)
4-
(libraries domain-local-await domain-local-timeout backoff multicore-magic))
4+
(libraries picos backoff multicore-magic))
55

66
(mdx
77
(package kcas)

0 commit comments

Comments
 (0)