Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 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" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 18 | #include "base/mutex.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 19 | #include "base/stl_util.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 20 | #include "thread-current-inl.h" |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 24 | std::vector<Mutex*>* QuasiAtomic::gSwapMutexes = nullptr; |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 25 | |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 26 | Mutex* QuasiAtomic::GetSwapMutex(const volatile int64_t* addr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 27 | return (*gSwapMutexes)[(reinterpret_cast<uintptr_t>(addr) >> 3U) % kSwapMutexCount]; |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 28 | } |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 29 | |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 30 | void QuasiAtomic::Startup() { |
Andreas Gampe | 0866f4e | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 31 | if (NeedSwapMutexes(kRuntimeISA)) { |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 32 | gSwapMutexes = new std::vector<Mutex*>; |
| 33 | for (size_t i = 0; i < kSwapMutexCount; ++i) { |
Raghu Gandham | 7de77dd | 2014-06-13 15:16:31 -0700 | [diff] [blame] | 34 | gSwapMutexes->push_back(new Mutex("QuasiAtomic stripe", kSwapMutexesLock)); |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 35 | } |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 36 | } |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 39 | void QuasiAtomic::Shutdown() { |
Andreas Gampe | 0866f4e | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 40 | if (NeedSwapMutexes(kRuntimeISA)) { |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 41 | STLDeleteElements(gSwapMutexes); |
| 42 | delete gSwapMutexes; |
| 43 | } |
Elliott Hughes | 557e027 | 2011-09-29 10:52:22 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 46 | int64_t QuasiAtomic::SwapMutexRead64(volatile const int64_t* addr) { |
| 47 | MutexLock mu(Thread::Current(), *GetSwapMutex(addr)); |
| 48 | return *addr; |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 51 | void QuasiAtomic::SwapMutexWrite64(volatile int64_t* addr, int64_t value) { |
| 52 | MutexLock mu(Thread::Current(), *GetSwapMutex(addr)); |
Ian Rogers | 0fed328 | 2013-04-19 11:13:20 -0700 | [diff] [blame] | 53 | *addr = value; |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 57 | bool QuasiAtomic::SwapMutexCas64(int64_t old_value, int64_t new_value, volatile int64_t* addr) { |
| 58 | MutexLock mu(Thread::Current(), *GetSwapMutex(addr)); |
Ian Rogers | 0fed328 | 2013-04-19 11:13:20 -0700 | [diff] [blame] | 59 | if (*addr == old_value) { |
| 60 | *addr = new_value; |
| 61 | return true; |
| 62 | } |
| 63 | return false; |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 64 | } |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 66 | } // namespace art |