NDEVR
API Documentation
BasicThread.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: BasicThread
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/Thread.h>
34#include "DLLInfo.h"
35#include <NDEVR/ConcurrentOperation.h>
36#include <functional>
37namespace NDEVR
38{
39
40#if NDEVR_SUPPORTS_THREADING
46 class NDEVR_BASE_API BasicThread : public Thread
47 {
48 static uint04 s_os_number_of_threads;
49 public:
56 explicit BasicThread(const StringView& name, const bool& is_loop, const std::function<void()>& callback);
60 void run() override
61 {
62 m_function();
63 }
64
69 static void splitThread(const std::function<void()>& callback_a, const std::function<void()>& callback_b);
76 static void splitThread(const StringView& split_name, const std::function<void()>& callback_a, const std::function<void()>& callback_b);
77
86 template<uint04 t_max_number_of_threads>
87 static typename std::enable_if<t_max_number_of_threads <= 32, void>::type ThreadLoop(uint04 number_of_loops, const std::function<void(uint04 index)>& callback)
88 {
89 volatile uint04 loops_performed = 0;
90 switch (getMin(t_max_number_of_threads, number_of_loops, s_os_number_of_threads))
91 {
92 case 0: break;
93 case 1: ThreadLoop<1>(number_of_loops, loops_performed, callback); break;
94 case 2: ThreadLoop<2>(number_of_loops, loops_performed, callback); break;
95 case 3: ThreadLoop<3>(number_of_loops, loops_performed, callback); break;
96 case 4: ThreadLoop<4>(number_of_loops, loops_performed, callback); break;
97 case 5: ThreadLoop<5>(number_of_loops, loops_performed, callback); break;
98 case 6: ThreadLoop<6>(number_of_loops, loops_performed, callback); break;
99 case 7: ThreadLoop<7>(number_of_loops, loops_performed, callback); break;
100 case 8: ThreadLoop<8>(number_of_loops, loops_performed, callback); break;
101 case 9: ThreadLoop<9>(number_of_loops, loops_performed, callback); break;
102 case 10: ThreadLoop<10>(number_of_loops, loops_performed, callback); break;
103 case 11: ThreadLoop<11>(number_of_loops, loops_performed, callback); break;
104 case 12: ThreadLoop<12>(number_of_loops, loops_performed, callback); break;
105 case 13: ThreadLoop<13>(number_of_loops, loops_performed, callback); break;
106 case 14: ThreadLoop<14>(number_of_loops, loops_performed, callback); break;
107 case 15: ThreadLoop<15>(number_of_loops, loops_performed, callback); break;
108 case 16: ThreadLoop<16>(number_of_loops, loops_performed, callback); break;
109 case 32: ThreadLoop<32>(number_of_loops, loops_performed, callback); break;
110 default: lib_assert(false, "Invalid thread count"); return;
111 }
112 }
113
122 template<uint04 t_max_number_of_threads>
123 static void ThreadLoop(uint04 number_of_loops, volatile uint04& loops_performed, const std::function<void(uint04 index)>& callback)
124 {
125 if constexpr (t_max_number_of_threads == 1)
126 {
127 while (loops_performed < number_of_loops)
128 {
129 uint04 loop = ConcurrentOperation::increment(loops_performed);
130 if(loop - 1 < number_of_loops)
131 callback(loop - 1);
132 }
133 }
134 else
135 {
137 [&callback, &loops_performed, number_of_loops]
138 {
139 ThreadLoop<t_max_number_of_threads / 2>(number_of_loops, loops_performed, callback);
140 },
141 [&callback, &loops_performed, number_of_loops]
142 {
143 ThreadLoop<t_max_number_of_threads - (t_max_number_of_threads / 2)>(number_of_loops, loops_performed, callback);
144 });
145 }
146 }
147 protected:
148 std::function<void()> m_function;
149 };
150#endif
151}
152
static std::enable_if< t_max_number_of_threads<=32, void >::type ThreadLoop(uint04 number_of_loops, const std::function< void(uint04 index)> &callback)
Distributes a loop across multiple threads up to t_max_number_of_threads.
Definition BasicThread.h:87
BasicThread(const StringView &name, const bool &is_loop, const std::function< void()> &callback)
Constructs a BasicThread with a name, loop flag, and callback function.
static void splitThread(const std::function< void()> &callback_a, const std::function< void()> &callback_b)
Splits execution into two concurrent threads and waits for both to complete.
std::function< void()> m_function
The callback function executed by this thread.
static void ThreadLoop(uint04 number_of_loops, volatile uint04 &loops_performed, const std::function< void(uint04 index)> &callback)
Executes a loop distributed across a fixed number of threads using recursive splitting.
void run() override
Executes the stored callback function.
Definition BasicThread.h:60
static void splitThread(const StringView &split_name, const std::function< void()> &callback_a, const std::function< void()> &callback_b)
Splits execution into two named concurrent threads and waits for both to complete.
static uint04 increment(volatile uint04 &value)
Atomically increments a shared value by one.
The core String View class for the NDEVR API.
Definition StringView.h:58
Thread()
Constructs a Thread with a default name.
The primary namespace for the NDEVR SDK.
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...