| 1 | /* |
| 2 | Copyright (C) 2022-2024 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "grn.h"cxx . . $ `<file>/lib/grn.h`/ |
| 22 | |
| 23 | #ifdef GRN_WITH_APACHE_ARROW |
| 24 | # include "grn_arrow.hpp" |
| 25 | # include <arrow/util/thread_pool.h> |
| 26 | # include <mutex> |
| 27 | # include <unordered_map> |
| 28 | #endif |
| 29 | |
| 30 | namespace grncxx . . $ grn/ { |
| 31 | class TaskExecutorcxx . . $ grn/TaskExecutor# { |
| 32 | private: |
| 33 | grn_ctx *ctx_cxx . . $ grn/TaskExecutor#ctx_.; |
| 34 | uint32_tcxx . . $ uint32_t# n_workers_cxx . . $ grn/TaskExecutor#n_workers_.; |
| 35 | #ifdef GRN_WITH_APACHE_ARROW |
| 36 | std::shared_ptr<::arrow::internal::ThreadPool> thread_pool_; |
| 37 | std::unordered_map<uintptr_t, ::arrow::Future<bool>> futures_; |
| 38 | std::mutex futures_mutex_; |
| 39 | #endif |
| 40 | |
| 41 | public: |
| 42 | TaskExecutorcxx . . $ grn/TaskExecutor#TaskExecutor(b578cd6e53d5fe91).(grn_ctx *ctx) |
| 43 | : ctx_cxx . . $ grn/TaskExecutor#ctx_.(ctx), |
| 44 | n_workers_cxx . . $ grn/TaskExecutor#n_workers_.(0) |
| 45 | #ifdef GRN_WITH_APACHE_ARROW |
| 46 | , |
| 47 | thread_pool_(nullptr), |
| 48 | futures_(), |
| 49 | futures_mutex_() |
| 50 | #endif |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | set_n_workerscxx . . $ grn/TaskExecutor#set_n_workers(d4f767463ce0a6b3).(int32_t n_workers) |
| 56 | { |
| 57 | #ifdef GRN_WITH_APACHE_ARROW |
| 58 | if (n_workers < 0) { |
| 59 | n_workers = ::arrow::internal::ThreadPool::DefaultCapacity(); |
| 60 | } |
| 61 | if (static_cast<uint32_t>(n_workers) == n_workers_) { |
| 62 | return; |
| 63 | } |
| 64 | n_workers_ = n_workers; |
| 65 | if (n_workers_ > 1) { |
| 66 | if (thread_pool_) { |
| 67 | if (!thread_pool_->SetCapacity(n_workers_).ok()) { |
| 68 | n_workers_ = 0; |
| 69 | } |
| 70 | } else { |
| 71 | auto thread_pool_result = |
| 72 | ::arrow::internal::ThreadPool::MakeEternal(n_workers_); |
| 73 | if (thread_pool_result.ok()) { |
| 74 | thread_pool_ = *thread_pool_result; |
| 75 | } else { |
| 76 | n_workers_ = 0; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | uint32_tcxx . . $ uint32_t# |
| 84 | get_n_workerscxx . . $ grn/TaskExecutor#get_n_workers(db52760e79bf9054).() |
| 85 | { |
| 86 | return n_workers_cxx . . $ grn/TaskExecutor#n_workers_.; |
| 87 | } |
| 88 | |
| 89 | bool |
| 90 | is_parallelcxx . . $ grn/TaskExecutor#is_parallel(c9857a73d68e59ba).() |
| 91 | { |
| 92 | #ifdef GRN_WITH_APACHE_ARROW |
| 93 | if (n_workers_ > 1) { |
| 94 | return true; |
| 95 | } |
| 96 | #endif |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | template <typename Function> |
| 101 | bool |
| 102 | executecxx . . $ grn/TaskExecutor#execute(61277e778d1aa7c1).(uintptr_tcxx . . $ uintptr_t# id, Function &&func, const char *tag) |
| 103 | { |
| 104 | #ifdef GRN_WITH_APACHE_ARROW |
| 105 | if (is_parallel()) { |
| 106 | auto future_result = thread_pool_->Submit(func); |
| 107 | if (!grnarrow::check(ctx_, |
| 108 | future_result, |
| 109 | tag, |
| 110 | " failed to submit a job")) { |
| 111 | return false; |
| 112 | } |
| 113 | { |
| 114 | std::unique_lock<std::mutex> lock(futures_mutex_); |
| 115 | futures_.emplace(id, *future_result); |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | #endif |
| 120 | return func(); |
| 121 | } |
| 122 | |
| 123 | bool |
| 124 | waitcxx . . $ grn/TaskExecutor#wait(88440efc1724c3fd).(uintptr_tcxx . . $ uintptr_t# id, const char *tag) |
| 125 | { |
| 126 | #ifdef GRN_WITH_APACHE_ARROW |
| 127 | if (is_parallel()) { |
| 128 | try { |
| 129 | std::unique_lock<std::mutex> lock(futures_mutex_); |
| 130 | auto future = futures_.at(id); |
| 131 | lock.unlock(); |
| 132 | auto status = future.status(); |
| 133 | lock.lock(); |
| 134 | futures_.erase(id); |
| 135 | lock.unlock(); |
| 136 | return grnarrow::check(ctx_, |
| 137 | status, |
| 138 | tag, |
| 139 | " failed to wait a job: ", |
| 140 | id); |
| 141 | } catch (std::out_of_range &) { |
| 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | #endif |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | bool |
| 150 | wait_allcxx . . $ grn/TaskExecutor#wait_all(c9857a73d68e59ba).() |
| 151 | { |
| 152 | #ifdef GRN_WITH_APACHE_ARROW |
| 153 | if (is_parallel()) { |
| 154 | thread_pool_->WaitForIdle(); |
| 155 | return ctx_->rc == GRN_SUCCESS; |
| 156 | } |
| 157 | #endif |
| 158 | return true; |
| 159 | } |
| 160 | }; |
| 161 | } // namespace grn |