]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGAtomic.cxx
Merge branch 'topic/gcintersect' into next
[simgear.git] / simgear / structure / SGAtomic.cxx
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 #include "SGAtomic.hxx"
22
23 #if defined(SGATOMIC_USE_GCC4_BUILTINS) && defined(__i386__)
24
25 // Usually the apropriate functions are inlined by gcc.
26 // But if gcc is called with something aequivalent to -march=i386,
27 // it will not assume that there is a lock instruction and instead
28 // calls this pair of functions. We will provide them here in this case.
29 // Note that this assembler code will not work on a i386 chip anymore.
30 // But I hardly believe that we can assume to run at least on a i486 ...
31
32 extern "C" {
33
34 unsigned __sync_sub_and_fetch_4(volatile void *ptr, unsigned value)
35 {
36   register volatile unsigned* mem = reinterpret_cast<volatile unsigned*>(ptr);
37   register unsigned result;
38   __asm__ __volatile__("lock; xadd{l} {%0,%1|%1,%0}"
39                        : "=r" (result), "=m" (*mem)
40                        : "0" (-value), "m" (*mem)
41                        : "memory");
42   return result - value;
43 }
44
45 unsigned __sync_add_and_fetch_4(volatile void *ptr, unsigned value)
46 {
47   register volatile unsigned* mem = reinterpret_cast<volatile unsigned*>(ptr);
48   register unsigned result;
49   __asm__ __volatile__("lock; xadd{l} {%0,%1|%1,%0}"
50                        : "=r" (result), "=m" (*mem)
51                        : "0" (value), "m" (*mem)
52                        : "memory");
53   return result + value;
54 }
55
56 void __sync_synchronize()
57 {
58   __asm__ __volatile__("": : : "memory");
59 }
60
61 } // extern "C"
62
63 #endif