package riot

  1. Overview
  2. Docs
An actor-model multi-core scheduler for OCaml 5

Install

Dune Dependency

Authors

Maintainers

Sources

riot-0.0.3.tbz
sha256=6201ce27997ec1c4b4509782c6be2fa2bf102b804b11dcbf9ebdb49a123c19c3
sha512=ad70a67601a892700e461efe57484d109b1d08e30d15464ad8611e71dd568c934d3f948afd645e096e4f97ad1935aaeaf5d9b6d9d59c52a82eeb5c4995421646

doc/src/riot.lib/gen_server.ml.html

Source file gen_server.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
open Runtime

type 'res req = ..

type Message.t +=
  | Call : Pid.t * 'res Ref.t * 'res req -> Message.t
  | Reply : 'res Ref.t * 'res -> Message.t

type 'state init_result = Ok of 'state | Error | Ignore

module type Impl = sig
  type args
  type state

  val init : args -> state init_result
  val handle_call : 'res. 'res req -> Pid.t -> state -> 'res
  val handle_info : Message.t -> state -> unit
end

type ('args, 'state) impl =
  (module Impl with type args = 'args and type state = 'state)

let call : type res. Pid.t -> res req -> res =
 fun pid req ->
  let ref = Ref.make () in
  send pid (Call (self (), ref, req));
  match receive () with
  | Reply (ref', res) -> (
      match Ref.type_equal ref ref' with
      | Some Type.Equal -> res
      | None -> failwith "bad message")
  | _ -> failwith "unexpected message"

let rec loop : type args state. (args, state) impl -> state -> unit =
 fun impl state ->
  let (module I : Impl with type args = args and type state = state) = impl in
  match receive () with
  | Call (pid, ref, req) ->
      let res = I.handle_call req pid state in
      send pid (Reply (ref, res));
      loop impl state
  | msg ->
      let _res = I.handle_info msg state in
      loop impl state

let start_link :
    type args state. (args, state) impl -> args -> (Pid.t, exn) result =
 fun impl args ->
  let pid =
    spawn_link (fun () ->
        let (module I : Impl with type args = args and type state = state) =
          impl
        in
        match I.init args with
        | Ok state -> loop impl state
        | Error | Ignore -> ())
  in
  Ok pid
OCaml

Innovation. Community. Security.