_aggregator_impl.h
Go to the documentation of this file.
/*
Copyright (c) 2005-2020 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__aggregator_impl_H
#define __TBB__aggregator_impl_H
#include "../atomic.h"
#if !__TBBMALLOC_BUILD
#include "../tbb_profiling.h"
#endif
namespace tbb {
namespace interface6 {
namespace internal {
using namespace tbb::internal;
template <typename Derived>
class aggregated_operation {
public:
uintptr_t status;
Derived *next;
aggregated_operation() : status(0), next(NULL) {}
};
template < typename operation_type >
class aggregator_generic {
public:
aggregator_generic() : handler_busy(false) { pending_operations = NULL; }
template < typename handler_type >
void execute(operation_type *op, handler_type &handle_operations, bool long_life_time = true) {
operation_type *res;
// op->status should be read before inserting the operation into the
// aggregator waitlist since it can become invalid after executing a
// handler (if the operation has 'short' life time.)
const uintptr_t status = op->status;
// ITT note: &(op->status) tag is used to cover accesses to this op node. This
// thread has created the operation, and now releases it so that the handler
// thread may handle the associated operation w/o triggering a race condition;
// thus this tag will be acquired just before the operation is handled in the
// handle_operations functor.
call_itt_notify(releasing, &(op->status));
// insert the operation in the queue.
do {
// Tools may flag the following line as a race; it is a false positive:
// This is an atomic read; we don't provide itt_hide_load_word for atomics
op->next = res = pending_operations; // NOT A RACE
} while (pending_operations.compare_and_swap(op, res) != res);
if (!res) { // first in the list; handle the operations.
// ITT note: &pending_operations tag covers access to the handler_busy flag,
// which this waiting handler thread will try to set before entering
// handle_operations.
call_itt_notify(acquired, &pending_operations);
start_handle_operations(handle_operations);
// The operation with 'short' life time can already be destroyed.
if (long_life_time)
__TBB_ASSERT(op->status, NULL);
}
// not first; wait for op to be ready.
else if (!status) { // operation is blocking here.
__TBB_ASSERT(long_life_time, "Waiting for an operation object that might be destroyed during processing.");
call_itt_notify(prepare, &(op->status));
spin_wait_while_eq(op->status, uintptr_t(0));
itt_load_word_with_acquire(op->status);
}
}
private:
atomic<operation_type *> pending_operations;
uintptr_t handler_busy;
template < typename handler_type >
void start_handle_operations( handler_type &handle_operations ) {
operation_type *op_list;
// ITT note: &handler_busy tag covers access to pending_operations as it is passed
// between active and waiting handlers. Below, the waiting handler waits until
// the active handler releases, and the waiting handler acquires &handler_busy as
// it becomes the active_handler. The release point is at the end of this
// function, when all operations in pending_operations have been handled by the
// owner of this aggregator.
call_itt_notify(prepare, &handler_busy);
// get the handler_busy:
// only one thread can possibly spin here at a time
spin_wait_until_eq(handler_busy, uintptr_t(0));
call_itt_notify(acquired, &handler_busy);
// acquire fence not necessary here due to causality rule and surrounding atomics
__TBB_store_with_release(handler_busy, uintptr_t(1));
// ITT note: &pending_operations tag covers access to the handler_busy flag
// itself. Capturing the state of the pending_operations signifies that
// handler_busy has been set and a new active handler will now process that list's
// operations.
call_itt_notify(releasing, &pending_operations);
// grab pending_operations
op_list = pending_operations.fetch_and_store(NULL);
// handle all the operations
handle_operations(op_list);
// release the handler
itt_store_word_with_release(handler_busy, uintptr_t(0));
}
};
template < typename handler_type, typename operation_type >
class aggregator : public aggregator_generic<operation_type> {
handler_type handle_operations;
public:
aggregator() {}
explicit aggregator(handler_type h) : handle_operations(h) {}
void initialize_handler(handler_type h) { handle_operations = h; }
void execute(operation_type *op) {
aggregator_generic<operation_type>::execute(op, handle_operations);
}
};
// the most-compatible friend declaration (vs, gcc, icc) is
// template<class U, class V> friend class aggregating_functor;
template<typename aggregating_class, typename operation_list>
class aggregating_functor {
aggregating_class *fi;
public:
aggregating_functor() : fi() {}
aggregating_functor(aggregating_class *fi_) : fi(fi_) {}
void operator()(operation_list* op_list) { fi->handle_operations(op_list); }
};
} // namespace internal
} // namespace interface6
namespace internal {
using interface6::internal::aggregated_operation;
using interface6::internal::aggregator_generic;
using interface6::internal::aggregator;
using interface6::internal::aggregating_functor;
} // namespace internal
} // namespace tbb
#endif // __TBB__aggregator_impl_H
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
h
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function h
Definition: ittnotify_static.h:159
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::internal
Identifiers declared inside namespace internal should never be used directly by client code.
Definition: atomic.h:65
tbb::internal::acquired
@ acquired
Definition: tbb_profiling.h:129
tbb::internal::releasing
@ releasing
Definition: tbb_profiling.h:129
tbb::internal::prepare
@ prepare
Definition: tbb_profiling.h:129
tbb::internal::spin_wait_until_eq
void spin_wait_until_eq(const volatile T &location, const U value)
Spin UNTIL the value of the variable is equal to a given value.
Definition: tbb_machine.h:402
tbb::internal::itt_load_word_with_acquire
T itt_load_word_with_acquire(const tbb::atomic< T > &src)
Definition: tbb_profiling.h:168
tbb::internal::itt_store_word_with_release
void itt_store_word_with_release(tbb::atomic< T > &dst, U src)
Definition: tbb_profiling.h:157
tbb::internal::call_itt_notify
void call_itt_notify(notify_type, void *)
Definition: tbb_profiling.h:276
tbb::internal::__TBB_store_with_release
void __TBB_store_with_release(volatile T &location, V value)
Definition: tbb_machine.h:716
tbb::internal::spin_wait_while_eq
void spin_wait_while_eq(const volatile T &location, U value)
Spin WHILE the value of the variable is equal to a given value.
Definition: tbb_machine.h:394
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::interface6::internal::aggregated_operation
aggregated_operation base class
Definition: _aggregator_impl.h:33
tbb::interface6::internal::aggregated_operation::aggregated_operation
aggregated_operation()
Definition: _aggregator_impl.h:39
tbb::interface6::internal::aggregated_operation::next
Derived * next
Definition: _aggregator_impl.h:38
tbb::interface6::internal::aggregated_operation::status
uintptr_t status
Zero value means "wait" status, all other values are "user" specified values and are defined into the...
Definition: _aggregator_impl.h:36
tbb::interface6::internal::aggregator_generic
Aggregator base class.
Definition: _aggregator_impl.h:49
tbb::interface6::internal::aggregator_generic::pending_operations
atomic< operation_type * > pending_operations
An atomically updated list (aka mailbox) of pending operations.
Definition: _aggregator_impl.h:104
tbb::interface6::internal::aggregator_generic::execute
void execute(operation_type *op, handler_type &handle_operations, bool long_life_time=true)
Execute an operation.
Definition: _aggregator_impl.h:64
tbb::interface6::internal::aggregator_generic::aggregator_generic
aggregator_generic()
Definition: _aggregator_impl.h:51
tbb::interface6::internal::aggregator_generic::handler_busy
uintptr_t handler_busy
Controls thread access to handle_operations.
Definition: _aggregator_impl.h:106
tbb::interface6::internal::aggregator_generic::start_handle_operations
void start_handle_operations(handler_type &handle_operations)
Trigger the handling of operations when the handler is free.
Definition: _aggregator_impl.h:110
tbb::interface6::internal::aggregator
Definition: _aggregator_impl.h:144
tbb::interface6::internal::aggregator::initialize_handler
void initialize_handler(handler_type h)
Definition: _aggregator_impl.h:150
tbb::interface6::internal::aggregator::aggregator
aggregator()
Definition: _aggregator_impl.h:147
tbb::interface6::internal::aggregator::aggregator
aggregator(handler_type h)
Definition: _aggregator_impl.h:148
tbb::interface6::internal::aggregator::handle_operations
handler_type handle_operations
Definition: _aggregator_impl.h:145
tbb::interface6::internal::aggregator::execute
void execute(operation_type *op)
Definition: _aggregator_impl.h:152
tbb::interface6::internal::aggregating_functor
Definition: _aggregator_impl.h:160
tbb::interface6::internal::aggregating_functor::operator()
void operator()(operation_list *op_list)
Definition: _aggregator_impl.h:165
tbb::interface6::internal::aggregating_functor::aggregating_functor
aggregating_functor(aggregating_class *fi_)
Definition: _aggregator_impl.h:164
tbb::interface6::internal::aggregating_functor::fi
aggregating_class * fi
Definition: _aggregator_impl.h:161
tbb::interface6::internal::aggregating_functor::aggregating_functor
aggregating_functor()
Definition: _aggregator_impl.h:163

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.