From 3ee9761b73aff34c7f30ee44b8aac276d413cc21 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 14 Oct 2020 16:15:41 +0300 Subject: Add builtin::timed_wait(), builtin::try_wait(), and pseudo_builtin() --- libbutl/builtin.cxx | 61 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 12 deletions(-) (limited to 'libbutl/builtin.cxx') diff --git a/libbutl/builtin.cxx b/libbutl/builtin.cxx index c6083b6..7a2c024 100644 --- a/libbutl/builtin.cxx +++ b/libbutl/builtin.cxx @@ -2164,17 +2164,22 @@ namespace butl const dir_path& cwd, const builtin_callbacks& cbs) { - return builtin ( - r, - thread ([fn, &r, &args, - in = move (in), - out = move (out), - err = move (err), - &cwd, - &cbs] () mutable noexcept - { - r = fn (args, move (in), move (out), move (err), cwd, cbs); - })); + unique_ptr s ( + new builtin::async_state ( + [fn, + &r, + &args, + in = move (in), out = move (out), err = move (err), + &cwd, + &cbs] () mutable noexcept + { + r = fn (args, + move (in), move (out), move (err), + cwd, + cbs); + })); + + return builtin (r, move (s)); } template @@ -2200,7 +2205,7 @@ namespace butl const builtin_callbacks& cbs) { r = fn (args, move (in), move (out), move (err), cwd, cbs); - return builtin (r, thread ()); + return builtin (r); } const builtin_map builtins @@ -2222,4 +2227,36 @@ namespace butl {"touch", {&sync_impl<&touch>, 2}}, {"true", {&true_, 0}} }; + + // builtin + // + uint8_t builtin:: + wait () + { + if (state_ != nullptr) + { + unique_lock l (state_->mutex); + + if (!state_->finished) + state_->condv.wait (l, [this] {return state_->finished;}); + } + + return result_; + } + + template <> + optional builtin:: + timed_wait (const chrono::milliseconds& tm) + { + if (state_ != nullptr) + { + unique_lock l (state_->mutex); + + if (!state_->finished && + !state_->condv.wait_for (l, tm, [this] {return state_->finished;})) + return nullopt; + } + + return result_; + } } -- cgit v1.1