Merged with tip.
[cacao.git] / src / vm / jit / powerpc64 / md-atomic.hpp
1 /* src/vm/jit/powerpc64/atomic.hpp - PowerPC64 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 temp;
48         uint32_t result;
49
50         __asm__ __volatile__ (
51                 "1:                   \n"
52                 "    lwarx  %0,0,%4   \n"
53                 "    subf.  %1,%0,%2  \n"
54                 "    bne-   2f        \n"
55                 "    or     %1,%3,%3  \n"
56                 "    stwcx. %1,0,%4   \n"
57                 "    bne-   1b        \n"
58                 "2:                   \n"
59                 : "=&r" (result), "=&r" (temp)
60                 : "r" (oldval), "r" (newval), "r" (p)
61                 : "cr0", "memory");
62
63         return result;
64 }
65
66
67 /**
68  * An atomic compare and swap for 64-bit integer values.
69  *
70  * @param p      Pointer to memory address.
71  * @param oldval Old value to be expected.
72  * @param newval New value to be stored.
73  *
74  * @return value of the memory location before the store
75  */
76 inline uint64_t Atomic::compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
77 {
78         uint64_t temp;
79         uint64_t result;
80
81         __asm__ __volatile__ (
82                 "1:                   \n"
83                 "    ldarx  %0,0,%4   \n"
84                 "    subf.  %1,%0,%2  \n"
85                 "    bne-   2f        \n"
86                 "    or     %1,%3,%3  \n"
87                 "    stdcx. %1,0,%4   \n"
88                 "    bne-   1b        \n"
89                 "2:                   \n"
90                 : "=&r" (result), "=&r" (temp)
91                 : "r" (oldval), "r" (newval), "r" (p)
92                 : "cr0", "memory");
93
94         return result;
95 }
96
97
98 /**
99  * An atomic compare and swap for pointer values.
100  *
101  * @param p      Pointer to memory address.
102  * @param oldval Old value to be expected.
103  * @param newval New value to be stored.
104  *
105  * @return value of the memory location before the store
106  */
107 inline void* Atomic::compare_and_swap(volatile void** p, void* oldval, void* newval)
108 {
109         return (void*) compare_and_swap((volatile uint64_t*) p, (uint64_t) oldval, (uint64_t) newval);
110 }
111
112
113 /**
114  * A memory barrier.
115  */
116 inline void Atomic::memory_barrier(void)
117 {
118         __asm__ __volatile__ ("sync" : : : "memory");
119 }
120
121
122 /**
123  * A write memory barrier.
124  */
125 inline void Atomic::write_memory_barrier(void)
126 {
127         __asm__ __volatile__ ("" : : : "memory");
128 }
129
130
131 /**
132  * An instruction memory barrier.
133  */
134 inline void Atomic::instruction_barrier(void)
135 {
136         __asm__ __volatile__ ("isync" : : : "memory");
137 }
138
139 #endif // _MD_ATOMIC_HPP
140
141
142 /*
143  * These are local overrides for various environment variables in Emacs.
144  * Please do not remove this and leave it at the end of the file, where
145  * Emacs will automagically detect them.
146  * ---------------------------------------------------------------------
147  * Local variables:
148  * mode: c++
149  * indent-tabs-mode: t
150  * c-basic-offset: 4
151  * tab-width: 4
152  * End:
153  * vim:noexpandtab:sw=4:ts=4:
154  */