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 <utility> // for declval
14 
15 namespace sycl {
16 inline namespace _V1 {
17 namespace ext::oneapi::detail {
18 template <typename SYCLObjT> class weak_object_base;
19 
20 // Helper function for getting the underlying weak_ptr from a weak_object.
21 template <typename SYCLObjT>
23 getSyclWeakObjImpl(const weak_object_base<SYCLObjT> &WeakObj) {
24  return WeakObj.MObjWeakPtr;
25 }
26 
27 // Common base class for weak_object.
28 template <typename SYCLObjT> class weak_object_base {
29 public:
30  using object_type = SYCLObjT;
31 
33  weak_object_base(const SYCLObjT &SYCLObj) noexcept
34  : MObjWeakPtr(GetWeakImpl(SYCLObj)) {}
35  weak_object_base(const weak_object_base &Other) noexcept = default;
37 
40 
41  void reset() noexcept { MObjWeakPtr.reset(); }
43  MObjWeakPtr.swap(Other.MObjWeakPtr);
44  }
45 
46  bool expired() const noexcept { return MObjWeakPtr.expired(); }
47 
48 #ifndef __SYCL_DEVICE_ONLY__
49  bool owner_before(const SYCLObjT &Other) const noexcept {
50  return MObjWeakPtr.owner_before(GetWeakImpl(Other));
51  }
52  bool owner_before(const weak_object_base &Other) const noexcept {
53  return MObjWeakPtr.owner_before(Other.MObjWeakPtr);
54  }
55 #else
56  // On device calls to these functions are disallowed, so declare them but
57  // don't define them to avoid compilation failures.
58  bool owner_before(const SYCLObjT &Other) const noexcept;
59  bool owner_before(const weak_object_base &Other) const noexcept;
60 #endif // __SYCL_DEVICE_ONLY__
61 
62 protected:
63 #ifndef __SYCL_DEVICE_ONLY__
64  // Store a weak variant of the impl in the SYCLObjT.
65  typename decltype(sycl::detail::getSyclObjImpl(
66  std::declval<SYCLObjT>()))::weak_type MObjWeakPtr;
67 
68  static decltype(MObjWeakPtr) GetWeakImpl(const SYCLObjT &SYCLObj) {
69  return sycl::detail::getSyclObjImpl(SYCLObj);
70  }
71 #else
72  // On device we may not have an impl, so we pad with an unused void pointer.
73  std::weak_ptr<void> MObjWeakPtr;
74  static std::weak_ptr<void> GetWeakImpl(const SYCLObjT &) { return {}; }
75 #endif // __SYCL_DEVICE_ONLY__
76 
77  template <class Obj>
78  friend decltype(weak_object_base<Obj>::MObjWeakPtr)
79  detail::getSyclWeakObjImpl(const weak_object_base<Obj> &WeakObj);
80 };
81 } // namespace ext::oneapi::detail
82 } // namespace _V1
83 } // 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