|
| 1 | +//===--------- common.hpp - Level Zero Adapter ---------------------------===// |
| 2 | +// |
| 3 | +// Copyright (C) 2024 Intel Corporation |
| 4 | +// |
| 5 | +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM |
| 6 | +// Exceptions. See LICENSE.TXT |
| 7 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <exception> |
| 14 | +#include <ze_api.h> |
| 15 | + |
| 16 | +#include "../common.hpp" |
| 17 | + |
| 18 | +namespace v2 { |
| 19 | + |
| 20 | +namespace raii { |
| 21 | + |
| 22 | +template <typename ZeHandleT, ze_result_t (*destroy)(ZeHandleT)> |
| 23 | +struct ze_handle_wrapper { |
| 24 | + ze_handle_wrapper(bool ownZeHandle = true) |
| 25 | + : handle(nullptr), ownZeHandle(ownZeHandle) {} |
| 26 | + |
| 27 | + ze_handle_wrapper(ZeHandleT handle, bool ownZeHandle = true) |
| 28 | + : handle(handle), ownZeHandle(ownZeHandle) {} |
| 29 | + |
| 30 | + ze_handle_wrapper(const ze_handle_wrapper &) = delete; |
| 31 | + ze_handle_wrapper &operator=(const ze_handle_wrapper &) = delete; |
| 32 | + |
| 33 | + ze_handle_wrapper(ze_handle_wrapper &&other) |
| 34 | + : handle(other.handle), ownZeHandle(other.ownZeHandle) { |
| 35 | + other.handle = nullptr; |
| 36 | + } |
| 37 | + |
| 38 | + ze_handle_wrapper &operator=(ze_handle_wrapper &&other) { |
| 39 | + if (this == &other) { |
| 40 | + return *this; |
| 41 | + } |
| 42 | + |
| 43 | + if (handle) { |
| 44 | + reset(); |
| 45 | + } |
| 46 | + handle = other.handle; |
| 47 | + ownZeHandle = other.ownZeHandle; |
| 48 | + other.handle = nullptr; |
| 49 | + return *this; |
| 50 | + } |
| 51 | + |
| 52 | + ~ze_handle_wrapper() { |
| 53 | + try { |
| 54 | + reset(); |
| 55 | + } catch (...) { |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + void reset() { |
| 60 | + if (!handle) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + auto zeResult = ZE_CALL_NOCHECK(destroy, (handle)); |
| 65 | + // Gracefully handle the case that L0 was already unloaded. |
| 66 | + if (zeResult && zeResult != ZE_RESULT_ERROR_UNINITIALIZED) |
| 67 | + throw ze2urResult(zeResult); |
| 68 | + |
| 69 | + handle = nullptr; |
| 70 | + } |
| 71 | + |
| 72 | + ZeHandleT release() { |
| 73 | + auto handle = this->handle; |
| 74 | + this->handle = nullptr; |
| 75 | + return handle; |
| 76 | + } |
| 77 | + |
| 78 | + ZeHandleT get() const { return handle; } |
| 79 | + |
| 80 | + ZeHandleT *ptr() { return &handle; } |
| 81 | + |
| 82 | +private: |
| 83 | + ZeHandleT handle; |
| 84 | + bool ownZeHandle; |
| 85 | +}; |
| 86 | + |
| 87 | +using ze_kernel_handle_t = |
| 88 | + ze_handle_wrapper<::ze_kernel_handle_t, zeKernelDestroy>; |
| 89 | + |
| 90 | +using ze_event_handle_t = |
| 91 | + ze_handle_wrapper<::ze_event_handle_t, zeEventDestroy>; |
| 92 | + |
| 93 | +using ze_event_pool_handle_t = |
| 94 | + ze_handle_wrapper<::ze_event_pool_handle_t, zeEventPoolDestroy>; |
| 95 | + |
| 96 | +} // namespace raii |
| 97 | +} // namespace v2 |
0 commit comments