Monitor_Base.cpp

Go to the documentation of this file.
00001 // $Id: Monitor_Base.cpp 81691 2008-05-14 11:09:21Z johnnyw $
00002 
00003 #include "ace/Monitor_Base.h"
00004 
00005 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
00006 
00007 #include "ace/Monitor_Admin_Manager.h"
00008 #include "ace/Monitor_Control_Action.h"
00009 #include "ace/Monitor_Point_Registry.h"
00010 #include "ace/Guard_T.h"
00011 #include "ace/Dynamic_Service.h"
00012 #include "ace/OS_NS_sys_time.h"
00013 
00014 #if !defined (__ACE_INLINE__)
00015 #include "ace/Monitor_Base.inl"
00016 #endif /* __ACE_INLINE__ */
00017 
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019 
00020 namespace ACE
00021 {
00022   namespace Monitor_Control
00023   {
00024     Monitor_Base::Monitor_Base (void)
00025       : ACE_Refcountable_T<ACE_SYNCH_MUTEX> (1)
00026     {
00027     }
00028 
00029     Monitor_Base::Monitor_Base (const char* name)
00030       : ACE_Refcountable_T<ACE_SYNCH_MUTEX> (1)
00031       , name_ (name)
00032     {
00033     }
00034 
00035     Monitor_Base::~Monitor_Base (void)
00036     {
00037     }
00038 
00039     long
00040     Monitor_Base::add_constraint (const char* expression,
00041                                   Control_Action* action)
00042     {
00043       /// Thread-safe and guaranteed to be unique.
00044       long id = Monitor_Point_Registry::instance ()->constraint_id ();
00045 
00046       CONSTRAINTS::value_type entry;
00047       entry.first = id;
00048       entry.second.expr = expression;
00049       entry.second.control_action = action;
00050 
00051       /// This is thread-safe on its own so we don't have
00052       /// to guard it here.
00053       action->add_ref ();
00054 
00055       {
00056         ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, -1);
00057 
00058         /// Since we know external key is unique,
00059         /// we don't check for failure.
00060         (void) this->constraints_.insert (entry);
00061       }
00062 
00063       return id;
00064     }
00065 
00066     Control_Action*
00067     Monitor_Base::remove_constraint (const long constraint_id)
00068     {
00069       Control_Action* retval = 0;
00070 
00071       {
00072         ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0);
00073 
00074         CONSTRAINT_ITERATOR i = this->constraints_.find (constraint_id);
00075 
00076         if (i != this->constraints_.end ())
00077           {
00078             retval = i->second.control_action;
00079             (void) this->constraints_.erase (constraint_id);
00080           }
00081       }
00082 
00083       return retval;
00084     }
00085 
00086     Monitor_Base::CONSTRAINTS&
00087     Monitor_Base::constraints (void)
00088     {
00089       return this->constraints_;
00090     }
00091 
00092     void
00093     Monitor_Base::retrieve (Monitor_Control_Types::Data& data) const
00094     {
00095       ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00096       data = this->data_;
00097     }
00098 
00099     void
00100     Monitor_Base::receive (size_t value)
00101     {
00102       ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00103 
00104       this->data_.timestamp_ = ACE_OS::gettimeofday ();
00105       this->data_.value_ = static_cast<double> (value);
00106     }
00107 
00108     void
00109     Monitor_Base::receive (double data)
00110     {
00111       ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00112       this->data_.timestamp_ = ACE_OS::gettimeofday ();
00113       this->data_.value_ = data;
00114     }
00115 
00116     void
00117     Monitor_Base::clear (void)
00118     {
00119       ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00120 
00121       this->clear_i ();
00122     }
00123 
00124     void
00125     Monitor_Base::clear_i (void)
00126     {
00127       this->data_.value_ = 0.0;
00128       this->data_.timestamp_ = ACE_Time_Value::zero;
00129     }
00130 
00131 
00132     void
00133     Monitor_Base::retrieve_and_clear (Monitor_Control_Types::Data& data)
00134     {
00135       ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00136 
00137       data = this->data_;
00138       this->clear_i ();
00139     }
00140 
00141     void
00142     Monitor_Base::add_to_registry (const ACE_Time_Value& time)
00143     {
00144       MC_ADMINMANAGER *mgr =
00145         ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");
00146 
00147       if (!mgr->admin ().monitor_point (this, time))
00148         {
00149           ACE_ERROR ((LM_ERROR,
00150                       "monitor point %s registration failed\n",
00151                       this->name ()));
00152         }
00153     }
00154 
00155     void
00156     Monitor_Base::remove_from_registry (void)
00157     {
00158       if (!Monitor_Point_Registry::instance ()->remove (this->name ()))
00159         {
00160           ACE_ERROR ((LM_ERROR,
00161                       "monitor point %s unregistration failed\n",
00162                       this->name ()));
00163         }
00164     }
00165 
00166     void
00167     Monitor_Base::add_ref (void)
00168     {
00169       (void) this->increment ();
00170     }
00171 
00172     void
00173     Monitor_Base::remove_ref (void)
00174     {
00175       long const new_count = this->decrement ();
00176 
00177       if (new_count == 0)
00178         {
00179           delete this;
00180         }
00181     }
00182   }
00183 }
00184 
00185 ACE_END_VERSIONED_NAMESPACE_DECL
00186 
00187 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */
00188 

Generated on Tue Feb 2 17:18:41 2010 for ACE by  doxygen 1.4.7