Skip to content

Commit

Permalink
thread.run.recurrent is now the main function.
Browse files Browse the repository at this point in the history
  • Loading branch information
smimram committed Nov 4, 2019
1 parent 2b06fe6 commit e55ff38
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
18 changes: 6 additions & 12 deletions libs/thread.liq
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# TODO: we cannot easly define recursive functions with optional arguments for
# now, see #1018.
def rec thread.run.recurrent(~fast, ~delay, f)
def f ()
delay = f ()
if delay >= 0. then thread.run.recurrent(fast=fast, delay=delay, f) end
end
thread.run(fast=fast, delay=delay, f)
end

# Run a recurrent function in a separate thread.
# Run a function in a separate thread.
# @category Control
# @param ~fast Whether the thread is supposed to return quickly or not. Typically, blocking tasks (e.g. fetching data over the internet) should not be considered to be fast. When set to `false` its priority will be lowered below that of request resolutions and fast timeouts. This is only effective if you set a dedicated queue for fast tasks, see the "scheduler" settings for more details.
# @param ~delay Delay (in sec.) after which the thread should be lauched.
# @param f Function to execute.
def thread.run.recurrent(~fast=true, ~delay=0., f)
def thread.run(~fast=true, ~delay=0., f)
def f() =
f()
(-1.)
end
thread.run.recurrent(fast=fast, delay=delay, f)
end

Expand Down
17 changes: 11 additions & 6 deletions src/lang/builtins_thread.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
open Lang_builtins

let () =
add_builtin "thread.run" ~cat:Control
add_builtin "thread.run.recurrent" ~cat:Control
[
"fast", Lang.bool_t, Some (Lang.bool true),
Some "Whether the thread is supposed to return quickly or not. Typically, \
Expand All @@ -34,10 +34,13 @@ let () =
\"scheduler\" settings for more details." ;
"delay", Lang.float_t, Some (Lang.float 0.),
Some "Delay (in sec.) after which the thread should be lauched.";
"", Lang.fun_t [] Lang.unit_t, None, Some "Function to execute."
"", Lang.fun_t [] Lang.float_t, None,
Some "Function to execute recurrently. The returned value is the delay (in \
sec.) in which the function should be run again (it won't be run if \
the value is strictly negative)."
]
Lang.unit_t
~descr:"Run a function in a separate thread."
~descr:"Run a recurrent function in a separate thread."
(fun p ->
let delay = Lang.to_float (List.assoc "delay" p) in
let f = List.assoc "" p in
Expand All @@ -47,14 +50,16 @@ let () =
else
Tutils.Blocking
in
let task =
let rec task delay =
{ Duppy.Task.
priority = priority ;
events = [`Delay delay] ;
handler = fun _ -> Lang.to_unit (Lang.apply ~t:Lang.unit_t f []); []
handler = fun _ ->
let d = Lang.to_float (Lang.apply ~t:Lang.float_t f []) in
if d >= 0. then [task delay] else []
}
in
Duppy.Task.add Tutils.scheduler task;
Duppy.Task.add Tutils.scheduler (task delay);
Lang.unit)

let () =
Expand Down

0 comments on commit e55ff38

Please sign in to comment.