Skip to content

Commit

Permalink
Merge branch 'riot-ml:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
emilpriver authored Jul 12, 2024
2 parents 4295453 + d05df0e commit 79cf4c6
Show file tree
Hide file tree
Showing 31 changed files with 154 additions and 63 deletions.
2 changes: 1 addition & 1 deletion bench/spawn_many.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Test_app = struct

let loop count =
match receive_any () with
| Loop_stop -> Log.debug (fun f -> f "dead at %d%!" count)
| Loop_stop -> Riot_runtime.Log.debug (fun f -> f "dead at %d%!" count)

let main t0 () =
Logger.info (fun f -> f "boot test app");
Expand Down
2 changes: 1 addition & 1 deletion riot/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(name riot)
(public_name riot)
(libraries lib runtime))
(libraries lib riot_runtime))
2 changes: 1 addition & 1 deletion riot/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(libraries
global
logger
runtime
riot_runtime
tls
bytestring
telemetry
Expand Down
35 changes: 30 additions & 5 deletions riot/lib/gen_server.ml
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
open Global

type 'res req = ..
type cast_req = ..
type cont_req = ..

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

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

type ('res, 'state) call_result =
| Reply of ('res * 'state)
| Reply_continue of ('res * 'state * cont_req)

type 'state cast_result = No_reply of 'state

module type Impl = sig
type args
type state

val init : args -> state init_result
val handle_call : 'res. 'res req -> Pid.t -> state -> 'res * state

val handle_call :
'res. 'res req -> Pid.t -> state -> ('res, state) call_result

val handle_cast : cast_req -> state -> state cast_result
val handle_continue : cont_req -> state -> state
val handle_info : Message.t -> state -> unit
end

Expand All @@ -35,14 +49,23 @@ let call : type res. Pid.t -> res req -> res =
in
receive ~selector ()

let cast pid req = send pid (Cast req)

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_any () with
| Call (pid, ref, req) ->
let res, state = I.handle_call req pid state in
send pid (Reply (ref, res));
loop impl state
| Call (pid, ref, req) -> (
match I.handle_call req pid state with
| Reply (res, state) ->
send pid (Reply (ref, res));
loop impl state
| Reply_continue (res, state, cont_req) ->
send pid (Reply (ref, res));
let state = I.handle_continue cont_req state in
loop impl state)
| Cast req -> (
match I.handle_cast req state with No_reply state -> loop impl state)
| msg ->
let _res = I.handle_info msg state in
loop impl state
Expand All @@ -65,5 +88,7 @@ let start_link :
module Default = struct
let init _args = Ignore
let handle_call _req _from _state = failwith "unimplemented"
let handle_cast _req _state = failwith "unimplemented"
let handle_continue _req _state = failwith "unimplemented"
let handle_info _msg _state = failwith "unimplemented"
end
2 changes: 1 addition & 1 deletion riot/lib/global/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(package riot)
(name global)
(libraries runtime))
(libraries riot_runtime))
6 changes: 3 additions & 3 deletions riot/lib/global/global.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include Runtime.Import
include Riot_runtime.Import

(* TODO(@leostera): move these into the Runtime module below *)
include Runtime.Core.Process.Exn
include Runtime.Core.Proc_registry.Exn
include Riot_runtime.Core.Process.Exn
include Riot_runtime.Core.Proc_registry.Exn

let ( let* ) = Result.bind
2 changes: 1 addition & 1 deletion riot/lib/hashmap.ml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include Runtime.Util.Dashmap
include Riot_runtime.Util.Dashmap
10 changes: 7 additions & 3 deletions riot/lib/key_value_store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ module MakeServer (B : Base) = struct
let init () = Gen_server.Ok { tbl = Hashtbl.create 0 }

let handle_call :
type res. res Gen_server.req -> Pid.t -> state -> res * state =
type res.
res Gen_server.req ->
Pid.t ->
state ->
(res, state) Gen_server.call_result =
fun req _from state ->
match req with
| Get k -> (Hashtbl.find_opt state.tbl k, state)
| Put (k, v) -> (Hashtbl.replace state.tbl k v, state)
| Get k -> Gen_server.Reply (Hashtbl.find_opt state.tbl k, state)
| Put (k, v) -> Gen_server.Reply (Hashtbl.replace state.tbl k v, state)
| _ -> failwith "invalid call"
end

