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{
40 {
41 public:
42 static uint04 increment(volatile uint04& value);
43 static uint04 decrement(volatile uint04& value);
44 static void wait(void* object, std::unique_lock<std::mutex>& lock);
45 static void wait(void* object, uint08 time, std::unique_lock<std::mutex>& lock);
46 static void notify(void* object);
47 static void notifyAll(void* object);
48 template<class t_type, class t_function_type>
49 static void ParallelSort(t_type* data, uint04 len, t_function_type function)
50 {
51 ParallelGrainSort(data, len, getMax(1024U, len / 16U), function);
52 }
53 template<class t_type, class t_function_type>
54 static void ParallelGrainSort(t_type* data, uint04 len, uint04 grain_size, const t_function_type& function)
55 {
56 // Use grainsize instead of thread count so that we don't e.g.
57 // spawn 4 threads just to sort 8 elements.
58 if (len < grain_size)
59 {
60 std::sort(data, data + len, function);
61 }
62 else
63 {
64 auto future = std::async(ParallelGrainSort<t_type, t_function_type>, data, len / 2, grain_size, function);
65
66 // No need to spawn another thread just to block the calling
67 // thread which would do nothing.
68 ParallelGrainSort(data + len / 2, len - len / 2, grain_size, function);
69
70 future.wait();
71
72 std::inplace_merge(data, data + len / 2, data + len, function);
73 }
74 }
75 };
76}
77
#define NDEVR_BASE_API
Definition DLLInfo.h:78
Definition ConcurrentOperation.h:40
static void ParallelSort(t_type *data, uint04 len, t_function_type function)
Definition ConcurrentOperation.h:49
static void ParallelGrainSort(t_type *data, uint04 len, uint04 grain_size, const t_function_type &function)
Definition ConcurrentOperation.h:54
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.
Definition BaseFunctions.hpp:116
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer -Can represent exact integer values 0 thro...
Definition BaseValues.hpp:132
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120