1 /* 2 Copyright (c) 2020-2021 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 18 #ifndef __TBB_task_handle_H 19 #define __TBB_task_handle_H 20 21 #include "_config.h" 22 #include "_task.h" 23 #include "_small_object_pool.h" 24 #include "_utils.h" 25 #include <memory> 26 27 namespace tbb { 28 namespace detail { 29 30 namespace d1 { class task_group_context; class wait_context; struct execution_data; } 31 namespace d2 { 32 33 #if __TBB_PREVIEW_TASK_GROUP_EXTENSIONS 34 class task_handle; 35 36 class task_handle_task : public d1::task { 37 std::uint64_t m_version_and_traits{}; 38 d1::wait_context& m_wait_ctx; 39 d1::task_group_context& m_ctx; 40 d1::small_object_allocator m_allocator; 41 public: 42 void finalize(const d1::execution_data* ed = nullptr) { 43 if (ed) { 44 m_allocator.delete_object(this, *ed); 45 } else { 46 m_allocator.delete_object(this); 47 } 48 } 49 50 task_handle_task(d1::wait_context& wo, d1::task_group_context& ctx, d1::small_object_allocator& alloc) 51 : m_wait_ctx(wo) 52 , m_ctx(ctx) 53 , m_allocator(alloc) { 54 suppress_unused_warning(m_version_and_traits); 55 } 56 57 ~task_handle_task(){ 58 m_wait_ctx.release(); 59 } 60 61 d1::task_group_context& ctx() const { return m_ctx; } 62 }; 63 64 65 class task_handle { 66 struct task_handle_task_finalizer_t{ 67 void operator()(task_handle_task* p){ p->finalize(); } 68 }; 69 using handle_impl_t = std::unique_ptr<task_handle_task, task_handle_task_finalizer_t>; 70 71 handle_impl_t m_handle = {nullptr}; 72 public: 73 task_handle() = default; 74 task_handle(task_handle&&) = default; 75 task_handle& operator=(task_handle&&) = default; 76 77 explicit operator bool() const noexcept { return static_cast<bool>(m_handle); } 78 79 friend bool operator==(task_handle const& th, std::nullptr_t) noexcept; 80 friend bool operator==(std::nullptr_t, task_handle const& th) noexcept; 81 82 friend bool operator!=(task_handle const& th, std::nullptr_t) noexcept; 83 friend bool operator!=(std::nullptr_t, task_handle const& th) noexcept; 84 85 private: 86 friend struct task_handle_accessor; 87 88 task_handle(task_handle_task* t) : m_handle {t}{}; 89 90 d1::task* release() { 91 return m_handle.release(); 92 } 93 }; 94 95 struct task_handle_accessor { 96 static task_handle construct(task_handle_task* t) { return {t}; } 97 static d1::task* release(task_handle& th) { return th.release(); } 98 static d1::task_group_context& ctx_of(task_handle& th) { 99 __TBB_ASSERT(th.m_handle, "ctx_of does not expect empty task_handle."); 100 return th.m_handle->ctx(); 101 } 102 }; 103 104 inline bool operator==(task_handle const& th, std::nullptr_t) noexcept { 105 return th.m_handle == nullptr; 106 } 107 inline bool operator==(std::nullptr_t, task_handle const& th) noexcept { 108 return th.m_handle == nullptr; 109 } 110 111 inline bool operator!=(task_handle const& th, std::nullptr_t) noexcept { 112 return th.m_handle != nullptr; 113 } 114 115 inline bool operator!=(std::nullptr_t, task_handle const& th) noexcept { 116 return th.m_handle != nullptr; 117 } 118 #endif // __TBB_PREVIEW_TASK_GROUP_EXTENSIONS 119 120 } // namespace d2 121 } // namespace detail 122 } // namespace tbb 123 124 #endif /* __TBB_task_handle_H */ 125