API Documentation
Loading...
Searching...
No Matches
ConcurrentOperation.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: 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{
39 /**----------------------------------------------------------------------------
40 \brief Contains thread-safe operations for inter-thread logic
41 Increment and Decrement can be used to safely add to a shared uint04 value.
42 Also contains sort, wait, and notify functions.
43 ----------------------------------------------------------------------------*/
45 {
46 public:
47 static uint04 increment(volatile uint04& value);
48 static uint04 decrement(volatile uint04& value);
49 static void wait(void* object, std::unique_lock<std::mutex>& lock);
50 static void wait(void* object, uint08 time, std::unique_lock<std::mutex>& lock);
51 static void notify(void* object);
52 static void notifyAll(void* object);
53 template<class t_type, class t_function_type>
54 static void ParallelSort(t_type* data, uint04 len, t_function_type function)
55 {
56 ParallelGrainSort(data, len, getMax(1024U, len / 16U), function);
57 }
58 template<class t_type, class t_function_type>
59 static void ParallelGrainSort(t_type* data, uint04 len, uint04 grain_size, const t_function_type& function)
60 {
61 // Use grainsize instead of thread count so that we don't e.g.
62 // spawn 4 threads just to sort 8 elements.
63 if (len < grain_size)
64 {
65 std::sort(data, data + len, function);
66 }
67 else
68 {
69 auto future = std::async(ParallelGrainSort<t_type, t_function_type>, data, len / 2, grain_size, function);
70
71 // No need to spawn another thread just to block the calling
72 // thread which would do nothing.
73 ParallelGrainSort(data + len / 2, len - len / 2, grain_size, function);
74
75 future.wait();
76
77 std::inplace_merge(data, data + len / 2, data + len, function);
78 }
79 }
80 };
81}
82
#define NDEVR_BASE_API
Definition DLLInfo.h:57
Contains thread-safe operations for inter-thread logic Increment and Decrement can be used to safely ...
Definition ConcurrentOperation.h:45
static uint04 decrement(volatile uint04 &value)
static void wait(void *object, std::unique_lock< std::mutex > &lock)
static void notify(void *object)
static void ParallelSort(t_type *data, uint04 len, t_function_type function)
Definition ConcurrentOperation.h:54
static void notifyAll(void *object)
static void wait(void *object, uint08 time, std::unique_lock< std::mutex > &lock)
static uint04 increment(volatile uint04 &value)
static void ParallelGrainSort(t_type *data, uint04 len, uint04 grain_size, const t_function_type &function)
Definition ConcurrentOperation.h:59
Definition ACIColor.h:37
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 > ...
Definition BaseFunctions.hpp:94
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
Definition BaseValues.hpp:106
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96