RAUL  0.5.1
AtomicPtr.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_ATOMIC_PTR_HPP
19 #define RAUL_ATOMIC_PTR_HPP
20 
21 #include <glib.h>
22 
23 namespace Raul {
24 
25 
26 template<typename T>
27 class AtomicPtr {
28 public:
29 
30  inline AtomicPtr()
31  { g_atomic_pointer_set(&_val, NULL); }
32 
33  inline AtomicPtr(const AtomicPtr& copy)
34  { g_atomic_pointer_set(&_val, copy.get()); }
35 
36  inline T* get() const
37  { return (T*)g_atomic_pointer_get(&_val); }
38 
39  inline void operator=(T* val)
40  { g_atomic_pointer_set(&_val, val); }
41 
43  inline bool compare_and_exchange(int oldval, int newval)
44  { return g_atomic_pointer_compare_and_exchange(&_val, oldval, newval); }
45 
46 private:
47  volatile T* _val;
48 };
49 
50 
51 } // namespace Raul
52 
53 #endif // RAUL_ATOMIC_PTR_HPP
Definition: Array.hpp:26