* src/native/vm/sun_misc_Unsafe.c: Use new atomic primitve functions.
[cacao.git] / src / vm / jit / x86_64 / atomic.hpp
1 /* src/vm/jit/i386/atomic.hpp - i386 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 _ATOMIC_HPP
27 #define _ATOMIC_HPP
28
29 #include "config.h"
30
31 #include <stdint.h>
32
33
34 /**
35  * An atomic compare and swap for 32-bit integer values.
36  *
37  * @param p      Pointer to memory address.
38  * @param oldval Old value to be expected.
39  * @param newval New value to be stored.
40  *
41  * @return value of the memory location before the store
42  */
43 inline static uint32_t Atomic_cas_32(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
44 {
45         int32_t result;
46
47         __asm__ __volatile__ ("lock; cmpxchgl %2, %1"
48                                                   : "=a" (result), "=m" (*p)
49                                                   : "r" (newval), "m" (*p), "0" (oldval));
50
51         return result;
52 }
53
54
55 /**
56  * An atomic compare and swap for 64-bit integer values.
57  *
58  * @param p      Pointer to memory address.
59  * @param oldval Old value to be expected.
60  * @param newval New value to be stored.
61  *
62  * @return value of the memory location before the store
63  */
64 inline static uint64_t Atomic_cas_64(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
65 {
66         int64_t result;
67
68         __asm__ __volatile__ ("lock; cmpxchgq %2, %1"
69                                                   : "=a" (result), "=m" (*p)
70                                                   : "r" (newval), "m" (*p), "0" (oldval));
71
72         return result;
73 }
74
75
76 /**
77  * An atomic compare and swap for pointer values.
78  *
79  * @param p      Pointer to memory address.
80  * @param oldval Old value to be expected.
81  * @param newval New value to be stored.
82  *
83  * @return value of the memory location before the store
84  */
85 inline static void* Atomic_cas_ptr(volatile void** p, void* oldval, void* newval)
86 {
87         return (void*) Atomic_cas_64((volatile uint64_t*) p, (uint64_t) oldval, (uint64_t) newval);
88 }
89
90
91 /**
92  * A memory barrier.
93  */
94 inline static void Atomic_mb(void)
95 {
96         __asm__ __volatile__ ("mfence" : : : "memory");
97 }
98
99
100 #define STORE_ORDER_BARRIER() __asm__ __volatile__ ("" : : : "memory");
101 #define MEMORY_BARRIER_AFTER_ATOMIC() /* nothing */
102
103 #endif /* _ATOMIC_HPP */
104
105
106 /*
107  * These are local overrides for various environment variables in Emacs.
108  * Please do not remove this and leave it at the end of the file, where
109  * Emacs will automagically detect them.
110  * ---------------------------------------------------------------------
111  * Local variables:
112  * mode: c++
113  * indent-tabs-mode: t
114  * c-basic-offset: 4
115  * tab-width: 4
116  * End:
117  * vim:noexpandtab:sw=4:ts=4:
118  */