atomic.h
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13  ***************************************************************************/
14 #ifndef ATOMIC_H_
15 #define ATOMIC_H_
16 
17 #include "atomic_prv_basic.h"
18 #include "atomic_smart_ptr.h"
19 
20 #include <type_traits>
21 
22 //! Atomic access for a copy-able class which does not require transactional writing.
23 template <typename T, class Enable>
24 class atomic {
25 public:
26  atomic() : m_var(new T) {}
27  atomic(T t) : m_var(new T(t)) {}
28  atomic(const atomic &t) noexcept : m_var(t.m_var) {}
29  operator T() const noexcept {
30  local_shared_ptr<T> x = m_var;
31  return *x;
32  }
33  atomic &operator=(T t) {
34  m_var.reset(new T(t));
35  return *this;
36  }
37  atomic &operator=(const atomic &x) noexcept {
38  m_var = x.m_var;
39  return *this;
40  }
41  bool compare_set_strong(const T &oldv, const T &newv) {
42  local_shared_ptr<T> oldx(m_var);
43  if( *oldx != oldv)
44  return false;
45  local_shared_ptr<T> newx(new T(newv));
46  bool ret = m_var.compareAndSet(oldx, newx);
47  return ret;
48  }
49 protected:
51 };
52 
53 #endif /*ATOMIC_H_*/

Generated for KAME4 by  doxygen 1.8.3