DPC++ Runtime
Runtime libraries for oneAPI DPC++
pi_utils.hpp
Go to the documentation of this file.
1 //==------------- pi_utils.hpp - Common PI utilities -----------------------==//
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 <detail/plugin.hpp>
13 #include <sycl/detail/pi.hpp>
14 
15 #include <optional>
16 
17 namespace sycl {
18 inline namespace _V1 {
19 namespace detail {
20 
21 // RAII object for keeping ownership of a PI event.
22 struct OwnedPiEvent {
23  OwnedPiEvent(const PluginPtr &Plugin)
24  : MEvent{std::nullopt}, MPlugin{Plugin} {}
26  bool TakeOwnership = false)
27  : MEvent(Event), MPlugin(Plugin) {
28  // If it is not instructed to take ownership, retain the event to share
29  // ownership of it.
30  if (!TakeOwnership)
31  MPlugin->call<PiApiKind::piEventRetain>(*MEvent);
32  }
34  // Release the event if the ownership was not transferred.
35  if (MEvent.has_value())
36  MPlugin->call<PiApiKind::piEventRelease>(*MEvent);
37  }
38 
40  : MEvent(Other.MEvent), MPlugin(Other.MPlugin) {
41  Other.MEvent = std::nullopt;
42  }
43 
44  // Copy constructor explicitly deleted for simplicity as it is not currently
45  // used. Implement if needed.
46  OwnedPiEvent(const OwnedPiEvent &Other) = delete;
47 
48  operator bool() { return MEvent.has_value(); }
49 
50  sycl::detail::pi::PiEvent GetEvent() { return *MEvent; }
51 
52  // Transfers the ownership of the event to the caller. The destructor will
53  // no longer release the event.
55  sycl::detail::pi::PiEvent Event = *MEvent;
56  MEvent = std::nullopt;
57  return Event;
58  }
59 
60 private:
61  std::optional<sycl::detail::pi::PiEvent> MEvent;
62  const PluginPtr &MPlugin;
63 };
64 
65 } // namespace detail
66 } // namespace _V1
67 } // namespace sycl
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
Definition: access.hpp:18
pi_result piEventRelease(pi_event event)
Definition: pi_cuda.cpp:600
pi_result piEventRetain(pi_event event)
Definition: pi_cuda.cpp:598
C++ wrapper of extern "C" PI interfaces.
sycl::detail::pi::PiEvent TransferOwnership()
Definition: pi_utils.hpp:54
OwnedPiEvent(const PluginPtr &Plugin)
Definition: pi_utils.hpp:23
OwnedPiEvent(sycl::detail::pi::PiEvent Event, const PluginPtr &Plugin, bool TakeOwnership=false)
Definition: pi_utils.hpp:25
OwnedPiEvent(const OwnedPiEvent &Other)=delete
OwnedPiEvent(OwnedPiEvent &&Other)
Definition: pi_utils.hpp:39
sycl::detail::pi::PiEvent GetEvent()
Definition: pi_utils.hpp:50