struct
type t = {
mutable lwt_t: unit Lwt.t;
mutable lwt_u: unit Lwt.u;
mutable color: [`Red | `Green];
}
let create () =
let lwt_t, lwt_u = Lwt.task () in
{lwt_u; lwt_t; color = `Red}
let try_to_pass w =
match w.color with
| `Green -> return ()
| `Red ->
begin match Lwt.state w.lwt_t with
| Lwt.Sleep -> ()
| Lwt.Return () | Lwt.Fail _ ->
let t, u = Lwt.task () in
w.lwt_t <- t;
w.lwt_u <- u;
end;
wrap_deferred ~on_exn:(fun e -> e) (fun () -> w.lwt_t)
>>< function
| `Error Lwt.Canceled -> return ()
| `Error other -> failwith "BUG: THIS SHOULD NOT HAPPEN"
| `Ok () -> return ()
let green t =
t.color <- `Green;
Lwt.wakeup_exn t.lwt_u Lwt.Canceled
end