Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include "GlobalParam.h" | ||
4 | |||
5 | namespace iguana { | ||
6 | |||
7 | /// concurrent hash key type | ||
8 | using concurrent_key_t = std::size_t; | ||
9 | |||
10 | // ================================================================================== | ||
11 | // ConcurrentParam | ||
12 | // ================================================================================== | ||
13 | |||
14 | /// @brief abstract base class for concurrently mutable configuration parameters | ||
15 | /// @see `iguana::ConcurrentParamFactory` for instantiation | ||
16 | template <typename T> | ||
17 | class ConcurrentParam { | ||
18 | |||
19 | public: | ||
20 | |||
21 | /// @param model the concurrency model this instance must be | ||
22 | /// @see `ConcurrentParamFactory`, the preferred instantiation method | ||
23 | ConcurrentParam(std::string const& model); | ||
24 | virtual ~ConcurrentParam() = default; | ||
25 | |||
26 | /// @brief access a stored value | ||
27 | /// @param key the access key | ||
28 | /// @returns the stored value | ||
29 | virtual T const Load(concurrent_key_t const key) const = 0; | ||
30 | |||
31 | /// @brief modify a value | ||
32 | /// @param value the value | ||
33 | /// @param key the access key | ||
34 | virtual void Save(T const& value, concurrent_key_t const key) = 0; | ||
35 | |||
36 | /// @param key the key | ||
37 | /// @returns `true` if key `key` is used | ||
38 | virtual bool HasKey(concurrent_key_t const key) const = 0; | ||
39 | |||
40 | /// @brief whether or not hashing is needed to use this parameter | ||
41 | /// @returns `true` if hashing is needed | ||
42 |
2/2✓ Branch 0 taken 4005 times.
✓ Branch 1 taken 4000 times.
|
8005 | bool NeedsHashing() const { return m_needs_hashing; } |
43 | |||
44 | /// @returns the size of the internal data storage container | ||
45 | virtual std::size_t GetSize() const = 0; | ||
46 | |||
47 | /// @returns `true` if no value has been saved | ||
48 |
2/2✓ Branch 0 taken 3996 times.
✓ Branch 1 taken 4 times.
|
4000 | bool IsEmpty() const { return m_empty; } |
49 | |||
50 | protected: | ||
51 | |||
52 | /// whether this `ConcurrentParam` needs hashing for calling `::Load` or `::Save` | ||
53 | bool m_needs_hashing; | ||
54 | |||
55 | /// mutex for this `ConcurrentParam` | ||
56 | std::mutex m_mutex; | ||
57 | |||
58 | /// whether this `ConcurrentParam` has something saved | ||
59 | bool m_empty{true}; | ||
60 | |||
61 | }; | ||
62 | |||
63 | // ================================================================================== | ||
64 | // SingleThreadParam | ||
65 | // ================================================================================== | ||
66 | |||
67 | /// @brief a parameter that is _not_ thread safe; | ||
68 | /// used when `iguana::GlobalConcurrencyModel` is "single" | ||
69 | template <typename T> | ||
70 | class SingleThreadParam : public ConcurrentParam<T> { | ||
71 | |||
72 | public: | ||
73 | SingleThreadParam(); | ||
74 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
18 | ~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 | |||
82 | /// the stored value; it is not `std::atomic` since `std::string` is one of | ||
83 | /// the specializations (for `T`), which is not trivially copyable | ||
84 | T m_value; | ||
85 | |||
86 | }; | ||
87 | |||
88 | // ================================================================================== | ||
89 | // MemoizedParam | ||
90 | // ================================================================================== | ||
91 | |||
92 | /// @brief an `iguana::ConcurrentParam` that uses memoization for thread safety; | ||
93 | /// used when `iguana::GlobalConcurrencyModel` is "memoize" | ||
94 | template <typename T> | ||
95 | class MemoizedParam : public ConcurrentParam<T> { | ||
96 | |||
97 | /// hash table container for memoization | ||
98 | using container_t = std::unordered_map<concurrent_key_t, T>; | ||
99 | |||
100 | public: | ||
101 | MemoizedParam(); | ||
102 | 23 | ~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 | |||
110 | /// the hash table for stored (memoized) values | ||
111 | container_t m_container; | ||
112 | |||
113 | }; | ||
114 | |||
115 | // ================================================================================== | ||
116 | // ConcurrentParamFactory | ||
117 | // ================================================================================== | ||
118 | |||
119 | /// @brief factory to create the appropriate `ConcurrentParam`-derived class instance for the current `GlobalConcurrencyModel` | ||
120 | class ConcurrentParamFactory { | ||
121 | |||
122 | public: | ||
123 | ConcurrentParamFactory() = delete; | ||
124 | |||
125 | /// @brief create a new `ConcurrentParam`-derived class instance | ||
126 | /// @warning if `GlobalConcurrencyModel` is not set, the model `"memoize"` will be chosen, | ||
127 | /// since it is thread safe and does not assume anything about the user's implementation | ||
128 | /// @returns a pointer to the new instance | ||
129 | template <typename T> | ||
130 | 34 | static std::unique_ptr<ConcurrentParam<T>> Create() { | |
131 | |||
132 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 29 times.
|
34 | if(GlobalConcurrencyModel() == "none") |
133 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
10 | GlobalConcurrencyModel = "memoize"; // the safest default, but not the fastest for single-threaded users |
134 | |||
135 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 23 times.
|
34 | if(GlobalConcurrencyModel() == "single") |
136 | 11 | return std::make_unique<SingleThreadParam<T>>(); | |
137 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
23 | else if(GlobalConcurrencyModel() == "memoize") |
138 | 23 | 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 | } | ||
146 |