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