task_scheduler_observer.h

00001 /*
00002     Copyright 2005-2012 Intel Corporation.  All Rights Reserved.
00003 
00004     The source code contained or described herein and all documents related
00005     to the source code ("Material") are owned by Intel Corporation or its
00006     suppliers or licensors.  Title to the Material remains with Intel
00007     Corporation or its suppliers and licensors.  The Material is protected
00008     by worldwide copyright laws and treaty provisions.  No part of the
00009     Material may be used, copied, reproduced, modified, published, uploaded,
00010     posted, transmitted, distributed, or disclosed in any way without
00011     Intel's prior express written permission.
00012 
00013     No license under any patent, copyright, trade secret or other
00014     intellectual property right is granted to or conferred upon you by
00015     disclosure or delivery of the Materials, either expressly, by
00016     implication, inducement, estoppel or otherwise.  Any license under such
00017     intellectual property rights must be express and approved by Intel in
00018     writing.
00019 */
00020 
00021 #ifndef __TBB_task_scheduler_observer_H
00022 #define __TBB_task_scheduler_observer_H
00023 
00024 #include "atomic.h"
00025 #if __TBB_TASK_ARENA
00026 #include "task_arena.h"
00027 #endif //__TBB_TASK_ARENA
00028 
00029 #if __TBB_SCHEDULER_OBSERVER
00030 
00031 namespace tbb {
00032 namespace interface6 {
00033 class task_scheduler_observer;
00034 }
00035 namespace internal {
00036 
00037 class observer_proxy;
00038 class observer_list;
00039 
00040 class task_scheduler_observer_v3 {
00041     friend class observer_proxy;
00042     friend class observer_list;
00043     friend class interface6::task_scheduler_observer;
00044 
00046 
00047     observer_proxy* my_proxy;
00048 
00050 
00051     atomic<intptr_t> my_busy_count;
00052 
00053 public:
00055 
00059     void __TBB_EXPORTED_METHOD observe( bool state=true );
00060 
00062     bool is_observing() const {return my_proxy!=NULL;}
00063 
00065     task_scheduler_observer_v3() : my_proxy(NULL) { my_busy_count.store<relaxed>(0); }
00066 
00068 
00075     virtual void on_scheduler_entry( bool /*is_worker*/ ) {} 
00076 
00078 
00083     virtual void on_scheduler_exit( bool /*is_worker*/ ) {}
00084 
00086     virtual ~task_scheduler_observer_v3() { if(my_proxy) observe(false);}
00087 };
00088 
00089 } // namespace internal
00090 
00091 #if TBB_PREVIEW_LOCAL_OBSERVER
00092 namespace interface6 {
00093 class task_scheduler_observer : public internal::task_scheduler_observer_v3 {
00094     friend class internal::task_scheduler_observer_v3;
00095     friend class internal::observer_proxy;
00096     friend class internal::observer_list;
00097 
00100     // TODO: take more high bits for version number
00101     static const intptr_t v6_trait = (intptr_t)((~(uintptr_t)0 >> 1) + 1);
00102 
00104     intptr_t my_context_tag;
00105     enum { global_tag = 0, implicit_tag = 1 };
00106 
00107 public:
00109 
00118     task_scheduler_observer( bool local = false ) {
00119         my_busy_count.store<relaxed>(v6_trait);
00120         my_context_tag = local? implicit_tag : global_tag;
00121     }
00122 
00123 #if __TBB_TASK_ARENA
00125 
00128     task_scheduler_observer( task_arena & a) {
00129         my_busy_count.store<relaxed>(v6_trait);
00130         my_context_tag = (intptr_t)&a;
00131     }
00132 #endif //__TBB_TASK_ARENA
00133 
00135 
00138     virtual bool on_scheduler_leaving() { return true; }
00139 
00141     // It is recommended to disable observation before destructor of a derived class starts,
00142     // otherwise it can lead to concurrent notification callback on partly destroyed object
00143     virtual ~task_scheduler_observer() { if(my_proxy) observe(false);}
00144 };
00145 
00146 } //namespace interface6
00147 using interface6::task_scheduler_observer;
00148 #else /*TBB_PREVIEW_LOCAL_OBSERVER*/
00149 typedef tbb::internal::task_scheduler_observer_v3 task_scheduler_observer;
00150 #endif /*TBB_PREVIEW_LOCAL_OBSERVER*/
00151 
00152 } // namespace tbb
00153 
00154 #endif /* __TBB_SCHEDULER_OBSERVER */
00155 
00156 #endif /* __TBB_task_scheduler_observer_H */

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