task_scheduler_observer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_task_scheduler_observer_H
  14. #define __TBB_task_scheduler_observer_H
  15. #define __TBB_task_scheduler_observer_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include "atomic.h"
  18. #if __TBB_ARENA_OBSERVER
  19. #include "task_arena.h"
  20. #endif
  21. #if __TBB_SCHEDULER_OBSERVER
  22. namespace tbb {
  23. namespace interface6 {
  24. class task_scheduler_observer;
  25. }
  26. namespace internal {
  27. class observer_proxy;
  28. class observer_list;
  29. class task_scheduler_observer_v3 {
  30. friend class observer_proxy;
  31. friend class observer_list;
  32. friend class interface6::task_scheduler_observer;
  33. //! Pointer to the proxy holding this observer.
  34. /** Observers are proxied by the scheduler to maintain persistent lists of them. **/
  35. observer_proxy* my_proxy;
  36. //! Counter preventing the observer from being destroyed while in use by the scheduler.
  37. /** Valid only when observation is on. **/
  38. atomic<intptr_t> my_busy_count;
  39. public:
  40. //! Enable or disable observation
  41. /** For local observers the method can be used only when the current thread
  42. has the task scheduler initialized or is attached to an arena.
  43. Repeated calls with the same state are no-ops. **/
  44. void __TBB_EXPORTED_METHOD observe( bool state=true );
  45. //! Returns true if observation is enabled, false otherwise.
  46. bool is_observing() const {return my_proxy!=NULL;}
  47. //! Construct observer with observation disabled.
  48. task_scheduler_observer_v3() : my_proxy(NULL) { my_busy_count.store<relaxed>(0); }
  49. //! Entry notification
  50. /** Invoked from inside observe(true) call and whenever a worker enters the arena
  51. this observer is associated with. If a thread is already in the arena when
  52. the observer is activated, the entry notification is called before it
  53. executes the first stolen task.
  54. Obsolete semantics. For global observers it is called by a thread before
  55. the first steal since observation became enabled. **/
  56. virtual void on_scheduler_entry( bool /*is_worker*/ ) {}
  57. //! Exit notification
  58. /** Invoked from inside observe(false) call and whenever a worker leaves the
  59. arena this observer is associated with.
  60. Obsolete semantics. For global observers it is called by a thread before
  61. the first steal since observation became enabled. **/
  62. virtual void on_scheduler_exit( bool /*is_worker*/ ) {}
  63. //! Destructor automatically switches observation off if it is enabled.
  64. virtual ~task_scheduler_observer_v3() { if(my_proxy) observe(false);}
  65. };
  66. } // namespace internal
  67. #if __TBB_ARENA_OBSERVER
  68. namespace interface6 {
  69. class task_scheduler_observer : public internal::task_scheduler_observer_v3 {
  70. friend class internal::task_scheduler_observer_v3;
  71. friend class internal::observer_proxy;
  72. friend class internal::observer_list;
  73. /** Negative numbers with the largest absolute value to minimize probability
  74. of coincidence in case of a bug in busy count usage. **/
  75. // TODO: take more high bits for version number
  76. static const intptr_t v6_trait = (intptr_t)((~(uintptr_t)0 >> 1) + 1);
  77. //! contains task_arena pointer or tag indicating local or global semantics of the observer
  78. intptr_t my_context_tag;
  79. enum { global_tag = 0, implicit_tag = 1 };
  80. public:
  81. //! Construct local or global observer in inactive state (observation disabled).
  82. /** For a local observer entry/exit notifications are invoked whenever a worker
  83. thread joins/leaves the arena of the observer's owner thread. If a thread is
  84. already in the arena when the observer is activated, the entry notification is
  85. called before it executes the first stolen task. **/
  86. /** TODO: Obsolete.
  87. Global observer semantics is obsolete as it violates master thread isolation
  88. guarantees and is not composable. Thus the current default behavior of the
  89. constructor is obsolete too and will be changed in one of the future versions
  90. of the library. **/
  91. explicit task_scheduler_observer( bool local = false ) {
  92. #if __TBB_ARENA_OBSERVER
  93. my_context_tag = local? implicit_tag : global_tag;
  94. #else
  95. __TBB_ASSERT_EX( !local, NULL );
  96. my_context_tag = global_tag;
  97. #endif
  98. }
  99. #if __TBB_ARENA_OBSERVER
  100. //! Construct local observer for a given arena in inactive state (observation disabled).
  101. /** entry/exit notifications are invoked whenever a thread joins/leaves arena.
  102. If a thread is already in the arena when the observer is activated, the entry notification
  103. is called before it executes the first stolen task. **/
  104. explicit task_scheduler_observer( task_arena & a) {
  105. my_context_tag = (intptr_t)&a;
  106. }
  107. #endif /* __TBB_ARENA_OBSERVER */
  108. /** Destructor protects instance of the observer from concurrent notification.
  109. It is recommended to disable observation before destructor of a derived class starts,
  110. otherwise it can lead to concurrent notification callback on partly destroyed object **/
  111. virtual ~task_scheduler_observer() { if(my_proxy) observe(false); }
  112. //! Enable or disable observation
  113. /** Warning: concurrent invocations of this method are not safe.
  114. Repeated calls with the same state are no-ops. **/
  115. void observe( bool state=true ) {
  116. if( state && !my_proxy ) {
  117. __TBB_ASSERT( !my_busy_count, "Inconsistent state of task_scheduler_observer instance");
  118. my_busy_count.store<relaxed>(v6_trait);
  119. }
  120. internal::task_scheduler_observer_v3::observe(state);
  121. }
  122. };
  123. } //namespace interface6
  124. using interface6::task_scheduler_observer;
  125. #else /*__TBB_ARENA_OBSERVER*/
  126. typedef tbb::internal::task_scheduler_observer_v3 task_scheduler_observer;
  127. #endif /*__TBB_ARENA_OBSERVER*/
  128. } // namespace tbb
  129. #endif /* __TBB_SCHEDULER_OBSERVER */
  130. #include "internal/_warning_suppress_disable_notice.h"
  131. #undef __TBB_task_scheduler_observer_H_include_area
  132. #endif /* __TBB_task_scheduler_observer_H */