* Merged in new atomic instructions (twisti branch).
[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
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 static uint32_t Atomic_compare_and_swap_32(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
46 {
47         uint32_t result;
48         uint32_t temp;
49
50         /* TODO: improve this one! */
51         __asm__ __volatile__ (
52                 "1:\t"
53                 "ldr   %0,[%2]\n\t"
54                 "cmp   %0,%4\n\t"
55                 "bne   2f\n\t"
56                 "swp   %1,%3,[%2]\n\t"
57                 "cmp   %1,%0\n\t"
58                 "swpne %0,%1,[%2]\n\t"
59                 "bne   1b\n\t"
60                 "2:"
61                 : "=&r" (result), "=&r" (temp)
62                 : "r" (p), "r" (newval), "r" (oldval)
63                 : "cc", "memory"
64         );
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 static uint64_t Atomic_compare_and_swap_64(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
80 {
81         return Atomic_generic_compare_and_swap_64(p, oldval, newval);
82 }
83
84
85 /**
86  * An atomic compare and swap for pointer values.
87  *
88  * @param p      Pointer to memory address.
89  * @param oldval Old value to be expected.
90  * @param newval New value to be stored.
91  *
92  * @return value of the memory location before the store
93  */
94 inline static void* Atomic_compare_and_swap_ptr(volatile void** p, void* oldval, void* newval)
95 {
96         return (void*) Atomic_compare_and_swap_32((volatile uint32_t*) p, (uint32_t) oldval, (uint32_t) newval);
97 }
98
99
100 /**
101  * A memory barrier.
102  */
103 inline static void Atomic_memory_barrier(void)
104 {
105         __asm__ __volatile__ ("" : : : "memory");
106 }
107
108
109 #define STORE_ORDER_BARRIER() __asm__ __volatile__ ("" : : : "memory");
110 #define MEMORY_BARRIER_AFTER_ATOMIC() __asm__ __volatile__ ("" : : : "memory");
111
112 #endif // _MD_ATOMIC_HPP
113
114
115 /*
116  * These are local overrides for various environment variables in Emacs.
117  * Please do not remove this and leave it at the end of the file, where
118  * Emacs will automagically detect them.
119  * ---------------------------------------------------------------------
120  * Local variables:
121  * mode: c++
122  * indent-tabs-mode: t
123  * c-basic-offset: 4
124  * tab-width: 4
125  * End:
126  * vim:noexpandtab:sw=4:ts=4:
127  */