concurrent_monitor.cpp
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.
*/
#include "concurrent_monitor.h"
namespace tbb {
namespace internal {
void concurrent_monitor::thread_context::init() {
new (sema.begin()) binary_semaphore;
ready = true;
}
concurrent_monitor::~concurrent_monitor() {
abort_all();
__TBB_ASSERT( waitset_ec.empty(), "waitset not empty?" );
}
void concurrent_monitor::prepare_wait( thread_context& thr, uintptr_t ctx ) {
if( !thr.ready )
thr.init();
// this is good place to pump previous skipped wakeup
else if( thr.skipped_wakeup ) {
thr.skipped_wakeup = false;
thr.semaphore().P();
}
thr.context = ctx;
thr.in_waitset = true;
{
tbb::spin_mutex::scoped_lock l( mutex_ec );
__TBB_store_relaxed( thr.epoch, __TBB_load_relaxed(epoch) );
waitset_ec.add( (waitset_t::node_t*)&thr );
}
atomic_fence();
}
void concurrent_monitor::cancel_wait( thread_context& thr ) {
// possible skipped wakeup will be pumped in the following prepare_wait()
thr.skipped_wakeup = true;
// try to remove node from waitset
bool th_in_waitset = thr.in_waitset;
if( th_in_waitset ) {
tbb::spin_mutex::scoped_lock l( mutex_ec );
if (thr.in_waitset) {
waitset_ec.remove( (waitset_t::node_t&)thr );
// node is removed from waitset, so there will be no wakeup
thr.in_waitset = false;
thr.skipped_wakeup = false;
}
}
}
void concurrent_monitor::notify_one_relaxed() {
if( waitset_ec.empty() )
return;
waitset_node_t* n;
const waitset_node_t* end = waitset_ec.end();
{
tbb::spin_mutex::scoped_lock l( mutex_ec );
__TBB_store_relaxed( epoch, __TBB_load_relaxed(epoch) + 1 );
n = waitset_ec.front();
if( n!=end ) {
waitset_ec.remove( *n );
to_thread_context(n)->in_waitset = false;
}
}
if( n!=end )
to_thread_context(n)->semaphore().V();
}
void concurrent_monitor::notify_all_relaxed() {
if( waitset_ec.empty() )
return;
waitset_t temp;
const waitset_node_t* end;
{
tbb::spin_mutex::scoped_lock l( mutex_ec );
__TBB_store_relaxed( epoch, __TBB_load_relaxed(epoch) + 1 );
waitset_ec.flush_to( temp );
end = temp.end();
for( waitset_node_t* n=temp.front(); n!=end; n=n->next )
to_thread_context(n)->in_waitset = false;
}
waitset_node_t* nxt;
for( waitset_node_t* n=temp.front(); n!=end; n=nxt ) {
nxt = n->next;
to_thread_context(n)->semaphore().V();
}
#if TBB_USE_ASSERT
temp.clear();
#endif
}
void concurrent_monitor::abort_all_relaxed() {
if( waitset_ec.empty() )
return;
waitset_t temp;
const waitset_node_t* end;
{
tbb::spin_mutex::scoped_lock l( mutex_ec );
__TBB_store_relaxed( epoch, __TBB_load_relaxed(epoch) + 1 );
waitset_ec.flush_to( temp );
end = temp.end();
for( waitset_node_t* n=temp.front(); n!=end; n=n->next )
to_thread_context(n)->in_waitset = false;
}
waitset_node_t* nxt;
for( waitset_node_t* n=temp.front(); n!=end; n=nxt ) {
nxt = n->next;
to_thread_context(n)->aborted = true;
to_thread_context(n)->semaphore().V();
}
#if TBB_USE_ASSERT
temp.clear();
#endif
}
} // namespace internal
} // namespace tbb
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
concurrent_monitor.h
end
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 __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp end
Definition: ittnotify_static.h:182
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::atomic_fence
void atomic_fence()
Sequentially consistent full memory fence.
Definition: tbb_machine.h:342
tbb::internal::__TBB_store_relaxed
void __TBB_store_relaxed(volatile T &location, V value)
Definition: tbb_machine.h:742
tbb::internal::__TBB_load_relaxed
T __TBB_load_relaxed(const volatile T &location)
Definition: tbb_machine.h:738
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::spin_mutex::scoped_lock
Represents acquisition of a mutex.
Definition: spin_mutex.h:53
tbb::internal::circular_doubly_linked_list_with_sentinel
Circular doubly-linked list with sentinel.
Definition: concurrent_monitor.h:33
tbb::internal::circular_doubly_linked_list_with_sentinel::add
void add(node_t *n)
add to the back of the list
Definition: concurrent_monitor.h:54
tbb::internal::circular_doubly_linked_list_with_sentinel::empty
bool empty() const
Definition: concurrent_monitor.h:47
tbb::internal::circular_doubly_linked_list_with_sentinel::flush_to
void flush_to(circular_doubly_linked_list_with_sentinel &lst)
move all elements to 'lst' and initialize the 'this' list
Definition: concurrent_monitor.h:71
tbb::internal::circular_doubly_linked_list_with_sentinel::remove
void remove(node_t &n)
remove node 'n'
Definition: concurrent_monitor.h:63
tbb::internal::circular_doubly_linked_list_with_sentinel::front
node_t * front() const
Definition: concurrent_monitor.h:48
tbb::internal::circular_doubly_linked_list_with_sentinel::clear
void clear()
Definition: concurrent_monitor.h:82
tbb::internal::circular_doubly_linked_list_with_sentinel::end
const node_t * end() const
Definition: concurrent_monitor.h:51
tbb::internal::circular_doubly_linked_list_with_sentinel::node_t
Definition: concurrent_monitor.h:35
tbb::internal::circular_doubly_linked_list_with_sentinel::node_t::next
node_t * next
Definition: concurrent_monitor.h:36
tbb::internal::concurrent_monitor::notify_one_relaxed
void notify_one_relaxed()
Notify one thread about the event. Relaxed version.
Definition: concurrent_monitor.cpp:66
tbb::internal::concurrent_monitor::notify_all_relaxed
void notify_all_relaxed()
Notify all waiting threads of the event; Relaxed version.
Definition: concurrent_monitor.cpp:84
tbb::internal::concurrent_monitor::mutex_ec
tbb::spin_mutex mutex_ec
Definition: concurrent_monitor.h:181
tbb::internal::concurrent_monitor::prepare_wait
void prepare_wait(thread_context &thr, uintptr_t ctx=0)
prepare wait by inserting 'thr' into the wait queue
Definition: concurrent_monitor.cpp:32
tbb::internal::concurrent_monitor::cancel_wait
void cancel_wait(thread_context &thr)
Cancel the wait. Removes the thread from the wait queue if not removed yet.
Definition: concurrent_monitor.cpp:50
tbb::internal::concurrent_monitor::to_thread_context
thread_context * to_thread_context(waitset_node_t *n)
Definition: concurrent_monitor.h:184
tbb::internal::concurrent_monitor::abort_all
void abort_all()
Abort any sleeping threads at the time of the call.
Definition: concurrent_monitor.h:175
tbb::internal::concurrent_monitor::abort_all_relaxed
void abort_all_relaxed()
Abort any sleeping threads at the time of the call; Relaxed version.
Definition: concurrent_monitor.cpp:107
tbb::internal::concurrent_monitor::waitset_ec
waitset_t waitset_ec
Definition: concurrent_monitor.h:182
tbb::internal::concurrent_monitor::epoch
__TBB_atomic unsigned epoch
Definition: concurrent_monitor.h:183
tbb::internal::concurrent_monitor::~concurrent_monitor
~concurrent_monitor()
dtor
Definition: concurrent_monitor.cpp:27
tbb::internal::concurrent_monitor::thread_context
Definition: concurrent_monitor.h:96
tbb::internal::concurrent_monitor::thread_context::in_waitset
tbb::atomic< bool > in_waitset
Definition: concurrent_monitor.h:117
tbb::internal::concurrent_monitor::thread_context::context
uintptr_t context
Definition: concurrent_monitor.h:121
tbb::internal::concurrent_monitor::thread_context::epoch
__TBB_atomic unsigned epoch
Definition: concurrent_monitor.h:116
tbb::internal::concurrent_monitor::thread_context::aborted
bool aborted
Definition: concurrent_monitor.h:119
tbb::internal::concurrent_monitor::thread_context::ready
bool ready
Definition: concurrent_monitor.h:120
tbb::internal::concurrent_monitor::thread_context::skipped_wakeup
bool skipped_wakeup
Definition: concurrent_monitor.h:118
tbb::internal::concurrent_monitor::thread_context::sema
tbb::aligned_space< binary_semaphore > sema
Definition: concurrent_monitor.h:115
tbb::internal::concurrent_monitor::thread_context::semaphore
binary_semaphore & semaphore()
Definition: concurrent_monitor.h:109
tbb::internal::binary_semaphore::V
void V()
post/release
Definition: semaphore.h:240
tbb::internal::binary_semaphore::P
void P()
wait/acquire
Definition: semaphore.h:235

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.