Skip to content

Commit b098648

Browse files
committed
CP-32622: Remove select using polly and thread.delay
Signed-off-by: Steven Woods <[email protected]>
1 parent 9f07596 commit b098648

File tree

2 files changed

+91
-47
lines changed

2 files changed

+91
-47
lines changed

lib/xapi-stdext-threads/threadext.ml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,22 @@ module Delay = struct
8686
pipe_out
8787
)
8888
in
89-
let r, _, _ = Unix.select [pipe_out] [] [] seconds in
90-
(* flush the single byte from the pipe *)
91-
if r <> [] then ignore (Unix.read pipe_out (Bytes.create 1) 0 1) ;
89+
let epoll = Polly.create () in
90+
Polly.add epoll pipe_out Polly.Events.inp ;
91+
let num_fds =
92+
Fun.protect
93+
~finally:(fun () -> Polly.close epoll)
94+
(fun () ->
95+
Polly.wait epoll 1
96+
(int_of_float (seconds *. 1000.))
97+
(fun _ fd _ ->
98+
(* flush the single byte from the pipe *)
99+
ignore (Unix.read pipe_out (Bytes.create 1) 0 1)
100+
)
101+
)
102+
in
92103
(* return true if we waited the full length of time, false if we were woken *)
93-
r = []
104+
num_fds = 0
94105
with Pre_signalled -> false
95106
)
96107
(fun () ->

lib/xapi-stdext-unix/unixext.ml

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ let kill_and_wait ?(signal = Sys.sigterm) ?(timeout = 10.) pid =
361361
let cmdline = readcmdline pid in
362362
if cmdline = reference then (
363363
(* still up, let's sleep a bit *)
364-
ignore (Unix.select [] [] [] loop_time_waiting) ;
364+
Thread.delay loop_time_waiting ;
365365
left := !left -. loop_time_waiting
366366
) else (* not the same, it's gone ! *)
367367
quit := true
@@ -419,19 +419,30 @@ let proxy (a : Unix.file_descr) (b : Unix.file_descr) =
419419
in
420420
(* If we can't make any progress (because fds have been closed), then stop *)
421421
if r = [] && w = [] then raise End_of_file ;
422-
let r, w, _ = Unix.select r w [] (-1.0) in
423-
(* Do the writing before the reading *)
424-
List.iter
425-
(fun fd -> if a = fd then CBuf.write b' a else CBuf.write a' b)
426-
w ;
427-
List.iter (fun fd -> if a = fd then CBuf.read a' a else CBuf.read b' b) r ;
428-
(* If there's nothing else to read or write then signal the other end *)
429-
List.iter
430-
(fun (buf, fd) ->
431-
if CBuf.end_of_reads buf then Unix.shutdown fd Unix.SHUTDOWN_SEND ;
432-
if CBuf.end_of_writes buf then Unix.shutdown fd Unix.SHUTDOWN_RECEIVE
422+
let epoll = Polly.create () in
423+
List.iter (fun fd -> Polly.add epoll fd Polly.Events.inp) r ;
424+
List.iter (fun fd -> Polly.add epoll fd Polly.Events.out) w ;
425+
Fun.protect
426+
~finally:(fun () -> Polly.close epoll)
427+
(fun () ->
428+
ignore
429+
@@ Polly.wait epoll 4 (-1) (fun _ file_desc event ->
430+
(* Do the writing before the reading *)
431+
if event = Polly.Events.out then
432+
if a = file_desc then CBuf.write b' a else CBuf.write a' b ;
433+
if event = Polly.Events.inp then
434+
if a = file_desc then CBuf.read a' a else CBuf.read b' b ;
435+
(* If there's nothing else to read or write then signal the other end *)
436+
List.iter
437+
(fun (buf, fd) ->
438+
if CBuf.end_of_reads buf then
439+
Unix.shutdown fd Unix.SHUTDOWN_SEND ;
440+
if CBuf.end_of_writes buf then
441+
Unix.shutdown fd Unix.SHUTDOWN_RECEIVE
442+
)
443+
[(a', b); (b', a)]
444+
)
433445
)
434-
[(a', b); (b', a)]
435446
done
436447
with _ -> (
437448
(try Unix.clear_nonblock a with _ -> ()) ;
@@ -517,21 +528,32 @@ let time_limited_write_internal
517528
let now = ref (Unix.gettimeofday ()) in
518529
while !bytes_written < total_bytes_to_write && !now < target_response_time do
519530
let remaining_time = target_response_time -. !now in
520-
let _, ready_to_write, _ = Unix.select [] [filedesc] [] remaining_time in
521-
(* Note: there is a possibility that the storage could go away after the select and before the write, so the write would block. *)
522-
( if List.mem filedesc ready_to_write then
523-
let bytes_to_write = total_bytes_to_write - !bytes_written in
524-
let bytes =
525-
try write filedesc data !bytes_written bytes_to_write
526-
with
527-
| Unix.Unix_error (Unix.EAGAIN, _, _)
528-
| Unix.Unix_error (Unix.EWOULDBLOCK, _, _)
529-
->
530-
0
531+
let epoll = Polly.create () in
532+
Polly.add epoll filedesc Polly.Events.out ;
533+
Fun.protect
534+
~finally:(fun () -> Polly.close epoll)
535+
(fun () ->
536+
let (_ : int) =
537+
Polly.wait epoll 1
538+
(int_of_float (remaining_time *. 1000.))
539+
(fun _ fd _ ->
540+
(* Note: there is a possibility that the storage could go away after the epoll and before the write, so the write would block. *)
541+
if fd = filedesc then
542+
let bytes_to_write = total_bytes_to_write - !bytes_written in
543+
let bytes =
544+
try write filedesc data !bytes_written bytes_to_write
545+
with
546+
| Unix.Unix_error (Unix.EAGAIN, _, _)
547+
| Unix.Unix_error (Unix.EWOULDBLOCK, _, _)
548+
->
549+
0
550+
in
551+
(* write from buffer=data from offset=bytes_written, length=bytes_to_write *)
552+
bytes_written := bytes + !bytes_written
553+
)
531554
in
532-
(* write from buffer=data from offset=bytes_written, length=bytes_to_write *)
533-
bytes_written := bytes + !bytes_written
534-
) ;
555+
()
556+
) ;
535557
now := Unix.gettimeofday ()
536558
done ;
537559
if !bytes_written = total_bytes_to_write then
@@ -557,23 +579,34 @@ let time_limited_read filedesc length target_response_time =
557579
let now = ref (Unix.gettimeofday ()) in
558580
while !bytes_read < total_bytes_to_read && !now < target_response_time do
559581
let remaining_time = target_response_time -. !now in
560-
let ready_to_read, _, _ = Unix.select [filedesc] [] [] remaining_time in
561-
( if List.mem filedesc ready_to_read then
562-
let bytes_to_read = total_bytes_to_read - !bytes_read in
563-
let bytes =
564-
try Unix.read filedesc buf !bytes_read bytes_to_read
565-
with
566-
| Unix.Unix_error (Unix.EAGAIN, _, _)
567-
| Unix.Unix_error (Unix.EWOULDBLOCK, _, _)
568-
->
569-
0
582+
let epoll = Polly.create () in
583+
Polly.add epoll filedesc Polly.Events.inp ;
584+
Fun.protect
585+
~finally:(fun () -> Polly.close epoll)
586+
(fun () ->
587+
let (_ : int) =
588+
Polly.wait epoll 1
589+
(int_of_float (remaining_time *. 1000.))
590+
(fun _ fd _ ->
591+
if fd = filedesc then
592+
let bytes_to_read = total_bytes_to_read - !bytes_read in
593+
let bytes =
594+
try Unix.read filedesc buf !bytes_read bytes_to_read
595+
with
596+
| Unix.Unix_error (Unix.EAGAIN, _, _)
597+
| Unix.Unix_error (Unix.EWOULDBLOCK, _, _)
598+
->
599+
0
600+
in
601+
(* read into buffer=buf from offset=bytes_read, length=bytes_to_read *)
602+
if bytes = 0 then
603+
raise End_of_file (* End of file has been reached *)
604+
else
605+
bytes_read := bytes + !bytes_read
606+
)
570607
in
571-
(* read into buffer=buf from offset=bytes_read, length=bytes_to_read *)
572-
if bytes = 0 then
573-
raise End_of_file (* End of file has been reached *)
574-
else
575-
bytes_read := bytes + !bytes_read
576-
) ;
608+
()
609+
) ;
577610
now := Unix.gettimeofday ()
578611
done ;
579612
if !bytes_read = total_bytes_to_read then

0 commit comments

Comments
 (0)