Skip to content

Commit

Permalink
Rename process types
Browse files Browse the repository at this point in the history
`Process.t` is now a process. The process manager is now `Process.mgr`.
  • Loading branch information
talex5 committed Aug 24, 2023
1 parent 316e43d commit 8b14ca3
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 62 deletions.
27 changes: 14 additions & 13 deletions lib_eio/process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ let () =
| _ -> false
)

type 'tag process_ty = [ `Pid | `Await | `Signal | `Platform of 'tag ]
type 'a t = ([> [> `Generic] process_ty] as 'a) r
type 'tag ty = [ `Process | `Platform of 'tag ]

type 'tag ty = [ `Mgr | `Platform of 'tag ]
type 'a t = ([> [> `Generic] ty] as 'a) r

type 'tag mgr_ty = [ `Mgr | `Platform of 'tag ]

type 'a mgr = 'a r
constraint 'a = [> [> `Generic] ty]
constraint 'a = [> [> `Generic] mgr_ty]

module Pi = struct
module type PROCESS = sig
Expand All @@ -51,7 +52,7 @@ module Pi = struct
end

type (_, _, _) Resource.pi +=
| Process : ('t, (module PROCESS with type t = 't and type tag = 'tag), [> 'tag process_ty]) Resource.pi
| Process : ('t, (module PROCESS with type t = 't and type tag = 'tag), [> 'tag ty]) Resource.pi

let process (type t tag) (module X : PROCESS with type t = t and type tag = tag) =
Resource.handler [
Expand All @@ -77,11 +78,11 @@ module Pi = struct
?env:string array ->
?executable:string ->
string list ->
tag process_ty r
tag ty r
end

type (_, _, _) Resource.pi +=
| Mgr : ('t, (module MGR with type t = 't and type tag = 'tag), [> 'tag ty]) Resource.pi
| Mgr : ('t, (module MGR with type t = 't and type tag = 'tag), [> 'tag mgr_ty]) Resource.pi

let mgr (type t tag) (module X : MGR with type t = t and type tag = tag) =
Resource.handler [
Expand All @@ -101,7 +102,7 @@ let pp_arg f x =

let pp_args = Fmt.hbox (Fmt.list ~sep:Fmt.sp pp_arg)

let await (type tag) ((Resource.T (v, ops)) : [> tag process_ty] r) =
let await (type tag) ((Resource.T (v, ops)) : [> tag ty] r) =
let module X = (val (Resource.get ops Pi.Process)) in
X.await v

Expand All @@ -110,17 +111,17 @@ let await_exn ?(is_success = Int.equal 0) proc =
| `Exited code when is_success code -> ()
| status -> raise (err (Child_error status))

let pid (type tag) (t : [> tag process_ty] r) =
let pid (type tag) (t : [> tag ty] r) =
let (Resource.T (v, ops)) = t in
let module X = (val (Resource.get ops Pi.Process)) in
X.pid v

let signal (type tag) (t : [> tag process_ty] r) s =
let signal (type tag) (t : [> tag ty] r) s =
let (Resource.T (v, ops)) = t in
let module X = (val (Resource.get ops Pi.Process)) in
X.signal v s

let spawn (type tag) ~sw (t : [> tag ty] r) ?cwd ?stdin ?stdout ?stderr ?env ?executable args : tag process_ty r =
let spawn (type tag) ~sw (t : [> tag mgr_ty] r) ?cwd ?stdin ?stdout ?stderr ?env ?executable args : tag ty r =
let (Resource.T (v, ops)) = t in
let module X = (val (Resource.get ops Pi.Mgr)) in
X.spawn v ~sw
Expand All @@ -140,11 +141,11 @@ let run t ?cwd ?stdin ?stdout ?stderr ?(is_success = Int.equal 0) ?env ?executab
let ex = err (Child_error status) in
raise (Exn.add_context ex "running command: %a" pp_args args)

let pipe (type tag) ~sw ((Resource.T (v, ops)) : [> tag ty] r) =
let pipe (type tag) ~sw ((Resource.T (v, ops)) : [> tag mgr_ty] r) =
let module X = (val (Resource.get ops Pi.Mgr)) in
X.pipe v ~sw

let parse_out (type tag) (t : [> tag ty] r) parse ?cwd ?stdin ?stderr ?is_success ?env ?executable args =
let parse_out (type tag) (t : [> tag mgr_ty] r) parse ?cwd ?stdin ?stderr ?is_success ?env ?executable args =
Switch.run @@ fun sw ->
let r, w = pipe t ~sw in
try
Expand Down
37 changes: 19 additions & 18 deletions lib_eio/process.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,33 @@ val pp_args : string list Fmt.t

(** {2 Types} *)

type 'tag ty = [ `Process | `Platform of 'tag ]

type 'a t = ([> [> `Generic] ty] as 'a) r
(** A process. *)
type 'tag process_ty = [ `Pid | `Await | `Signal | `Platform of 'tag ]
type 'a t = ([> [> `Generic] process_ty] as 'a) r

