blob: cb6f86b921336cff7ad7198377b4cbd9d369a518 [file] [log] [blame]
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_ATOMIC_H_
18#define ART_RUNTIME_ATOMIC_H_
Elliott Hughes5ea047b2011-09-13 14:38:18 -070019
Elliott Hughes7c6169d2012-05-02 16:11:48 -070020#include <stdint.h>
21
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/macros.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070023
24namespace art {
25
Elliott Hughes7c6169d2012-05-02 16:11:48 -070026// 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 Hughes7c6169d2012-05-02 16:11:48 -070032class QuasiAtomic {
33 public:
34 static void Startup();
Elliott Hughes5ea047b2011-09-13 14:38:18 -070035
Elliott Hughes7c6169d2012-05-02 16:11:48 -070036 static void Shutdown();
Elliott Hughes5ea047b2011-09-13 14:38:18 -070037
Ian Rogers9adbff52013-01-23 18:19:03 -080038 // Reads the 64-bit value at "addr" without tearing.
Elliott Hughes7c6169d2012-05-02 16:11:48 -070039 static int64_t Read64(volatile const int64_t* addr);
40
Ian Rogers9adbff52013-01-23 18:19:03 -080041 // 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 Hughes7c6169d2012-05-02 16:11:48 -070050
51 private:
52 DISALLOW_COPY_AND_ASSIGN(QuasiAtomic);
53};
Elliott Hughes5ea047b2011-09-13 14:38:18 -070054
55} // namespace art
56
Brian Carlstromfc0e3212013-07-17 14:40:12 -070057#endif // ART_RUNTIME_ATOMIC_H_