Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
observer_proxy.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 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 
18 
19 */
20 
21 #ifndef _TBB_observer_proxy_H
22 #define _TBB_observer_proxy_H
23 
24 #if __TBB_SCHEDULER_OBSERVER
25 
26 #include "scheduler_common.h" // to include task.h
28 #include "tbb/spin_rw_mutex.h"
29 #include "tbb/aligned_space.h"
30 
31 namespace tbb {
32 namespace internal {
33 
34 class observer_list {
35  friend class arena;
36 
37  // Mutex is wrapped with aligned_space to shut up warnings when its destructor
38  // is called while threads are still using it.
39  typedef aligned_space<spin_rw_mutex> my_mutex_type;
40 
42  observer_proxy* my_head;
43 
45  observer_proxy* my_tail;
46 
48  my_mutex_type my_mutex;
49 
51  arena* my_arena;
52 
54 
55  inline static void remove_ref_fast( observer_proxy*& p );
56 
58  void do_notify_entry_observers( observer_proxy*& last, bool worker );
59 
61  void do_notify_exit_observers( observer_proxy* last, bool worker );
62 
63 public:
64  observer_list () : my_head(NULL), my_tail(NULL) {}
65 
67 
68  void clear ();
69 
71  void insert ( observer_proxy* p );
72 
74  void remove ( observer_proxy* p );
75 
77 
78  void remove_ref( observer_proxy* p );
79 
81  typedef spin_rw_mutex::scoped_lock scoped_lock;
82 
84  spin_rw_mutex& mutex () { return my_mutex.begin()[0]; }
85 
86  bool empty () const { return my_head == NULL; }
87 
89 
91  inline void notify_entry_observers( observer_proxy*& last, bool worker );
92 
94  inline void notify_exit_observers( observer_proxy*& last, bool worker );
95 }; // class observer_list
96 
98 
101 class observer_proxy {
102  friend class task_scheduler_observer_v3;
103  friend class observer_list;
105 
108  atomic<int> my_ref_count;
110  observer_list* my_list;
112 
113  observer_proxy* my_next;
115 
116  observer_proxy* my_prev;
118  task_scheduler_observer_v3* my_observer;
120  char my_version;
121 
122 #if __TBB_ARENA_OBSERVER
123  interface6::task_scheduler_observer* get_v6_observer();
124 #endif
125 #if __TBB_ARENA_OBSERVER
126  bool is_global(); //TODO: move them back inline when un-CPF'ing
127 #endif
128 
130  observer_proxy( task_scheduler_observer_v3& );
131 
132 #if TBB_USE_ASSERT
133  ~observer_proxy();
134 #endif /* TBB_USE_ASSERT */
135 
137  observer_proxy& operator = ( const observer_proxy& );
138 }; // class observer_proxy
139 
140 inline void observer_list::remove_ref_fast( observer_proxy*& p ) {
141  if( p->my_observer ) {
142  // Can decrement refcount quickly, as it cannot drop to zero while under the lock.
143  int r = --p->my_ref_count;
144  __TBB_ASSERT_EX( r, NULL );
145  p = NULL;
146  } else {
147  // Use slow form of refcount decrementing, after the lock is released.
148  }
149 }
150 
151 inline void observer_list::notify_entry_observers( observer_proxy*& last, bool worker ) {
152  if ( last == my_tail )
153  return;
154  do_notify_entry_observers( last, worker );
155 }
156 
157 inline void observer_list::notify_exit_observers( observer_proxy*& last, bool worker ) {
158  if ( !last )
159  return;
160  __TBB_ASSERT(is_alive((uintptr_t)last), NULL);
161  do_notify_exit_observers( last, worker );
162  __TBB_ASSERT(last, NULL);
164 }
165 
166 extern padded<observer_list> the_global_observer_list;
167 
168 } // namespace internal
169 } // namespace tbb
170 
171 #endif /* __TBB_SCHEDULER_OBSERVER */
172 
173 #endif /* _TBB_observer_proxy_H */
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
#define __TBB_ASSERT_EX(predicate, comment)
"Extended" version is useful to suppress warnings if a variable is only used with an assert
Definition: tbb_stddef.h:171
void const char const char int ITT_FORMAT __itt_group_sync p
#define poison_value(g)
auto last(Container &c) -> decltype(begin(c))
The graph class.
spin_rw_mutex_v3 spin_rw_mutex
Definition: spin_rw_mutex.h:37

Copyright © 2005-2019 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.