Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
concurrent_monitor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2020 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #include "concurrent_monitor.h"
18 
19 namespace tbb {
20 namespace internal {
21 
22 void concurrent_monitor::thread_context::init() {
23  new (sema.begin()) binary_semaphore;
24  ready = true;
25 }
26 
28  abort_all();
29  __TBB_ASSERT( waitset_ec.empty(), "waitset not empty?" );
30 }
31 
32 void concurrent_monitor::prepare_wait( thread_context& thr, uintptr_t ctx ) {
33  if( !thr.ready )
34  thr.init();
35  // this is good place to pump previous skipped wakeup
36  else if( thr.skipped_wakeup ) {
37  thr.skipped_wakeup = false;
38  thr.semaphore().P();
39  }
40  thr.context = ctx;
41  thr.in_waitset = true;
42  {
46  }
47  atomic_fence();
48 }
49 
51  // possible skipped wakeup will be pumped in the following prepare_wait()
52  thr.skipped_wakeup = true;
53  // try to remove node from waitset
54  bool th_in_waitset = thr.in_waitset;
55  if( th_in_waitset ) {
57  if (thr.in_waitset) {
59  // node is removed from waitset, so there will be no wakeup
60  thr.in_waitset = false;
61  thr.skipped_wakeup = false;
62  }
63  }
64 }
65 
67  if( waitset_ec.empty() )
68  return;
69  waitset_node_t* n;
70  const waitset_node_t* end = waitset_ec.end();
71  {
74  n = waitset_ec.front();
75  if( n!=end ) {
76  waitset_ec.remove( *n );
77  to_thread_context(n)->in_waitset = false;
78  }
79  }
80  if( n!=end )
82 }
83 
85  if( waitset_ec.empty() )
86  return;
87  waitset_t temp;
88  const waitset_node_t* end;
89  {
92  waitset_ec.flush_to( temp );
93  end = temp.end();
94  for( waitset_node_t* n=temp.front(); n!=end; n=n->next )
95  to_thread_context(n)->in_waitset = false;
96  }
97  waitset_node_t* nxt;
98  for( waitset_node_t* n=temp.front(); n!=end; n=nxt ) {
99  nxt = n->next;
100  to_thread_context(n)->semaphore().V();
101  }
102 #if TBB_USE_ASSERT
103  temp.clear();
104 #endif
105 }
106 
108  if( waitset_ec.empty() )
109  return;
110  waitset_t temp;
111  const waitset_node_t* end;
112  {
115  waitset_ec.flush_to( temp );
116  end = temp.end();
117  for( waitset_node_t* n=temp.front(); n!=end; n=n->next )
118  to_thread_context(n)->in_waitset = false;
119  }
120  waitset_node_t* nxt;
121  for( waitset_node_t* n=temp.front(); n!=end; n=nxt ) {
122  nxt = n->next;
123  to_thread_context(n)->aborted = true;
124  to_thread_context(n)->semaphore().V();
125  }
126 #if TBB_USE_ASSERT
127  temp.clear();
128 #endif
129 }
130 
131 } // namespace internal
132 } // namespace tbb
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::circular_doubly_linked_list_with_sentinel
Circular doubly-linked list with sentinel.
Definition: concurrent_monitor.h:33
internal
Definition: _flow_graph_async_msg_impl.h:24
concurrent_monitor.h
tbb::internal::concurrent_monitor::thread_context::sema
tbb::aligned_space< binary_semaphore > sema
Definition: concurrent_monitor.h:115
tbb::internal::concurrent_monitor::~concurrent_monitor
~concurrent_monitor()
dtor
Definition: concurrent_monitor.cpp:27
tbb::internal::concurrent_monitor::thread_context::epoch
__TBB_atomic unsigned epoch
Definition: concurrent_monitor.h:116
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_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
tbb::internal::concurrent_monitor::thread_context::aborted
bool aborted
Definition: concurrent_monitor.h:119
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::to_thread_context
thread_context * to_thread_context(waitset_node_t *n)
Definition: concurrent_monitor.h:184
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
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::node_t::next
node_t * next
Definition: concurrent_monitor.h:36
tbb::internal::concurrent_monitor::waitset_ec
waitset_t waitset_ec
Definition: concurrent_monitor.h:182
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::thread_context::semaphore
binary_semaphore & semaphore()
Definition: concurrent_monitor.h:109
tbb::internal::concurrent_monitor::epoch
__TBB_atomic unsigned epoch
Definition: concurrent_monitor.h:183
tbb::internal::__TBB_store_relaxed
void __TBB_store_relaxed(volatile T &location, V value)
Definition: tbb_machine.h:742
tbb::internal::circular_doubly_linked_list_with_sentinel::remove
void remove(node_t &n)
remove node 'n'
Definition: concurrent_monitor.h:63
tbb::internal::concurrent_monitor::thread_context::skipped_wakeup
bool skipped_wakeup
Definition: concurrent_monitor.h:118
tbb::internal::concurrent_monitor::thread_context::ready
bool ready
Definition: concurrent_monitor.h:120
tbb::internal::concurrent_monitor::thread_context::context
uintptr_t context
Definition: concurrent_monitor.h:121
tbb::internal::circular_doubly_linked_list_with_sentinel::empty
bool empty() const
Definition: concurrent_monitor.h:47
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::thread_context
Definition: concurrent_monitor.h:96
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::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::concurrent_monitor::mutex_ec
tbb::spin_mutex mutex_ec
Definition: concurrent_monitor.h:181
tbb::atomic_fence
void atomic_fence()
Sequentially consistent full memory fence.
Definition: tbb_machine.h:342
tbb::internal::binary_semaphore::P
void P()
wait/acquire
Definition: semaphore.h:235
tbb::spin_mutex::scoped_lock
Represents acquisition of a mutex.
Definition: spin_mutex.h:53
tbb::internal::circular_doubly_linked_list_with_sentinel::node_t
Definition: concurrent_monitor.h:35
tbb::internal::concurrent_monitor::thread_context::in_waitset
tbb::atomic< bool > in_waitset
Definition: concurrent_monitor.h:117
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::concurrent_monitor::notify_one_relaxed
void notify_one_relaxed()
Notify one thread about the event. Relaxed version.
Definition: concurrent_monitor.cpp:66
tbb::internal::binary_semaphore::V
void V()
post/release
Definition: semaphore.h:240
tbb::internal::__TBB_load_relaxed
T __TBB_load_relaxed(const volatile T &location)
Definition: tbb_machine.h:738
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

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.