aggregator.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_H
#define __TBB__aggregator_H
#define __TBB_aggregator_H_include_area
#include "internal/_warning_suppress_enable_notice.h"
#if !TBB_PREVIEW_AGGREGATOR
#error Set TBB_PREVIEW_AGGREGATOR before including aggregator.h
#endif
#include "atomic.h"
#include "tbb_profiling.h"
namespace tbb {
namespace interface6 {
using namespace tbb::internal;
class aggregator_operation {
template<typename handler_type> friend class aggregator_ext;
uintptr_t status;
aggregator_operation* my_next;
public:
enum aggregator_operation_status { agg_waiting=0, agg_finished };
aggregator_operation() : status(agg_waiting), my_next(NULL) {}
void start() { call_itt_notify(acquired, &status); }
void finish() { itt_store_word_with_release(status, uintptr_t(agg_finished)); }
aggregator_operation* next() { return itt_hide_load_word(my_next);}
void set_next(aggregator_operation* n) { itt_hide_store_word(my_next, n); }
};
namespace internal {
class basic_operation_base : public aggregator_operation {
friend class basic_handler;
virtual void apply_body() = 0;
public:
basic_operation_base() : aggregator_operation() {}
virtual ~basic_operation_base() {}
};
template<typename Body>
class basic_operation : public basic_operation_base, no_assign {
const Body& my_body;
void apply_body() __TBB_override { my_body(); }
public:
basic_operation(const Body& b) : basic_operation_base(), my_body(b) {}
};
class basic_handler {
public:
basic_handler() {}
void operator()(aggregator_operation* op_list) const {
while (op_list) {
// ITT note: &(op_list->status) tag is used to cover accesses to the operation data.
// The executing thread "acquires" the tag (see start()) and then performs
// the associated operation w/o triggering a race condition diagnostics.
// A thread that created the operation is waiting for its status (see execute_impl()),
// so when this thread is done with the operation, it will "release" the tag
// and update the status (see finish()) to give control back to the waiting thread.
basic_operation_base& request = static_cast<basic_operation_base&>(*op_list);
// IMPORTANT: need to advance op_list to op_list->next() before calling request.finish()
op_list = op_list->next();
request.start();
request.apply_body();
request.finish();
}
}
};
} // namespace internal
template <typename handler_type>
class aggregator_ext : tbb::internal::no_copy {
public:
aggregator_ext(const handler_type& h) : handler_busy(0), handle_operations(h) { mailbox = NULL; }
void process(aggregator_operation *op) { execute_impl(*op); }
protected:
void execute_impl(aggregator_operation& op) {
aggregator_operation* res;
// ITT note: &(op.status) tag is used to cover accesses to this operation. 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 into the list
do {
// ITT 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.my_next = res = mailbox; // NOT A RACE
} while (mailbox.compare_and_swap(&op, res) != res);
if (!res) { // first in the list; handle the operations
// ITT note: &mailbox 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, &mailbox);
start_handle_operations();
__TBB_ASSERT(op.status, NULL);
}
else { // not first; wait for op to be ready
call_itt_notify(prepare, &(op.status));
spin_wait_while_eq(op.status, uintptr_t(aggregator_operation::agg_waiting));
itt_load_word_with_acquire(op.status);
}
}
private:
atomic<aggregator_operation *> mailbox;
uintptr_t handler_busy;
handler_type handle_operations;
void start_handle_operations() {
aggregator_operation *pending_operations;
// ITT note: &handler_busy tag covers access to mailbox 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 mailbox have been handled by the
// owner of this aggregator.
call_itt_notify(prepare, &handler_busy);
// get 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: &mailbox tag covers access to the handler_busy flag itself.
// Capturing the state of the mailbox signifies that handler_busy has been
// set and a new active handler will now process that list's operations.
call_itt_notify(releasing, &mailbox);
// grab pending_operations
pending_operations = mailbox.fetch_and_store(NULL);
// handle all the operations
handle_operations(pending_operations);
// release the handler
itt_store_word_with_release(handler_busy, uintptr_t(0));
}
};
class aggregator : private aggregator_ext<internal::basic_handler> {
public:
aggregator() : aggregator_ext<internal::basic_handler>(internal::basic_handler()) {}
template<typename Body>
void execute(const Body& b) {
internal::basic_operation<Body> op(b);
this->execute_impl(op);
}
};
} // namespace interface6
using interface6::aggregator;
using interface6::aggregator_ext;
using interface6::aggregator_operation;
} // namespace tbb
#include "internal/_warning_suppress_disable_notice.h"
#undef __TBB_aggregator_H_include_area
#endif // __TBB__aggregator_H
tbb::interface6::aggregator_operation::finish
void finish()
Call finish when done handling this operation.
Definition: aggregator.h:46
tbb::interface6::aggregator::execute
void execute(const Body &b)
BASIC INTERFACE: Enter a function for exclusive execution by the aggregator.
Definition: aggregator.h:187
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::interface6::internal::basic_operation
Definition: aggregator.h:62
tbb::interface6::internal::basic_operation_base::apply_body
virtual void apply_body()=0
tbb::interface6::aggregator_ext::handler_busy
uintptr_t handler_busy
Controls thread access to handle_operations.
Definition: aggregator.h:143
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::interface6::aggregator_ext::mailbox
atomic< aggregator_operation * > mailbox
An atomically updated list (aka mailbox) of aggregator_operations.
Definition: aggregator.h:139
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::itt_load_word_with_acquire
T itt_load_word_with_acquire(const tbb::atomic< T > &src)
Definition: tbb_profiling.h:168
tbb::interface6::aggregator_ext
Aggregator base class and expert interface.
Definition: aggregator.h:96
tbb::interface6::internal::basic_operation_base
Definition: aggregator.h:53
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::interface6::aggregator_operation::status
uintptr_t status
Definition: aggregator.h:37
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
tbb::interface6::aggregator_ext::start_handle_operations
void start_handle_operations()
Trigger the handling of operations when the handler is free.
Definition: aggregator.h:148
tbb::interface6::aggregator_ext::handle_operations
handler_type handle_operations
Definition: aggregator.h:145
tbb::interface6::internal::basic_handler::operator()
void operator()(aggregator_operation *op_list) const
Definition: aggregator.h:72
tbb_profiling.h
tbb::interface6::aggregator_operation::aggregator_operation
aggregator_operation()
Definition: aggregator.h:41
tbb::internal::prepare
Definition: tbb_profiling.h:129
tbb::interface6::internal::basic_operation::my_body
const Body & my_body
Definition: aggregator.h:63
tbb::interface6::internal::basic_operation_base::basic_operation_base
basic_operation_base()
Definition: aggregator.h:57
tbb::internal::acquired
Definition: tbb_profiling.h:129
tbb::interface6::aggregator_operation::next
aggregator_operation * next()
Definition: aggregator.h:47
tbb::interface6::aggregator_operation::start
void start()
Call start before handling this operation.
Definition: aggregator.h:43
tbb::internal::__TBB_store_with_release
void __TBB_store_with_release(volatile T &location, V value)
Definition: tbb_machine.h:716
tbb::interface6::aggregator_operation::agg_waiting
Definition: aggregator.h:40
tbb::interface6::internal::basic_handler::basic_handler
basic_handler()
Definition: aggregator.h:71
atomic.h
tbb::interface6::aggregator_ext::execute_impl
void execute_impl(aggregator_operation &op)
Definition: aggregator.h:107
tbb::interface6::aggregator::aggregator
aggregator()
Definition: aggregator.h:182
tbb::interface6::aggregator_operation::aggregator_operation_status
aggregator_operation_status
Definition: aggregator.h:40
_warning_suppress_disable_notice.h
__TBB_override
#define __TBB_override
Definition: tbb_stddef.h:240
tbb::interface6::aggregator
Basic aggregator interface.
Definition: aggregator.h:180
tbb::interface6::aggregator_operation::set_next
void set_next(aggregator_operation *n)
Definition: aggregator.h:48
tbb::internal::itt_hide_store_word
void itt_hide_store_word(T &dst, T src)
Definition: tbb_profiling.h:210
tbb::internal::no_assign
Base class for types that should not be assigned.
Definition: tbb_stddef.h:322
tbb::internal::releasing
Definition: tbb_profiling.h:129
tbb::interface6::internal::basic_handler
Definition: aggregator.h:69
tbb::interface6::aggregator_operation
Definition: aggregator.h:35
tbb::interface6::aggregator_ext::aggregator_ext
aggregator_ext(const handler_type &h)
Definition: aggregator.h:98
tbb::interface6::internal::basic_operation_base::~basic_operation_base
virtual ~basic_operation_base()
Definition: aggregator.h:58
tbb::internal
Identifiers declared inside namespace internal should never be used directly by client code.
Definition: atomic.h:65
_warning_suppress_enable_notice.h
tbb::internal::itt_hide_load_word
T itt_hide_load_word(const T &src)
Definition: tbb_profiling.h:222
tbb::interface6::internal::basic_operation::basic_operation
basic_operation(const Body &b)
Definition: aggregator.h:66
tbb::internal::call_itt_notify
void call_itt_notify(notify_type, void *)
Definition: tbb_profiling.h:276
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::interface6::aggregator_operation::my_next
aggregator_operation * my_next
Definition: aggregator.h:38
tbb::interface6::internal::basic_operation::apply_body
void apply_body() __TBB_override
Definition: aggregator.h:64
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::interface6::aggregator_ext::process
void process(aggregator_operation *op)
EXPERT INTERFACE: Enter a user-made operation into the aggregator's mailbox.
Definition: aggregator.h:102

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.