1 /*
2 * Copyright (C) 2010 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
17 #include "atomic.h"
18 #include "base/mutex.h"
19 #include "base/stl_util.h"
20 #include "thread-inl.h"
21
22 namespace art {
23
24 std::vector<Mutex*>* QuasiAtomic::gSwapMutexes = nullptr;
25
GetSwapMutex(const volatile int64_t * addr)26 Mutex* QuasiAtomic::GetSwapMutex(const volatile int64_t* addr) {
27 return (*gSwapMutexes)[(reinterpret_cast<uintptr_t>(addr) >> 3U) % kSwapMutexCount];
28 }
29
Startup()30 void QuasiAtomic::Startup() {
31 if (kNeedSwapMutexes) {
32 gSwapMutexes = new std::vector<Mutex*>;
33 for (size_t i = 0; i < kSwapMutexCount; ++i) {
34 gSwapMutexes->push_back(new Mutex("QuasiAtomic stripe", kSwapMutexesLock));
35 }
36 }
37 }
38
Shutdown()39 void QuasiAtomic::Shutdown() {
40 if (kNeedSwapMutexes) {
41 STLDeleteElements(gSwapMutexes);
42 delete gSwapMutexes;
43 }
44 }
45
SwapMutexRead64(volatile const int64_t * addr)46 int64_t QuasiAtomic::SwapMutexRead64(volatile const int64_t* addr) {
47 MutexLock mu(Thread::Current(), *GetSwapMutex(addr));
48 return *addr;
49 }
50
SwapMutexWrite64(volatile int64_t * addr,int64_t value)51 void QuasiAtomic::SwapMutexWrite64(volatile int64_t* addr, int64_t value) {
52 MutexLock mu(Thread::Current(), *GetSwapMutex(addr));
53 *addr = value;
54 }
55
56
SwapMutexCas64(int64_t old_value,int64_t new_value,volatile int64_t * addr)57 bool QuasiAtomic::SwapMutexCas64(int64_t old_value, int64_t new_value, volatile int64_t* addr) {
58 MutexLock mu(Thread::Current(), *GetSwapMutex(addr));
59 if (*addr == old_value) {
60 *addr = new_value;
61 return true;
62 }
63 return false;
64 }
65
66 } // namespace art
67