CARLA
AtomicSharedPtr.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include <memory>
10 
11 namespace carla {
12 
13  /// A very simple atomic shared ptr with release-acquire memory order.
14  template <typename T>
16  public:
17 
18  template <typename... Args>
19  explicit AtomicSharedPtr(Args &&... args)
20  : _ptr(std::forward<Args>(args)...) {}
21 
23  : _ptr(rhs.load()) {}
24 
25  AtomicSharedPtr(AtomicSharedPtr &&) = delete;
26 
27  void store(std::shared_ptr<T> ptr) noexcept {
28  std::atomic_store_explicit(&_ptr, ptr, std::memory_order_release);
29  }
30 
31  void reset(std::shared_ptr<T> ptr = nullptr) noexcept {
32  store(ptr);
33  }
34 
35  std::shared_ptr<T> load() const noexcept {
36  return std::atomic_load_explicit(&_ptr, std::memory_order_acquire);
37  }
38 
39  bool compare_exchange(std::shared_ptr<T> *expected, std::shared_ptr<T> desired) noexcept {
40  return std::atomic_compare_exchange_strong_explicit(
41  &_ptr,
42  expected,
43  desired,
44  std::memory_order_acq_rel,
45  std::memory_order_acq_rel);
46  }
47 
48  AtomicSharedPtr &operator=(std::shared_ptr<T> ptr) noexcept {
49  store(std::move(ptr));
50  return *this;
51  }
52 
53  AtomicSharedPtr &operator=(const AtomicSharedPtr &rhs) noexcept {
54  store(rhs.load());
55  return *this;
56  }
57 
59 
60  private:
61 
62  std::shared_ptr<T> _ptr;
63  };
64 
65 } // namespace carla
std::shared_ptr< T > load() const noexcept
void store(std::shared_ptr< T > ptr) noexcept
AtomicSharedPtr(const AtomicSharedPtr &rhs)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
AtomicSharedPtr & operator=(const AtomicSharedPtr &rhs) noexcept
AtomicSharedPtr & operator=(std::shared_ptr< T > ptr) noexcept
bool compare_exchange(std::shared_ptr< T > *expected, std::shared_ptr< T > desired) noexcept
std::shared_ptr< T > _ptr
void reset(std::shared_ptr< T > ptr=nullptr) noexcept
A very simple atomic shared ptr with release-acquire memory order.
AtomicSharedPtr(Args &&... args)