• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RUNTIME_ATOMIC_H_
18 #define ART_RUNTIME_ATOMIC_H_
19 
20 #include <stdint.h>
21 
22 #include "base/macros.h"
23 
24 namespace art {
25 
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.
32 class QuasiAtomic {
33  public:
34   static void Startup();
35 
36   static void Shutdown();
37 
38   // Reads the 64-bit value at "addr" without tearing.
39   static int64_t Read64(volatile const int64_t* addr);
40 
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();
50 
51  private:
52   DISALLOW_COPY_AND_ASSIGN(QuasiAtomic);
53 };
54 
55 }  // namespace art
56 
57 #endif  // ART_RUNTIME_ATOMIC_H_
58