libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
anon_spin_event_impl.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2014 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace ppd { namespace lock {
20 
21  template<class LkT> inline __fastcall REALLY_FORCE_INLINE
22  anon_spin_event<LkT>::anon_spin_event() noexcept(true) {
23  }
24 
25  template<class LkT> inline __fastcall REALLY_FORCE_INLINE
26  anon_spin_event<LkT>::anon_spin_event(const atomic_state_type state) noexcept(true) {
27  if (state==atomic_state_type::atom_set) {
28  set();
29  }
30  }
31 
32  template<class LkT>
33  inline typename anon_spin_event<LkT>::atomic_state_type __fastcall REALLY_FORCE_INLINE
34  anon_spin_event<LkT>::set() noexcept(true) {
35  while (state.test_and_set(std::memory_order_acquire)) {
36 // TODO: I do not use "_mm_pause();" because my test revealed that it takes 100+/-5% nsec versus 55+/-4% nsec for "yield()" on my AMD Opteron 4180s.
37  std::this_thread::yield();
38  }
39  return atomic_state_type::atom_set;
40  }
41 
42  template<class LkT>
43  inline typename anon_spin_event<LkT>::lock_result_type __fastcall REALLY_FORCE_INLINE
44  anon_spin_event<LkT>::unlock() noexcept(true) {
45  state.clear(std::memory_order_release);
46  return atomic_state_type::atom_unset;
47  }
48 
49  template<class LkT>
50  inline typename anon_spin_event<LkT>::atomic_state_type __fastcall REALLY_FORCE_INLINE
51  anon_spin_event<LkT>::reset() noexcept(true) {
52  return unlock();
53  }
54 
55  template<class LkT>
56  inline typename anon_spin_event<LkT>::lock_result_type __fastcall REALLY_FORCE_INLINE
57  anon_spin_event<LkT>::lock() noexcept(true) {
58  return set();
59  }
60 
61  template<class LkT>
62  inline typename anon_spin_event<LkT>::lock_result_type __fastcall REALLY_FORCE_INLINE
63  anon_spin_event<LkT>::lock(const timeout_type) noexcept(true) {
64  return lock();
65  }
66 
67 } } } }