46 my_token_ready =
false;
80 void grow( size_type minimum_size );
84 static const size_type initial_buffer_size = 4;
108 array(NULL), my_sem(NULL), array_size(0),
109 low_token(0), high_token(0),
110 is_ordered(is_ordered_), is_bound(is_bound_),
111 end_of_input_tls_allocated(false) {
112 grow(initial_buffer_size);
114 if(is_bound) create_sema(0);
125 if(end_of_input_tls_allocated) {
144 bool was_empty = !array[low_token&(array_size-1)].
is_valid;
152 token = high_token++;
154 if( token!=low_token || is_bound || force_put ) {
157 if( token-low_token>=array_size )
158 grow( token-low_token+1 );
160 array[token&(array_size-1)] = info_;
161 if(was_empty && is_bound) {
177 template<
typename StageTask>
183 if( !is_ordered || token==low_token ) {
185 task_info& item = array[++low_token & (array_size-1)];
192 spawner.spawn_stage_task(wakee);
195 #if __TBB_TASK_GROUP_CONTEXT 199 for( size_type i=0; i<array_size; ++i, ++t ){
200 task_info& temp = array[t&(array_size-1)];
214 task_info& item = array[low_token&(array_size-1)];
219 if (advance) low_token++;
238 while( new_size<minimum_size )
245 for(
size_type i=0; i<old_size; ++i, ++t )
246 new_array[t&(new_size-1)] = old_array[t&(old_size-1)];
265 my_pipeline(pipeline),
274 my_pipeline(pipeline),
286 #if __TBB_TASK_GROUP_CONTEXT 290 __TBB_ASSERT(is_cancelled(),
"Trying to finalize the task that wasn't cancelled");
295 #endif // __TBB_TASK_GROUP_CONTEXT 296 void spawn_stage_task(
const task_info& info)
309 if( my_filter->is_serial() ) {
311 if(
my_object || ( my_filter->object_may_be_null() && !my_pipeline.end_of_input) )
313 if( my_filter->is_ordered() ) {
314 my_token = my_pipeline.token_counter++;
317 if( my_pipeline.has_thread_bound_filters )
318 my_pipeline.token_counter++;
320 if( !my_filter->next_filter_in_pipeline ) {
322 goto process_another_stage;
325 if( --my_pipeline.input_tokens>0 )
326 spawn( *
new( allocate_additional_child_of(*
parent()) )
stage_task( my_pipeline ) );
329 my_pipeline.end_of_input =
true;
333 if( my_pipeline.end_of_input )
336 if( my_pipeline.has_thread_bound_filters )
337 my_pipeline.token_counter++;
340 if( --my_pipeline.input_tokens>0 )
341 spawn( *
new( allocate_additional_child_of(*
parent()) )
stage_task( my_pipeline ) );
343 if( !
my_object && (!my_filter->object_may_be_null() || my_filter->my_input_buffer->my_tls_end_of_input()) )
345 my_pipeline.end_of_input =
true;
347 if( my_pipeline.has_thread_bound_filters )
348 my_pipeline.token_counter--;
356 if( my_filter->is_serial() )
357 my_filter->my_input_buffer->note_done(
my_token, *
this);
359 my_filter = my_filter->next_filter_in_pipeline;
362 if( my_filter->is_serial() ) {
364 if( my_filter->my_input_buffer->put_token(*
this) ){
366 if( my_filter->is_bound() ) {
369 my_filter = my_filter->next_filter_in_pipeline;
370 }
while( my_filter && my_filter->is_bound() );
372 if( my_filter && my_filter->my_input_buffer->return_item(*
this, !my_filter->is_serial()))
373 goto process_another_stage;
381 size_t ntokens_avail = ++my_pipeline.input_tokens;
382 if(my_pipeline.filter_list->is_bound() ) {
383 if(ntokens_avail == 1) {
384 my_pipeline.filter_list->my_input_buffer->sema_V();
389 || my_pipeline.end_of_input ) {
392 ITT_NOTIFY( sync_acquired, &my_pipeline.input_tokens );
396 process_another_stage:
400 recycle_as_continuation();
412 recycle_as_continuation();
414 return new( allocate_child() )
stage_task( my_pipeline );
416 if( do_segment_scanning ) {
420 filter* first_suitable_filter = current_filter;
421 while( current_filter ) {
430 recycle_as_continuation();
431 return new( allocate_child() )
stage_task( my_pipeline, current_filter, info);
434 if( !current_filter ) {
436 recycle_as_continuation();
439 current_filter = first_suitable_filter;
445 first_suitable_filter = first_suitable_filter->
next_segment;
446 current_filter = first_suitable_filter;
452 recycle_as_continuation();
470 if( subfilter->prev_filter_in_pipeline->is_bound() && !subfilter->is_bound() ) {
471 do_segment_scanning =
true;
473 head_of_previous_segment = subfilter;
480 #if _MSC_VER && !defined(__INTEL_COMPILER) 483 #pragma warning (disable: 4127) 491 my_pipeline(_pipeline)
494 #if __TBB_TASK_GROUP_CONTEXT 508 #if __TBB_TASK_GROUP_CONTEXT 512 if( internal::input_buffer* b = f->my_input_buffer )
523 has_thread_bound_filters(false)
536 if( internal::input_buffer* b = f->my_input_buffer ) {
544 f->my_pipeline = NULL;
547 f->next_segment = NULL;
638 __TBB_ASSERT( max_number_of_live_tokens>0,
"pipeline::run must have at least one token" );
650 #if __TBB_TASK_GROUP_CONTEXT 661 f->my_input_buffer->sema_V();
668 #if __TBB_TASK_GROUP_CONTEXT 677 run(max_number_of_live_tokens, context);
680 #endif // __TBB_TASK_GROUP_CONTEXT 684 __TBB_ASSERT(my_input_buffer,
"has_more_work() called for filter with no input buffer");
691 my_pipeline->remove_filter(*
this);
704 my_pipeline->end_of_input =
true;
707 __TBB_ASSERT(my_input_buffer->end_of_input_tls_allocated, NULL);
708 my_input_buffer->set_my_tls_end_of_input();
713 return internal_process_item(
true);
717 return internal_process_item(
false);
721 __TBB_ASSERT(my_pipeline != NULL,
"It's not supposed that process_item is called for a filter that is not in a pipeline.");
722 internal::task_info info;
725 if( my_pipeline->end_of_input && !has_more_work() )
726 return end_of_stream;
728 if( !prev_filter_in_pipeline ) {
729 if( my_pipeline->end_of_input )
730 return end_of_stream;
731 while( my_pipeline->input_tokens == 0 ) {
733 return item_not_available;
734 my_input_buffer->sema_P();
736 info.my_object = (*this)(info.my_object);
737 if( info.my_object ) {
738 __TBB_ASSERT(my_pipeline->input_tokens > 0,
"Token failed in thread-bound filter");
739 my_pipeline->input_tokens--;
741 info.my_token = my_pipeline->token_counter;
742 info.my_token_ready =
true;
744 my_pipeline->token_counter++;
746 my_pipeline->end_of_input =
true;
747 return end_of_stream;
750 while( !my_input_buffer->has_item() ) {
752 return item_not_available;
754 my_input_buffer->sema_P();
755 if( my_pipeline->end_of_input && !has_more_work() ) {
756 return end_of_stream;
759 if( !my_input_buffer->return_item(info,
true) ) {
762 info.my_object = (*this)(info.my_object);
764 if( next_filter_in_pipeline ) {
765 if ( !next_filter_in_pipeline->my_input_buffer->put_token(info,
true) ) {
766 __TBB_ASSERT(
false,
"Couldn't put token after thread-bound buffer");
769 size_t ntokens_avail = ++(my_pipeline->input_tokens);
770 if( my_pipeline->filter_list->is_bound() ) {
771 if( ntokens_avail == 1 ) {
772 my_pipeline->filter_list->my_input_buffer->sema_V();
task * execute() __TBB_override
The virtual task execution method.
pipeline_root_task(pipeline &pipeline)
bool is_valid
True if my_object is valid.
void __TBB_EXPORTED_METHOD inject_token(task &self)
Not used, but retained to satisfy old export files.
pipeline * my_pipeline
Pointer to the pipeline.
static const unsigned char exact_exception_propagation
7th bit defines exception propagation mode expected by the application.
result_type __TBB_EXPORTED_METHOD process_item()
Wait until a data item becomes available, and invoke operator() on that item.
void reset()
Roughly equivalent to the constructor of input stage task.
pipeline_cleaner(pipeline &_pipeline)
A processing pipeline that applies filters to items.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
filter * filter_list
Pointer to first filter in the pipeline.
virtual __TBB_EXPORTED_METHOD ~filter()
Destroy filter.
end_of_input_tls_t end_of_input_tls
auto first(Container &c) -> decltype(begin(c))
atomic< internal::Token > input_tokens
Number of idle tokens waiting for input stage.
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 void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t new_size
bool my_at_start
True if this task has not yet read the input.
Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5.
filter * next_segment
Pointer to the next "segment" of filters, or NULL if not required.
const unsigned char my_filter_mode
Storage for filter mode and dynamically checked implementation version.
bool has_item()
true if the current low_token is valid.
bool end_of_input_tls_allocated
task_info * array
Array of deferred tasks that cannot yet start executing.
Represents acquisition of a mutex.
void grow(size_type minimum_size)
Resize "array".
void note_done(Token token, StageTask &spawner)
Note that processing of a token is finished.
bool is_bound() const
True if filter is thread-bound.
Edsger Dijkstra's counting semaphore.
friend class internal::pipeline_cleaner
bool is_cancelled() const
Returns true if the context has received cancellation request.
bool is_bound
True for thread-bound filter, false otherwise.
A buffer of input items for a filter.
__TBB_EXPORTED_METHOD pipeline()
Construct empty pipeline.
Token low_token
Lowest token that can start executing.
void __TBB_EXPORTED_METHOD add_filter(filter &filter_)
Add filter to end of pipeline.
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 * lock
internal::input_buffer * my_input_buffer
Buffer for incoming tokens, or NULL if not required.
bool put_token(task_info &info_, bool force_put=false)
Put a token into the buffer.
bool has_thread_bound_filters
True if the pipeline contains a thread-bound filter; false otherwise.
stage_task(pipeline &pipeline, filter *filter_, const task_info &info)
Construct stage_task for a subsequent stage in a pipeline.
basic_tls< intptr_t > end_of_input_tls_t
for parallel filters that accepts NULLs, thread-local flag for reaching end_of_input ...
Token my_token
Invalid unless a task went through an ordered stage.
result_type __TBB_EXPORTED_METHOD try_process_item()
If a data item is available, invoke operator() on that item.
Token high_token
Used for out of order buffer, and for assigning my_token if is_ordered and my_token not already assig...
void create_sema(size_t initial_tokens)
filter * filter_end
Pointer to location where address of next filter to be added should be stored.
bool return_item(task_info &info, bool advance)
return an item, invalidate the queued item, but only advance if advance
void remove_filter(filter &filter_)
Remove filter from pipeline.
bool my_tls_end_of_input()
void clear_filters()
Does clean up if pipeline is cancelled or exception occurred.
#define __TBB_PIPELINE_VERSION(x)
filter * prev_filter_in_pipeline
Pointer to previous filter in the pipeline.
task * end_counter
task who's reference count is used to determine when all stages are done.
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 void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id parent
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 sync_releasing
bool is_ordered
True for ordered filter, false otherwise.
void reset()
Set to initial state (no object, no token)
bool my_token_ready
False until my_token is set.
void poison_pointer(T *__TBB_atomic &)
semaphore * my_sem
for thread-bound filter, semaphore for waiting, NULL otherwise.
Used to form groups of tasks.
size_type array_size
Size of array.
static filter * not_in_pipeline()
Value used to mark "not in pipeline".
A stage in a pipeline served by a user thread.
virtual __TBB_EXPORTED_METHOD ~pipeline()
#define ITT_NOTIFY(name, obj)
static internal::allocate_root_proxy allocate_root()
Returns proxy for overloaded new that allocates a root task.
~input_buffer()
Destroy the buffer.
filter * next_filter_in_pipeline
Pointer to next filter in the pipeline.
void __TBB_EXPORTED_METHOD run(size_t max_number_of_live_tokens)
Run the pipeline to completion.
static void spawn_root_and_wait(task &root)
Spawn task allocated by allocate_root, wait for it to complete, and deallocate it.
spin_mutex array_mutex
Serializes updates.
atomic< internal::Token > token_counter
Global counter of tokens.
Base class for user-defined tasks.
virtual void finalize(void *)
Destroys item if pipeline was cancelled.
input_buffer(bool is_ordered_, bool is_bound_)
Construct empty buffer.
void __TBB_EXPORTED_METHOD set_end_of_input()
void __TBB_EXPORTED_METHOD clear()
Remove all filters from the pipeline.
static const unsigned char version_mask
bool is_ordered() const
True if filter must receive stream in order.
friend class internal::pipeline_root_task
bool end_of_input
False until fetch_input returns NULL.
result_type internal_process_item(bool is_blocking)
Internal routine for item processing.
task * execute() __TBB_override
Should be overridden by derived classes.
void __TBB_EXPORTED_FUNC handle_perror(int error_code, const char *aux_info)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info...
A lock that occupies a single byte.
Base class for types that should not be copied or assigned.
void set_my_tls_end_of_input()
bool has_more_work()
has the filter not yet processed all the tokens it will ever see?
bool object_may_be_null()
true if an input filter can emit null
stage_task(pipeline &pipeline)
Construct stage_task for first stage in a pipeline.
#define __TBB_TASK_GROUP_CONTEXT
bool is_serial() const
True if filter is serial.
This structure is used to store task information in a input buffer.