mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-06 03:05:30 -05:00
Fut.wait_block: a bit of spinning before blocking
This commit is contained in:
parent
7d014b0586
commit
f9ba356657
2 changed files with 28 additions and 11 deletions
|
|
@ -55,6 +55,8 @@ let get_id (self:t) : int = Thread.id self
|
|||
|
||||
let spawn f : t =
|
||||
Thread.create f ()
|
||||
|
||||
let relax () = Thread.yield ()
|
||||
|}
|
||||
|
||||
let domain_post_5 =
|
||||
|
|
@ -65,8 +67,9 @@ type t = unit Domain.t
|
|||
|
||||
let get_id (self:t) : int = (Domain.get_id self :> int)
|
||||
|
||||
let spawn f : t =
|
||||
Domain.spawn f
|
||||
let spawn : _ -> t = Domain.spawn
|
||||
|
||||
let relax = Domain.cpu_relax
|
||||
|}
|
||||
|
||||
let p_version s = Scanf.sscanf s "%d.%d" (fun x y -> x, y)
|
||||
|
|
|
|||
|
|
@ -346,15 +346,29 @@ module Fut = struct
|
|||
(* ### blocking ### *)
|
||||
|
||||
let wait_block (self : 'a t) : 'a or_error =
|
||||
match peek self with
|
||||
| Some x ->
|
||||
(* fast path *)
|
||||
x
|
||||
| None ->
|
||||
match A.get self.st with
|
||||
| Done x -> x (* fast path *)
|
||||
| Waiting _ ->
|
||||
let real_block () =
|
||||
(* use queue only once *)
|
||||
let q = S_queue.create () in
|
||||
on_result self (fun r -> S_queue.push q r);
|
||||
S_queue.pop q
|
||||
in
|
||||
|
||||
(* a bit of spinlock *)
|
||||
let rec loop i =
|
||||
if i = 0 then
|
||||
real_block ()
|
||||
else (
|
||||
match A.get self.st with
|
||||
| Done x -> x
|
||||
| Waiting _ ->
|
||||
Domain_.relax ();
|
||||
(loop [@tailcall]) (i - 1)
|
||||
)
|
||||
in
|
||||
loop 50
|
||||
|
||||
let wait_block_exn self =
|
||||
match wait_block self with
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue