CACAO
atomic.hpp
Go to the documentation of this file.
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;
61  return Atomic_md::compare_and_swap(p, o, n);
62  }
63  };
64 
65  struct CAS_64_functor {
66  typedef uint64_t value_type;
68  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:
76  };
77  template<> class CAS_chooser<8> {
78  public:
80  };
81 
82  template<class T> class CAS {
83  public:
85  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  (typename S::value_type) n);
89  }
90  };
91 
92  template<class T> T compare_and_swap(T *p, T o, T n) {
93  return CAS<T>::compare_and_swap(p, o, n);
94  }
95 
96  inline void memory_barrier(void) { Atomic_md::memory_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  */
void write_memory_barrier(void)
Definition: atomic.hpp:97
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
T compare_and_swap(T *p, T o, T n)
Definition: atomic.hpp:92
static value_type compare_and_swap(value_type *p, value_type o, value_type n)
Definition: atomic.hpp:67
void memory_barrier(void)
A memory barrier.
Definition: md-atomic.hpp:104
static value_type compare_and_swap(value_type *p, value_type o, value_type n)
Definition: atomic.hpp:60
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
void memory_barrier(void)
Definition: atomic.hpp:96
static T compare_and_swap(T *p, T o, T n)
Definition: atomic.hpp:85
CAS_64_functor the_type
Definition: atomic.hpp:79
uint64_t compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
An atomic compare and swap for 64-bit integer values.
Definition: md-atomic.hpp:76
void generic_memory_barrier(void)
A generic memory barrier.
Definition: atomic.cpp:129
CAS_32_functor the_type
Definition: atomic.hpp:75
CAS_chooser< sizeof(T)>::the_type S
Definition: atomic.hpp:84
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
void instruction_barrier(void)
Definition: atomic.hpp:98