ATLAS Offline Software
threading.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 /*
3  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
4 */
17 #ifndef ATHCONTAINERS_THREADING_H
18 #define ATHCONTAINERS_THREADING_H
19 
20 
21 #ifdef ATHCONTAINERS_NO_THREADS
22 
23 
24 #include <vector>
25 
26 
27 namespace AthContainers_detail {
28 
29 
33 class mutex {};
34 
35 
39 class recursive_mutex {};
40 
41 
45 template <class LOCKABLE>
46 class lock_guard
47 {
48 public:
49  lock_guard() {}
50  lock_guard(LOCKABLE&) {}
51 };
52 
53 
57 class upgrade_mutex
58 {
59 public:
60  void lock() {}
61  void unlock() {}
62  void lock_shared() {}
63  void unlock_shared() {}
64  void lock_upgrade() {}
65  void unlock_upgrade() {}
66  void unlock_upgrade_and_lock() {}
67 private:
68  // Real upgrade_mutex is not assignable. Need this for proper
69  // dictionary generation.
70  upgrade_mutex& operator= (const upgrade_mutex&);
71 };
72 
73 
77 template <class T>
78 class thread_specific_ptr
79 {
80 public:
81  thread_specific_ptr() : m_ptr(0) {}
82  ~thread_specific_ptr() { delete m_ptr; }
83  thread_specific_ptr (const thread_specific_ptr&) = delete;
84  thread_specific_ptr& operator= (const thread_specific_ptr&) = delete;
85  T* get() { return m_ptr; }
86  T* operator->() { return m_ptr; }
87  T& operator*() { return *m_ptr; }
88  const T* get() const { return m_ptr; }
89  const T* operator->() const { return m_ptr; }
90  const T& operator*() const { return *m_ptr; }
91  void reset (T* new_value=0) { delete m_ptr; m_ptr = new_value; }
92  T* release() { T* ret = m_ptr; m_ptr = 0; return ret; }
93 
94 private:
95  T* m_ptr;
96 };
97 
98 
100 inline void fence_acq_rel() {}
101 inline void fence_seq_cst() {}
102 
103 
104 template <class T>
105 using concurrent_vector = std::vector<T>;
106 
107 
109 template <class T>
110 class atomic
111 {
112 public:
113  atomic() : m_val() {}
114  operator T() const { return m_val; }
115  atomic& operator= (const T& val) { m_val = val; return *this; }
116 
117 private:
118  T m_val;
119 };
120 
121 
122 } // namespace AthContainers_detail
123 
124 
125 #else // not ATHCONTAINERS_NO_THREADS
126 
127 //the shared_mutex include generates annoying unused_variable warnings during compilations... silence this in athanalysisbase
128 #ifdef XAOD_ANALYSIS
129 #ifndef BOOST_SYSTEM_NO_DEPRECATED
130 #define BOOST_SYSTEM_NO_DEPRECATED 1
131 #endif
132 #endif
133 
134 #include "boost/thread/shared_mutex.hpp"
135 #include "boost/thread/tss.hpp"
136 
137 // See ATLINFR-4996 for an explanation of this clunky
138 // warning suppression.
139 #if (__cplusplus > 201703L) && defined(__ROOTCLING__)
140 #pragma clang diagnostic push
141 #pragma clang diagnostic ignored "-Wdeprecated-volatile"
142 #endif // >C++17 with ROOT
143 #include "tbb/concurrent_vector.h"
144 #if (__cplusplus > 201703L) && defined(__ROOTCLING__)
145 #pragma clang diagnostic pop
146 #endif // >C++17 with ROOT
147 
148 #include <atomic>
149 #include <mutex>
150 #include <thread>
151 
152 
153 namespace AthContainers_detail {
154 
155 
156 // Take these from boost. (Boost extensions not in C++.)
157 using boost::upgrade_mutex;
158 using boost::thread_specific_ptr;
159 
160 using std::mutex;
161 using std::recursive_mutex;
162 using std::lock_guard;
163 using std::thread;
164 using std::atomic;
165 
166 using tbb::concurrent_vector;
167 
168 
173 
174 
179 
180 
181 } // namespace AthContainers_detail
182 
183 
184 #endif // not ATHCONTAINERS_NO_THREADS
185 
186 
187 
188 namespace AthContainers_detail {
189 
190 
197 template <typename LOCKABLE>
199 {
200 public:
202  typedef LOCKABLE lockable_type;
203 
204 
210 
211 
217 
218 
223 
224 
225 private:
226  // Disallow these.
230 
231 
232 private:
235 };
236 
237 
246 template <class LOCKABLE>
248 {
249 public:
251  typedef LOCKABLE lockable_type;
252 
253 
259 
260 
265 
266 
270  void upgrade();
271 
272 
273 private:
274  // Disallow these.
278 
279 
280 private:
283 
286 };
287 
288 
289 } // namespace AthContainers_detail
290 
291 
293 
294 
295 #endif // not ATHCONTAINERS_THREADING_H
AthContainers_detail::fence_acq_rel
void fence_acq_rel()
An acquire/release fence.
AthContainers_detail::upgrading_lock::upgrading_lock
upgrading_lock(upgrading_lock const &)
AthContainers_detail::upgrading_lock::upgrading_lock
upgrading_lock()
AthContainers_detail::upgrading_lock::~upgrading_lock
~upgrading_lock()
Release the held lock.
AthContainers_detail::strict_shared_lock::strict_shared_lock
strict_shared_lock(const lockable_type &obj)
Take out a shared lock on obj and remember it.
AthContainers_detail::strict_shared_lock::strict_shared_lock
strict_shared_lock()
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
AthContainers_detail::upgrading_lock::upgrading_lock
upgrading_lock(lockable_type &obj)
Take out an upgrade lock on obj and remember it.
AthContainers_detail::strict_shared_lock::strict_shared_lock
strict_shared_lock(lockable_type &obj)
Take out a shared lock on obj and remember it.
AthContainers_detail::strict_shared_lock::strict_shared_lock
strict_shared_lock(strict_shared_lock const &)
AthContainers_detail::fence_seq_cst
void fence_seq_cst()
A sequentially-consistent fence.
AthContainers_detail
Definition: error.h:53
AthContainers_detail::strict_shared_lock::lockable_type
LOCKABLE lockable_type
The underlying object type.
Definition: threading.h:202
InDetDD::operator*
SiLocalPosition operator*(const SiLocalPosition &position, const double factor)
Definition: SiLocalPosition.cxx:98
ret
T ret(T t)
Definition: rootspy.cxx:260
AthContainers_detail::strict_shared_lock
Lock object for taking out shared locks.
Definition: threading.h:199
AthContainers_detail::upgrading_lock::upgrade
void upgrade()
Convert the lock from upgrade to exclusive.
AthContainers_detail::strict_shared_lock::~strict_shared_lock
~strict_shared_lock()
Release the held lock.
python.EventInfoMgtInit.release
release
Definition: EventInfoMgtInit.py:24
AthContainers_detail::upgrading_lock::operator=
upgrading_lock & operator=(upgrading_lock const &)
AthContainers_detail::upgrading_lock::lockable_type
LOCKABLE lockable_type
The underlying object type.
Definition: threading.h:251
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
CxxUtils::reset
constexpr std::enable_if_t< is_bitmask_v< E >, E & > reset(E &lhs, E rhs)
Convenience function to clear bits in a class enum bitmask.
Definition: bitmask.h:243
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
AthContainers_detail::upgrading_lock::m_obj
lockable_type & m_obj
The lock being held.
Definition: threading.h:282
AthContainers_detail::upgrading_lock
Lock object for taking out upgradable locks.
Definition: threading.h:248
AthContainers_detail::strict_shared_lock::m_obj
lockable_type & m_obj
The lock being held.
Definition: threading.h:234
python.PyAthena.obj
obj
Definition: PyAthena.py:135
AthContainers_detail::upgrading_lock::m_exclusive
bool m_exclusive
Has the lock been converted to exclusive?
Definition: threading.h:285
AthContainers_detail::strict_shared_lock::operator=
strict_shared_lock & operator=(strict_shared_lock const &)
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
threading.icc