CACAO
md-atomic.hpp
Go to the documentation of this file.
1 /* src/vm/jit/alpha/atomic.hpp - Alpha 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 #include "vm/os.hpp"
36 
37 namespace Atomic_md {
38 
39 /**
40  * An atomic compare and swap for 32-bit integer values.
41  *
42  * @param p Pointer to memory address.
43  * @param oldval Old value to be expected.
44  * @param newval New value to be stored.
45  *
46  * @return value of the memory location before the store
47  */
48 inline uint32_t compare_and_swap(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
49 {
50  uint32_t temp;
51  uint32_t result;
52 
53  __asm__ __volatile__ (
54  "1: \n"
55  " ldxr %0, %5 \n"
56  " cmp %0, %3 \n"
57  " b.ne 2f \n"
58  " mov %2, %4 \n"
59  " stxr w9, %2, %1 \n"
60  " cbnz w9, 1b \n"
61  "2: \n"
62  : "=&r" (result), "=m" (*p), "=&r" (temp)
63  : "r" (oldval), "r" (newval), "m" (*p)
64  : "w9");
65 
66  return result;
67 }
68 
69 
70 /**
71  * An atomic compare and swap for 64-bit integer values.
72  *
73  * @param p Pointer to memory address.
74  * @param oldval Old value to be expected.
75  * @param newval New value to be stored.
76  *
77  * @return value of the memory location before the store
78  */
79 inline uint64_t compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
80 {
81  uint64_t temp;
82  uint64_t result;
83 
84  __asm__ __volatile__ (
85  "1: \n"
86  " ldxr %0, %5 \n"
87  " cmp %0, %3 \n"
88  " b.ne 2f \n"
89  " mov %2, %4 \n"
90  " stxr w9, %2, %1 \n"
91  " cbnz w9, 1b \n"
92  "2: \n"
93  : "=&r" (result), "=m" (*p), "=&r" (temp)
94  : "r" (oldval), "r" (newval), "m" (*p)
95  : "w9");
96 
97  return result;
98 }
99 
100 
101 /**
102  * A memory barrier.
103  */
104 inline void memory_barrier(void)
105 {
106  //__asm__ __volatile__ ("dsb LD" : : : "memory");
107  __asm__ __volatile__ ("dmb SY" : : : "memory");
108 }
109 
110 
111 /**
112  * A write memory barrier.
113  */
114 inline void write_memory_barrier(void)
115 {
116  // __asm__ __volatile__ ("dsb ST" : : : "memory");
117  __asm__ __volatile__ ("dmb SY" : : : "memory");
118 }
119 
120 
121 /**
122  * An instruction barrier.
123  */
124 inline void instruction_barrier(void)
125 {
126  __asm__ __volatile__ ("isb" : : : "memory");
127 }
128 
129 }
130 
131 #endif // MD_ATOMIC_HPP_
132 
133 
134 /*
135  * These are local overrides for various environment variables in Emacs.
136  * Please do not remove this and leave it at the end of the file, where
137  * Emacs will automagically detect them.
138  * ---------------------------------------------------------------------
139  * Local variables:
140  * mode: c++
141  * indent-tabs-mode: t
142  * c-basic-offset: 4
143  * tab-width: 4
144  * End:
145  * vim:noexpandtab:sw=4:ts=4:
146  */
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