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