1 /*
2 * Copyright (C) 2008 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29 #include <cxxtools/atomicity.gcc.x86_64.h>
30 #include <unistd.h>
31
32 namespace cxxtools {
33
34
atomicGet(volatile atomic_t & val)35 atomic_t atomicGet(volatile atomic_t& val)
36 {
37 asm volatile ("mfence" : : : "memory");
38 return val;
39 }
40
41
atomicSet(volatile atomic_t & val,atomic_t n)42 void atomicSet(volatile atomic_t& val, atomic_t n)
43 {
44 val = n;
45 asm volatile ("mfence" : : : "memory");
46 }
47
48
atomicIncrement(volatile atomic_t & val)49 atomic_t atomicIncrement(volatile atomic_t& val)
50 {
51 static const atomic_t d = 1;
52 atomic_t tmp;
53
54 asm volatile ("lock; xaddq %0, %1"
55 : "=r" (tmp), "=m" (val)
56 : "0" (d), "m" (val));
57
58 return tmp+1;
59 }
60
atomicDecrement(volatile atomic_t & val)61 atomic_t atomicDecrement(volatile atomic_t& val)
62 {
63 static const atomic_t d = -1;
64 volatile register atomic_t tmp;
65
66 asm volatile ("lock; xaddq %0, %1"
67 : "=r" (tmp), "=m" (val)
68 : "0" (d), "m" (val));
69
70 return tmp-1;
71 }
72
73
atomicExchangeAdd(volatile atomic_t & val,atomic_t add)74 atomic_t atomicExchangeAdd(volatile atomic_t& val, atomic_t add)
75 {
76 volatile register atomic_t ret;
77
78 asm volatile ("lock; xaddq %0, %1"
79 : "=r" (ret), "=m" (val)
80 : "0" (add), "m" (val));
81
82 return ret;
83 }
84
85
atomicExchange(volatile atomic_t & val,atomic_t new_val)86 atomic_t atomicExchange(volatile atomic_t& val, atomic_t new_val)
87 {
88 volatile register atomic_t ret;
89
90 // using cmpxchg and a loop here on purpose
91 asm volatile ("1:; lock; cmpxchgq %2, %0; jne 1b"
92 : "=m" (val), "=a" (ret)
93 : "r" (new_val), "m" (val), "a" (val));
94
95 return ret;
96 }
97
98
atomicExchange(void * volatile & val,void * new_val)99 void* atomicExchange(void* volatile& val, void* new_val)
100 {
101 void* ret;
102
103 asm volatile ("1:; lock; cmpxchgq %2, %0; jne 1b"
104 : "=m" (val), "=a" (ret)
105 : "r" (new_val), "m" (val), "a" (val));
106
107 return ret;
108 }
109
110
atomicCompareExchange(volatile atomic_t & dest,atomic_t exch,atomic_t comp)111 atomic_t atomicCompareExchange(volatile atomic_t& dest, atomic_t exch, atomic_t comp)
112 {
113 volatile register atomic_t old;
114
115 asm volatile ("lock; cmpxchgq %2, %0"
116 : "=m" (dest), "=a" (old)
117 : "r" (exch), "m" (dest), "a" (comp));
118 return old;
119 }
120
121
atomicCompareExchange(void * volatile & dest,void * exch,void * comp)122 void* atomicCompareExchange(void* volatile& dest, void* exch, void* comp)
123 {
124 void* old;
125
126 asm volatile ("lock; cmpxchgq %2, %0"
127 : "=m" (dest), "=a" (old)
128 : "r" (exch), "m" (dest), "a" (comp));
129 return old;
130 }
131
132 } // namespace cxxtools
133