Expand Down
2 changes: 1 addition & 1 deletion riot/lib/logger/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(package riot)
(name logger)
(libraries global runtime))
(libraries global riot_runtime))
8 changes: 4 additions & 4 deletions riot/lib/logger/logger.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Scheduler_uid = Runtime.Core.Scheduler_uid
module Log = Runtime.Log
module Scheduler_uid = Riot_runtime.Core.Scheduler_uid
module Log = Riot_runtime.Log
open Global

type opts = { print_source : bool; print_time : bool; color_output : bool }
Expand Down Expand Up @@ -43,7 +43,7 @@ end
type log = {
level : level;
ts : Ptime.t;
src : Scheduler_uid.t * Core.Pid.t;
src : Scheduler_uid.t * Riot_runtime.Core.Pid.t;
ns : namespace;
message : string;
}
Expand All @@ -55,7 +55,7 @@ let on_log log = !__on_log__ log
let write : type a. level -> namespace -> (a, unit) logger_format -> unit =
fun level ns msgf ->
let ts = Ptime_clock.now () in
let sch = Scheduler.get_current_scheduler () in
let sch = Riot_runtime.Scheduler.get_current_scheduler () in
let pid = self () in
let src = (sch.uid, pid) in
let buf = Buffer.create 128 in
Expand Down
2 changes: 1 addition & 1 deletion riot/lib/message.ml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include Runtime.Core.Message
include Riot_runtime.Core.Message
2 changes: 1 addition & 1 deletion riot/lib/pid.ml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include Runtime.Core.Pid
include Riot_runtime.Core.Pid
4 changes: 2 additions & 2 deletions riot/lib/process.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
open Runtime.Import
module P = Runtime.Core.Process
open Riot_runtime.Import
module P = Riot_runtime.Core.Process

open Logger.Make (struct
let namespace = [ "riot"; "process" ]
Expand Down
2 changes: 1 addition & 1 deletion riot/lib/queue.ml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include Runtime.Util.Lf_queue
include Riot_runtime.Util.Lf_queue
2 changes: 1 addition & 1 deletion riot/lib/ref.ml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include Runtime.Core.Ref
include Riot_runtime.Core.Ref
10 changes: 5 additions & 5 deletions riot/lib/runtime_lib.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Global

let set_log_level = Runtime.Log.set_log_level
let set_log_level = Riot_runtime.Log.set_log_level

let syscalls () =
let pool = _get_pool () in
Expand All @@ -26,11 +26,11 @@ module Stats = struct
let total_schedulers = pool.schedulers |> List.length in
let breakdown =
pool.schedulers
|> List.map (fun (sch : Scheduler.t) ->
|> List.map (fun (sch : Riot_runtime.Scheduler.t) ->
Format.asprintf " sch #%a [live_procs=%d; timers=%d]"
Runtime.Core.Scheduler_uid.pp sch.uid
(Runtime.Core.Proc_queue.size sch.run_queue)
(Runtime.Time.Timer_wheel.size sch.timers))
Riot_runtime.Core.Scheduler_uid.pp sch.uid
(Riot_runtime.Core.Proc_queue.size sch.run_queue)
(Riot_runtime.Time.Timer_wheel.size sch.timers))
|> String.concat "\n"
in
info (fun f ->
Expand Down
2 changes: 1 addition & 1 deletion riot/lib/telemetry_app.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type event = Telemetry.event = ..
let name = "Riot.Telemetry"

module Dispatcher = struct
type Core.Message.t += Event of Telemetry.event
type Riot_runtime.Core.Message.t += Event of Telemetry.event

let __main_dispatcher__ : Pid.t ref = ref Pid.zero

Expand Down
10 changes: 10 additions & 0 deletions riot/riot.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
include Lib

open struct
open Riot_runtime
module Log = Log
module Core = Core
module Import = Import
module Util = Util
module Scheduler = Scheduler
module Time = Time
end

open Logger.Make (struct
let namespace = [ "riot" ]
end)
Expand Down
23 changes: 21 additions & 2 deletions riot/riot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module Process : sig
[None] if no process was registered for that name.
*)

val sid : t -> Core.Scheduler_uid.t
val sid : t -> Riot_runtime.Core.Scheduler_uid.t
(** [sid t] returns the scheduler id for the scheduler in charge of the
process. *)

Expand Down Expand Up @@ -352,6 +352,9 @@ module Gen_server : sig
]}
*)

