RAUL 0.8.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List

AtomicPtr.hpp

    00001 /* This file is part of Raul.
    00002  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
    00003  *
    00004  * Raul is free software; you can redistribute it and/or modify it under the
    00005  * terms of the GNU General Public License as published by the Free Software
    00006  * Foundation; either version 2 of the License, or (at your option) any later
    00007  * version.
    00008  *
    00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
    00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
    00012  *
    00013  * You should have received a copy of the GNU General Public License along
    00014  * with this program; if not, write to the Free Software Foundation, Inc.,
    00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    00016  */
    00017
    00018 #ifndef RAUL_ATOMIC_PTR_HPP
    00019 #define RAUL_ATOMIC_PTR_HPP
    00020
    00021 #include <glib.h>
    00022
    00023 namespace Raul {
    00024
    00025
    00029 template<typename T>
    00030 class AtomicPtr {
    00031 public:
    00032     inline AtomicPtr()
    00033         { g_atomic_pointer_set(static_cast<volatile gpointer*>(&_val), NULL); }
    00034
    00035     inline AtomicPtr(const AtomicPtr& copy)
    00036         { g_atomic_pointer_set(static_cast<volatile gpointer*>(&_val),
    00037                 static_cast<gpointer>(copy.get())); }
    00038
    00039     inline T* get() const
    00040         { return static_cast<T*>(g_atomic_pointer_get(static_cast<volatile gpointer*>(&_val))); }
    00041
    00042     inline void operator=(T* val)
    00043         { g_atomic_pointer_set(&_val, static_cast<gpointer>(val)); }
    00044
    00046     inline bool compare_and_exchange(gpointer oldval, gpointer newval)
    00047         { return g_atomic_pointer_compare_and_exchange(&_val, oldval, newval); }
    00048
    00049 private:
    00050     mutable volatile gpointer _val;
    00051 };
    00052
    00053
    00054 } // namespace Raul
    00055
    00056 #endif // RAUL_ATOMIC_PTR_HPP