RAUL  0.8.0
AtomicPtr.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David 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 
29 template<typename T>
30 class AtomicPtr {
31 public:
32  inline AtomicPtr()
33  { g_atomic_pointer_set(static_cast<volatile gpointer*>(&_val), NULL); }
34 
35  inline AtomicPtr(const AtomicPtr& copy)
36  { g_atomic_pointer_set(static_cast<volatile gpointer*>(&_val),
37  static_cast<gpointer>(copy.get())); }
38 
39  inline T* get() const
40  { return static_cast<T*>(g_atomic_pointer_get(static_cast<volatile gpointer*>(&_val))); }
41 
42  inline void operator=(T* val)
43  { g_atomic_pointer_set(&_val, static_cast<gpointer>(val)); }
44 
46  inline bool compare_and_exchange(gpointer oldval, gpointer newval)
47  { return g_atomic_pointer_compare_and_exchange(&_val, oldval, newval); }
48 
49 private:
50  mutable volatile gpointer _val;
51 };
52 
53 
54 } // namespace Raul
55 
56 #endif // RAUL_ATOMIC_PTR_HPP