Merge "Better solution for Docomo 1339 bug." into tizen_2.1
[framework/web/webkit-efl.git] / Source / WTF / wtf / Atomics.h
1 /*
2  * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer. 
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution. 
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission. 
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *
30  * Note: The implementations of InterlockedIncrement and InterlockedDecrement are based
31  * on atomic_increment and atomic_exchange_and_add from the Boost C++ Library. The license
32  * is virtually identical to the Apple license above but is included here for completeness.
33  *
34  * Boost Software License - Version 1.0 - August 17th, 2003
35  * 
36  * Permission is hereby granted, free of charge, to any person or organization
37  * obtaining a copy of the software and accompanying documentation covered by
38  * this license (the "Software") to use, reproduce, display, distribute,
39  * execute, and transmit the Software, and to prepare derivative works of the
40  * Software, and to permit third-parties to whom the Software is furnished to
41  * do so, all subject to the following:
42  * 
43  * The copyright notices in the Software and this entire statement, including
44  * the above license grant, this restriction and the following disclaimer,
45  * must be included in all copies of the Software, in whole or in part, and
46  * all derivative works of the Software, unless such copies or derivative
47  * works are solely in the form of machine-executable object code generated by
48  * a source language processor.
49  * 
50  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
53  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
54  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
55  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
56  * DEALINGS IN THE SOFTWARE.
57  */
58
59 #ifndef Atomics_h
60 #define Atomics_h
61
62 #include <wtf/Platform.h>
63 #include <wtf/StdLibExtras.h>
64 #include <wtf/UnusedParam.h>
65
66 #if OS(WINDOWS)
67 #include <windows.h>
68 #elif OS(DARWIN)
69 #include <libkern/OSAtomic.h>
70 #elif OS(QNX)
71 #include <atomic.h>
72 #elif OS(ANDROID)
73 #include <sys/atomics.h>
74 #elif COMPILER(GCC)
75 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2))
76 #include <ext/atomicity.h>
77 #else
78 #include <bits/atomicity.h>
79 #endif
80 #endif
81
82 namespace WTF {
83
84 #if OS(WINDOWS)
85 #define WTF_USE_LOCKFREE_THREADSAFEREFCOUNTED 1
86
87 #if COMPILER(MINGW) || COMPILER(MSVC7_OR_LOWER) || OS(WINCE)
88 inline int atomicIncrement(int* addend) { return InterlockedIncrement(reinterpret_cast<long*>(addend)); }
89 inline int atomicDecrement(int* addend) { return InterlockedDecrement(reinterpret_cast<long*>(addend)); }
90 #else
91 inline int atomicIncrement(int volatile* addend) { return InterlockedIncrement(reinterpret_cast<long volatile*>(addend)); }
92 inline int atomicDecrement(int volatile* addend) { return InterlockedDecrement(reinterpret_cast<long volatile*>(addend)); }
93 #endif
94
95 #elif OS(DARWIN)
96 #define WTF_USE_LOCKFREE_THREADSAFEREFCOUNTED 1
97
98 inline int atomicIncrement(int volatile* addend) { return OSAtomicIncrement32Barrier(const_cast<int*>(addend)); }
99 inline int atomicDecrement(int volatile* addend) { return OSAtomicDecrement32Barrier(const_cast<int*>(addend)); }
100
101 #elif OS(QNX)
102 #define WTF_USE_LOCKFREE_THREADSAFEREFCOUNTED 1
103
104 // Note, atomic_{add, sub}_value() return the previous value of addend's content.
105 inline int atomicIncrement(int volatile* addend) { return static_cast<int>(atomic_add_value(reinterpret_cast<unsigned volatile*>(addend), 1)) + 1; }
106 inline int atomicDecrement(int volatile* addend) { return static_cast<int>(atomic_sub_value(reinterpret_cast<unsigned volatile*>(addend), 1)) - 1; }
107
108 #elif OS(ANDROID)
109 #define WTF_USE_LOCKFREE_THREADSAFEREFCOUNTED 1
110
111 inline int atomicIncrement(int volatile* addend) { return __atomic_inc(addend); }
112 inline int atomicDecrement(int volatile* addend) { return __atomic_dec(addend); }
113
114 #elif COMPILER(GCC) && !CPU(SPARC64) // sizeof(_Atomic_word) != sizeof(int) on sparc64 gcc
115 #define WTF_USE_LOCKFREE_THREADSAFEREFCOUNTED 1
116
117 inline int atomicIncrement(int volatile* addend) { return __gnu_cxx::__exchange_and_add(addend, 1) + 1; }
118 inline int atomicDecrement(int volatile* addend) { return __gnu_cxx::__exchange_and_add(addend, -1) - 1; }
119
120 #endif
121
122 #if OS(WINDOWS)
123 inline bool weakCompareAndSwap(volatile unsigned* location, unsigned expected, unsigned newValue)
124 {
125 #if OS(WINCE)
126     return InterlockedCompareExchange(reinterpret_cast<LONG*>(const_cast<unsigned*>(location)), static_cast<LONG>(newValue), static_cast<LONG>(expected)) == static_cast<LONG>(expected);
127 #else
128     return InterlockedCompareExchange(reinterpret_cast<LONG volatile*>(location), static_cast<LONG>(newValue), static_cast<LONG>(expected)) == static_cast<LONG>(expected);
129 #endif
130 }
131
132 inline bool weakCompareAndSwap(void*volatile* location, void* expected, void* newValue)
133 {
134     return InterlockedCompareExchangePointer(location, newValue, expected) == expected;
135 }
136 #else // OS(WINDOWS) --> not windows
137 #if COMPILER(GCC) && !COMPILER(CLANG) // Work around a gcc bug 
138 inline bool weakCompareAndSwap(volatile unsigned* location, unsigned expected, unsigned newValue) 
139 #else
140 inline bool weakCompareAndSwap(unsigned* location, unsigned expected, unsigned newValue)
141 #endif
142 {
143 #if ENABLE(COMPARE_AND_SWAP)
144     bool result;
145 #if CPU(X86) || CPU(X86_64)
146     asm volatile(
147         "lock; cmpxchgl %3, %2\n\t"
148         "sete %1"
149         : "+a"(expected), "=q"(result), "+m"(*location)
150         : "r"(newValue)
151         : "memory"
152         );
153 #elif CPU(ARM_THUMB2)
154     unsigned tmp;
155     asm volatile(
156         "movw %1, #1\n\t"
157         "ldrex %2, %0\n\t"
158         "cmp %3, %2\n\t"
159         "bne.n 0f\n\t"
160         "strex %1, %4, %0\n\t"
161         "0:"
162         : "+m"(*location), "=&r"(result), "=&r"(tmp)
163         : "r"(expected), "r"(newValue)
164         : "memory");
165     result = !result;
166 #else
167 #error "Bad architecture for compare and swap."
168 #endif
169     return result;
170 #else
171     UNUSED_PARAM(location);
172     UNUSED_PARAM(expected);
173     UNUSED_PARAM(newValue);
174     CRASH();
175     return 0;
176 #endif
177 }
178
179 inline bool weakCompareAndSwap(void*volatile* location, void* expected, void* newValue)
180 {
181 #if ENABLE(COMPARE_AND_SWAP)
182 #if CPU(X86_64)
183     bool result;
184     asm volatile(
185         "lock; cmpxchgq %3, %2\n\t"
186         "sete %1"
187         : "+a"(expected), "=q"(result), "+m"(*location)
188         : "r"(newValue)
189         : "memory"
190         );
191     return result;
192 #else
193     return weakCompareAndSwap(bitwise_cast<unsigned*>(location), bitwise_cast<unsigned>(expected), bitwise_cast<unsigned>(newValue));
194 #endif
195 #else // ENABLE(COMPARE_AND_SWAP)
196     UNUSED_PARAM(location);
197     UNUSED_PARAM(expected);
198     UNUSED_PARAM(newValue);
199     CRASH();
200     return 0;
201 #endif // ENABLE(COMPARE_AND_SWAP)
202 }
203 #endif // OS(WINDOWS) (end of the not-windows case)
204
205 inline bool weakCompareAndSwapUIntPtr(volatile uintptr_t* location, uintptr_t expected, uintptr_t newValue)
206 {
207     return weakCompareAndSwap(reinterpret_cast<void*volatile*>(location), reinterpret_cast<void*>(expected), reinterpret_cast<void*>(newValue));
208 }
209
210 #if CPU(ARM_THUMB2)
211
212 inline void memoryBarrierAfterLock()
213 {
214     asm volatile("dmb" ::: "memory");
215 }
216
217 inline void memoryBarrierBeforeUnlock()
218 {
219     asm volatile("dmb" ::: "memory");
220 }
221
222 #else
223
224 inline void memoryBarrierAfterLock() { }
225 inline void memoryBarrierBeforeUnlock() { }
226
227 #endif
228
229 } // namespace WTF
230
231 #if USE(LOCKFREE_THREADSAFEREFCOUNTED)
232 using WTF::atomicDecrement;
233 using WTF::atomicIncrement;
234 #endif
235
236 #endif // Atomics_h