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