• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_FML_SYNCHRONIZATION_ATOMIC_OBJECT_H_
6 #define FLUTTER_FML_SYNCHRONIZATION_ATOMIC_OBJECT_H_
7 
8 #include <mutex>
9 
10 namespace fml {
11 
12 // A wrapper for an object instance that can be read or written atomically.
13 template <typename T>
14 class AtomicObject {
15  public:
16   AtomicObject() = default;
AtomicObject(T object)17   AtomicObject(T object) : object_(object) {}
18 
Load()19   T Load() const {
20     std::scoped_lock lock(mutex_);
21     return object_;
22   }
23 
Store(const T & object)24   void Store(const T& object) {
25     std::scoped_lock lock(mutex_);
26     object_ = object;
27   }
28 
29  private:
30   mutable std::mutex mutex_;
31   T object_;
32 };
33 
34 }  // namespace fml
35 
36 #endif  // FLUTTER_FML_SYNCHRONIZATION_ATOMIC_OBJECT_H_
37