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 class task_handle;
34 
35 class task_handle_task : public d1::task {
36     std::uint64_t m_version_and_traits{};
37     d1::wait_context& m_wait_ctx;
38     d1::task_group_context& m_ctx;
39     d1::small_object_allocator m_allocator;
40 public:
41     void finalize(const d1::execution_data* ed = nullptr) {
42         if (ed) {
43             m_allocator.delete_object(this, *ed);
44         } else {
45             m_allocator.delete_object(this);
46         }
47     }
48 
task_handle_task(d1::wait_context & wo,d1::task_group_context & ctx,d1::small_object_allocator & alloc)49     task_handle_task(d1::wait_context& wo, d1::task_group_context& ctx, d1::small_object_allocator& alloc)
50         : m_wait_ctx(wo)
51         , m_ctx(ctx)
52         , m_allocator(alloc) {
53         suppress_unused_warning(m_version_and_traits);
54     }
55 
~task_handle_task()56     ~task_handle_task() override {
57         m_wait_ctx.release();
58     }
59 
ctx()60     d1::task_group_context& ctx() const { return m_ctx; }
61 };
62 
63 
64 class task_handle {
65     struct task_handle_task_finalizer_t{
operatortask_handle_task_finalizer_t66         void operator()(task_handle_task* p){ p->finalize(); }
67     };
68     using handle_impl_t = std::unique_ptr<task_handle_task, task_handle_task_finalizer_t>;
69 
70     handle_impl_t m_handle = {nullptr};
71 public:
72     task_handle() = default;
73     task_handle(task_handle&&) = default;
74     task_handle& operator=(task_handle&&) = default;
75 
76     explicit operator bool() const noexcept { return static_cast<bool>(m_handle); }
77 
78     friend bool operator==(task_handle const& th, std::nullptr_t) noexcept;
79     friend bool operator==(std::nullptr_t, task_handle const& th) noexcept;
80 
81     friend bool operator!=(task_handle const& th, std::nullptr_t) noexcept;
82     friend bool operator!=(std::nullptr_t, task_handle const& th) noexcept;
83 
84 private:
85     friend struct task_handle_accessor;
86 
task_handle(task_handle_task * t)87     task_handle(task_handle_task* t) : m_handle {t}{};
88 
release()89     d1::task* release() {
90        return m_handle.release();
91     }
92 };
93 
94 struct task_handle_accessor {
constructtask_handle_accessor95 static task_handle              construct(task_handle_task* t)  { return {t}; }
releasetask_handle_accessor96 static d1::task*                release(task_handle& th)        { return th.release(); }
ctx_oftask_handle_accessor97 static d1::task_group_context&  ctx_of(task_handle& th)         {
98     __TBB_ASSERT(th.m_handle, "ctx_of does not expect empty task_handle.");
99     return th.m_handle->ctx();
100 }
101 };
102 
103 inline bool operator==(task_handle const& th, std::nullptr_t) noexcept {
104     return th.m_handle == nullptr;
105 }
106 inline bool operator==(std::nullptr_t, task_handle const& th) noexcept {
107     return th.m_handle == nullptr;
108 }
109 
110 inline bool operator!=(task_handle const& th, std::nullptr_t) noexcept {
111     return th.m_handle != nullptr;
112 }
113 
114 inline bool operator!=(std::nullptr_t, task_handle const& th) noexcept {
115     return th.m_handle != nullptr;
116 }
117 
118 } // namespace d2
119 } // namespace detail
120 } // namespace tbb
121 
122 #endif /* __TBB_task_handle_H */
123