BRE12
task_scheduler_observer.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
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 {
33 class task_scheduler_observer;
34 }
35 namespace internal {
36 
37 class observer_proxy;
38 class observer_list;
39 
41  friend class observer_proxy;
42  friend class observer_list;
43  friend class interface6::task_scheduler_observer;
44 
46 
47  observer_proxy* my_proxy;
48 
50 
51  atomic<intptr_t> my_busy_count;
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 {
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  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 
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
165 using interface6::task_scheduler_observer;
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 */
virtual void on_scheduler_entry(bool)
Entry notification.
Definition: task_scheduler_observer.h:75
No ordering.
Definition: atomic.h:51
bool is_observing() const
Returns true if observation is enabled, false otherwise.
Definition: task_scheduler_observer.h:62
virtual ~task_scheduler_observer_v3()
Destructor automatically switches observation off if it is enabled.
Definition: task_scheduler_observer.h:86
Definition: _flow_graph_async_msg_impl.h:32
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
1-to-1 proxy representation class of scheduler&#39;s arena Constructors set up settings only...
Definition: task_arena.h:120
void __TBB_EXPORTED_METHOD observe(bool state=true)
Enable or disable observation.
Definition: task_scheduler_observer.h:40
task_scheduler_observer_v3()
Construct observer with observation disabled.
Definition: task_scheduler_observer.h:65
virtual void on_scheduler_exit(bool)
Exit notification.
Definition: task_scheduler_observer.h:83