|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "bthread/worker_idle.h" |
| 19 | + |
| 20 | +#include <errno.h> |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <new> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "butil/atomicops.h" |
| 27 | +#include "butil/containers/doubly_buffered_data.h" |
| 28 | +#include "butil/time.h" |
| 29 | +#include "butil/thread_local.h" |
| 30 | + |
| 31 | +namespace bthread { |
| 32 | +namespace { |
| 33 | + |
| 34 | +enum InitState : uint8_t { |
| 35 | + INIT_STATE_NOT_RUN = 0, |
| 36 | + INIT_STATE_OK = 1, |
| 37 | + INIT_STATE_FAILED = 2, |
| 38 | +}; |
| 39 | + |
| 40 | +struct WorkerIdleEntry { |
| 41 | + int id; |
| 42 | + int (*init_fn)(void); |
| 43 | + bool (*idle_fn)(void); |
| 44 | + uint64_t timeout_us; |
| 45 | +}; |
| 46 | + |
| 47 | +typedef std::vector<WorkerIdleEntry> WorkerIdleEntryList; |
| 48 | + |
| 49 | +static butil::DoublyBufferedData<WorkerIdleEntryList, butil::Void, true> g_entries; |
| 50 | +static butil::atomic<int> g_next_id(1); |
| 51 | + |
| 52 | +struct WorkerIdleTLS { |
| 53 | + std::vector<uint8_t> init_states; |
| 54 | +}; |
| 55 | + |
| 56 | +BAIDU_THREAD_LOCAL WorkerIdleTLS* tls_worker_idle = NULL; |
| 57 | + |
| 58 | +static WorkerIdleTLS* get_or_create_tls() { |
| 59 | + if (tls_worker_idle) { |
| 60 | + return tls_worker_idle; |
| 61 | + } |
| 62 | + tls_worker_idle = new (std::nothrow) WorkerIdleTLS; |
| 63 | + return tls_worker_idle; |
| 64 | +} |
| 65 | + |
| 66 | +} // namespace |
| 67 | + |
| 68 | +int register_worker_idle_function(int (*init_fn)(void), |
| 69 | + bool (*idle_fn)(void), |
| 70 | + uint64_t timeout_us, |
| 71 | + int* handle) { |
| 72 | + if (idle_fn == NULL) { |
| 73 | + return EINVAL; |
| 74 | + } |
| 75 | + if (timeout_us == 0) { |
| 76 | + return EINVAL; |
| 77 | + } |
| 78 | + const int id = g_next_id.fetch_add(1, butil::memory_order_relaxed); |
| 79 | + WorkerIdleEntry e; |
| 80 | + e.id = id; |
| 81 | + e.init_fn = init_fn; |
| 82 | + e.idle_fn = idle_fn; |
| 83 | + e.timeout_us = timeout_us; |
| 84 | + g_entries.Modify([&](WorkerIdleEntryList& bg) { |
| 85 | + bg.push_back(e); |
| 86 | + return static_cast<size_t>(1); |
| 87 | + }); |
| 88 | + if (handle) { |
| 89 | + *handle = id; |
| 90 | + } |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
| 94 | +int unregister_worker_idle_function(int handle) { |
| 95 | + if (handle <= 0) { |
| 96 | + return EINVAL; |
| 97 | + } |
| 98 | + size_t removed = g_entries.Modify([&](WorkerIdleEntryList& bg) { |
| 99 | + const size_t old_size = bg.size(); |
| 100 | + bg.erase(std::remove_if(bg.begin(), bg.end(), |
| 101 | + [&](const WorkerIdleEntry& e) { |
| 102 | + return e.id == handle; |
| 103 | + }), |
| 104 | + bg.end()); |
| 105 | + return old_size - bg.size(); |
| 106 | + }); |
| 107 | + return removed ? 0 : EINVAL; |
| 108 | +} |
| 109 | + |
| 110 | +bool has_worker_idle_functions() { |
| 111 | + butil::DoublyBufferedData<WorkerIdleEntryList, butil::Void, true>::ScopedPtr p; |
| 112 | + if (g_entries.Read(&p) != 0) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + return !p->empty(); |
| 116 | +} |
| 117 | + |
| 118 | +void run_worker_idle_functions() { |
| 119 | + if (!has_worker_idle_functions()) { |
| 120 | + return; |
| 121 | + } |
| 122 | + butil::DoublyBufferedData<WorkerIdleEntryList, butil::Void, true>::ScopedPtr p; |
| 123 | + if (g_entries.Read(&p) != 0) { |
| 124 | + return; |
| 125 | + } |
| 126 | + if (p->empty()) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + WorkerIdleTLS* tls = get_or_create_tls(); |
| 131 | + if (tls == NULL) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + // Step 1: Ensure per-worker init is called at most once for each entry. |
| 136 | + // Step 2: Run idle callbacks for initialized entries. |
| 137 | + // Step 3: Ignore callback return values. The caller decides how to proceed. |
| 138 | + for (const auto& e : *p) { |
| 139 | + if (e.id <= 0 || e.idle_fn == NULL) { |
| 140 | + continue; |
| 141 | + } |
| 142 | + if (tls->init_states.size() <= static_cast<size_t>(e.id)) { |
| 143 | + tls->init_states.resize(static_cast<size_t>(e.id) + 1, INIT_STATE_NOT_RUN); |
| 144 | + } |
| 145 | + uint8_t& st = tls->init_states[static_cast<size_t>(e.id)]; |
| 146 | + if (st == INIT_STATE_NOT_RUN) { |
| 147 | + // Run the init callback function once. |
| 148 | + if (e.init_fn) { |
| 149 | + const int rc = e.init_fn(); |
| 150 | + st = (rc == 0) ? INIT_STATE_OK : INIT_STATE_FAILED; |
| 151 | + } else { |
| 152 | + st = INIT_STATE_OK; |
| 153 | + } |
| 154 | + } |
| 155 | + if (st != INIT_STATE_OK) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + // Run the idle callback function. |
| 159 | + e.idle_fn(); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +timespec get_worker_idle_timeout() { |
| 164 | + butil::DoublyBufferedData<WorkerIdleEntryList, butil::Void, true>::ScopedPtr p; |
| 165 | + if (g_entries.Read(&p) != 0) { |
| 166 | + return {0, 0}; |
| 167 | + } |
| 168 | + if (p->empty()) { |
| 169 | + return {0, 0}; |
| 170 | + } |
| 171 | + uint64_t min_us = 0; |
| 172 | + for (const auto& e : *p) { |
| 173 | + if (e.timeout_us == 0) { |
| 174 | + continue; |
| 175 | + } |
| 176 | + if (min_us == 0 || e.timeout_us < min_us) { |
| 177 | + min_us = e.timeout_us; |
| 178 | + } |
| 179 | + } |
| 180 | + if (min_us == 0) { |
| 181 | + return {0, 0}; |
| 182 | + } |
| 183 | + return butil::microseconds_to_timespec(min_us); |
| 184 | +} |
| 185 | + |
| 186 | +} // namespace bthread |
| 187 | + |
| 188 | + |
0 commit comments