CACAO
md-atomic.hpp
Go to the documentation of this file.
1 /* src/vm/jit/x86_64/atomic.hpp - x86_64 atomic instructions
2 
3  Copyright (C) 1996-2013
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_ 1
28 
29 #include "config.h"
30 
31 #include <stdint.h>
32 
33 #include "threads/atomic.hpp"
34 
35 namespace Atomic_md {
36 
37 /**
38  * An atomic compare and swap for 32-bit integer values.
39  *
40  * @param p Pointer to memory address.
41  * @param oldval Old value to be expected.
42  * @param newval New value to be stored.
43  *
44  * @return value of the memory location before the store
45  */
46 inline uint32_t compare_and_swap(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
47 {
48  uint32_t result;
49 
50  __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
51  : "=a" (result), "=m" (*p)
52  : "r" (newval), "m" (*p), "0" (oldval)
53  : "cc");
54 
55  return result;
56 }
57 
58 
59 /**
60  * An atomic compare and swap for 64-bit integer values.
61  *
62  * @param p Pointer to memory address.
63  * @param oldval Old value to be expected.
64  * @param newval New value to be stored.
65  *
66  * @return value of the memory location before the store
67  */
68 inline uint64_t compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
69 {
70  uint64_t result;
71 
72  __asm__ __volatile__ ("lock; cmpxchgq %2, %1"
73  : "=a" (result), "=m" (*p)
74  : "r" (newval), "m" (*p), "0" (oldval)
75  : "cc");
76 
77  return result;
78 }
79 
80 
81 /**
82  * A memory barrier.
83  */
84 inline void memory_barrier(void)
85 {
86  __asm__ __volatile__ ("mfence" : : : "memory");
87 }
88 
89 
90 /**
91  * A write memory barrier.
92  */
93 inline void write_memory_barrier(void)
94 {
95  __asm__ __volatile__ ("" : : : "memory");
96 }
97 
98 
99 /**
100  * An instruction barrier.
101  */
102 inline void instruction_barrier(void)
103 {
104  // We need the "memory" constraint here because compare_and_swap does not
105  // have it.
106  __asm__ __volatile__ ("" : : : "memory");
107 }
108 
109 }
110 
111 #endif // MD_ATOMIC_HPP_
112 
113 
114 /*
115  * These are local overrides for various environment variables in Emacs.
116  * Please do not remove this and leave it at the end of the file, where
117  * Emacs will automagically detect them.
118  * ---------------------------------------------------------------------
119  * Local variables:
120  * mode: c++
121  * indent-tabs-mode: t
122  * c-basic-offset: 4
123  * tab-width: 4
124  * End:
125  * vim:noexpandtab:sw=4:ts=4:
126  */
uint32_t compare_and_swap(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
An atomic compare and swap for 32-bit integer values.
Definition: md-atomic.hpp:48
void memory_barrier(void)
A memory barrier.
Definition: md-atomic.hpp:104
void write_memory_barrier(void)
A write memory barrier.
Definition: md-atomic.hpp:114
void instruction_barrier(void)
An instruction barrier.
Definition: md-atomic.hpp:124