ATLAS Offline Software
threading.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
6 /**
7  * @file AthContainers/tools.threading.icc
8  * @author scott snyder <snyder@bnl.gov>
9  * @date Sep, 2013
10  * @brief Threading definitions.
11  */
12 
13 
14 namespace AthContainers_detail {
15 
16 
17 #ifndef ATHCONTAINERS_NO_THREADS
18 
19 
20 /**
21  * @brief An acquire/release fence.
22  */
23 inline
24 void fence_acq_rel()
25 {
26  std::atomic_thread_fence (std::memory_order_acq_rel);
27 }
28 
29 
30 /**
31  * @brief A sequentially-consistent fence.
32  */
33 inline
34 void fence_seq_cst()
35 {
36  std::atomic_thread_fence (std::memory_order_seq_cst);
37 }
38 
39 
40 #endif
41 
42 
43 /**
44  * @brief Take out a shared lock on @c obj and remember it.
45  * @param obj The lockable object.
46  */
47 template <typename LOCKABLE>
48 inline
49 strict_shared_lock<LOCKABLE>::strict_shared_lock (lockable_type& obj)
50  : m_obj(obj)
51 {
52  obj.lock_shared(); // locks on construction
53 }
54 
55 
56 /**
57  * @brief Take out a shared lock on @c obj and remember it.
58  * @param obj The lockable object.
59  */
60 template <typename LOCKABLE>
61 inline
62 strict_shared_lock<LOCKABLE>::strict_shared_lock (const lockable_type& obj)
63  : m_obj(const_cast<lockable_type&>(obj))
64 {
65  obj.lock_shared(); // locks on construction
66 }
67 
68 
69 /**
70  * @brief Release the held lock.
71  */
72 template <typename LOCKABLE>
73 inline
74 strict_shared_lock<LOCKABLE>::~strict_shared_lock()
75 {
76  m_obj.unlock_shared(); // unlocks on destruction
77 }
78 
79 
80 /**
81  * @brief Take out an upgrade lock on @c obj and remember it.
82  * @param obj The lockable object.
83  */
84 template <typename LOCKABLE>
85 inline
86 upgrading_lock<LOCKABLE>::upgrading_lock (lockable_type& obj)
87  : m_obj (obj)
88 {
89  m_obj.lock_upgrade();
90  m_exclusive = false;
91 }
92 
93 
94 /**
95  * @brief Release the held lock.
96  */
97 template <typename LOCKABLE>
98 inline
99 upgrading_lock<LOCKABLE>::~upgrading_lock()
100 {
101  if (m_exclusive)
102  m_obj.unlock();
103  else
104  m_obj.unlock_upgrade();
105 }
106 
107 
108 /**
109  * @brief Convert the lock from upgrade to exclusive.
110  */
111 template <typename LOCKABLE>
112 inline
113 void upgrading_lock<LOCKABLE>::upgrade()
114 {
115  if (!m_exclusive) {
116  m_obj.unlock_upgrade_and_lock();
117  m_exclusive = true;
118  }
119 }
120 
121 
122 
123 } // namespace AthContainers_detail