type 'tag ty = [ `Mgr | `Platform of 'tag ]
type 'tag mgr_ty = [ `Mgr | `Platform of 'tag ]

type 'a mgr = 'a r
constraint 'a = [> [> `Generic] ty]
constraint 'a = [> [> `Generic] mgr_ty]
(** A process manager capable of spawning new processes. *)

(** {2 Processes} *)

val pid : [> 'tag process_ty ] r -> int
val pid : [> 'tag ty ] r -> int
(** [pid t] is the process ID of [t]. *)

val await : [> 'tag process_ty ] r -> exit_status
val await : [> 'tag ty ] r -> exit_status
(** [await t] waits for process [t] to exit and then reports the status. *)

val await_exn : ?is_success:(int -> bool) -> [> 'tag process_ty ] r -> unit
val await_exn : ?is_success:(int -> bool) -> [> 'tag ty ] r -> unit
(** Like {! await} except an exception is raised if does not return a successful
exit status.
@param is_success Used to determine if an exit code is successful.
Default is [Int.equal 0]. *)

val signal : [> 'tag process_ty ] r -> int -> unit
val signal : [> 'tag ty ] r -> int -> unit
(** [signal t i] sends the signal [i] to process [t].
If the process has already exited then this does nothing
Expand All @@ -73,14 +74,14 @@ val signal : [> 'tag process_ty ] r -> int -> unit

val spawn :
sw:Switch.t ->
[> 'tag ty] r ->
[> 'tag mgr_ty] r ->
?cwd:Fs.dir_ty Path.t ->
?stdin:_ Flow.source ->
?stdout:_ Flow.sink ->
?stderr:_ Flow.sink ->
?env:string array ->
?executable:string ->
string list -> 'tag process_ty r
string list -> 'tag ty r
(** [spawn ~sw mgr args] creates a new child process that is connected to the switch [sw].
The child process will be sent {! Sys.sigkill} when the switch is released.
Expand All @@ -99,7 +100,7 @@ val spawn :
searching $PATH for it if necessary. *)

val run :
[> 'tag ty] r ->
[> 'tag mgr_ty] r ->
?cwd:_ Path.t ->
?stdin:_ Flow.source ->
?stdout:_ Flow.sink ->
Expand All @@ -117,7 +118,7 @@ val run :
Note: If [spawn] needed to create extra fibers to copy [stdin], etc, then it also waits for those to finish. *)

val parse_out :
[> 'tag ty] r ->
[> 'tag mgr_ty] r ->
'a Buf_read.parser ->
?cwd:_ Path.t ->
?stdin:_ Flow.source ->
Expand All @@ -139,7 +140,7 @@ val parse_out :

(** {2 Pipes} *)

val pipe : sw:Switch.t -> [> 'tag ty] r -> [Flow.source_ty | Resource.close_ty] r * [Flow.sink_ty | Resource.close_ty] r
val pipe : sw:Switch.t -> [> 'tag mgr_ty] r -> [Flow.source_ty | Resource.close_ty] r * [Flow.sink_ty | Resource.close_ty] r
(** [pipe ~sw mgr] creates a pipe backed by the OS.
The flows can be used by {!spawn} without the need for extra fibers to copy the data.
Expand All @@ -157,11 +158,11 @@ module Pi : sig
end

type (_, _, _) Resource.pi +=
| Process : ('t, (module PROCESS with type t = 't and type tag = 'tag), [> 'tag process_ty]) Resource.pi
| Process : ('t, (module PROCESS with type t = 't and type tag = 'tag), [> 'tag ty]) Resource.pi

val process :
(module PROCESS with type t = 't and type tag = 'tag) ->
('t, 'tag process_ty) Resource.handler
('t, 'tag ty) Resource.handler

module type MGR = sig
type tag
Expand All @@ -182,13 +183,13 @@ module Pi : sig
?env:string array ->
?executable:string ->
string list ->
tag process_ty r
tag ty r
end

type (_, _, _) Resource.pi +=
| Mgr : ('t, (module MGR with type t = 't and type tag = 'tag), [> 'tag ty]) Resource.pi
| Mgr : ('t, (module MGR with type t = 't and type tag = 'tag), [> 'tag mgr_ty]) Resource.pi

val mgr :
(module MGR with type t = 't and type tag = 'tag) ->
('t, 'tag ty) Resource.handler
('t, 'tag mgr_ty) Resource.handler
end
2 changes: 1 addition & 1 deletion lib_eio/unix/eio_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Stdenv = struct
stderr : sink_ty r;
net : [`Unix | `Generic] Eio.Net.ty r;
domain_mgr : Eio.Domain_manager.t;
process_mgr : Process.ty r;
process_mgr : Process.mgr_ty r;
clock : float Eio.Time.clock_ty r;
mono_clock : Eio.Time.Mono.ty r;
fs : Eio.Fs.dir_ty Eio.Path.t;
Expand Down
2 changes: 1 addition & 1 deletion lib_eio/unix/eio_unix.mli
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module Stdenv : sig
stderr : sink_ty r;
net : [`Unix | `Generic] Eio.Net.ty r;
domain_mgr : Eio.Domain_manager.t;
process_mgr : Process.ty r;
process_mgr : Process.mgr_ty r;
clock : float Eio.Time.clock_ty r;
mono_clock : Eio.Time.Mono.ty r;
fs : Eio.Fs.dir_ty Eio.Path.t;
Expand Down
16 changes: 8 additions & 8 deletions lib_eio/unix/process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ let get_env = function
| Some e -> e
| None -> Unix.environment ()

type process_ty = [ `Generic | `Unix ] Eio.Process.process_ty
type 'a process = ([> process_ty] as 'a) r

type ty = [`Generic | `Unix] Eio.Process.ty
type ty = [ `Generic | `Unix ] Eio.Process.ty
type 'a t = ([> ty] as 'a) r

type mgr_ty = [`Generic | `Unix] Eio.Process.mgr_ty
type 'a mgr = ([> mgr_ty] as 'a) r

module Pi = struct
module type MGR = sig
include Eio.Process.Pi.MGR
Expand All @@ -87,11 +87,11 @@ module Pi = struct
fds:(int * Fd.t * Fork_action.blocking) list ->
executable:string ->
string list ->
process_ty r
ty r
end

type (_, _, _) Eio.Resource.pi +=
| Mgr_unix : ('t, (module MGR with type t = 't), [> ty]) Eio.Resource.pi
| Mgr_unix : ('t, (module MGR with type t = 't), [> mgr_ty]) Eio.Resource.pi

let mgr_unix (type t tag) (module X : MGR with type t = t and type tag = tag) =
Eio.Resource.handler [
Expand All @@ -100,7 +100,7 @@ module Pi = struct
]
end

module Make_proc (X : sig
module Make_mgr (X : sig
type t

val spawn_unix :
Expand All @@ -111,7 +111,7 @@ module Make_proc (X : sig
fds:(int * Fd.t * Fork_action.blocking) list ->
executable:string ->
string list ->
process_ty r
ty r
end) = struct
type t = X.t

Expand Down
22 changes: 11 additions & 11 deletions lib_eio/unix/process.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ open Eio.Std
These extend the types in {!Eio.Process} with support for file descriptors. *)

type process_ty = [ `Generic | `Unix ] Eio.Process.process_ty
type 'a process = ([> process_ty] as 'a) r

type ty = [`Generic | `Unix] Eio.Process.ty
type ty = [ `Generic | `Unix ] Eio.Process.ty
type 'a t = ([> ty] as 'a) r

type mgr_ty = [`Generic | `Unix] Eio.Process.mgr_ty
type 'a mgr = ([> mgr_ty] as 'a) r

module Pi : sig
module type MGR = sig
include Eio.Process.Pi.MGR
Expand All @@ -24,18 +24,18 @@ module Pi : sig
fds:(int * Fd.t * Fork_action.blocking) list ->
executable:string ->
string list ->
process_ty r
ty r
end

type (_, _, _) Eio.Resource.pi +=
| Mgr_unix : ('t, (module MGR with type t = 't), [> ty]) Eio.Resource.pi
| Mgr_unix : ('t, (module MGR with type t = 't), [> mgr_ty]) Eio.Resource.pi

val mgr_unix :
(module MGR with type t = 't and type tag = 'tag) ->
('t, 'tag Eio.Process.ty) Eio.Resource.handler
('t, 'tag Eio.Process.mgr_ty) Eio.Resource.handler
end

module Make_proc (X : sig
module Make_mgr (X : sig
type t

val spawn_unix :
Expand All @@ -46,18 +46,18 @@ module Make_proc (X : sig
fds:(int * Fd.t * Fork_action.blocking) list ->
executable:string ->
string list ->
process_ty r
ty r
end) : Pi.MGR with type t = X.t and type tag = [`Generic | `Unix]

val spawn_unix :
sw:Switch.t ->
_ t ->
_ mgr ->
?cwd:Eio.Fs.dir_ty Eio.Path.t ->
fds:(int * Fd.t * Fork_action.blocking) list ->
?env:string array ->
?executable:string ->
string list ->
process_ty r
ty r
(** [spawn_unix ~sw mgr ~fds args] spawns a child process running the command [args].
The arguments are as for {!Eio.Process.spawn},
Expand Down
10 changes: 5 additions & 5 deletions lib_eio_linux/eio_linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ type stdenv = Eio_unix.Stdenv.base

module Process = Low_level.Process

let process proc : Eio_unix.Process.process_ty Eio.Resource.t =
let process proc : Eio_unix.Process.ty Eio.Resource.t =
let module T = struct
type t = unit
type tag = [ `Generic | `Unix ]
Expand Down Expand Up @@ -347,7 +347,7 @@ let with_dir dir_fd path fn =
dir_fd (if path = "" then "." else path)
|> fn

module Process_impl = struct
module Process_mgr = struct
module T = struct
type t = unit

Expand All @@ -369,11 +369,11 @@ module Process_impl = struct
process (Process.spawn ~sw actions)
end

include Eio_unix.Process.Make_proc (T)
include Eio_unix.Process.Make_mgr (T)
end

let process_mgr : Eio_unix.Process.ty r =
let h = Eio_unix.Process.Pi.mgr_unix (module Process_impl) in
let process_mgr : Eio_unix.Process.mgr_ty r =
let h = Eio_unix.Process.Pi.mgr_unix (module Process_mgr) in
Eio.Resource.T ((), h)

let wrap_backtrace fn x =
Expand Down
2 changes: 1 addition & 1 deletion lib_eio_posix/eio_posix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let run main =
method clock = Time.clock
method mono_clock = Time.mono_clock
method net = Net.v
method process_mgr = Process.v
method process_mgr = Process.mgr
method domain_mgr = Domain_mgr.v
method cwd = ((Fs.cwd, "") :> Eio.Fs.dir_ty Eio.Path.t)
method fs = ((Fs.fs, "") :> Eio.Fs.dir_ty Eio.Path.t)
Expand Down
8 changes: 4 additions & 4 deletions lib_eio_posix/process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Eio.Std

module Process = Low_level.Process

let process proc : Eio_unix.Process.process_ty Eio.Resource.t =
let process proc : Eio_unix.Process.ty Eio.Resource.t =
let module T = struct
type t = unit
type tag = [ `Generic | `Unix ]
Expand Down Expand Up @@ -44,9 +44,9 @@ module Impl = struct
process (Process.spawn ~sw actions)
end

include Eio_unix.Process.Make_proc (T)
include Eio_unix.Process.Make_mgr (T)
end

let v : Eio_unix.Process.ty r =
let mgr : Eio_unix.Process.mgr_ty r =
let h = Eio_unix.Process.Pi.mgr_unix (module Impl) in
Eio.Resource.T ((), h)
Eio.Resource.T ((), h)

0 comments on commit 8b14ca3

Please sign in to comment.