Skip to content

Commit 8204060

Browse files
committed
Translate 'Lwt_preemptive.detach'
1 parent 93c95d1 commit 8204060

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

bin/lwt_to_direct_style/ast_rewrite.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ let rewrite_apply ~backend ~state full_ident args =
390390
| "Lwt_io", "flush" -> take @@ fun fd -> return (Some (backend#io_flush fd))
391391
| "Lwt_main", "run" ->
392392
take @@ fun promise -> return (Some (backend#main_run promise))
393+
| "Lwt_preemptive", "detach" ->
394+
take @@ fun f ->
395+
take @@ fun arg ->
396+
return
397+
(Some
398+
(backend#domain_detach (mk_thunk (Exp.apply f [ (Nolabel, arg) ]))))
393399
| _ -> return None
394400

395401
(** Transform a [binding_op] into a [pattern] and an [expression] while

bin/lwt_to_direct_style/concurrency_backend.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,16 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
326326
(Labelled (mk_loc "follow"), mk_constr_of_bool follow);
327327
(Nolabel, mk_apply_simple [ "Eio"; "Path"; "/" ] [ env "cwd"; path ]);
328328
]
329+
330+
method domain_detach thunk =
331+
mk_apply_ident
332+
(fiber_ident "fork_promise")
333+
[
334+
get_current_switch_arg ();
335+
( Nolabel,
336+
mk_thunk
337+
(mk_apply_simple
338+
[ "Eio"; "Domain_manager"; "run" ]
339+
[ env "domain_mgr"; thunk ]) );
340+
]
329341
end

test/lwt_to_direct_style/to_direct_style.t/run.t

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,11 @@ Make a writable directory tree:
230230
Lwt_mutex.lock (line 136 column 9)
231231
Lwt_mutex.unlock (line 137 column 9)
232232
Lwt_mutex.with_lock (line 138 column 9)
233-
lib/test_lwt_unix.ml: (43 occurrences)
233+
lib/test_lwt_unix.ml: (47 occurrences)
234234
Lwt_io (line 7 column 8)
235235
Lwt.return (line 12 column 3)
236+
Lwt.return (line 46 column 11)
237+
Lwt.return_unit (line 44 column 43)
236238
Lwt.let* (line 10 column 3)
237239
Lwt.let* (line 11 column 3)
238240
Lwt.let* (line 30 column 3)
@@ -264,6 +266,8 @@ Make a writable directory tree:
264266
Lwt_io.stdout (line 26 column 33)
265267
Lwt_io.open_file (line 30 column 13)
266268
Lwt_io.open_file (line 34 column 13)
269+
Lwt_preemptive.detach (line 44 column 10)
270+
Lwt_preemptive.detach (line 47 column 3)
267271
Lwt_unix.Timeout (line 14 column 9)
268272
Lwt_unix.of_unix_file_descr (line 6 column 8)
269273
Lwt_unix.stat (line 37 column 16)
@@ -816,3 +820,18 @@ Make a writable directory tree:
816820
?count:(Some 42) chan
817821
818822
let _f chan = Eio.Buf_write.flush chan
823+
824+
let _f =
825+
Fiber.fork_promise ~sw (fun () ->
826+
Eio.Domain_manager.run env#domain_mgr (fun () ->
827+
(* TODO: lwt-to-direct-style: [sw] (of type Switch.t) must be propagated here. *)
828+
(* TODO: lwt-to-direct-style: [env] must be propagated from the main loop *)
829+
(fun () -> ()) ()))
830+
831+
let _f =
832+
let f = fun x1 -> x1 in
833+
Fiber.fork_promise ~sw (fun () ->
834+
Eio.Domain_manager.run env#domain_mgr (fun () ->
835+
(* TODO: lwt-to-direct-style: [env] must be propagated from the main loop *)
836+
(* TODO: lwt-to-direct-style: [sw] (of type Switch.t) must be propagated here. *)
837+
f 12))

test/lwt_to_direct_style/to_direct_style.t/src/lib/test_lwt_unix.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ let _f chan = Lwt_io.read chan
4040
let _f chan = Lwt_io.read ~count:42 chan
4141
let _f chan = Lwt_io.read ?count:(Some 42) chan
4242
let _f chan = Lwt_io.flush chan
43+
44+
let _f = Lwt_preemptive.detach (fun () -> Lwt.return_unit) ()
45+
let _f =
46+
let f = Lwt.return in
47+
Lwt_preemptive.detach f 12

0 commit comments

Comments
 (0)