/
usr
/
include
/
oneapi
/
tbb
/
/usr/include/oneapi/tbb
mkdir
upload
Name
Size
Mode
Actions
detail/
-
0755
rm
blocked_range.h
6453
0644
edit
dl
rm
blocked_range2d.h
3369
0644
edit
dl
rm
blocked_range3d.h
4455
0644
edit
dl
rm
blocked_rangeNd.h
5508
0644
edit
dl
rm
cache_aligned_allocator.h
6905
0644
edit
dl
rm
collaborative_call_once.h
9053
0644
edit
dl
rm
combinable.h
1969
0644
edit
dl
rm
concurrent_hash_map.h
71893
0644
edit
dl
rm
concurrent_lru_cache.h
13737
0644
edit
dl
rm
concurrent_map.h
13416
0644
edit
dl
rm
concurrent_priority_queue.h
19531
0644
edit
dl
rm
concurrent_queue.h
24030
0644
edit
dl
rm
concurrent_set.h
10241
0644
edit
dl
rm
concurrent_unordered_map.h
18697
0644
edit
dl
rm
concurrent_unordered_set.h
14850
0644
edit
dl
rm
concurrent_vector.h
46199
0644
edit
dl
rm
enumerable_thread_specific.h
42403
0644
edit
dl
rm
flow_graph.h
128339
0644
edit
dl
rm
flow_graph_abstractions.h
1446
0644
edit
dl
rm
global_control.h
6139
0644
edit
dl
rm
info.h
4179
0644
edit
dl
rm
memory_pool.h
9973
0644
edit
dl
rm
mutex.h
2628
0644
edit
dl
rm
null_mutex.h
2149
0644
edit
dl
rm
null_rw_mutex.h
2514
0644
edit
dl
rm
parallel_for.h
23451
0644
edit
dl
rm
parallel_for_each.h
27064
0644
edit
dl
rm
parallel_invoke.h
7783
0644
edit
dl
rm
parallel_pipeline.h
5755
0644
edit
dl
rm
parallel_reduce.h
37078
0644
edit
dl
rm
parallel_scan.h
22810
0644
edit
dl
rm
parallel_sort.h
11114
0644
edit
dl
rm
partitioner.h
28500
0644
edit
dl
rm
profiling.h
10005
0644
edit
dl
rm
queuing_mutex.h
6391
0644
edit
dl
rm
queuing_rw_mutex.h
6631
0644
edit
dl
rm
rw_mutex.h
8150
0644
edit
dl
rm
scalable_allocator.h
11655
0644
edit
dl
rm
spin_mutex.h
3513
0644
edit
dl
rm
spin_rw_mutex.h
7839
0644
edit
dl
rm
task.h
1090
0644
edit
dl
rm
task_arena.h
17853
0644
edit
dl
rm
task_group.h
27329
0644
edit
dl
rm
task_scheduler_observer.h
4585
0644
edit
dl
rm
tbbmalloc_proxy.h
1907
0644
edit
dl
rm
tbb_allocator.h
3782
0644
edit
dl
rm
tick_count.h
3201
0644
edit
dl
rm
version.h
4007
0644
edit
dl
rm
Edit:
/usr/include/oneapi/tbb/collaborative_call_once.h
(9053B)
/* Copyright (c) 2021 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #ifndef __TBB_collaborative_call_once_H #define __TBB_collaborative_call_once_H #if __TBB_PREVIEW_COLLABORATIVE_CALL_ONCE #include "task_arena.h" #include "task_group.h" #include "task.h" #include <atomic> namespace tbb { namespace detail { namespace d1 { #if _MSC_VER && !defined(__INTEL_COMPILER) // Suppress warning: structure was padded due to alignment specifier #pragma warning (push) #pragma warning (disable: 4324) #endif constexpr std::uintptr_t collaborative_once_max_references = max_nfs_size; constexpr std::uintptr_t collaborative_once_references_mask = collaborative_once_max_references-1; class alignas(max_nfs_size) collaborative_once_runner : no_copy { struct storage_t { task_arena m_arena{ task_arena::attach{} }; wait_context m_wait_context{1}; }; std::atomic<std::int64_t> m_ref_count{0}; std::atomic<bool> m_is_ready{false}; // Storage with task_arena and wait_context must be initialized only by winner thread union { storage_t m_storage; }; template<typename Fn> void isolated_execute(Fn f) { auto func = [f] { f(); // delegate_base requires bool returning functor while isolate_within_arena ignores the result return true; }; delegated_function<decltype(func)> delegate(func); r1::isolate_within_arena(delegate, reinterpret_cast<std::intptr_t>(this)); } public: class lifetime_guard : no_copy { collaborative_once_runner& m_runner; public: lifetime_guard(collaborative_once_runner& r) : m_runner(r) { m_runner.m_ref_count++; } ~lifetime_guard() { m_runner.m_ref_count--; } }; collaborative_once_runner() {} ~collaborative_once_runner() { spin_wait_until_eq(m_ref_count, 0, std::memory_order_acquire); if (m_is_ready.load(std::memory_order_relaxed)) { m_storage.~storage_t(); } } std::uintptr_t to_bits() { return reinterpret_cast<std::uintptr_t>(this); } static collaborative_once_runner* from_bits(std::uintptr_t bits) { __TBB_ASSERT( (bits & collaborative_once_references_mask) == 0, "invalid pointer, last log2(max_nfs_size) bits must be zero" ); return reinterpret_cast<collaborative_once_runner*>(bits); } template <typename F> void run_once(F&& f) { __TBB_ASSERT(!m_is_ready.load(std::memory_order_relaxed), "storage with task_arena and wait_context is already initialized"); // Initialize internal state new(&m_storage) storage_t(); m_storage.m_arena.execute([&] { isolated_execute([&] { task_group_context context{ task_group_context::bound, task_group_context::default_traits | task_group_context::concurrent_wait }; function_stack_task<F> t{ std::forward<F>(f), m_storage.m_wait_context }; // Set the ready flag after entering the execute body to prevent // moonlighting threads from occupying all slots inside the arena. m_is_ready.store(true, std::memory_order_release); execute_and_wait(t, context, m_storage.m_wait_context, context); }); }); } void assist() noexcept { // Do not join the arena until the winner thread takes the slot spin_wait_while_eq(m_is_ready, false); m_storage.m_arena.execute([&] { isolated_execute([&] { // We do not want to get an exception from user functor on moonlighting threads. // The exception is handled with the winner thread task_group_context stub_context; wait(m_storage.m_wait_context, stub_context); }); }); } }; class collaborative_once_flag : no_copy { enum state : std::uintptr_t { uninitialized, done, #if TBB_USE_ASSERT dead #endif }; std::atomic<std::uintptr_t> m_state{ state::uninitialized }; template <typename Fn, typename... Args> friend void collaborative_call_once(collaborative_once_flag& flag, Fn&& f, Args&&... args); void set_completion_state(std::uintptr_t runner_bits, std::uintptr_t desired) { std::uintptr_t expected = runner_bits; do { expected = runner_bits; // Possible inefficiency: when we start waiting, // some moonlighting threads might continue coming that will prolong our waiting. // Fortunately, there are limited number of threads on the system so wait time is limited. spin_wait_until_eq(m_state, expected); } while (!m_state.compare_exchange_strong(expected, desired)); } template <typename Fn> void do_collaborative_call_once(Fn&& f) { std::uintptr_t expected = m_state.load(std::memory_order_acquire); collaborative_once_runner runner; do { if (expected == state::uninitialized && m_state.compare_exchange_strong(expected, runner.to_bits())) { // Winner thread runner.run_once([&] { try_call([&] { std::forward<Fn>(f)(); }).on_exception([&] { // Reset the state to uninitialized to allow other threads to try initialization again set_completion_state(runner.to_bits(), state::uninitialized); }); // We successfully executed functor set_completion_state(runner.to_bits(), state::done); }); break; } else { // Moonlighting thread: we need to add a reference to the state to prolong runner lifetime. // However, the maximum number of references are limited with runner alignment. // So, we use CAS loop and spin_wait to guarantee that references never exceed "max_value". do { auto max_value = expected | collaborative_once_references_mask; expected = spin_wait_while_eq(m_state, max_value); // "expected > state::done" prevents storing values, when state is uninitialized or done } while (expected > state::done && !m_state.compare_exchange_strong(expected, expected + 1)); if (auto shared_runner = collaborative_once_runner::from_bits(expected & ~collaborative_once_references_mask)) { collaborative_once_runner::lifetime_guard guard{*shared_runner}; m_state.fetch_sub(1); // The moonlighting threads are not expected to handle exceptions from user functor. // Therefore, no exception is expected from assist(). shared_runner->assist(); } } __TBB_ASSERT(m_state.load(std::memory_order_relaxed) != state::dead, "collaborative_once_flag has been prematurely destroyed"); } while (expected != state::done); } #if TBB_USE_ASSERT public: ~collaborative_once_flag() { m_state.store(state::dead, std::memory_order_relaxed); } #endif }; template <typename Fn, typename... Args> void collaborative_call_once(collaborative_once_flag& flag, Fn&& fn, Args&&... args) { __TBB_ASSERT(flag.m_state.load(std::memory_order_relaxed) != collaborative_once_flag::dead, "collaborative_once_flag has been prematurely destroyed"); if (flag.m_state.load(std::memory_order_acquire) != collaborative_once_flag::done) { #if __TBB_GCC_PARAMETER_PACK_IN_LAMBDAS_BROKEN // Using stored_pack to suppress bug in GCC 4.8 // with parameter pack expansion in lambda auto stored_pack = save_pack(std::forward<Args>(args)...); auto func = [&] { call(std::forward<Fn>(fn), std::move(stored_pack)); }; #else auto func = [&] { fn(std::forward<Args>(args)...); }; #endif flag.do_collaborative_call_once(func); } } #if _MSC_VER && !defined(__INTEL_COMPILER) #pragma warning (pop) // 4324 warning #endif } // namespace d1 } // namespace detail using detail::d1::collaborative_call_once; using detail::d1::collaborative_once_flag; } // namespace tbb #endif // __TBB_PREVIEW_COLLABORATIVE_CALL_ONCE #endif // __TBB_collaborative_call_once_H
Save
cmd:
run