* src/native/jni.cpp: [OPENJDK] Implemented jni_GetDirectBufferCapacity.
[cacao.git] / src / threads / atomic.hpp
1 /* src/threads/atomic.hpp - 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 #ifdef __cplusplus
34
35 namespace Atomic_md {
36         // Machine dependent functions.
37         uint32_t compare_and_swap(volatile uint32_t *p, uint32_t oldval, uint32_t newval);
38         uint64_t compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval);
39
40         void     memory_barrier(void);
41         void     write_memory_barrier(void);
42         void     instruction_barrier(void);
43 }
44
45 namespace Atomic {
46
47         // Generic functions.
48         uint32_t generic_compare_and_swap(volatile uint32_t *p, uint32_t oldval, uint32_t newval);
49         uint64_t generic_compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval);
50         void*    generic_compare_and_swap(volatile void** p, void* oldval, void* newval);
51         void     generic_memory_barrier(void);
52
53 }
54
55 // Include machine dependent implementation.
56 #include "md-atomic.hpp"
57
58 namespace Atomic {
59
60         struct CAS_32_functor {
61                 typedef uint32_t value_type;
62                 static value_type compare_and_swap(value_type *p, value_type o, value_type n) {
63                         return Atomic_md::compare_and_swap(p, o, n);
64                 }
65         };
66
67         struct CAS_64_functor {
68                 typedef uint64_t value_type;
69                 static value_type compare_and_swap(value_type *p, value_type o, value_type n) {
70                         return Atomic_md::compare_and_swap(p, o, n);
71                 }
72         };
73
74         template<int N> class CAS_chooser;
75         template<> class CAS_chooser<4> {
76                 public:
77                         typedef CAS_32_functor the_type;
78         };
79         template<> class CAS_chooser<8> {
80                 public:
81                         typedef CAS_64_functor the_type;
82         };
83
84         template<class T> class CAS {
85                 public:
86                         typedef typename CAS_chooser<sizeof(T)>::the_type S;
87                         static T compare_and_swap(T *p, T o, T n) {
88                                 return (T) S::compare_and_swap((typename S::value_type*) p,
89                                                 (typename S::value_type) o,
90                                                 (typename S::value_type) n);
91                         }
92         };
93
94         template<class T> T compare_and_swap(T *p, T o, T n) {
95                 return CAS<T>::compare_and_swap(p, o, n);
96         }
97
98         inline void     memory_barrier(void)       { Atomic_md::memory_barrier(); }
99         inline void     write_memory_barrier(void) { Atomic_md::write_memory_barrier(); }
100         inline void     instruction_barrier(void)  { Atomic_md::instruction_barrier(); }
101 }
102
103 #else
104
105 // Legacy C interface.
106
107 uint32_t Atomic_compare_and_swap_32(uint32_t *p, uint32_t oldval, uint32_t newval);
108 uint64_t Atomic_compare_and_swap_64(uint64_t *p, uint64_t oldval, uint64_t newval);
109 void*    Atomic_compare_and_swap_ptr(void** p, void* oldval, void* newval);
110 void     Atomic_memory_barrier(void);
111 void     Atomic_write_memory_barrier(void);
112 void     Atomic_instruction_barrier(void);
113
114 #endif
115
116 #endif // _ATOMIC_HPP
117
118
119 /*
120  * These are local overrides for various environment variables in Emacs.
121  * Please do not remove this and leave it at the end of the file, where
122  * Emacs will automagically detect them.
123  * ---------------------------------------------------------------------
124  * Local variables:
125  * mode: c++
126  * indent-tabs-mode: t
127  * c-basic-offset: 4
128  * tab-width: 4
129  * End:
130  * vim:noexpandtab:sw=4:ts=4:
131  */