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 std::remove_reference<decltype(sycl::detail::getSyclObjImpl(
67  std::declval<SYCLObjT>()))>::type::weak_type MObjWeakPtr;
68  // relies on <type_traits> from impl_utils.h
69 
70  static decltype(MObjWeakPtr) GetWeakImpl(const SYCLObjT &SYCLObj) {
71  return sycl::detail::getSyclObjImpl(SYCLObj);
72  }
73 #else
74  // On device we may not have an impl, so we pad with an unused void pointer.
75  std::weak_ptr<void> MObjWeakPtr;
76  static std::weak_ptr<void> GetWeakImpl(const SYCLObjT &) { return {}; }
77 #endif // __SYCL_DEVICE_ONLY__
78 
79  template <class Obj>
80  friend decltype(weak_object_base<Obj>::MObjWeakPtr)
81  detail::getSyclWeakObjImpl(const weak_object_base<Obj> &WeakObj);
82 };
83 } // namespace ext::oneapi::detail
84 } // namespace _V1
85 } // namespace sycl
weak_object_base(weak_object_base &&Other) noexcept=default
weak_object_base & operator=(weak_object_base &&Other) noexcept=default
bool owner_before(const SYCLObjT &Other) const noexcept
std::remove_reference< decltype(sycl::detail::getSyclObjImpl(std::declval< SYCLObjT >)))>::type::weak_type MObjWeakPtr
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) const & getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:31
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