CACAO
md-atomic.hpp
Go to the documentation of this file.
1 /* src/vm/jit/mips/atomic.hpp - MIPS 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 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  __asm__ __volatile__ (
52  " .set push \n"
53  " .set mips2 \n"
54  "1: \n"
55  " ll %0,%5 \n"
56  " bne %0,%3,2f \n"
57  " move %1,%4 \n"
58  " sc %1,%2 \n"
59  " .set pop \n"
60  " beqz %1,1b \n"
61  "2: \n"
62  : "=&r" (result), "=&r" (temp), "=m" (*p)
63  : "r" (oldval), "r" (newval), "m" (*p)
64  : "memory");
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 #if SIZEOF_VOID_P == 8
82  uint64_t result;
83  uint64_t temp;
84 
85  __asm__ __volatile__ (
86  " .set push \n"
87  " .set mips2 \n"
88  "1: \n"
89  " lld %0,%5 \n"
90  " bne %0,%3,2f \n"
91  " move %1,%4 \n"
92  " scd %1,%2 \n"
93  " .set pop \n"
94  " beqz %1,1b \n"
95  "2: \n"
96  : "=&r" (result), "=&r" (temp), "=m" (*p)
97  : "r" (oldval), "r" (newval), "m" (*p)
98  : "memory");
99 
100  return result;
101 #else
102  return Atomic::generic_compare_and_swap(p, oldval, newval);
103 #endif
104 }
105 
106 
107 /**
108  * A memory barrier.
109  */
110 inline void memory_barrier(void)
111 {
112  __asm__ __volatile__ ("" : : : "memory");
113 }
114 
115 
116 /**
117  * A write memory barrier.
118  */
119 inline void write_memory_barrier(void)
120 {
121  __asm__ __volatile__ ("" : : : "memory");
122 }
123 
124 
125 /**
126  * An instruction barrier.
127  */
128 inline void instruction_barrier(void)
129 {
130  __asm__ __volatile__ ("" : : : "memory");
131 }
132 
133 }
134 
135 #endif // MD_ATOMIC_HPP_
136 
137 
138 /*
139  * These are local overrides for various environment variables in Emacs.
140  * Please do not remove this and leave it at the end of the file, where
141  * Emacs will automagically detect them.
142  * ---------------------------------------------------------------------
143  * Local variables:
144  * mode: c++
145  * indent-tabs-mode: t
146  * c-basic-offset: 4
147  * tab-width: 4
148  * End:
149  * vim:noexpandtab:sw=4:ts=4:
150  */
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
uint32_t generic_compare_and_swap(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
A generic atomic compare and swap for 32-bit integer values.
Definition: atomic.cpp:48