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