type cast_req = ..
type cont_req = ..

(** [state init_result] is used to initialize a new generic server. *)
type 'state init_result =
| Ok of 'state
Expand All @@ -360,6 +363,12 @@ module Gen_server : sig
(** use this value to crash the process and notify a supervisor of it *)
| Ignore (** use this value to exit the process normally *)

type ('res, 'state) call_result =
| Reply of ('res * 'state)
| Reply_continue of ('res * 'state * cont_req)

type 'state cast_result = No_reply of 'state

(** [Impl] is the module type of the generic server base implementations. You
can use this type when defining new gen servers like this:
Expand All @@ -380,7 +389,12 @@ module Gen_server : sig
type state

val init : args -> state init_result
val handle_call : 'res. 'res req -> Pid.t -> state -> 'res * state

val handle_call :
'res. 'res req -> Pid.t -> state -> ('res, state) call_result

val handle_cast : cast_req -> state -> state cast_result
val handle_continue : cont_req -> state -> state
val handle_info : Message.t -> state -> unit
end

Expand All @@ -396,6 +410,11 @@ module Gen_server : sig
TODO(leostera): add ?timeout param
*)

val cast : Pid.t -> cast_req -> unit
(** [cast pid req] will send a type-safe request [req] to the generic server behind [pid]
and does not wait for a response.
*)

val start_link :
('args, 'state) impl -> 'args -> (Pid.t, [> `Exn of exn ]) result
(** [start_link (module S) args] will spawn and link a new process that will
Expand Down
13 changes: 13 additions & 0 deletions riot/runtime/core/core.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Core = Core
module Mailbox = Mailbox
module Message = Message
module Pid = Pid
module Proc_effect = Proc_effect
module Proc_queue = Proc_queue
module Proc_registry = Proc_registry
module Proc_set = Proc_set
module Proc_state = Proc_state
module Proc_table = Proc_table
module Process = Process
module Ref = Ref
module Scheduler_uid = Scheduler_uid
4 changes: 0 additions & 4 deletions riot/runtime/core/dune

This file was deleted.

14 changes: 12 additions & 2 deletions riot/runtime/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
(library
(package riot)
(name runtime)
(libraries core log scheduler time util rio))
(name riot_runtime)
(libraries
gluon
mtime
mtime.clock.os
ptime
ptime.clock.os
rio
runtime_events
unix))

(include_subdirs unqualified)
4 changes: 0 additions & 4 deletions riot/runtime/log/dune

This file was deleted.

1 change: 1 addition & 0 deletions riot/runtime/runtime.ml → riot/runtime/riot_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Log = Log
module Core = Core
module Import = Import
module Util = Util
module Scheduler = Scheduler
module Time = Time

let set_log_level = Log.set_log_level
4 changes: 0 additions & 4 deletions riot/runtime/scheduler/dune

This file was deleted.

4 changes: 0 additions & 4 deletions riot/runtime/time/dune

This file was deleted.

1 change: 1 addition & 0 deletions riot/runtime/time/time.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Timer_wheel = Timer_wheel
4 changes: 0 additions & 4 deletions riot/runtime/util/dune

This file was deleted.

9 changes: 9 additions & 0 deletions riot/runtime/util/util.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Dashmap = Dashmap
module Lf_queue = Lf_queue
module Min_heap = Min_heap
module Thread_local = Thread_local
module Timeout = Timeout
module Trace = Trace
module Uid = Uid
module Util = Util
module Weak_ref = Weak_ref
Loading

0 comments on commit 79cf4c6

Please sign in to comment.