API Documentation
Loading...
Searching...
No Matches
BasicThread.h
Go to the documentation of this file.
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#if NDEVR_SUPPORTS_THREADING
40 /**----------------------------------------------------------------------------
41 \brief A class that allows user to use a function inline to execute something
42 concurrently.
43 ----------------------------------------------------------------------------*/
45 {
46 public:
47 explicit BasicThread(const String& name, const bool& is_loop, const std::function<void()>& callback);
48 void run() override
49 {
50 m_function();
51 }
52 static void splitThread(const std::function<void()>& callback_a, const std::function<void()>& callback_b);
53 static void splitThread(const String& split_name, const std::function<void()>& callback_a, const std::function<void()>& callback_b);
54
55 template<uint04 t_max_number_of_threads>
56 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)
57 {
58 volatile uint04 loops_performed = 0;
59 switch (getMin(t_max_number_of_threads, number_of_loops))
60 {
61 case 0: break;
62 case 1: ThreadLoop<1>(number_of_loops, loops_performed, callback); break;
63 case 2: ThreadLoop<2>(number_of_loops, loops_performed, callback); break;
64 case 3: ThreadLoop<3>(number_of_loops, loops_performed, callback); break;
65 case 4: ThreadLoop<4>(number_of_loops, loops_performed, callback); break;
66 case 5: ThreadLoop<5>(number_of_loops, loops_performed, callback); break;
67 case 6: ThreadLoop<6>(number_of_loops, loops_performed, callback); break;
68 case 7: ThreadLoop<7>(number_of_loops, loops_performed, callback); break;
69 case 8: ThreadLoop<8>(number_of_loops, loops_performed, callback); break;
70 case 9: ThreadLoop<9>(number_of_loops, loops_performed, callback); break;
71 case 10: ThreadLoop<10>(number_of_loops, loops_performed, callback); break;
72 case 11: ThreadLoop<11>(number_of_loops, loops_performed, callback); break;
73 case 12: ThreadLoop<12>(number_of_loops, loops_performed, callback); break;
74 case 13: ThreadLoop<13>(number_of_loops, loops_performed, callback); break;
75 case 14: ThreadLoop<14>(number_of_loops, loops_performed, callback); break;
76 case 15: ThreadLoop<15>(number_of_loops, loops_performed, callback); break;
77 case 16: ThreadLoop<16>(number_of_loops, loops_performed, callback); break;
78 case 32: ThreadLoop<32>(number_of_loops, loops_performed, callback); break;
79 default: lib_assert(false, "Invalid thread count"); return;
80 }
81 }
82 template<uint04 t_max_number_of_threads>
83 static void ThreadLoop(uint04 number_of_loops, volatile uint04& loops_performed, const std::function<void(uint04 index)>& callback)
84 {
85 if constexpr (t_max_number_of_threads == 1)
86 {
87 while (loops_performed < number_of_loops)
88 {
89 uint04 loop = ConcurrentOperation::increment(loops_performed);
90 if(loop - 1 < number_of_loops)
91 callback(loop - 1);
92 }
93 }
94 else
95 {
96 splitThread(
97 [&callback, &loops_performed, number_of_loops]
98 {
99 ThreadLoop<t_max_number_of_threads / 2>(number_of_loops, loops_performed, callback);
100 },
101 [&callback, &loops_performed, number_of_loops]
102 {
103 ThreadLoop<t_max_number_of_threads - (t_max_number_of_threads / 2)>(number_of_loops, loops_performed, callback);
104 });
105 }
106 }
107 protected:
108 std::function<void()> m_function;
109 };
110#endif
111}
112
#define NDEVR_BASE_API
Definition DLLInfo.h:78
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:68
A class that allows user to use a function inline to execute something.
Definition BasicThread.h:45
std::function< void()> m_function
Definition BasicThread.h:108
static void splitThread(const String &split_name, const std::function< void()> &callback_a, const std::function< void()> &callback_b)
static void ThreadLoop(uint04 number_of_loops, volatile uint04 &loops_performed, const std::function< void(uint04 index)> &callback)
Definition BasicThread.h:83
static std::enable_if< t_max_number_of_threads<=32, void >::type ThreadLoop(uint04 number_of_loops, const std::function< void(uint04 index)> &callback)
Definition BasicThread.h:56
void run() override
Definition BasicThread.h:48
static void splitThread(const std::function< void()> &callback_a, const std::function< void()> &callback_b)
BasicThread(const String &name, const bool &is_loop, const std::function< void()> &callback)
The core String class for the software.
Definition String.h:47
A thread is a single sequence stream within the software.
Definition Thread.h:66
Definition ACIColor.h:37
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:94
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...
Definition BaseFunctions.hpp:56