Skip to content

Commit 575948b

Browse files
kboyarinovakukanov
andauthored
Add current_task_ptr entry point (#1821)
Co-authored-by: Alexey Kukanov <[email protected]>
1 parent fcf40fc commit 575948b

File tree

11 files changed

+172
-4
lines changed

11 files changed

+172
-4
lines changed

include/oneapi/tbb/detail/_task.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ TBB_EXPORT d1::slot_id __TBB_EXPORTED_FUNC execution_slot(const d1::execution_da
6363
TBB_EXPORT d1::slot_id __TBB_EXPORTED_FUNC execution_slot(const d1::task_arena_base&);
6464
TBB_EXPORT d1::task_group_context* __TBB_EXPORTED_FUNC current_context();
6565
TBB_EXPORT d1::wait_tree_vertex_interface* get_thread_reference_vertex(d1::wait_tree_vertex_interface* wc);
66+
TBB_EXPORT d1::task* __TBB_EXPORTED_FUNC current_task_ptr();
6667

6768
// Do not place under __TBB_RESUMABLE_TASKS. It is a stub for unsupported platforms.
6869
struct suspend_point_type;
@@ -243,6 +244,7 @@ inline void wait(wait_context& wait_ctx, task_group_context& ctx) {
243244
call_itt_task_notify(destroy, &wait_ctx);
244245
}
245246

247+
using r1::current_task_ptr;
246248
using r1::current_context;
247249

248250
class task_traits {

src/tbb/def/lin32-tbb.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright (c) 2005-2025 Intel Corporation
3+
Copyright (c) 2025 UXL Foundation Contributors
34

45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -78,6 +79,7 @@ _ZN3tbb6detail2r16resumeEPNS1_18suspend_point_typeE;
7879
_ZN3tbb6detail2r121current_suspend_pointEv;
7980
_ZN3tbb6detail2r114notify_waitersEj;
8081
_ZN3tbb6detail2r127get_thread_reference_vertexEPNS0_2d126wait_tree_vertex_interfaceE;
82+
_ZN3tbb6detail2r116current_task_ptrEv;
8183

8284
/* Task dispatcher (task_dispatcher.cpp) */
8385
_ZN3tbb6detail2r114execution_slotEPKNS0_2d114execution_dataE;

src/tbb/def/lin64-tbb.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright (c) 2005-2025 Intel Corporation
3+
Copyright (c) 2025 UXL Foundation Contributors
34

45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -78,6 +79,7 @@ _ZN3tbb6detail2r16resumeEPNS1_18suspend_point_typeE;
7879
_ZN3tbb6detail2r121current_suspend_pointEv;
7980
_ZN3tbb6detail2r114notify_waitersEm;
8081
_ZN3tbb6detail2r127get_thread_reference_vertexEPNS0_2d126wait_tree_vertex_interfaceE;
82+
_ZN3tbb6detail2r116current_task_ptrEv;
8183

8284
/* Task dispatcher (task_dispatcher.cpp) */
8385
_ZN3tbb6detail2r114execution_slotEPKNS0_2d114execution_dataE;

src/tbb/def/mac64-tbb.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright (c) 2005-2025 Intel Corporation
2+
# Copyright (c) 2025 UXL Foundation Contributors
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -80,6 +81,7 @@ __ZN3tbb6detail2r16resumeEPNS1_18suspend_point_typeE
8081
__ZN3tbb6detail2r121current_suspend_pointEv
8182
__ZN3tbb6detail2r114notify_waitersEm
8283
__ZN3tbb6detail2r127get_thread_reference_vertexEPNS0_2d126wait_tree_vertex_interfaceE
84+
__ZN3tbb6detail2r116current_task_ptrEv
8385

8486
# Task dispatcher (task_dispatcher.cpp)
8587
__ZN3tbb6detail2r114execution_slotEPKNS0_2d114execution_dataE

src/tbb/def/win32-tbb.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; Copyright (c) 2005-2025 Intel Corporation
2+
; Copyright (c) 2025 UXL Foundation Contributors
23
;
34
; Licensed under the Apache License, Version 2.0 (the "License");
45
; you may not use this file except in compliance with the License.
@@ -72,6 +73,7 @@ EXPORTS
7273
?suspend@r1@detail@tbb@@YAXP6AXPAXPAUsuspend_point_type@123@@Z0@Z
7374
?notify_waiters@r1@detail@tbb@@YAXI@Z
7475
?get_thread_reference_vertex@r1@detail@tbb@@YAPAVwait_tree_vertex_interface@d1@23@PAV4523@@Z
76+
?current_task_ptr@r1@detail@tbb@@YAPAVtask@d1@23@XZ
7577

7678
; Task dispatcher (task_dispatcher.cpp)
7779
?spawn@r1@detail@tbb@@YAXAAVtask@d1@23@AAVtask_group_context@523@G@Z

src/tbb/def/win64-tbb.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; Copyright (c) 2005-2025 Intel Corporation
2+
; Copyright (c) 2025 UXL Foundation Contributors
23
;
34
; Licensed under the Apache License, Version 2.0 (the "License");
45
; you may not use this file except in compliance with the License.
@@ -72,6 +73,7 @@ EXPORTS
7273
?current_suspend_point@r1@detail@tbb@@YAPEAUsuspend_point_type@123@XZ
7374
?notify_waiters@r1@detail@tbb@@YAX_K@Z
7475
?get_thread_reference_vertex@r1@detail@tbb@@YAPEAVwait_tree_vertex_interface@d1@23@PEAV4523@@Z
76+
?current_task_ptr@r1@detail@tbb@@YAPEAVtask@d1@23@XZ
7577

7678
; Task dispatcher (task_dispatcher.cpp)
7779
?spawn@r1@detail@tbb@@YAXAEAVtask@d1@23@AEAVtask_group_context@523@@Z

src/tbb/scheduler_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ class alignas (max_nfs_size) task_dispatcher {
559559
>
560560
m_reference_vertex_map;
561561

562+
d1::task* m_innermost_running_task{ nullptr };
563+
562564
//! Attempt to get a task from the mailbox.
563565
/** Gets a task only if it has not been executed by its sender or a thief
564566
that has stolen it from the sender's task pool. Otherwise returns nullptr.

src/tbb/task.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ d1::wait_tree_vertex_interface* get_thread_reference_vertex(d1::wait_tree_vertex
256256
return ref_counter;
257257
}
258258

259+
d1::task* __TBB_EXPORTED_FUNC current_task_ptr() {
260+
return governor::get_thread_data()->get_innermost_running_task();
261+
}
262+
259263
} // namespace r1
260264
} // namespace detail
261265
} // namespace tbb

src/tbb/task_dispatcher.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,13 @@ d1::task* task_dispatcher::local_wait_for_all(d1::task* t, Waiter& waiter ) {
254254
task_dispatcher& task_disp;
255255
execution_data_ext old_execute_data_ext;
256256
properties old_properties;
257+
d1::task* old_innermost_running_task;
257258
bool is_initially_registered;
258259

259260
~dispatch_loop_guard() {
260261
task_disp.m_execute_data_ext = old_execute_data_ext;
261262
task_disp.m_properties = old_properties;
263+
task_disp.m_innermost_running_task = old_innermost_running_task;
262264

263265
if (!is_initially_registered) {
264266
task_disp.m_thread_data->my_arena->my_tc_client.get_pm_client()->unregister_thread();
@@ -268,7 +270,7 @@ d1::task* task_dispatcher::local_wait_for_all(d1::task* t, Waiter& waiter ) {
268270
__TBB_ASSERT(task_disp.m_thread_data && governor::is_thread_data_set(task_disp.m_thread_data), nullptr);
269271
__TBB_ASSERT(task_disp.m_thread_data->my_task_dispatcher == &task_disp, nullptr);
270272
}
271-
} dl_guard{ *this, m_execute_data_ext, m_properties, m_thread_data->my_is_registered };
273+
} dl_guard{ *this, m_execute_data_ext, m_properties, m_innermost_running_task, m_thread_data->my_is_registered };
272274

273275
// The context guard to track fp setting and itt tasks.
274276
context_guard_helper</*report_tasks=*/ITTPossible> context_guard;
@@ -331,6 +333,9 @@ d1::task* task_dispatcher::local_wait_for_all(d1::task* t, Waiter& waiter ) {
331333
void* itt_caller = ed.context->my_itt_caller;
332334
suppress_unused_warning(itt_caller);
333335

336+
d1::task* prev_innermost_running_task = m_innermost_running_task;
337+
m_innermost_running_task = t;
338+
334339
ITT_CALLEE_ENTER(ITTPossible, t, itt_caller);
335340

336341
if (ed.context->is_group_execution_cancelled()) {
@@ -346,6 +351,7 @@ d1::task* task_dispatcher::local_wait_for_all(d1::task* t, Waiter& waiter ) {
346351
ed.affinity_slot = d1::no_slot;
347352
// Reset task owner id for bypassed task
348353
ed.original_slot = m_thread_data->my_arena_index;
354+
m_innermost_running_task = prev_innermost_running_task;
349355
t = get_critical_task(t, ed, isolation, critical_allowed);
350356
}
351357
__TBB_ASSERT(m_thread_data && governor::is_thread_data_set(m_thread_data), nullptr);

src/tbb/thread_data.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
Copyright (c) 2020-2024 Intel Corporation
2+
Copyright (c) 2020-2025 Intel Corporation
3+
Copyright (c) 2025 UXL Foundation Contributors
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -139,6 +140,7 @@ class thread_data : public ::rml::job
139140
void enter_task_dispatcher(task_dispatcher& task_disp, std::uintptr_t stealing_threshold);
140141
void leave_task_dispatcher();
141142
void propagate_task_group_state(std::atomic<uint32_t> d1::task_group_context::* mptr_state, d1::task_group_context& src, uint32_t new_state);
143+
d1::task* get_innermost_running_task();
142144

143145
//! Index of the arena slot the scheduler occupies now, or occupied last time
144146
unsigned short my_arena_index;
@@ -254,9 +256,12 @@ inline void thread_data::propagate_task_group_state(std::atomic<std::uint32_t> d
254256
my_context_list->epoch.store(the_context_state_propagation_epoch.load(std::memory_order_relaxed), std::memory_order_release);
255257
}
256258

259+
inline d1::task* thread_data::get_innermost_running_task() {
260+
return my_task_dispatcher->m_innermost_running_task;
261+
}
262+
257263
} // namespace r1
258264
} // namespace detail
259265
} // namespace tbb
260266

261267
#endif // __TBB_thread_data_H
262-

0 commit comments

Comments
 (0)