RAUL  0.5.1
DoubleBuffer.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_DOUBLE_BUFFER_HPP
19 #define RAUL_DOUBLE_BUFFER_HPP
20 
21 #include <raul/AtomicInt.hpp>
22 #include <raul/AtomicPtr.hpp>
23 
24 namespace Raul {
25 
35 template<typename T>
36 class DoubleBuffer {
37 public:
38 
39  inline DoubleBuffer(T val)
40  : _state(RAUL_DB_READ_WRITE)
41  {
42  _vals[0] = val;
43  _read_val = &_vals[0];
44  }
45 
46  inline DoubleBuffer(const DoubleBuffer& copy)
47  : _state(RAUL_DB_READ_WRITE)
48  {
49  T val = copy.get();
50  _vals[0] = val;
51  _read_val = &_vals[0];
52  }
53 
54  inline T& get() const {
55  return *_read_val.get();
56  }
57 
58  inline bool set(T new_val) {
59  if (_state.compare_and_exchange(RAUL_DB_READ_WRITE, RAUL_DB_READ_LOCK)) {
60 
61  // locked _vals[1] for write
62  _vals[1] = new_val;
63  _read_val = &_vals[1];
64  _state = RAUL_DB_WRITE_READ;
65  return true;
66 
67  // concurrent calls here are fine. good, actually - caught
68  // the WRITE_READ state immediately after it was set above
69 
70  } else if (_state.compare_and_exchange(RAUL_DB_WRITE_READ, RAUL_DB_LOCK_READ)) {
71 
72  // locked _vals[0] for write
73  _vals[0] = new_val;
74  _read_val = &_vals[0];
75  _state = RAUL_DB_READ_WRITE;
76  return true;
77 
78  } else {
79 
80  return false;
81 
82  }
83  }
84 
85 private:
86  enum States {
87  // vals[0] state _ vals[1] state
88  RAUL_DB_READ_WRITE = 0,
89  RAUL_DB_READ_LOCK,
90  RAUL_DB_WRITE_READ,
91  RAUL_DB_LOCK_READ
92  };
93 
94  AtomicInt _state;
95  AtomicPtr<T> _read_val;
96  T _vals[2];
97 };
98 
99 
100 } // namespace Raul
101 
102 #endif // RAUL_DOUBLE_BUFFER_HPP
Definition: Array.hpp:26
Double buffer.
Definition: DoubleBuffer.hpp:36