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 | |
| 22 | #include "cutils/atomic.h" |
| 23 | #include "cutils/atomic-inline.h" |
| 24 | #include "macros.h" |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 28 | // NOTE: Two "quasiatomic" operations on the exact same memory address |
| 29 | // are guaranteed to operate atomically with respect to each other, |
| 30 | // but no guarantees are made about quasiatomic operations mixed with |
| 31 | // non-quasiatomic operations on the same address, nor about |
| 32 | // quasiatomic operations that are performed on partially-overlapping |
| 33 | // memory. |
| 34 | // |
| 35 | // Only the "Sync" functions provide a memory barrier. |
| 36 | class QuasiAtomic { |
| 37 | public: |
| 38 | static void Startup(); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 40 | static void Shutdown(); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 42 | // Swaps the 64-bit value at "addr" with "value". Returns the previous |
| 43 | // value. No memory barriers. |
| 44 | static int64_t Swap64(int64_t value, volatile int64_t* addr); |
Elliott Hughes | 557e027 | 2011-09-29 10:52:22 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 46 | // Swaps the 64-bit value at "addr" with "value". Returns the previous |
| 47 | // value. Provides memory barriers. |
| 48 | static int64_t Swap64Sync(int64_t value, volatile int64_t* addr); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 50 | // Reads the 64-bit value at "addr". |
| 51 | static int64_t Read64(volatile const int64_t* addr); |
| 52 | |
| 53 | // If the value at "addr" is equal to "old_value", replace it with "new_value" |
| 54 | // and return 0. Otherwise, don't swap, and return nonzero. |
| 55 | static int Cas64(int64_t old_value, int64_t new_value, volatile int64_t* addr); |
| 56 | |
| 57 | private: |
| 58 | DISALLOW_COPY_AND_ASSIGN(QuasiAtomic); |
| 59 | }; |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 60 | |
| 61 | } // namespace art |
| 62 | |
| 63 | #endif // ART_SRC_ATOMIC_H_ |