Iguana 0.0.0
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
ConcurrentParam.h
1#pragma once
2
3#include "GlobalParam.h"
4
5namespace iguana {
6
8 using concurrent_key_t = std::size_t;
9
10 // ==================================================================================
11 // ConcurrentParam
12 // ==================================================================================
13
16 template <typename T>
18
19 public:
20
23 ConcurrentParam(std::string const& model);
24 virtual ~ConcurrentParam() = default;
25
29 virtual T const Load(concurrent_key_t const key) const = 0;
30
34 virtual void Save(T const& value, concurrent_key_t const key) = 0;
35
38 virtual bool HasKey(concurrent_key_t const key) const = 0;
39
42 bool NeedsHashing() const { return m_needs_hashing; }
43
45 virtual std::size_t GetSize() const = 0;
46
48 bool IsEmpty() const { return m_empty; }
49
50 protected:
51
54
56 std::mutex m_mutex;
57
59 bool m_empty{true};
60
61 };
62
63 // ==================================================================================
64 // SingleThreadParam
65 // ==================================================================================
66
69 template <typename T>
71
72 public:
74 ~SingleThreadParam() override = default;
75 T const Load(concurrent_key_t const key) const override;
76 void Save(T const& value, concurrent_key_t const key) override;
77 bool HasKey(concurrent_key_t const key) const override;
78 std::size_t GetSize() const override;
79
80 private:
81
84 T m_value;
85
86 };
87
88 // ==================================================================================
89 // MemoizedParam
90 // ==================================================================================
91
94 template <typename T>
95 class MemoizedParam : public ConcurrentParam<T> {
96
98 using container_t = std::unordered_map<concurrent_key_t, T>;
99
100 public:
102 ~MemoizedParam() override = default;
103 T const Load(concurrent_key_t const key) const override;
104 void Save(T const& value, concurrent_key_t const key) override;
105 bool HasKey(concurrent_key_t const key) const override;
106 std::size_t GetSize() const override;
107
108 private:
109
111 container_t m_container;
112
113 };
114
115 // ==================================================================================
116 // ConcurrentParamFactory
117 // ==================================================================================
118
121
122 public:
123 ConcurrentParamFactory() = delete;
124
129 template <typename T>
130 static std::unique_ptr<ConcurrentParam<T>> Create() {
131
132 if(GlobalConcurrencyModel() == "none")
133 GlobalConcurrencyModel = "memoize"; // the safest default, but not the fastest for single-threaded users
134
135 if(GlobalConcurrencyModel() == "single")
136 return std::make_unique<SingleThreadParam<T>>();
137 else if(GlobalConcurrencyModel() == "memoize")
138 return std::make_unique<MemoizedParam<T>>();
139
140 throw std::runtime_error("unknown GlobalConcurrencyModel '" + GlobalConcurrencyModel() + "'; valid options are 'single' or 'memoize'");
141 }
142
143 };
144
145}
factory to create the appropriate ConcurrentParam-derived class instance for the current GlobalConcur...
static std::unique_ptr< ConcurrentParam< T > > Create()
create a new ConcurrentParam-derived class instance
abstract base class for concurrently mutable configuration parameters
virtual void Save(T const &value, concurrent_key_t const key)=0
modify a value
std::mutex m_mutex
mutex for this ConcurrentParam
bool m_needs_hashing
whether this ConcurrentParam needs hashing for calling Load or Save
virtual bool HasKey(concurrent_key_t const key) const =0
bool NeedsHashing() const
whether or not hashing is needed to use this parameter
virtual std::size_t GetSize() const =0
bool m_empty
whether this ConcurrentParam has something saved
virtual T const Load(concurrent_key_t const key) const =0
access a stored value
ConcurrentParam(std::string const &model)
an iguana::ConcurrentParam that uses memoization for thread safety; used when iguana::GlobalConcurren...
T const Load(concurrent_key_t const key) const override
access a stored value
bool HasKey(concurrent_key_t const key) const override
std::size_t GetSize() const override
void Save(T const &value, concurrent_key_t const key) override
modify a value
a parameter that is not thread safe; used when iguana::GlobalConcurrencyModel is "single"
std::size_t GetSize() const override
void Save(T const &value, concurrent_key_t const key) override
modify a value
T const Load(concurrent_key_t const key) const override
access a stored value
bool HasKey(concurrent_key_t const key) const override
General, top-level namespace for algorithms and infrastructure. For algorithms and bindings,...
Definition Algorithm.h:14
std::size_t concurrent_key_t
concurrent hash key type
GlobalParam< std::string > GlobalConcurrencyModel
The concurrency model, for running certain algorithms in a thread-safe way.