atomic_list.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_LIST_H_
15 #define ATOMIC_LIST_H_
16 
17 #include "atomic_smart_ptr.h"
18 #include <deque>
19 
20 template <typename T, class LIST = std::deque<T> >
21 class atomic_list : public atomic_shared_ptr<LIST> {
22 public:
23  typedef typename LIST::iterator iterator;
24  typedef typename LIST::const_iterator const_iterator;
26 
27  atomic_list() {}
28  ~atomic_list() {}
29 
30  class writer : public local_shared_ptr<LIST> {
31  public:
32  writer(atomic_list &x) :
34  m_target(x),
35  m_old_var(x) {
36  reset_unsafe(m_old_var ? (new LIST(*m_old_var)) : (new LIST()));
37  }
38  ~writer() {}
39  bool commit() {
40  return (m_target.compareAndSet(m_old_var, *this));
41  }
42  protected:
43  writer();
44  private:
45  atomic_list &m_target;
46  const local_shared_ptr<LIST> m_old_var;
47  };
48 
49  void push_back(const T &x) {
50  for(;;) {
51  auto tr(*this);
52  tr->push_back(x);
53  if(tr.commit())
54  break;
55  }
56  }
57 };
58 
59 #endif /* ATOMIC_LIST_H_ */

Generated for KAME4 by  doxygen 1.8.3