diff options
author | 2021-11-15 04:15:13 +1100 | |
---|---|---|
committer | 2021-11-15 04:15:13 +1100 | |
commit | 44ec6d2e0ee569202b75b73eea40972595866779 (patch) | |
tree | b286443c264704198789514e0594dc24b3ca9f96 /3rdparty/asio/src/examples/cpp14/executors/priority_scheduler.cpp | |
parent | 137f254b918f1f1ebee43dfbed86793b0dae73eb (diff) |
3rdparty: Updated ASIO to version 1.20.0.
The doc folder isn't included as it's pretty big.
This required include/asio/detail/win_iocp_socket_accept_op.hpp due to
mismatched order in the member declarations and initialiser list for the
win_iocp_socket_accept_op class. I reversed the declaration order so it
matches win_iocp_socket_move_accept_op.
Diffstat (limited to '3rdparty/asio/src/examples/cpp14/executors/priority_scheduler.cpp')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp14/executors/priority_scheduler.cpp | 102 |
1 files changed, 58 insertions, 44 deletions
diff --git a/3rdparty/asio/src/examples/cpp14/executors/priority_scheduler.cpp b/3rdparty/asio/src/examples/cpp14/executors/priority_scheduler.cpp index 3b28af1eec0..da4caf0d0ef 100644 --- a/3rdparty/asio/src/examples/cpp14/executors/priority_scheduler.cpp +++ b/3rdparty/asio/src/examples/cpp14/executors/priority_scheduler.cpp @@ -1,66 +1,74 @@ -#include <asio/ts/executor.hpp> +#include <asio/execution.hpp> #include <condition_variable> #include <iostream> #include <memory> #include <mutex> #include <queue> -using asio::dispatch; -using asio::execution_context; +namespace execution = asio::execution; -class priority_scheduler : public execution_context +namespace custom_props { + + struct priority + { + template <typename T> + static constexpr bool is_applicable_property_v = + execution::is_executor<T>::value; + + static constexpr bool is_requirable = true; + static constexpr bool is_preferable = true; + + using polymorphic_query_result_type = int; + + int value() const { return value_; } + + int value_ = 1; + }; + + constexpr priority low_priority{0}; + constexpr priority normal_priority{1}; + constexpr priority high_priority{2}; + +} // namespace custom_props + +class priority_scheduler { public: // A class that satisfies the Executor requirements. class executor_type { public: - executor_type(priority_scheduler& ctx, int pri) noexcept - : context_(ctx), priority_(pri) + executor_type(priority_scheduler& ctx) noexcept + : context_(ctx), priority_(custom_props::normal_priority.value()) { } - priority_scheduler& context() const noexcept + priority_scheduler& query(execution::context_t) const noexcept { return context_; } - void on_work_started() const noexcept - { - // This executor doesn't count work. Instead, the scheduler simply runs - // until explicitly stopped. - } - - void on_work_finished() const noexcept + int query(custom_props::priority) const noexcept { - // This executor doesn't count work. Instead, the scheduler simply runs - // until explicitly stopped. + return priority_; } - template <class Func, class Alloc> - void dispatch(Func&& f, const Alloc& a) const + executor_type require(custom_props::priority pri) const { - post(std::forward<Func>(f), a); + executor_type new_ex(*this); + new_ex.priority_ = pri.value(); + return new_ex; } - template <class Func, class Alloc> - void post(Func f, const Alloc& a) const + template <class Func> + void execute(Func f) const { - auto p(std::allocate_shared<item<Func>>( - typename std::allocator_traits< - Alloc>::template rebind_alloc<char>(a), - priority_, std::move(f))); + auto p(std::make_shared<item<Func>>(priority_, std::move(f))); std::lock_guard<std::mutex> lock(context_.mutex_); context_.queue_.push(p); context_.condition_.notify_one(); } - template <class Func, class Alloc> - void defer(Func&& f, const Alloc& a) const - { - post(std::forward<Func>(f), a); - } - friend bool operator==(const executor_type& a, const executor_type& b) noexcept { @@ -78,9 +86,9 @@ public: int priority_; }; - executor_type get_executor(int pri = 0) noexcept + executor_type executor() noexcept { - return executor_type(*const_cast<priority_scheduler*>(this), pri); + return executor_type(*const_cast<priority_scheduler*>(this)); } void run() @@ -152,16 +160,22 @@ private: int main() { priority_scheduler sched; - auto low = sched.get_executor(0); - auto med = sched.get_executor(1); - auto high = sched.get_executor(2); - dispatch(low, []{ std::cout << "1\n"; }); - dispatch(low, []{ std::cout << "11\n"; }); - dispatch(med, []{ std::cout << "2\n"; }); - dispatch(med, []{ std::cout << "22\n"; }); - dispatch(high, []{ std::cout << "3\n"; }); - dispatch(high, []{ std::cout << "33\n"; }); - dispatch(high, []{ std::cout << "333\n"; }); - dispatch(sched.get_executor(-1), [&]{ sched.stop(); }); + auto ex = sched.executor(); + auto prefer_low = asio::prefer(ex, custom_props::low_priority); + auto low = asio::require(ex, custom_props::low_priority); + auto med = asio::require(ex, custom_props::normal_priority); + auto high = asio::require(ex, custom_props::high_priority); + execution::any_executor<custom_props::priority> poly_high(high); + execution::execute(prefer_low, []{ std::cout << "1\n"; }); + execution::execute(low, []{ std::cout << "11\n"; }); + execution::execute(low, []{ std::cout << "111\n"; }); + execution::execute(med, []{ std::cout << "2\n"; }); + execution::execute(med, []{ std::cout << "22\n"; }); + execution::execute(high, []{ std::cout << "3\n"; }); + execution::execute(high, []{ std::cout << "33\n"; }); + execution::execute(high, []{ std::cout << "333\n"; }); + execution::execute(poly_high, []{ std::cout << "3333\n"; }); + execution::execute(asio::require(ex, custom_props::priority{-1}), [&]{ sched.stop(); }); sched.run(); + std::cout << "polymorphic query result = " << asio::query(poly_high, custom_props::priority{}) << "\n"; } |