aboutsummaryrefslogtreecommitdiff
path: root/libbutl/builtin.ixx
diff options
context:
space:
mode:
Diffstat (limited to 'libbutl/builtin.ixx')
-rw-r--r--libbutl/builtin.ixx78
1 files changed, 78 insertions, 0 deletions
diff --git a/libbutl/builtin.ixx b/libbutl/builtin.ixx
new file mode 100644
index 0000000..0356f8b
--- /dev/null
+++ b/libbutl/builtin.ixx
@@ -0,0 +1,78 @@
+// file : libbutl/builtin.ixx -*- C++ -*-
+// license : MIT; see accompanying LICENSE file
+
+namespace butl
+{
+ // builtin
+ //
+ // Implement timed_wait() function templates in terms of their milliseconds
+ // specialization.
+ //
+ template <>
+ LIBBUTL_SYMEXPORT optional<std::uint8_t> builtin::
+ timed_wait (const std::chrono::milliseconds&);
+
+ template <typename R, typename P>
+ inline optional<std::uint8_t> builtin::
+ timed_wait (const std::chrono::duration<R, P>& d)
+ {
+ using namespace std::chrono;
+ return timed_wait (duration_cast<milliseconds> (d));
+ }
+
+ inline optional<std::uint8_t> builtin::
+ try_wait ()
+ {
+ if (state_ != nullptr)
+ {
+ std::unique_lock<std::mutex> l (state_->mutex);
+
+ if (!state_->finished)
+ return nullopt;
+ }
+
+ return result_;
+ }
+
+ // builtin_map
+ //
+ inline const builtin_info* builtin_map::
+ find (const std::string& n) const
+ {
+ auto i (base::find (n));
+ return i != end () ? &i->second : nullptr;
+ }
+
+ // builtin::async_state
+ //
+ template <typename F>
+ inline builtin::async_state::
+ async_state (F f)
+ : thread ([f = std::move (f), this] () mutable noexcept
+ {
+ f ();
+
+ {
+ std::unique_lock<std::mutex> l (this->mutex);
+ finished = true;
+ }
+
+ condv.notify_all ();
+ })
+ {
+ }
+
+ template <typename F>
+ inline builtin
+ pseudo_builtin (std::uint8_t& r, F f)
+ {
+ std::unique_ptr<builtin::async_state> s (
+ new builtin::async_state (
+ [f = std::move (f), &r] () mutable noexcept
+ {
+ r = f ();
+ }));
+
+ return builtin (r, move (s));
+ }
+}