blob: e766a8d77d1f00f5b5cb6194d7aa2e483df94cc8 [file] [log] [blame]
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001/*
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"
Elliott Hughes76b61672012-12-12 17:47:30 -080018#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080019#include "base/stl_util.h"
Brian Carlstrom78463962013-11-07 15:48:35 -080020#include "thread-inl.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070021
22namespace art {
23
Ian Rogersb122a4b2013-11-19 18:00:50 -080024std::vector<Mutex*>* QuasiAtomic::gSwapMutexes = nullptr;
Elliott Hughes5ea047b2011-09-13 14:38:18 -070025
Ian Rogersb122a4b2013-11-19 18:00:50 -080026Mutex* QuasiAtomic::GetSwapMutex(const volatile int64_t* addr) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080027 return (*gSwapMutexes)[(reinterpret_cast<uintptr_t>(addr) >> 3U) % kSwapMutexCount];
Ian Rogers9adbff52013-01-23 18:19:03 -080028}
Elliott Hughes7c6169d2012-05-02 16:11:48 -070029
Ian Rogers9adbff52013-01-23 18:19:03 -080030void QuasiAtomic::Startup() {
Ian Rogersb122a4b2013-11-19 18:00:50 -080031 if (kNeedSwapMutexes) {
32 gSwapMutexes = new std::vector<Mutex*>;
33 for (size_t i = 0; i < kSwapMutexCount; ++i) {
Raghu Gandham7de77dd2014-06-13 15:16:31 -070034 gSwapMutexes->push_back(new Mutex("QuasiAtomic stripe", kSwapMutexesLock));
Ian Rogersb122a4b2013-11-19 18:00:50 -080035 }
Ian Rogers9adbff52013-01-23 18:19:03 -080036 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -070037}
38
Ian Rogers9adbff52013-01-23 18:19:03 -080039void QuasiAtomic::Shutdown() {
Ian Rogersb122a4b2013-11-19 18:00:50 -080040 if (kNeedSwapMutexes) {
41 STLDeleteElements(gSwapMutexes);
42 delete gSwapMutexes;
43 }
Elliott Hughes557e0272011-09-29 10:52:22 -070044}
45
Ian Rogersb122a4b2013-11-19 18:00:50 -080046int64_t QuasiAtomic::SwapMutexRead64(volatile const int64_t* addr) {
47 MutexLock mu(Thread::Current(), *GetSwapMutex(addr));
48 return *addr;
Elliott Hughes7c6169d2012-05-02 16:11:48 -070049}
50
Ian Rogersb122a4b2013-11-19 18:00:50 -080051void QuasiAtomic::SwapMutexWrite64(volatile int64_t* addr, int64_t value) {
52 MutexLock mu(Thread::Current(), *GetSwapMutex(addr));
Ian Rogers0fed3282013-04-19 11:13:20 -070053 *addr = value;
Ian Rogers9adbff52013-01-23 18:19:03 -080054}
55
56
Ian Rogersb122a4b2013-11-19 18:00:50 -080057bool QuasiAtomic::SwapMutexCas64(int64_t old_value, int64_t new_value, volatile int64_t* addr) {
58 MutexLock mu(Thread::Current(), *GetSwapMutex(addr));
Ian Rogers0fed3282013-04-19 11:13:20 -070059 if (*addr == old_value) {
60 *addr = new_value;
61 return true;
62 }
63 return false;
Ian Rogers9adbff52013-01-23 18:19:03 -080064}
Elliott Hughes5ea047b2011-09-13 14:38:18 -070065
Elliott Hughes5ea047b2011-09-13 14:38:18 -070066} // namespace art