NDEVR
API Documentation
ConcurrentOperation.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: ConcurrentOperation
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include "DLLInfo.h"
35#include <mutex>
36#include <future>
37namespace NDEVR
38{
46 class NDEVR_BASE_API ConcurrentOperation
47 {
48 public:
54 static uint04 increment(volatile uint04& value);
60 static uint04 decrement(volatile uint04& value);
66 static void wait(void* object, std::unique_lock<std::mutex>& lock);
73 static void wait(void* object, uint08 time, std::unique_lock<std::mutex>& lock);
78 static void notify(void* object);
83 static void notifyAll(void* object);
90 template<class t_type, class t_function_type>
91 static void ParallelSort(t_type* data, uint04 len, t_function_type function)
92 {
93 ParallelGrainSort(data, len, getMax(1024U, len / 16U), function);
94 }
95
103 template<class t_type, class t_function_type>
104 static void ParallelGrainSort(t_type* data, uint04 len, uint04 grain_size, const t_function_type& function)
105 {
106 // Use grainsize instead of thread count so that we don't e.g.
107 // spawn 4 threads just to sort 8 elements.
108 if (len < grain_size)
109 {
110 std::sort(data, data + len, function);
111 }
112 else
113 {
114 auto future = std::async(ParallelGrainSort<t_type, t_function_type>, data, len / 2, grain_size, function);
115
116 // No need to spawn another thread just to block the calling
117 // thread which would do nothing.
118 ParallelGrainSort(data + len / 2, len - len / 2, grain_size, function);
119
120 future.wait();
121
122 std::inplace_merge(data, data + len / 2, data + len, function);
123 }
124 }
125 };
126}
127
Contains thread-safe operations for inter-thread logic Increment and Decrement can be used to safely ...
static void wait(void *object, std::unique_lock< std::mutex > &lock)
Blocks the calling thread until notified on the given object.
static void ParallelGrainSort(t_type *data, uint04 len, uint04 grain_size, const t_function_type &function)
Recursively sorts an array in parallel, splitting work into async tasks when the array exceeds the gr...
static uint04 decrement(volatile uint04 &value)
Atomically decrements a shared value by one.
static void notifyAll(void *object)
Wakes all threads waiting on the given object.
static void wait(void *object, uint08 time, std::unique_lock< std::mutex > &lock)
Blocks the calling thread until notified or until the timeout elapses.
static void ParallelSort(t_type *data, uint04 len, t_function_type function)
Sorts an array in parallel using a default grain size.
static void notify(void *object)
Wakes one thread waiting on the given object.
static uint04 increment(volatile uint04 &value)
Atomically increments a shared value by one.
The primary namespace for the NDEVR SDK.
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...