blob: b1e9870acfc091d9287b5bfa5177feb1a0a3c347 [file] [log] [blame]
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_ATOMIC_H_
18#define ART_RUNTIME_ATOMIC_H_
Elliott Hughes5ea047b2011-09-13 14:38:18 -070019
Elliott Hughes7c6169d2012-05-02 16:11:48 -070020#include <stdint.h>
Ian Rogersb122a4b2013-11-19 18:00:50 -080021#include <vector>
Elliott Hughes7c6169d2012-05-02 16:11:48 -070022
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/macros.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024
25namespace art {
26
Ian Rogersb122a4b2013-11-19 18:00:50 -080027class Mutex;
28
Elliott Hughes7c6169d2012-05-02 16:11:48 -070029// NOTE: Two "quasiatomic" operations on the exact same memory address
30// are guaranteed to operate atomically with respect to each other,
31// but no guarantees are made about quasiatomic operations mixed with
32// non-quasiatomic operations on the same address, nor about
33// quasiatomic operations that are performed on partially-overlapping
34// memory.
Elliott Hughes7c6169d2012-05-02 16:11:48 -070035class QuasiAtomic {
Ian Rogersb122a4b2013-11-19 18:00:50 -080036#if !defined(__arm__) && !defined(__i386__)
37 static constexpr bool kNeedSwapMutexes = true;
38#else
39 static constexpr bool kNeedSwapMutexes = false;
40#endif
41
Elliott Hughes7c6169d2012-05-02 16:11:48 -070042 public:
43 static void Startup();
Elliott Hughes5ea047b2011-09-13 14:38:18 -070044
Elliott Hughes7c6169d2012-05-02 16:11:48 -070045 static void Shutdown();
Elliott Hughes5ea047b2011-09-13 14:38:18 -070046
Ian Rogers9adbff52013-01-23 18:19:03 -080047 // Reads the 64-bit value at "addr" without tearing.
Ian Rogersb122a4b2013-11-19 18:00:50 -080048 static int64_t Read64(volatile const int64_t* addr) {
49 if (!kNeedSwapMutexes) {
50 return *addr;
51 } else {
52 return SwapMutexRead64(addr);
53 }
54 }
Elliott Hughes7c6169d2012-05-02 16:11:48 -070055
Ian Rogers9adbff52013-01-23 18:19:03 -080056 // Writes to the 64-bit value at "addr" without tearing.
Ian Rogersb122a4b2013-11-19 18:00:50 -080057 static void Write64(volatile int64_t* addr, int64_t val) {
58 if (!kNeedSwapMutexes) {
59 *addr = val;
60 } else {
61 SwapMutexWrite64(addr, val);
62 }
63 }
Ian Rogers9adbff52013-01-23 18:19:03 -080064
65 // Atomically compare the value at "addr" to "old_value", if equal replace it with "new_value"
66 // and return true. Otherwise, don't swap, and return false.
Ian Rogersb122a4b2013-11-19 18:00:50 -080067 static bool Cas64(int64_t old_value, int64_t new_value, volatile int64_t* addr) {
68 if (!kNeedSwapMutexes) {
69 return __sync_bool_compare_and_swap(addr, old_value, new_value);
70 } else {
71 return SwapMutexCas64(old_value, new_value, addr);
72 }
73 }
Ian Rogers9adbff52013-01-23 18:19:03 -080074
75 // Does the architecture provide reasonable atomic long operations or do we fall back on mutexes?
Ian Rogersb122a4b2013-11-19 18:00:50 -080076 static bool LongAtomicsUseMutexes() {
77 return !kNeedSwapMutexes;
78 }
79
80 static void MembarLoadStore() {
81 #if defined(__arm__)
82 __asm__ __volatile__("dmb ish" : : : "memory");
83 #elif defined(__i386__)
84 __asm__ __volatile__("" : : : "memory");
85 #elif defined(__mips__)
86 __asm__ __volatile__("sync" : : : "memory");
87 #else
88 #error Unexpected architecture
89 #endif
90 }
91
92 static void MembarLoadLoad() {
93 #if defined(__arm__)
94 __asm__ __volatile__("dmb ish" : : : "memory");
95 #elif defined(__i386__)
96 __asm__ __volatile__("" : : : "memory");
97 #elif defined(__mips__)
98 __asm__ __volatile__("sync" : : : "memory");
99 #else
100 #error Unexpected architecture
101 #endif
102 }
103
104 static void MembarStoreStore() {
105 #if defined(__arm__)
106 __asm__ __volatile__("dmb ishst" : : : "memory");
107 #elif defined(__i386__)
108 __asm__ __volatile__("" : : : "memory");
109 #elif defined(__mips__)
110 __asm__ __volatile__("sync" : : : "memory");
111 #else
112 #error Unexpected architecture
113 #endif
114 }
115
116 static void MembarStoreLoad() {
117 #if defined(__arm__)
118 __asm__ __volatile__("dmb ish" : : : "memory");
119 #elif defined(__i386__)
120 __asm__ __volatile__("mfence" : : : "memory");
121 #elif defined(__mips__)
122 __asm__ __volatile__("sync" : : : "memory");
123 #else
124 #error Unexpected architecture
125 #endif
126 }
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700127
128 private:
Ian Rogersb122a4b2013-11-19 18:00:50 -0800129 static Mutex* GetSwapMutex(const volatile int64_t* addr);
130 static int64_t SwapMutexRead64(volatile const int64_t* addr);
131 static void SwapMutexWrite64(volatile int64_t* addr, int64_t val);
132 static bool SwapMutexCas64(int64_t old_value, int64_t new_value, volatile int64_t* addr);
133
134 // We stripe across a bunch of different mutexes to reduce contention.
135 static constexpr size_t kSwapMutexCount = 32;
136 static std::vector<Mutex*>* gSwapMutexes;
137
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700138 DISALLOW_COPY_AND_ASSIGN(QuasiAtomic);
139};
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700140
141} // namespace art
142
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700143#endif // ART_RUNTIME_ATOMIC_H_