Stronger assembler constraints for cmpxchg on i386/x86_64.
[cacao.git] / src / vm / jit / x86_64 / md-atomic.hpp
1 /* src/vm/jit/x86_64/atomic.hpp - x86_64 atomic instructions
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _MD_ATOMIC_HPP
27 #define _MD_ATOMIC_HPP
28
29 #include "config.h"
30
31 #include <stdint.h>
32
33 #include "threads/atomic.hpp"
34
35
36 /**
37  * An atomic compare and swap for 32-bit integer values.
38  *
39  * @param p      Pointer to memory address.
40  * @param oldval Old value to be expected.
41  * @param newval New value to be stored.
42  *
43  * @return value of the memory location before the store
44  */
45 inline uint32_t Atomic::compare_and_swap(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
46 {
47         uint32_t result;
48
49         __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
50                                                   : "=a" (result), "=m" (*p)
51                                                   : "r" (newval), "m" (*p), "0" (oldval)
52                                                   : "cc");
53
54         return result;
55 }
56
57
58 /**
59  * An atomic compare and swap for 64-bit integer values.
60  *
61  * @param p      Pointer to memory address.
62  * @param oldval Old value to be expected.
63  * @param newval New value to be stored.
64  *
65  * @return value of the memory location before the store
66  */
67 inline uint64_t Atomic::compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
68 {
69         uint64_t result;
70
71         __asm__ __volatile__ ("lock; cmpxchgq %2, %1"
72                                                   : "=a" (result), "=m" (*p)
73                                                   : "r" (newval), "m" (*p), "0" (oldval)
74                                                   : "cc");
75
76         return result;
77 }
78
79
80 /**
81  * An atomic compare and swap for pointer values.
82  *
83  * @param p      Pointer to memory address.
84  * @param oldval Old value to be expected.
85  * @param newval New value to be stored.
86  *
87  * @return value of the memory location before the store
88  */
89 inline void* Atomic::compare_and_swap(volatile void** p, void* oldval, void* newval)
90 {
91         return (void*) compare_and_swap((volatile uint64_t*) p, (uint64_t) oldval, (uint64_t) newval);
92 }
93
94
95 /**
96  * A memory barrier.
97  */
98 inline void Atomic::memory_barrier(void)
99 {
100         __asm__ __volatile__ ("mfence" : : : "memory");
101 }
102
103
104 /**
105  * A write memory barrier.
106  */
107 inline void Atomic::write_memory_barrier(void)
108 {
109         __asm__ __volatile__ ("" : : : "memory");
110 }
111
112
113 /**
114  * An instruction barrier.
115  */
116 inline void Atomic::instruction_barrier(void)
117 {
118         // We need the "memory" constraint here because compare_and_swap does not
119         // have it.
120         __asm__ __volatile__ ("" : : : "memory");
121 }
122
123 #endif // _MD_ATOMIC_HPP
124
125
126 /*
127  * These are local overrides for various environment variables in Emacs.
128  * Please do not remove this and leave it at the end of the file, where
129  * Emacs will automagically detect them.
130  * ---------------------------------------------------------------------
131  * Local variables:
132  * mode: c++
133  * indent-tabs-mode: t
134  * c-basic-offset: 4
135  * tab-width: 4
136  * End:
137  * vim:noexpandtab:sw=4:ts=4:
138  */