-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstance.h
111 lines (94 loc) · 3.87 KB
/
instance.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* EEDI2CUDA: EEDI2 filter using CUDA
*
* Copyright (C) 2005-2006 Kevin Stone
* Copyright (C) 2014-2019 HolyWu
* Copyright (C) 2021 Misaki Kasumi
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
#include <atomic>
#include <tuple>
#include <type_traits>
#include <utility>
#include <boost/lockfree/policies.hpp>
#include <boost/lockfree/queue.hpp>
#include <boost/sync/semaphore.hpp>
#include "common.h"
namespace {
template <typename T> class Pipeline;
template <typename T> class Instance;
template <typename T> [[nodiscard]] Instance<T> *allocInstance(std::size_t num_streams);
template <typename T> class BaseInstance {
static constexpr unsigned max_num_streams = 32;
boost::sync::semaphore semaphore;
boost::lockfree::queue<unsigned, boost::lockfree::capacity<max_num_streams>> available;
Pipeline<T> *items() noexcept { return reinterpret_cast<Pipeline<T> *>(reinterpret_cast<std::size_t *>(this + 1) + 1); }
unsigned num_streams() const noexcept { return static_cast<unsigned>(*reinterpret_cast<const std::size_t *>(this + 1)); }
friend Instance<T> *allocInstance<>(std::size_t num_streams);
template <typename... Args, size_t... Indexes>
void initPipeline(Pipeline<T> *p, std::tuple<Args...> args, std::index_sequence<Indexes...>) {
new (p) Pipeline<T>(std::get<Indexes>(std::move(args))...);
}
protected:
template <typename... Args1, typename... Args2>
BaseInstance(std::tuple<Args1...> primaryPipelineArgs, std::tuple<Args2...> secondaryPipelineAdditionalArgs)
: semaphore(num_streams()) {
if (num_streams() > max_num_streams) {
throw std::runtime_error("too many streams");
}
auto items = this->items();
initPipeline(items, primaryPipelineArgs, std::index_sequence_for<Args1...>{});
available.push(0);
for (unsigned i = 1; i < num_streams(); ++i) {
initPipeline(items + i, std::tuple_cat(std::forward_as_tuple(firstReactor()), secondaryPipelineAdditionalArgs),
std::make_index_sequence<sizeof...(Args2) + 1>{});
available.push(i);
}
}
public:
~BaseInstance() {
auto items = this->items();
for (unsigned i = 0; i < num_streams(); ++i)
items[i].~Pipeline<T>();
}
Pipeline<T> &firstReactor() { return items()[0]; }
Pipeline<T> &acquireReactor() {
if (num_streams() == 1)
return firstReactor();
semaphore.wait();
auto items = this->items();
unsigned i;
available.pop(i);
return items[i];
}
void releaseReactor(const Pipeline<T> &reactor) {
if (num_streams() == 1)
return;
auto items = this->items();
for (unsigned i = 0; i < num_streams(); ++i) {
if (&reactor == items + i) {
available.push(i);
break;
}
}
semaphore.post();
}
};
template <typename T> [[nodiscard]] Instance<T> *allocInstance(std::size_t num_streams) {
constexpr std::size_t fr = sizeof(Instance<T>) + sizeof(num_streams);
static_assert(__STDCPP_DEFAULT_NEW_ALIGNMENT__ % alignof(Pipeline<T>) == 0 && fr % alignof(Pipeline<T>) == 0,
"unable to allocate instance with proper alignment");
auto p = static_cast<Instance<T> *>(::operator new(fr + sizeof(Pipeline<T>) * num_streams));
*reinterpret_cast<std::size_t *>(p + 1) = num_streams;
return p;
}
} // namespace