|
22 | 22 | #include <thread> |
23 | 23 | #include <unordered_map> |
24 | 24 |
|
| 25 | +#ifdef R__HAS_TBB |
| 26 | +#include "tbb/enumerable_thread_specific.h" |
| 27 | +#endif |
| 28 | + |
25 | 29 | namespace ROOT { |
26 | 30 | namespace Internal { |
27 | 31 | struct UniqueLockRecurseCount { |
@@ -147,6 +151,127 @@ struct RecurseCounts { |
147 | 151 |
|
148 | 152 |
|
149 | 153 | }; |
| 154 | + |
| 155 | +#ifdef R__HAS_TBB |
| 156 | +struct RecurseCountsTBB { |
| 157 | + using Hint_t = TVirtualRWMutex::Hint_t; |
| 158 | + |
| 159 | + struct LocalCounts { |
| 160 | + size_t fReadersCount = 0; |
| 161 | + bool fIsWriter = false; |
| 162 | + }; |
| 163 | + tbb::enumerable_thread_specific<LocalCounts> fLocalCounts; |
| 164 | + size_t fWriteRecurse = 0; ///<! Number of re-entry in the lock by the same thread. |
| 165 | + |
| 166 | + using local_t = LocalCounts*; |
| 167 | + |
| 168 | + local_t GetLocal(){ |
| 169 | + return &fLocalCounts.local(); |
| 170 | + } |
| 171 | + |
| 172 | + Hint_t *IncrementReadCount(local_t &local) { |
| 173 | + ++(local->fReadersCount); |
| 174 | + return reinterpret_cast<TVirtualRWMutex::Hint_t *>(&(local->fReadersCount)); |
| 175 | + } |
| 176 | + |
| 177 | + template <typename MutexT> |
| 178 | + Hint_t *IncrementReadCount(local_t &local, MutexT &) { |
| 179 | + return IncrementReadCount(local); |
| 180 | + } |
| 181 | + |
| 182 | + Hint_t *DecrementReadCount(local_t &local) { |
| 183 | + --(local->fReadersCount); |
| 184 | + return reinterpret_cast<TVirtualRWMutex::Hint_t *>(&(local->fReadersCount)); |
| 185 | + } |
| 186 | + |
| 187 | + template <typename MutexT> |
| 188 | + Hint_t *DecrementReadCount(local_t &local, MutexT &) { |
| 189 | + return DecrementReadCount(local); |
| 190 | + } |
| 191 | + |
| 192 | + void ResetReadCount(local_t &local, int newvalue) { |
| 193 | + local->fReadersCount = newvalue; |
| 194 | + } |
| 195 | + |
| 196 | + bool IsCurrentWriter(local_t &local) { return local->fIsWriter; } |
| 197 | + bool IsNotCurrentWriter(local_t &local) { return !local->fIsWriter; } |
| 198 | + |
| 199 | + void SetIsWriter(local_t &local) |
| 200 | + { |
| 201 | + // if (fWriteRecurse == std::numeric_limits<decltype(fWriteRecurse)>::max()) { |
| 202 | + // ::Fatal("TRWSpinLock::WriteLock", "Too many recursions in TRWSpinLock!"); |
| 203 | + // } |
| 204 | + ++fWriteRecurse; |
| 205 | + local->fIsWriter = true; |
| 206 | + } |
| 207 | + |
| 208 | + void DecrementWriteCount() { --fWriteRecurse; } |
| 209 | + |
| 210 | + void ResetIsWriter(local_t &local) { local->fIsWriter = false; } |
| 211 | + |
| 212 | + size_t &GetLocalReadersCount(local_t &local) { return local->fReadersCount; } |
| 213 | +}; |
| 214 | + |
| 215 | +struct RecurseCountsTBBUnique { |
| 216 | + using Hint_t = TVirtualRWMutex::Hint_t; |
| 217 | + |
| 218 | + struct LocalCounts { |
| 219 | + size_t fReadersCount = 0; |
| 220 | + bool fIsWriter = false; |
| 221 | + }; |
| 222 | + tbb::enumerable_thread_specific<LocalCounts, tbb::cache_aligned_allocator<LocalCounts>, tbb::ets_key_per_instance> fLocalCounts; |
| 223 | + size_t fWriteRecurse = 0; ///<! Number of re-entry in the lock by the same thread. |
| 224 | + |
| 225 | + using local_t = LocalCounts*; |
| 226 | + |
| 227 | + local_t GetLocal(){ |
| 228 | + return &fLocalCounts.local(); |
| 229 | + } |
| 230 | + |
| 231 | + Hint_t *IncrementReadCount(local_t &local) { |
| 232 | + ++(local->fReadersCount); |
| 233 | + return reinterpret_cast<TVirtualRWMutex::Hint_t *>(&(local->fReadersCount)); |
| 234 | + } |
| 235 | + |
| 236 | + template <typename MutexT> |
| 237 | + Hint_t *IncrementReadCount(local_t &local, MutexT &) { |
| 238 | + return IncrementReadCount(local); |
| 239 | + } |
| 240 | + |
| 241 | + Hint_t *DecrementReadCount(local_t &local) { |
| 242 | + --(local->fReadersCount); |
| 243 | + return reinterpret_cast<TVirtualRWMutex::Hint_t *>(&(local->fReadersCount)); |
| 244 | + } |
| 245 | + |
| 246 | + template <typename MutexT> |
| 247 | + Hint_t *DecrementReadCount(local_t &local, MutexT &) { |
| 248 | + return DecrementReadCount(local); |
| 249 | + } |
| 250 | + |
| 251 | + void ResetReadCount(local_t &local, int newvalue) { |
| 252 | + local->fReadersCount = newvalue; |
| 253 | + } |
| 254 | + |
| 255 | + bool IsCurrentWriter(local_t &local) { return local->fIsWriter; } |
| 256 | + bool IsNotCurrentWriter(local_t &local) { return !local->fIsWriter; } |
| 257 | + |
| 258 | + void SetIsWriter(local_t &local) |
| 259 | + { |
| 260 | + // if (fWriteRecurse == std::numeric_limits<decltype(fWriteRecurse)>::max()) { |
| 261 | + // ::Fatal("TRWSpinLock::WriteLock", "Too many recursions in TRWSpinLock!"); |
| 262 | + // } |
| 263 | + ++fWriteRecurse; |
| 264 | + local->fIsWriter = true; |
| 265 | + } |
| 266 | + |
| 267 | + void DecrementWriteCount() { --fWriteRecurse; } |
| 268 | + |
| 269 | + void ResetIsWriter(local_t &local) { local->fIsWriter = false; } |
| 270 | + |
| 271 | + size_t &GetLocalReadersCount(local_t &local) { return local->fReadersCount; } |
| 272 | +}; |
| 273 | +#endif |
| 274 | + |
150 | 275 | } // Internal |
151 | 276 |
|
152 | 277 | template <typename MutexT = ROOT::TSpinMutex, typename RecurseCountsT = Internal::RecurseCounts> |
|
0 commit comments