00001
00002
00003 #ifndef TAO_ESF_COPY_ON_WRITE_CPP
00004 #define TAO_ESF_COPY_ON_WRITE_CPP
00005
00006 #include "orbsvcs/ESF/ESF_Copy_On_Write.h"
00007 #include "ace/Guard_T.h"
00008
00009 #if ! defined (__ACE_INLINE__)
00010 #include "orbsvcs/ESF/ESF_Copy_On_Write.inl"
00011 #endif
00012
00013 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00014
00015 template<class COLLECTION, class ITERATOR> CORBA::ULong
00016 TAO_ESF_Copy_On_Write_Collection<COLLECTION,ITERATOR>::_incr_refcnt (void)
00017 {
00018
00019 return this->refcount_++;
00020 }
00021
00022 template<class COLLECTION, class ITERATOR> CORBA::ULong
00023 TAO_ESF_Copy_On_Write_Collection<COLLECTION,ITERATOR>::_decr_refcnt (void)
00024 {
00025
00026 {
00027 --this->refcount_;
00028 if (this->refcount_ != 0)
00029 return this->refcount_;
00030 }
00031
00032 ITERATOR end = this->collection.end ();
00033 for (ITERATOR i = this->collection.begin (); i != end; ++i)
00034 {
00035 (*i)->_decr_refcnt ();
00036 }
00037
00038 delete this;
00039 return 0;
00040 }
00041
00042
00043
00044 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00045 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00046 TAO_ESF_Copy_On_Write (void)
00047 : pending_writes_ (0),
00048 writing_ (0),
00049 cond_ (mutex_)
00050 {
00051 ACE_NEW (this->collection_, Collection);
00052 }
00053
00054 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00055 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00056 ~TAO_ESF_Copy_On_Write (void)
00057 {
00058 ACE_GUARD (ACE_SYNCH_MUTEX_T, ace_mon, this->mutex_);
00059
00060 while (this->pending_writes_ != 0)
00061 this->cond_.wait ();
00062
00063 this->collection_->_decr_refcnt ();
00064 this->collection_ = 0;
00065 }
00066
00067 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00068 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00069 for_each (TAO_ESF_Worker<PROXY> *worker)
00070 {
00071 Read_Guard ace_mon (this->mutex_,
00072 this->collection_);
00073
00074 worker->set_size(ace_mon.collection->collection.size());
00075 ITERATOR end = ace_mon.collection->collection.end ();
00076 for (ITERATOR i = ace_mon.collection->collection.begin (); i != end; ++i)
00077 {
00078 worker->work (*i);
00079 }
00080 }
00081
00082 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00083 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00084 connected (PROXY *proxy)
00085 {
00086 Write_Guard ace_mon (this->mutex_,
00087 this->cond_,
00088 this->pending_writes_,
00089 this->writing_,
00090 this->collection_);
00091
00092 proxy->_incr_refcnt ();
00093 ace_mon.copy->collection.connected (proxy);
00094 }
00095
00096 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00097 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00098 reconnected (PROXY *proxy)
00099 {
00100 Write_Guard ace_mon (this->mutex_,
00101 this->cond_,
00102 this->pending_writes_,
00103 this->writing_,
00104 this->collection_);
00105
00106 proxy->_incr_refcnt ();
00107 ace_mon.copy->collection.reconnected (proxy);
00108 }
00109
00110 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00111 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00112 disconnected (PROXY *proxy)
00113 {
00114 Write_Guard ace_mon (this->mutex_,
00115 this->cond_,
00116 this->pending_writes_,
00117 this->writing_,
00118 this->collection_);
00119
00120 ace_mon.copy->collection.disconnected (proxy);
00121 }
00122
00123 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00124 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::shutdown (void)
00125 {
00126
00127 Write_Guard ace_mon (this->mutex_,
00128 this->cond_,
00129 this->pending_writes_,
00130 this->writing_,
00131 this->collection_);
00132
00133 ace_mon.copy->collection.shutdown ();
00134 }
00135
00136 template<class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00137 TAO_ESF_Copy_On_Write_Write_Guard<COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00138 TAO_ESF_Copy_On_Write_Write_Guard (ACE_SYNCH_MUTEX_T &m,
00139 ACE_SYNCH_CONDITION_T &c,
00140 int &p,
00141 int &w,
00142 Collection*& cr)
00143 : copy (0),
00144 mutex (m),
00145 cond (c),
00146 pending_writes (p),
00147 writing_flag (w),
00148 collection (cr)
00149 {
00150 {
00151 ACE_GUARD (ACE_SYNCH_MUTEX_T, ace_mon, this->mutex);
00152
00153 this->pending_writes++;
00154
00155 while (this->writing_flag != 0)
00156 this->cond.wait ();
00157
00158 this->writing_flag = 1;
00159 }
00160
00161
00162
00163
00164
00165
00166 ACE_NEW (this->copy, Collection);
00167
00168 this->copy->collection = this->collection->collection;
00169
00170
00171 ITERATOR end = this->copy->collection.end ();
00172 for (ITERATOR i = this->copy->collection.begin (); i != end; ++i)
00173 {
00174 (*i)->_incr_refcnt ();
00175 }
00176 }
00177
00178 template<class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00179 TAO_ESF_Copy_On_Write_Write_Guard<COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00180 ~TAO_ESF_Copy_On_Write_Write_Guard (void)
00181 {
00182 Collection *tmp = 0;
00183 {
00184 ACE_GUARD (ACE_SYNCH_MUTEX_T, ace_mon, this->mutex);
00185
00186 tmp = this->collection;
00187 this->collection = this->copy;
00188 this->writing_flag = 0;
00189 this->pending_writes--;
00190
00191 this->cond.signal ();
00192 }
00193
00194 tmp->_decr_refcnt ();
00195 }
00196
00197
00198
00199 TAO_END_VERSIONED_NAMESPACE_DECL
00200
00201 #endif