Coverage Report

Created: 2026-10-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/trafficserver/include/tsutil/AtomicSharedPtr.h
Line
Count
Source
1
/** @file
2
3
  Atomic wrapper around std::shared_ptr with the C++20
4
  std::atomic<std::shared_ptr<T>> API.
5
6
  @section license License
7
8
  Licensed to the Apache Software Foundation (ASF) under one
9
  or more contributor license agreements.  See the NOTICE file
10
  distributed with this work for additional information
11
  regarding copyright ownership.  The ASF licenses this file
12
  to you under the Apache License, Version 2.0 (the
13
  "License"); you may not use this file except in compliance
14
  with the License.  You may obtain a copy of the License at
15
16
      http://www.apache.org/licenses/LICENSE-2.0
17
18
  Unless required by applicable law or agreed to in writing, software
19
  distributed under the License is distributed on an "AS IS" BASIS,
20
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
  See the License for the specific language governing permissions and
22
  limitations under the License.
23
 */
24
25
#pragma once
26
27
#include <atomic>
28
#include <memory>
29
30
// Use the C++20 std::atomic<std::shared_ptr<T>> specialization when its
31
// feature-test macro reports support, otherwise fall back to the pre-C++20
32
// std::atomic_*_explicit free-function overloads on shared_ptr.  When all
33
// supported toolchains provide the specialization, delete the #else branch
34
// and the surrounding #if; call sites do not change.
35
#if defined(__cpp_lib_atomic_shared_ptr) && __cpp_lib_atomic_shared_ptr >= 201711L
36
37
template <class T> using AtomicSharedPtr = std::atomic<std::shared_ptr<T>>;
38
39
#else
40
41
// Belt-and-suspenders: on toolchains that take this branch, the free-function
42
// overloads are normally not marked [[deprecated]], so the suppression below is
43
// usually a no-op.  It matters only if someone forces the fallback on a modern
44
// library (e.g. -D__cpp_lib_atomic_shared_ptr=0) or compiles against a library
45
// that ships the deprecation markers ahead of the specialization.
46
#pragma GCC diagnostic push
47
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
48
49
template <class T> class AtomicSharedPtr
50
{
51
public:
52
  AtomicSharedPtr() noexcept = default;
53
  AtomicSharedPtr(std::shared_ptr<T> desired) noexcept : _p(std::move(desired)) {}
54
55
  AtomicSharedPtr(const AtomicSharedPtr &)            = delete;
56
  AtomicSharedPtr &operator=(const AtomicSharedPtr &) = delete;
57
58
  std::shared_ptr<T>
59
  load(std::memory_order order = std::memory_order_seq_cst) const noexcept
60
0
  {
61
0
    return std::atomic_load_explicit(&_p, order);
62
0
  }
63
64
  void
65
  store(std::shared_ptr<T> desired, std::memory_order order = std::memory_order_seq_cst) noexcept
66
0
  {
67
0
    std::atomic_store_explicit(&_p, std::move(desired), order);
68
0
  }
69
70
  std::shared_ptr<T>
71
  exchange(std::shared_ptr<T> desired, std::memory_order order = std::memory_order_seq_cst) noexcept
72
0
  {
73
0
    return std::atomic_exchange_explicit(&_p, std::move(desired), order);
74
0
  }
75
76
private:
77
  std::shared_ptr<T> _p;
78
};
79
80
#pragma GCC diagnostic pop
81
82
#endif