forked from savonet/liquidsoap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup thread functions. (savonet#1019)
* First stab. * Second stab. * Replace add_timeout by thread.run(.delay). * Fix tests. * Replace exec_at by thread.when. * Changelog entry. * Forgot the main file... * Move mutexify. * Rename mutexify to thread.mutexify. * Doc. * Doc.
- Loading branch information
Showing
21 changed files
with
187 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 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. | ||
# @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) | ||
thread.run.recurrent(fast=fast, delay=delay, f) | ||
end | ||
|
||
# Execute a given action when a predicate is true. | ||
# @category System | ||
# @param ~every How often (in sec.) to check for the predicate. | ||
# @param ~once Execute the function only once. | ||
# @param ~changed Execute the function only if the predicate was false when last checked. | ||
# @param p Predicate indicating when to execute the function, typically a time interval such as `{10h-10h30}`. | ||
# @param f Function to execute when the predicate is true. | ||
def thread.when(~every=1., ~once=false, ~changed=true, p, f) | ||
last = ref(false) | ||
def check() | ||
b = p() | ||
if b and not (changed and !last) then f() end | ||
last := b | ||
if b and once then (-1.) else every end | ||
end | ||
thread.run.recurrent(delay=0., check) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
(***************************************************************************** | ||
Liquidsoap, a programmable audio stream generator. | ||
Copyright 2003-2019 Savonet team | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details, fully stated in the COPYING | ||
file at the root of the liquidsoap distribution. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*****************************************************************************) | ||
|
||
open Lang_builtins | ||
|
||
let () = | ||
add_builtin "thread.run" ~cat:Control | ||
[ | ||
"fast", Lang.bool_t, Some (Lang.bool true), | ||
Some "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." ; | ||
"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.unit_t | ||
~descr:"Run a function in a separate thread." | ||
(fun p -> | ||
let delay = Lang.to_float (List.assoc "delay" p) in | ||
let f = List.assoc "" p in | ||
let priority = | ||
if Lang.to_bool (List.assoc "fast" p) then | ||
Tutils.Maybe_blocking | ||
else | ||
Tutils.Blocking | ||
in | ||
let task = | ||
{ Duppy.Task. | ||
priority = priority ; | ||
events = [`Delay delay] ; | ||
handler = fun _ -> Lang.to_unit (Lang.apply ~t:Lang.unit_t f []); [] | ||
} | ||
in | ||
Duppy.Task.add Tutils.scheduler task; | ||
Lang.unit) | ||
|
||
let () = | ||
let t = Lang.univ_t 1 in | ||
add_builtin "thread.mutexify" ~cat:Liq | ||
~descr:"Protect functions with a mutex in order to avoid concurrent \ | ||
calls. It returns the original value when the argument is not a \ | ||
function." | ||
["",t,None,None] t | ||
(fun p -> | ||
let m = Mutex.create () in | ||
let v = List.assoc "" p in | ||
match v.Lang.value with | ||
| Lang.Fun (p,args,env,body) -> | ||
let fn (args:Lang.full_env) t = Tutils.mutexify m (fun () -> | ||
let args = List.map (fun (x,gv) -> x, Lazy.from_val gv) args in | ||
let env = List.rev_append args env in | ||
let v = {v with Lang.value = | ||
Lang.Fun ([],[],env,body)} | ||
in | ||
Lang.apply ~t v []) () | ||
in | ||
{ v with Lang.value = | ||
Lang.FFI (p, args, fn) } | ||
| Lang.FFI (p, args, fn) -> | ||
let fn args t = Tutils.mutexify m (fun () -> | ||
fn args t) () | ||
in | ||
{ v with Lang.value = | ||
Lang.FFI (p, args, fn) } | ||
| _ -> v) |
Oops, something went wrong.