Home ⌂Doc Index ◂Up ▴
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
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
tbb::aligned_space< binary_semaphore > sema
void add(node_t *n)
add to the back of the list
Circular doubly-linked list with sentinel.
void notify_one_relaxed()
Notify one thread about the event. Relaxed version.
void prepare_wait(thread_context &thr, uintptr_t ctx=0)
prepare wait by inserting 'thr' into the wait queue
void __TBB_store_relaxed(volatile T &location, V value)
Definition: tbb_machine.h:739
void flush_to(circular_doubly_linked_list_with_sentinel &lst)
move all elements to 'lst' and initialize the 'this' list
void abort_all_relaxed()
Abort any sleeping threads at the time of the call; Relaxed version.
thread_context * to_thread_context(waitset_node_t *n)
void notify_all_relaxed()
Notify all waiting threads of the event; Relaxed version.
Represents acquisition of a mutex.
Definition: spin_mutex.h:53
void cancel_wait(thread_context &thr)
Cancel the wait. Removes the thread from the wait queue if not removed yet.
void atomic_fence()
Sequentially consistent full memory fence.
Definition: tbb_machine.h:339
void abort_all()
Abort any sleeping threads at the time of the call.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
T __TBB_load_relaxed(const volatile T &location)
Definition: tbb_machine.h:735
The graph class.

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.