]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGAtomic.hxx
7bf8226f6f7b82870d5587ef252398dd796bc14c
[simgear.git] / simgear / structure / SGAtomic.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2005-2006 Mathias Froehlich 
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  */
20
21 #ifndef SGAtomic_HXX
22 #define SGAtomic_HXX
23
24 #if defined(__GNUC__) && (4 <= __GNUC__) && (1 <= __GNUC_MINOR__) \
25   && (defined(__i386__) || defined(__x86_64__))
26 // No need to include something. Is a Compiler API ...
27 # define SGATOMIC_USE_GCC4_BUILTINS
28 #elif defined(WIN32) 
29 # include <windows.h>
30 # define SGATOMIC_USE_WIN32_INTERLOCKED
31 #else
32 // The sledge hammer ...
33 # include <simgear/threads/SGThread.hxx>
34 # include <simgear/threads/SGGuard.hxx>
35 #endif
36
37 class SGAtomic {
38 public:
39   SGAtomic(unsigned value = 0) : mValue(value)
40   { }
41   unsigned operator++()
42   {
43 #if defined(SGATOMIC_USE_GCC4_BUILTINS)
44     return __sync_add_and_fetch(&mValue, 1);
45 #elif defined(SGATOMIC_USE_WIN32_INTERLOCKED)
46     return InterlockedIncrement(reinterpret_cast<long volatile*>(&mValue));
47 #else
48     SGGuard<SGMutex> lock(mMutex);
49     return ++mValue;
50 #endif
51   }
52   unsigned operator--()
53   {
54 #if defined(SGATOMIC_USE_GCC4_BUILTINS)
55     return __sync_sub_and_fetch(&mValue, 1);
56 #elif defined(SGATOMIC_USE_WIN32_INTERLOCKED)
57     return InterlockedDecrement(reinterpret_cast<long volatile*>(&mValue));
58 #else
59     SGGuard<SGMutex> lock(mMutex);
60     return --mValue;
61 #endif
62   }
63   operator unsigned() const
64   {
65 #if defined(SGATOMIC_USE_GCC4_BUILTINS)
66     __sync_synchronize();
67     return mValue;
68 #elif defined(SGATOMIC_USE_WIN32_INTERLOCKED)
69     return static_cast<unsigned const volatile &>(mValue);
70 #else
71     SGGuard<SGMutex> lock(mMutex);
72     return mValue;
73 #endif
74  }
75
76 private:
77   SGAtomic(const SGAtomic&);
78   SGAtomic& operator=(const SGAtomic&);
79
80 #if !defined(SGATOMIC_USE_GCC4_BUILTINS) \
81   && !defined(SGATOMIC_USE_WIN32_INTERLOCKED)
82   mutable SGMutex mMutex;
83 #endif
84 #ifdef SGATOMIC_USE_WIN32_INTERLOCKED
85   __declspec(align(32))
86 #endif
87   unsigned mValue;
88 };
89
90 #endif