f0e74eaa695b62b92dfdc9ac3bcaad412a77b2a9
[cacao.git] / src / vm / jit / arm / md-atomic.hpp
1 /* src/vm/jit/arm/atomic.hpp - ARM 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 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         uint32_t temp;
50
51         /* TODO: improve this one! */
52         __asm__ __volatile__ (
53                 "1:\t"
54                 "ldr   %0,[%2]\n\t"
55                 "cmp   %0,%4\n\t"
56                 "bne   2f\n\t"
57                 "swp   %1,%3,[%2]\n\t"
58                 "cmp   %1,%0\n\t"
59                 "swpne %0,%1,[%2]\n\t"
60                 "bne   1b\n\t"
61                 "2:"
62                 : "=&r" (result), "=&r" (temp)
63                 : "r" (p), "r" (newval), "r" (oldval)
64                 : "cc", "memory"
65         );
66
67         return result;
68 }
69
70
71 /**
72  * An atomic compare and swap for 64-bit integer values.
73  *
74  * @param p      Pointer to memory address.
75  * @param oldval Old value to be expected.
76  * @param newval New value to be stored.
77  *
78  * @return value of the memory location before the store
79  */
80 inline uint64_t compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
81 {
82         return Atomic::generic_compare_and_swap(p, oldval, newval);
83 }
84
85
86 /**
87  * A memory barrier.
88  */
89 inline void memory_barrier(void)
90 {
91         __asm__ __volatile__ ("" : : : "memory");
92 }
93
94
95 /**
96  * A write memory barrier.
97  */
98 inline void write_memory_barrier(void)
99 {
100         __asm__ __volatile__ ("" : : : "memory");
101 }
102
103
104 /**
105  * An instruction barrier.
106  */
107 inline void instruction_barrier(void)
108 {
109         __asm__ __volatile__ ("" : : : "memory");
110 }
111
112 }
113
114 #endif // _MD_ATOMIC_HPP
115
116
117 /*
118  * These are local overrides for various environment variables in Emacs.
119  * Please do not remove this and leave it at the end of the file, where
120  * Emacs will automagically detect them.
121  * ---------------------------------------------------------------------
122  * Local variables:
123  * mode: c++
124  * indent-tabs-mode: t
125  * c-basic-offset: 4
126  * tab-width: 4
127  * End:
128  * vim:noexpandtab:sw=4:ts=4:
129  */