Skip to content

Commit

Permalink
CP-32622: Replace: Unix.select and Thread.wait_timed_* with Unixext e…
Browse files Browse the repository at this point in the history
…quivalents

Also add xapi-stdext-unix or xapi-stext-threads as needed to dune.

Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed Aug 5, 2024
1 parent 6354eca commit e4e1f5b
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ocaml/database/block_device_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ let accept_conn s latest_response_time =
let now = Unix.gettimeofday () in
let timeout = latest_response_time -. now in
(* Await an incoming connection... *)
let ready_to_read, _, _ = Unix.select [s] [] [] timeout in
let ready_to_read, _, _ = Xapi_stdext_unix.Unixext.select [s] [] [] timeout in
R.info "Finished selecting" ;
if List.mem s ready_to_read then
(* We've received a connection. Accept it and return the socket. *)
Expand Down
6 changes: 5 additions & 1 deletion ocaml/database/master_connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ let open_secure_connection () =
~write_to_log:(fun x -> debug "stunnel: %s\n" x)
~verify_cert host port
@@ fun st_proc ->
let fd_closed = Thread.wait_timed_read Unixfd.(!(st_proc.Stunnel.fd)) 5. in
let fd_closed =
Xapi_stdext_threads.Threadext.wait_timed_read
Unixfd.(!(st_proc.Stunnel.fd))
5.
in
let proc_quit =
try
Unix.kill (Stunnel.getpid st_proc.Stunnel.pid) 0 ;
Expand Down
8 changes: 6 additions & 2 deletions ocaml/forkexecd/src/child.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ let handle_comms_sock comms_sock state =

let handle_comms_no_fd_sock2 comms_sock fd_sock state =
debug "Selecting in handle_comms_no_fd_sock2" ;
let ready, _, _ = Unix.select [comms_sock; fd_sock] [] [] (-1.0) in
let ready, _, _ =
Xapi_stdext_unix.Unixext.select [comms_sock; fd_sock] [] [] (-1.0)
in
debug "Done" ;
if List.mem fd_sock ready then (
debug "fd sock" ;
Expand All @@ -74,7 +76,9 @@ let handle_comms_no_fd_sock2 comms_sock fd_sock state =

let handle_comms_with_fd_sock2 comms_sock _fd_sock fd_sock2 state =
debug "Selecting in handle_comms_with_fd_sock2" ;
let ready, _, _ = Unix.select [comms_sock; fd_sock2] [] [] (-1.0) in
let ready, _, _ =
Xapi_stdext_unix.Unixext.select [comms_sock; fd_sock2] [] [] (-1.0)
in
debug "Done" ;
if List.mem fd_sock2 ready then (
debug "fd sock2" ;
Expand Down
1 change: 1 addition & 0 deletions ocaml/libs/ezxenstore/core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
(re_export xenstore)
(re_export xenstore_transport)
threads.posix
xapi-stdext-unix
(re_export xenstore.unix))
)
2 changes: 1 addition & 1 deletion ocaml/libs/ezxenstore/core/watch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let wait_for ~xs ?(timeout = 300.) (x : 'a t) =
let thread =
Thread.create
(fun () ->
let r, _, _ = Unix.select [p1] [] [] timeout in
let r, _, _ = Xapi_stdext_unix.Unixext.select [p1] [] [] timeout in
if r <> [] then
()
else
Expand Down
2 changes: 1 addition & 1 deletion ocaml/libs/http-lib/buf_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ let is_full ic = ic.cur = 0 && ic.max = Bytes.length ic.buf
let fill_buf ~buffered ic timeout =
let buf_size = Bytes.length ic.buf in
let fill_no_exc timeout len =
let l, _, _ = Unix.select [ic.fd] [] [] timeout in
let l, _, _ = Xapi_stdext_unix.Unixext.select [ic.fd] [] [] timeout in
if l <> [] then (
let n = Unix.read ic.fd ic.buf ic.max len in
ic.max <- n + ic.max ;
Expand Down
2 changes: 1 addition & 1 deletion ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/unixext.mli
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ val domain_of_addr : string -> Unix.socket_domain option

val test_open : int -> unit
(** [test_open n] opens n file descriptors. This is useful for testing that the application makes no calls
to [Unix.select] that use file descriptors, because such calls will then immediately fail.
to [Xapi_stdext_unix.Unixext.select] that use file descriptors, because such calls will then immediately fail.
This assumes that [ulimit -n] has been suitably increased in the test environment.
Expand Down
1 change: 1 addition & 0 deletions ocaml/message-switch/unix/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
rpclib.json
threads.posix
xapi-stdext-threads
xapi-stdext-unix
)
(preprocess (per_module ((pps ppx_deriving_rpc) Protocol_unix_scheduler)))
)
Expand Down
4 changes: 2 additions & 2 deletions ocaml/networkd/lib/jsonrpc_client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let timeout_read fd timeout =
in
let rec inner max_time max_bytes =
let ready_to_read, _, _ =
try Unix.select [fd] [] [] (to_s max_time)
try Xapi_stdext_unix.Unixext.select [fd] [] [] (to_s max_time)
with
(* in case the unix.select call fails in situation like interrupt *)
| Unix.Unix_error (Unix.EINTR, _, _) ->
Expand Down Expand Up @@ -96,7 +96,7 @@ let timeout_write filedesc total_length data response_time =
in
let rec inner_write offset max_time =
let _, ready_to_write, _ =
try Unix.select [] [filedesc] [] (to_s max_time)
try Xapi_stdext_unix.Unixext.select [] [filedesc] [] (to_s max_time)
with
(* in case the unix.select call fails in situation like interrupt *)
| Unix.Unix_error (Unix.EINTR, _, _) ->
Expand Down
1 change: 1 addition & 0 deletions ocaml/xapi-idl/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
xapi-open-uri
xapi-stdext-pervasives
xapi-stdext-threads
xapi-stdext-unix
xapi-inventory
xmlm
)
Expand Down
2 changes: 2 additions & 0 deletions ocaml/xapi-idl/lib_test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@
xapi-idl.xen
xapi-idl.xen.interface
xapi-log
xapi-stdext-unix
xapi-stdext-threads
)
(preprocess (per_module ((pps ppx_deriving_rpc) Task_server_test Updates_test))))
2 changes: 1 addition & 1 deletion ocaml/xapi-idl/lib_test/scheduler_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let timed_wait_callback ~msg ?(time_min = 0.) ?(eps = 0.1) ?(time_max = 60.) f =
()
in
f callback ;
let ready = Thread.wait_timed_read rd time_max in
let ready = Xapi_stdext_threads.Threadext.wait_timed_read rd time_max in
match (ready, !after) with
| true, None ->
Alcotest.fail "pipe ready to read, but after is not set"
Expand Down
3 changes: 2 additions & 1 deletion ocaml/xe-cli/newcli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ let main_loop ifd ofd permitted_filenames =
finished := true
else
let r, _, _ =
Unix.select [Unix.stdin; fd] [] [] heartbeat_interval
Xapi_stdext_unix.Unixext.select [Unix.stdin; fd] [] []
heartbeat_interval
in
let now = Unix.time () in
if now -. !last_heartbeat >= heartbeat_interval then (
Expand Down
1 change: 1 addition & 0 deletions ocaml/xenopsd/cli/dune
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
xapi-idl.xen.interface
xapi-idl.xen.interface.types
xapi-stdext-pervasives
xapi-stdext-unix
)
(preprocess (per_module ((pps ppx_deriving_rpc) Common Xn_cfg_types)))
)
Expand Down
4 changes: 3 additions & 1 deletion ocaml/xenopsd/cli/xn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,9 @@ let raw_console_proxy sockaddr =
) else if !final then
finished := true
else
let r, _, _ = Unix.select [Unix.stdin; fd] [] [] (-1.) in
let r, _, _ =
Xapi_stdext_unix.Unixext.select [Unix.stdin; fd] [] [] (-1.)
in
if List.mem Unix.stdin r then (
let b =
Unix.read Unix.stdin buf_remote !buf_remote_end
Expand Down
1 change: 1 addition & 0 deletions ocaml/xsh/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
safe-resources
xapi-consts
xapi-log
xapi-stdext-unix
)
)

2 changes: 1 addition & 1 deletion ocaml/xsh/xsh.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let proxy (ain : Unix.file_descr) (aout : Unix.file_descr) (bin : Unixfd.t)
(if can_write a' then [bout] else [])
@ if can_write b' then [aout] else []
in
let r, w, _ = Unix.select r w [] (-1.0) in
let r, w, _ = Xapi_stdext_unix.Unixext.select r w [] (-1.0) in
(* Do the writing before the reading *)
List.iter
(fun fd -> if aout = fd then write_from b' a' else write_from a' b')
Expand Down

0 comments on commit e4e1f5b

Please sign in to comment.