atomic.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2010-2012, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 
00021 #ifndef CLASP_UTIL_ATOMIC_H_INCLUDED
00022 #define CLASP_UTIL_ATOMIC_H_INCLUDED
00023 #ifdef _MSC_VER
00024 #pragma once
00025 #endif
00026 
00027 #if WITH_THREADS
00028 #include <tbb/atomic.h>
00029 namespace Clasp { using tbb::atomic; }
00030 #else
00031 namespace no_multi_threading {
00032 template <class T>
00033 struct atomic {
00034         typedef T value_type;
00035         atomic() : value(value_type()) {}
00036         atomic& operator=(value_type t) { value = t; return *this; }
00037         operator value_type() const { return value; }
00038         value_type operator+=(value_type v) { return value += v; }
00039         value_type operator-=(value_type v) { return value -= v; }
00040         value_type operator++()             { return ++value;    }
00041         value_type operator--()             { return --value;    }
00042         value_type operator->() const       { return value;    }
00043         value_type fetch_and_store(value_type v) {
00044                 value_type last= value;
00045                 value          = v;
00046                 return last;
00047         }
00048         value_type compare_and_swap(value_type y, value_type z) {
00049                 if (value == z) {
00050                         value = y;
00051                         return z;
00052                 }
00053                 return value;
00054         }
00055         T value;
00056 };
00057 }
00058 namespace Clasp { using no_multi_threading::atomic; }
00059 #endif
00060 
00061 
00062 // effect: T temp; a |= mask; return temp
00063 template <class T>
00064 inline T fetch_and_or(Clasp::atomic<T>& a, T mask) {
00065         T x;
00066         do {
00067                 x = a;
00068         } while (a.compare_and_swap(x|mask, x) != x);
00069         return x;
00070 }
00071 
00072 // effect: T temp; a &= mask; return temp
00073 template <class T>
00074 inline T fetch_and_and(Clasp::atomic<T>& a, T mask) {
00075         T x;
00076         do {
00077                 x = a;
00078         } while (a.compare_and_swap(x&mask, x) != x);
00079         return x;
00080 }
00081 
00082 #endif


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:38