Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
task_scheduler_observer.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_task_scheduler_observer_H
22 #define __TBB_task_scheduler_observer_H
23 
24 #include "atomic.h"
25 #if __TBB_ARENA_OBSERVER || __TBB_SLEEP_PERMISSION
26 #include "task_arena.h"
27 #endif
28 
29 #if __TBB_SCHEDULER_OBSERVER
30 
31 namespace tbb {
32 namespace interface6 {
34 }
35 namespace internal {
36 
37 class observer_proxy;
38 class observer_list;
39 
41  friend class observer_proxy;
42  friend class observer_list;
44 
46 
48 
50 
52 
53 public:
55 
59  void __TBB_EXPORTED_METHOD observe( bool state=true );
60 
62  bool is_observing() const {return my_proxy!=NULL;}
63 
65  task_scheduler_observer_v3() : my_proxy(NULL) { my_busy_count.store<relaxed>(0); }
66 
68 
75  virtual void on_scheduler_entry( bool /*is_worker*/ ) {}
76 
78 
83  virtual void on_scheduler_exit( bool /*is_worker*/ ) {}
84 
86  virtual ~task_scheduler_observer_v3() { if(my_proxy) observe(false);}
87 };
88 
89 } // namespace internal
90 
91 #if __TBB_ARENA_OBSERVER || __TBB_SLEEP_PERMISSION
92 namespace interface6 {
93 class task_scheduler_observer : public internal::task_scheduler_observer_v3 {
94  friend class internal::task_scheduler_observer_v3;
95  friend class internal::observer_proxy;
96  friend class internal::observer_list;
97 
100  // TODO: take more high bits for version number
101  static const intptr_t v6_trait = (intptr_t)((~(uintptr_t)0 >> 1) + 1);
102 
104  intptr_t my_context_tag;
105  enum { global_tag = 0, implicit_tag = 1 };
106 
107 public:
109 
118  explicit task_scheduler_observer( bool local = false ) {
119 #if __TBB_ARENA_OBSERVER
120  my_context_tag = local? implicit_tag : global_tag;
121 #else
122  __TBB_ASSERT_EX( !local, NULL );
123  my_context_tag = global_tag;
124 #endif
125  }
126 
127 #if __TBB_ARENA_OBSERVER
128 
132  explicit task_scheduler_observer( task_arena & a) {
133  my_context_tag = (intptr_t)&a;
134  }
135 #endif /* __TBB_ARENA_OBSERVER */
136 
140  virtual ~task_scheduler_observer() { if(my_proxy) observe(false); }
141 
143 
145  void observe( bool state=true ) {
146  if( state && !my_proxy ) {
147  __TBB_ASSERT( !my_busy_count, "Inconsistent state of task_scheduler_observer instance");
148  my_busy_count.store<relaxed>(v6_trait);
149  }
151  }
152 
153 #if __TBB_SLEEP_PERMISSION
154  enum { keep_awake = false, allow_sleep = true };
156 
158 
160  virtual bool may_sleep() { return allow_sleep; }
161 #endif /*__TBB_SLEEP_PERMISSION*/
162 };
163 
164 } //namespace interface6
166 #else /*__TBB_ARENA_OBSERVER || __TBB_SLEEP_PERMISSION*/
168 #endif /*__TBB_ARENA_OBSERVER || __TBB_SLEEP_PERMISSION*/
169 
170 } // namespace tbb
171 
172 #endif /* __TBB_SCHEDULER_OBSERVER */
173 
174 #endif /* __TBB_task_scheduler_observer_H */
observer_proxy * my_proxy
Pointer to the proxy holding this observer.
No ordering.
Definition: atomic.h:51
tbb::internal::task_scheduler_observer_v3 task_scheduler_observer
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
void __TBB_EXPORTED_METHOD observe(bool state=true)
Enable or disable observation.
#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
virtual void on_scheduler_entry(bool)
Entry notification.
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:102
The graph class.
atomic< intptr_t > my_busy_count
Counter preventing the observer from being destroyed while in use by the scheduler.
void store(value_type value)
Definition: atomic.h:317
virtual ~task_scheduler_observer_v3()
Destructor automatically switches observation off if it is enabled.
bool is_observing() const
Returns true if observation is enabled, false otherwise.
virtual void on_scheduler_exit(bool)
Exit notification.
task_scheduler_observer_v3()
Construct observer with observation disabled.

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.