ESF_Copy_On_Write.cpp

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

Generated on Tue Feb 2 17:43:47 2010 for TAO_ESF by  doxygen 1.4.7