1 /* 2 * Copyright (C) 2019 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 CHRE_PLATFORM_ATOMIC_H_ 18 #define CHRE_PLATFORM_ATOMIC_H_ 19 20 #include "chre/target_platform/atomic_base.h" 21 #include "chre/util/non_copyable.h" 22 23 namespace chre { 24 25 /** 26 * Provides an implementation of an atomic boolean. AtomicBoolBase is 27 * subclassed here to allow platforms to use their own underlying atomic APIs. 28 */ 29 class AtomicBool : public AtomicBoolBase, public NonCopyable { 30 public: 31 /** 32 * Allows the platform to do any atomic initialization at construction time. 33 * 34 * @param The starting value for the object. 35 */ 36 AtomicBool(bool startingValue); 37 38 /** 39 * Atomically assigns the desired value to the atomic object. Equivalent to 40 * store(). 41 * 42 * @param The value the object will be replaced with. 43 * 44 * @return The desired value. 45 */ 46 bool operator=(bool desired); 47 48 /** 49 * Atomically loads the current value of the atomic object. Equivalent to 50 * load(). 51 * 52 * @return The current value of the object. 53 */ 54 operator bool() const { 55 return load(); 56 }; 57 58 /** 59 * Atomically loads the current value of the atomic object. 60 * 61 * @return The current value of the object. 62 */ 63 bool load() const; 64 65 /** 66 * Atomically replaces the current value of the atomic object. 67 * 68 * @param The value the object will be replaced with. 69 */ 70 void store(bool desired); 71 72 /** 73 * Atomically replaces the value of the atomic object. 74 * 75 * @param The value the object should have when the method returns. 76 * 77 * @return The previous value of the object. 78 */ 79 bool exchange(bool desired); 80 }; 81 82 /** 83 * Provides an implementation of an atomic uint32_t. AtomicUint32Base is 84 * subclassed here to allow platforms to use their own underlying atomic APIs. 85 */ 86 class AtomicUint32 : public AtomicUint32Base, public NonCopyable { 87 public: 88 /** 89 * Allows the platform to do any atomic initialization at construction time. 90 * 91 * @param The starting value for the object. 92 */ 93 AtomicUint32(uint32_t startingValue); 94 95 /** 96 * Atomically assigns the desired value to the atomic object. Equivalent to 97 * store(). 98 * 99 * @param The value the object will be replaced with. 100 * 101 * @return The desired value. 102 */ 103 uint32_t operator=(uint32_t desired); 104 105 /** 106 * Atomically loads the current value of the atomic object. Equivalent to 107 * load(). 108 * 109 * @return The current value of the object. 110 */ uint32_t()111 operator uint32_t() const { 112 return load(); 113 } 114 115 /** 116 * Atomically loads the current value of the atomic object. 117 * 118 * @return The current value of the object. 119 */ 120 uint32_t load() const; 121 122 /** 123 * Atomically replaces the current value of the atomic object. 124 * 125 * @param The value the object will be replaced with. 126 */ 127 void store(uint32_t desired); 128 129 /** 130 * Atomically replaces the value of the atomic object. 131 * 132 * @param The value the object should have when the method returns. 133 * 134 * @return The previous value of the object. 135 */ 136 uint32_t exchange(uint32_t desired); 137 138 /** 139 * Atomically adds the argument to the current value of the object. 140 * 141 * @param The amount which the object should be increased by. 142 * 143 * @return The previous value of the object. 144 */ 145 uint32_t fetch_add(uint32_t arg); 146 147 /** 148 * Atomically increments the value stored in the atomic object by 1. 149 * 150 * @return The previous value of the object. 151 */ 152 uint32_t fetch_increment(); 153 154 /** 155 * Atomically subtracts the argument from the current value of the object. 156 * 157 * @param The amount which the object should be decreased by. 158 * 159 * @return The previous value of the object. 160 */ 161 uint32_t fetch_sub(uint32_t arg); 162 163 /** 164 * Atomically decrements the value stored in the atomic object by 1. 165 * 166 * @return The previous value of the object. 167 */ 168 uint32_t fetch_decrement(); 169 }; 170 171 } // namespace chre 172 173 #include "chre/target_platform/atomic_base_impl.h" 174 175 #endif // CHRE_PLATFORM_ATOMIC_H_ 176