00001
00002
00003 #ifndef TAO_ESF_DELAYED_CHANGES_CPP
00004 #define TAO_ESF_DELAYED_CHANGES_CPP
00005
00006 #include "orbsvcs/ESF/ESF_Delayed_Changes.h"
00007
00008 #if ! defined (__ACE_INLINE__)
00009 #include "orbsvcs/ESF/ESF_Delayed_Changes.inl"
00010 #endif
00011
00012 #include "orbsvcs/ESF/ESF_Defaults.h"
00013 #include "orbsvcs/ESF/ESF_Worker.h"
00014 #include "orbsvcs/ESF/ESF_Delayed_Command.h"
00015 #include "tao/SystemException.h"
00016
00017 #include "ace/Functor.h"
00018 #include "ace/Guard_T.h"
00019
00020 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00021
00022
00023
00024 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00025 TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00026 TAO_ESF_Delayed_Changes (void)
00027 : lock_ (this),
00028 busy_cond_ (busy_lock_),
00029 busy_count_ (0),
00030 write_delay_count_ (0),
00031 busy_hwm_ (TAO_ESF_DEFAULT_BUSY_HWM),
00032 max_write_delay_ (TAO_ESF_DEFAULT_MAX_WRITE_DELAY)
00033 {
00034 }
00035
00036 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00037 TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00038 TAO_ESF_Delayed_Changes (const COLLECTION &collection)
00039 : collection_ (collection),
00040 lock_ (this),
00041 busy_cond_ (busy_lock_),
00042 busy_count_ (0),
00043 write_delay_count_ (0),
00044 busy_hwm_ (TAO_ESF_DEFAULT_BUSY_HWM),
00045 max_write_delay_ (TAO_ESF_DEFAULT_MAX_WRITE_DELAY)
00046 {
00047 }
00048
00049 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00050 TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00051 for_each (TAO_ESF_Worker<PROXY> *worker)
00052 {
00053 ACE_GUARD (Busy_Lock, ace_mon, this->lock_);
00054
00055 worker->set_size(this->collection_.size());
00056 ITERATOR end = this->collection_.end ();
00057 for (ITERATOR i = this->collection_.begin (); i != end; ++i)
00058 {
00059 worker->work (*i);
00060
00061 }
00062 }
00063
00064 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> int
00065 TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00066 busy (void)
00067 {
00068 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX_T, ace_mon, this->busy_lock_, -1);
00069
00070 while (this->busy_count_ >= this->busy_hwm_
00071 || this->write_delay_count_ >= this->max_write_delay_)
00072 this->busy_cond_.wait ();
00073 ++this->busy_count_;
00074
00075 return 0;
00076 }
00077
00078 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> int
00079 TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00080 idle (void)
00081 {
00082 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX_T, ace_mon, this->busy_lock_, -1);
00083
00084 --this->busy_count_;
00085 if (this->busy_count_ == 0)
00086 {
00087 this->write_delay_count_ = 0;
00088 this->execute_delayed_operations ();
00089 this->busy_cond_.broadcast ();
00090 }
00091 return 0;
00092 }
00093
00094 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> int
00095 TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00096 execute_delayed_operations (void)
00097 {
00098 while (!this->command_queue_.is_empty ())
00099 {
00100 ACE_Command_Base* command = 0;
00101 this->command_queue_.dequeue_head (command);
00102
00103 command->execute ();
00104
00105 delete command;
00106 }
00107 return 0;
00108 }
00109
00110 template<class PROXY, class C, class I,ACE_SYNCH_DECL> void
00111 TAO_ESF_Delayed_Changes<PROXY,C,I,ACE_SYNCH_USE>::
00112 connected (PROXY *proxy)
00113 {
00114 ACE_GUARD_THROW_EX (ACE_SYNCH_MUTEX_T, ace_mon, this->busy_lock_,
00115 CORBA::INTERNAL ());
00116
00117 proxy->_incr_refcnt ();
00118 if (this->busy_count_ == 0)
00119 {
00120
00121 this->connected_i (proxy);
00122 }
00123 else
00124 {
00125 ACE_Command_Base* command = 0;
00126 ACE_NEW (command,
00127 Connected_Command (this,
00128 proxy));
00129 this->command_queue_.enqueue_tail (command);
00130 this->write_delay_count_++;
00131 }
00132 }
00133
00134 template<class PROXY, class C, class I,ACE_SYNCH_DECL> void
00135 TAO_ESF_Delayed_Changes<PROXY,C,I,ACE_SYNCH_USE>::
00136 reconnected (PROXY *proxy)
00137 {
00138 ACE_GUARD_THROW_EX (ACE_SYNCH_MUTEX_T, ace_mon, this->busy_lock_,
00139 CORBA::INTERNAL ());
00140
00141 proxy->_incr_refcnt ();
00142 if (this->busy_count_ == 0)
00143 {
00144
00145 this->reconnected_i (proxy);
00146 }
00147 else
00148 {
00149 ACE_Command_Base* command;
00150 ACE_NEW (command,
00151 Reconnected_Command (this,
00152 proxy));
00153 this->command_queue_.enqueue_tail (command);
00154 this->write_delay_count_++;
00155 }
00156 }
00157
00158 template<class PROXY, class C, class I,ACE_SYNCH_DECL> void
00159 TAO_ESF_Delayed_Changes<PROXY,C,I,ACE_SYNCH_USE>::
00160 disconnected (PROXY *proxy)
00161 {
00162 ACE_GUARD_THROW_EX (ACE_SYNCH_MUTEX_T, ace_mon, this->busy_lock_,
00163 CORBA::INTERNAL ());
00164
00165 if (this->busy_count_ == 0)
00166 {
00167
00168 this->disconnected_i (proxy);
00169 }
00170 else
00171 {
00172 ACE_Command_Base* command;
00173 ACE_NEW (command,
00174 Disconnected_Command (this,
00175 proxy));
00176 this->command_queue_.enqueue_tail (command);
00177 this->write_delay_count_++;
00178 }
00179 }
00180
00181 template<class PROXY, class C, class I,ACE_SYNCH_DECL> void
00182 TAO_ESF_Delayed_Changes<PROXY,C,I,ACE_SYNCH_USE>::shutdown (void)
00183 {
00184 ACE_GUARD_THROW_EX (ACE_SYNCH_MUTEX_T, ace_mon, this->busy_lock_,
00185 CORBA::INTERNAL ());
00186
00187 if (this->busy_count_ == 0)
00188 {
00189
00190 this->shutdown_i ();
00191 }
00192 else
00193 {
00194 ACE_Command_Base* command;
00195 ACE_NEW (command,
00196 Shutdown_Command (this));
00197 this->command_queue_.enqueue_tail (command);
00198 this->write_delay_count_++;
00199 }
00200 }
00201
00202 TAO_END_VERSIONED_NAMESPACE_DECL
00203
00204 #endif