bfe72af8040073b016d06ae14708b4041d428283
[cacao.git] / src / threads / atomic.cpp
1 /* src/threads/atomic.cpp - 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 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "threads/atomic.hpp"
31 #include "threads/mutex.hpp"
32
33 // Gobal mutex for generic atomic instructions.
34 static Mutex lock;
35
36
37 /**
38  * A generic atomic compare and swap for 32-bit integer values.  This
39  * function is using a mutex to provide atomicity.
40  *
41  * @param p      Pointer to memory address.
42  * @param oldval Old value to be expected.
43  * @param newval New value to be stored.
44  *
45  * @return value of the memory location before the store
46  */
47 uint32_t Atomic::generic_compare_and_swap(volatile uint32_t *p, uint32_t oldval, uint32_t newval)
48 {
49         uint32_t result;
50
51         lock.lock();
52
53         // Do the compare-and-swap.
54
55         result = *p;
56
57         if (oldval == result)
58                 *p = newval;
59
60         lock.unlock();
61
62         return result;
63 }
64
65
66 /**
67  * A generic atomic compare and swap for 64-bit integer values.  This
68  * function is using a mutex to provide atomicity.
69  *
70  * @param p      Pointer to memory address.
71  * @param oldval Old value to be expected.
72  * @param newval New value to be stored.
73  *
74  * @return value of the memory location before the store
75  */
76 uint64_t Atomic::generic_compare_and_swap(volatile uint64_t *p, uint64_t oldval, uint64_t newval)
77 {
78         uint64_t result;
79
80         lock.lock();
81
82         // Do the compare-and-swap.
83
84         result = *p;
85
86         if (oldval == result)
87                 *p = newval;
88
89         lock.unlock();
90
91         return result;
92 }
93
94
95 /**
96  * A generic atomic compare and swap for pointer values.  This
97  * function is using a mutex to provide atomicity.
98  *
99  * @param p      Pointer to memory address.
100  * @param oldval Old value to be expected.
101  * @param newval New value to be stored.
102  *
103  * @return value of the memory location before the store
104  */
105 void* Atomic::generic_compare_and_swap(volatile void** p, void* oldval, void* newval)
106 {
107         void* result;
108
109         lock.lock();
110
111         // Do the compare-and-swap.
112
113         result = (void*) *p;
114
115         if (oldval == result)
116                 *p = newval;
117
118         lock.unlock();
119
120         return result;
121 }
122
123
124 /**
125  * A generic memory barrier.  This function is using a mutex to
126  * provide atomicity.
127  */
128 void Atomic::generic_memory_barrier(void)
129 {
130         lock.lock();
131         lock.unlock();
132 }
133
134
135 // Legacy C interface.
136
137 extern "C" {
138 uint32_t Atomic_compare_and_swap_32(volatile uint32_t *p, uint32_t oldval, uint32_t newval) { return Atomic::compare_and_swap(p, oldval, newval); }
139 uint64_t Atomic_compare_and_swap_64(volatile uint64_t *p, uint64_t oldval, uint64_t newval) { return Atomic::compare_and_swap(p, oldval, newval); }
140 void*    Atomic_compare_and_swap_ptr(volatile void** p, void* oldval, void* newval) { return Atomic::compare_and_swap(p, oldval, newval); }
141 void     Atomic_memory_barrier(void) { Atomic::memory_barrier(); }
142 void     Atomic_write_memory_barrier(void) { Atomic::write_memory_barrier(); }
143 void     Atomic_instruction_barrier(void) { Atomic::instruction_barrier(); }
144 }
145
146
147 /*
148  * These are local overrides for various environment variables in Emacs.
149  * Please do not remove this and leave it at the end of the file, where
150  * Emacs will automagically detect them.
151  * ---------------------------------------------------------------------
152  * Local variables:
153  * mode: c++
154  * indent-tabs-mode: t
155  * c-basic-offset: 4
156  * tab-width: 4
157  * End:
158  * vim:noexpandtab:sw=4:ts=4:
159  */