asynchrony 0.0.0
Add asynchrony to your C++ applications using standard C++20
Loading...
Searching...
No Matches
roundrobin_pool.hpp
1/*
2 roundrobin pool : Add asynchrony to your apps
3
4 BSD 3-Clause License
5
6 Copyright (c) 2021, Siddiq Software LLC
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 1. Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
14
15 2. Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 3. Neither the name of the copyright holder nor the names of its
20 contributors may be used to endorse or promote products derived from
21 this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#pragma once
36#ifndef ROUNDROBIN_POOL_HPP
37#define ROUNDROBIN_POOL_HPP
38
39#include <concepts>
40#include <deque>
41#include "simple_worker.hpp"
42
43
44namespace siddiqsoft
45{
51 template <typename T, uint16_t N = 0>
52 requires std::is_move_constructible_v<T>
53 struct roundrobin_pool
54 {
55 public:
56 roundrobin_pool(roundrobin_pool&&) = delete;
57 auto operator=(roundrobin_pool&&) = delete;
58 roundrobin_pool(roundrobin_pool&) = delete;
59 auto operator=(roundrobin_pool&) = delete;
60
61
64 roundrobin_pool(std::function<void(T&&)> c)
65 {
66 // Create as many threads as reported by the system.
67 // std::deque does not relocate elements on growth, so non-movable types are safe.
68 for (unsigned i = 0; i < ((N > 0) ? N : std::thread::hardware_concurrency()); i++) {
69 workers.emplace_back(c);
70 }
71
72 // Shortcut; save the size of the array
73 workersSize = workers.size();
74 }
75
82 void queue(T&& item)
83 {
84 // Add into the thread's internal queue using round-robin index
85 // Atomic fetch_add to ensures each thread gets a unique index
86 // and cast to size_t to avoid type mismatch issues
87 size_t idx = nextWorkerIndex();
88 workers.at(idx).queue(std::move(item));
89 // Increment the queue counter with release semantics for visibility
90 queueCounter.fetch_add(1, std::memory_order_release);
91 }
92
93#if defined(NLOHMANN_JSON_VERSION_MAJOR)
97 nlohmann::json toJson() const
98 {
99 return {{"_typver", "siddiqsoft.asynchrony-lib.roundrobin_pool/0.10"},
100 {"workersSize", workersSize},
101 {"queueCounter", queueCounter.load(std::memory_order_acquire)}};
102 }
103#endif
104
105#ifdef _DEBUG
106 public:
107 std::atomic_uint64_t queueCounter {0};
108#else
109 private:
110 std::atomic_uint64_t queueCounter {0};
111#endif
112
113 private:
117 std::deque<simple_worker<T>> workers {};
118
120 uint64_t workersSize {};
121
127 size_t nextWorkerIndex()
128 {
129 if (workersSize == 0) return 0;
130 return static_cast<size_t>(queueCounter.load(std::memory_order_acquire) % workersSize);
131 }
132 };
133
134#if defined(NLOHMANN_JSON_VERSION_MAJOR)
139 template <typename T, uint16_t N = 0>
140 static void to_json(nlohmann::json& dest, const siddiqsoft::roundrobin_pool<T, N>& src)
141 {
142 dest = src.toJson();
143 }
144#endif
145
146} // namespace siddiqsoft
147#endif // !ROUNDROBIN_POOL_HPP
roundrobin_pool(std::function< void(T &&)> c)
Consturcts a deque of simple_worker<T> with the given callback.
void queue(T &&item)
Queue item into one of the thread's queue.