Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #ifndef ART_SRC_ATOMIC_H_ |
| 18 | #define ART_SRC_ATOMIC_H_ |
| 19 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 22 | #include "base/macros.h" |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 26 | // NOTE: Two "quasiatomic" operations on the exact same memory address |
| 27 | // are guaranteed to operate atomically with respect to each other, |
| 28 | // but no guarantees are made about quasiatomic operations mixed with |
| 29 | // non-quasiatomic operations on the same address, nor about |
| 30 | // quasiatomic operations that are performed on partially-overlapping |
| 31 | // memory. |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 32 | class QuasiAtomic { |
| 33 | public: |
| 34 | static void Startup(); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 36 | static void Shutdown(); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 37 | |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 38 | // Reads the 64-bit value at "addr" without tearing. |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 39 | static int64_t Read64(volatile const int64_t* addr); |
| 40 | |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 41 | // Writes to the 64-bit value at "addr" without tearing. |
| 42 | static void Write64(volatile int64_t* addr, int64_t val); |
| 43 | |
| 44 | // Atomically compare the value at "addr" to "old_value", if equal replace it with "new_value" |
| 45 | // and return true. Otherwise, don't swap, and return false. |
| 46 | static bool Cas64(int64_t old_value, int64_t new_value, volatile int64_t* addr); |
| 47 | |
| 48 | // Does the architecture provide reasonable atomic long operations or do we fall back on mutexes? |
| 49 | static bool LongAtomicsUseMutexes(); |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | DISALLOW_COPY_AND_ASSIGN(QuasiAtomic); |
| 53 | }; |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 54 | |
| 55 | } // namespace art |
| 56 | |
| 57 | #endif // ART_SRC_ATOMIC_H_ |