DPC++ Runtime
Runtime libraries for oneAPI DPC++
weak_object_base.hpp
Go to the documentation of this file.
1 //==------- weak_object_base.hpp --- SYCL weak objects base class ----------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <sycl/detail/impl_utils.hpp> // for getSyclObjImpl
12 
13 #include <memory> // for weak_ptr
14 #include <utility> // for declval
15 
16 namespace sycl {
17 inline namespace _V1 {
18 namespace ext::oneapi::detail {
19 template <typename SYCLObjT> class weak_object_base;
20 
21 // Helper function for getting the underlying weak_ptr from a weak_object.
22 template <typename SYCLObjT>
24 getSyclWeakObjImpl(const weak_object_base<SYCLObjT> &WeakObj) {
25  return WeakObj.MObjWeakPtr;
26 }
27 
28 // Common base class for weak_object.
29 template <typename SYCLObjT> class weak_object_base {
30 public:
31  using object_type = SYCLObjT;
32 
34  weak_object_base(const SYCLObjT &SYCLObj) noexcept
35  : MObjWeakPtr(GetWeakImpl(SYCLObj)) {}
36  weak_object_base(const weak_object_base &Other) noexcept = default;
38 
41 
42  void reset() noexcept { MObjWeakPtr.reset(); }
44  MObjWeakPtr.swap(Other.MObjWeakPtr);
45  }
46 
47  bool expired() const noexcept { return MObjWeakPtr.expired(); }
48 
49 #ifndef __SYCL_DEVICE_ONLY__
50  bool owner_before(const SYCLObjT &Other) const noexcept {
51  return MObjWeakPtr.owner_before(GetWeakImpl(Other));
52  }
53  bool owner_before(const weak_object_base &Other) const noexcept {
54  return MObjWeakPtr.owner_before(Other.MObjWeakPtr);
55  }
56 #else
57  // On device calls to these functions are disallowed, so declare them but
58  // don't define them to avoid compilation failures.
59  bool owner_before(const SYCLObjT &Other) const noexcept;
60  bool owner_before(const weak_object_base &Other) const noexcept;
61 #endif // __SYCL_DEVICE_ONLY__
62 
63 protected:
64 #ifndef __SYCL_DEVICE_ONLY__
65  // Store a weak variant of the impl in the SYCLObjT.
66  typename decltype(sycl::detail::getSyclObjImpl(
67  std::declval<SYCLObjT>()))::weak_type MObjWeakPtr;
68 
69  static decltype(MObjWeakPtr) GetWeakImpl(const SYCLObjT &SYCLObj) {
70  return sycl::detail::getSyclObjImpl(SYCLObj);
71  }
72 #else
73  // On device we may not have an impl, so we pad with an unused void pointer.
74  std::weak_ptr<void> MObjWeakPtr;
75  static std::weak_ptr<void> GetWeakImpl(const SYCLObjT &) { return {}; }
76 #endif // __SYCL_DEVICE_ONLY__
77 
78  template <class Obj>
79  friend decltype(weak_object_base<Obj>::MObjWeakPtr)
80  detail::getSyclWeakObjImpl(const weak_object_base<Obj> &WeakObj);
81 };
82 } // namespace ext::oneapi::detail
83 } // namespace _V1
84 } // namespace sycl
weak_object_base(weak_object_base &&Other) noexcept=default
weak_object_base & operator=(weak_object_base &&Other) noexcept=default
decltype(sycl::detail::getSyclObjImpl(std::declval< SYCLObjT >())) ::weak_type MObjWeakPtr
bool owner_before(const SYCLObjT &Other) const noexcept
void swap(weak_object_base &Other) noexcept
weak_object_base(const SYCLObjT &SYCLObj) noexcept
bool owner_before(const weak_object_base &Other) const noexcept
weak_object_base(const weak_object_base &Other) noexcept=default
weak_object_base & operator=(const weak_object_base &Other) noexcept=default
static decltype(MObjWeakPtr) GetWeakImpl(const SYCLObjT &SYCLObj)
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
decltype(weak_object_base< SYCLObjT >::MObjWeakPtr) getSyclWeakObjImpl(const weak_object_base< SYCLObjT > &WeakObj)
Definition: access.hpp:18
_Abi const simd< _Tp, _Abi > & noexcept
Definition: simd.hpp:1324