]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGAtomic.cxx
Working 'noshadow' animation
[simgear.git] / simgear / structure / SGAtomic.cxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2005-2009,2011 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 #ifdef HAVE_CONFIG_H
22 #  include <simgear_config.h>
23 #endif
24
25 #include "SGAtomic.hxx"
26
27 #if defined(SGATOMIC_USE_LIBRARY_FUNCTIONS)
28
29 #if defined(_WIN32)
30 # include <windows.h>
31 #elif defined(GCC_ATOMIC_BUILTINS_FOUND)
32 #elif defined(__GNUC__) && defined(__i386__)
33 #elif defined(SGATOMIC_USE_MUTEX)
34 # include <simgear/threads/SGGuard.hxx>
35 #else
36 # error
37 #endif
38
39 unsigned
40 SGAtomic::operator++()
41 {
42 #if defined(_WIN32)
43     return InterlockedIncrement(reinterpret_cast<long volatile*>(&mValue));
44 #elif defined(GCC_ATOMIC_BUILTINS_FOUND)
45     return __sync_add_and_fetch(&mValue, 1);
46 #elif defined(__GNUC__) && defined(__i386__)
47     register volatile unsigned* mem = reinterpret_cast<volatile unsigned*>(&mValue);
48     register unsigned result;
49     __asm__ __volatile__("lock; xadd{l} {%0,%1|%1,%0}"
50                          : "=r" (result), "=m" (*mem)
51                          : "0" (1), "m" (*mem)
52                          : "memory");
53     return result + 1;
54 #else
55     SGGuard<SGMutex> lock(mMutex);
56     return ++mValue;
57 #endif
58 }
59
60 unsigned
61 SGAtomic::operator--()
62 {
63 #if defined(_WIN32)
64     return InterlockedDecrement(reinterpret_cast<long volatile*>(&mValue));
65 #elif defined(GCC_ATOMIC_BUILTINS_FOUND)
66     return __sync_sub_and_fetch(&mValue, 1);
67 #elif defined(__GNUC__) && defined(__i386__)
68     register volatile unsigned* mem = reinterpret_cast<volatile unsigned*>(&mValue);
69     register unsigned result;
70     __asm__ __volatile__("lock; xadd{l} {%0,%1|%1,%0}"
71                          : "=r" (result), "=m" (*mem)
72                          : "0" (-1), "m" (*mem)
73                          : "memory");
74     return result - 1;
75 #else
76     SGGuard<SGMutex> lock(mMutex);
77     return --mValue;
78 #endif
79 }
80
81 SGAtomic::operator unsigned() const
82 {
83 #if defined(_WIN32)
84     return static_cast<unsigned const volatile &>(mValue);
85 #elif defined(GCC_ATOMIC_BUILTINS_FOUND)
86     __sync_synchronize();
87     return mValue;
88 #elif defined(__GNUC__) && defined(__i386__)
89     __asm__ __volatile__("": : : "memory");
90     return mValue;
91 #else
92     SGGuard<SGMutex> lock(mMutex);
93     return mValue;
94 #endif
95 }
96
97 bool
98 SGAtomic::compareAndExchange(unsigned oldValue, unsigned newValue)
99 {
100 #if defined(_WIN32)
101     long volatile* lvPtr = reinterpret_cast<long volatile*>(&mValue);
102     return oldValue == InterlockedCompareExchange(lvPtr, newValue, oldValue);
103 #elif defined(GCC_ATOMIC_BUILTINS_FOUND)
104     return __sync_bool_compare_and_swap(&mValue, oldValue, newValue);
105 #elif defined(__GNUC__) && defined(__i386__)
106     register volatile unsigned* mem = reinterpret_cast<volatile unsigned*>(&mValue);
107     unsigned before;
108     __asm__ __volatile__("lock; cmpxchg{l} {%1,%2|%1,%2}"
109                          : "=a"(before)
110                          : "q"(newValue), "m"(*mem), "0"(oldValue)
111                          : "memory");
112     return before == oldValue;
113 #else
114     SGGuard<SGMutex> lock(mMutex);
115     if (mValue != oldValue)
116         return false;
117     mValue = newValue;
118     return true;
119 #endif
120 }
121
122 #endif