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