|
2 | 2 | #include <atomic>
|
3 | 3 | #include <memory>
|
4 | 4 |
|
5 |
| -static std::atomic<coro::default_executor*> s_default_executor = {nullptr}; |
| 5 | +static coro::thread_pool::options s_default_executor_options; |
| 6 | +static std::atomic<std::shared_ptr<coro::thread_pool>> s_default_executor = {nullptr}; |
6 | 7 |
|
7 | 8 | #ifdef LIBCORO_FEATURE_NETWORKING
|
8 |
| -coro::io_scheduler::options coro::default_executor::s_io_scheduler_options; |
9 |
| -#else |
10 |
| -coro::thread_pool::options coro::default_executor::s_thread_pool_options; |
| 9 | +static coro::io_scheduler::options s_default_io_executor_options; |
| 10 | +static std::atomic<std::shared_ptr<coro::io_scheduler>> s_default_io_executor = {nullptr}; |
11 | 11 | #endif
|
12 | 12 |
|
13 |
| -coro::default_executor* coro::default_executor::instance() |
| 13 | +void coro::default_executor::set_executor_options(thread_pool::options thread_pool_options) |
14 | 14 | {
|
15 |
| - auto* result = s_default_executor.load(std::memory_order::acquire); |
| 15 | + s_default_executor_options = thread_pool_options; |
| 16 | +} |
| 17 | + |
| 18 | +std::shared_ptr<coro::thread_pool> coro::default_executor::executor() |
| 19 | +{ |
| 20 | + auto result = s_default_executor.load(std::memory_order::acquire); |
16 | 21 | if (!result)
|
17 | 22 | {
|
18 |
| - auto ptr = std::make_unique<default_executor>(); |
| 23 | + auto ptr = std::make_shared<coro::thread_pool>(s_default_executor_options); |
19 | 24 | if (s_default_executor.compare_exchange_strong(
|
20 |
| - result, ptr.get(), std::memory_order::release, std::memory_order::acquire)) |
21 |
| - result = ptr.release(); |
| 25 | + result, ptr, std::memory_order::release, std::memory_order::acquire)) |
| 26 | + result = ptr; |
22 | 27 | }
|
23 | 28 | return result;
|
24 | 29 | }
|
25 | 30 |
|
26 |
| -coro::default_executor::default_executor() |
27 |
| -{ |
28 |
| -#ifdef LIBCORO_FEATURE_NETWORKING |
29 |
| - m_io_scheduler = io_scheduler::make_shared(s_io_scheduler_options); |
30 |
| -#else |
31 |
| - m_thread_pool = std::make_shared<thread_pool>(s_thread_pool_options); |
32 |
| -#endif |
33 |
| -} |
34 |
| - |
35 |
| -bool coro::default_executor::spawn(coro::task<void>&& task) noexcept |
36 |
| -{ |
37 |
| -#ifdef LIBCORO_FEATURE_NETWORKING |
38 |
| - return m_io_scheduler->spawn(std::move(task)); |
39 |
| -#else |
40 |
| - return m_thread_pool->spawn(std::move(task)); |
41 |
| -#endif |
42 |
| -} |
43 |
| - |
44 |
| -bool coro::default_executor::resume(std::coroutine_handle<> handle) |
45 |
| -{ |
46 |
| -#ifdef LIBCORO_FEATURE_NETWORKING |
47 |
| - return m_io_scheduler->resume(handle); |
48 |
| -#else |
49 |
| - return m_thread_pool->resume(handle); |
50 |
| -#endif |
51 |
| -} |
52 |
| - |
53 |
| -std::size_t coro::default_executor::size() |
54 |
| -{ |
55 |
| -#ifdef LIBCORO_FEATURE_NETWORKING |
56 |
| - return m_io_scheduler->size(); |
57 |
| -#else |
58 |
| - return m_thread_pool->size(); |
59 |
| -#endif |
60 |
| -} |
61 |
| - |
62 |
| -bool coro::default_executor::empty() |
| 31 | +void coro::default_executor::set_io_executor_options(io_scheduler::options io_scheduler_options) |
63 | 32 | {
|
64 |
| -#ifdef LIBCORO_FEATURE_NETWORKING |
65 |
| - return m_io_scheduler->empty(); |
66 |
| -#else |
67 |
| - return m_thread_pool->empty(); |
68 |
| -#endif |
| 33 | + s_default_io_executor_options = io_scheduler_options; |
69 | 34 | }
|
70 | 35 |
|
71 |
| -void coro::default_executor::shutdown() |
| 36 | +std::shared_ptr<coro::io_scheduler> coro::default_executor::io_executor() |
72 | 37 | {
|
73 |
| -#ifdef LIBCORO_FEATURE_NETWORKING |
74 |
| - m_io_scheduler->shutdown(); |
75 |
| -#else |
76 |
| - m_thread_pool->shutdown(); |
77 |
| -#endif |
78 |
| -} |
79 |
| - |
80 |
| -#ifdef LIBCORO_FEATURE_NETWORKING |
81 |
| -auto coro::default_executor::schedule() -> coro::io_scheduler::schedule_operation |
82 |
| -{ |
83 |
| - return m_io_scheduler->schedule(); |
84 |
| -} |
85 |
| -#else |
86 |
| -auto coro::default_executor::schedule() -> coro::thread_pool::operation |
87 |
| -{ |
88 |
| - return m_thread_pool->schedule(); |
89 |
| -} |
90 |
| -#endif |
91 |
| - |
92 |
| -#ifdef LIBCORO_FEATURE_NETWORKING |
93 |
| - |
94 |
| -void coro::default_executor::set_io_scheduler_options(io_scheduler::options io_scheduler_options) |
95 |
| -{ |
96 |
| - s_io_scheduler_options = io_scheduler_options; |
97 |
| -} |
98 |
| - |
99 |
| -std::shared_ptr<coro::io_scheduler> coro::default_executor::get_io_scheduler() |
100 |
| -{ |
101 |
| - return m_io_scheduler; |
102 |
| -} |
103 |
| - |
104 |
| -#else |
105 |
| - |
106 |
| -void coro::default_executor::set_thread_pool_options(thread_pool::options thread_pool_options) |
107 |
| -{ |
108 |
| - s_thread_pool_options = thread_pool_options; |
109 |
| -} |
110 |
| - |
111 |
| -std::shared_ptr<coro::thread_pool> coro::default_executor::get_thread_pool() |
112 |
| -{ |
113 |
| - return m_thread_pool; |
| 38 | + auto result = s_default_io_executor.load(std::memory_order::acquire); |
| 39 | + if (!result) |
| 40 | + { |
| 41 | + auto ptr = coro::io_scheduler::make_shared(s_default_io_executor_options); |
| 42 | + if (s_default_io_executor.compare_exchange_strong( |
| 43 | + result, ptr, std::memory_order::release, std::memory_order::acquire)) |
| 44 | + result = ptr; |
| 45 | + } |
| 46 | + return result; |
114 | 47 | }
|
115 |
| - |
116 |
| -#endif |
0 commit comments