ESF_Delayed_Changes.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /**
00003  *  @file   ESF_Delayed_Changes.h
00004  *
00005  *  ESF_Delayed_Changes.h,v 1.13 2006/03/15 07:52:21 jtc Exp
00006  *
00007  *  @author Carlos O'Ryan (coryan@cs.wustl.edu)
00008  *
00009  *  http://doc.ece.uci.edu/~coryan/EC/index.html
00010  */
00011 
00012 #ifndef TAO_ESF_DELAYED_CHANGES_H
00013 #define TAO_ESF_DELAYED_CHANGES_H
00014 
00015 #include "orbsvcs/ESF/ESF_Proxy_Collection.h"
00016 
00017 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00018 # pragma once
00019 #endif /* ACE_LACKS_PRAGMA_ONCE */
00020 
00021 #include "orbsvcs/ESF/ESF_Busy_Lock.h"
00022 
00023 #include "ace/Containers.h"
00024 
00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00026 class ACE_Command_Base;
00027 ACE_END_VERSIONED_NAMESPACE_DECL
00028 
00029 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00030 
00031 template<class Target,class Object> class TAO_ESF_Connected_Command;
00032 template<class Target,class Object> class TAO_ESF_Disconnected_Command;
00033 template<class Target,class Object> class TAO_ESF_Reconnected_Command;
00034 template<class Target> class TAO_ESF_Shutdown_Command;
00035 
00036 /**
00037  * @class TAO_ESF_Delayed_Changes
00038  *
00039  * @brief TAO_ESF_Delayed_Operations
00040  *
00041  * This class implements the Delayed Operations protocol to solve
00042  * the concurrency challenges outlined in the documentation of
00043  * TAO_ESF_Proxy_Collection.
00044  * In short the class delays changes by putting them on an
00045  * "operation queue", the operations are stored as command objects
00046  * in this queue and executed once the system is quiescent
00047  * (i.e. no threads are iterating over the collection).
00048  * The algorithm implemented so far is:
00049  * - If a thread is using the set then it increases the busy
00050  * count, this is done by calling the busy() method. Once the
00051  * thread has stopped using the collection the idle() method is
00052  * invoked and the busy count is decreased.
00053  * A helper class (Busy_Lock) is used to hide this protocol
00054  * behind the familiar GUARD idiom.
00055  * - If the busy count reaches the busy_hwm then the thread must
00056  * wait until the count reaches 0 again.
00057  * This can be used to control the maximum concurrency in the
00058  * EC, matching it (for example) with the number of
00059  * processors. Setting the concurrency to a high value (say one
00060  * million) allows for an arbitrary number of threads to execute
00061  * concurrently.
00062  * - If a modification is posted to the collection we need to
00063  * execute it at some point.
00064  * Just using the busy_hwm would not work, the HWM may not be
00065  * reached ever, so another form of control is needed.
00066  * Instead we use another counter, that keeps track of how many
00067  * threads have used the set since the modification was
00068  * posted. If this number of threads reaches max_write_delay then
00069  * we don't allow any more threads to go in, eventually the
00070  * thread count reaches 0 and we can proceed with the operations.
00071  * - There is one aspect of concurrency that can be problematic: if
00072  * thread pushes events as part of an upcall then the same thread
00073  * could be counted twice, we need to keep track of the threads
00074  * that are dispatching events and not increase (or decrease) the
00075  * reference count when a thread iterates twice over the same
00076  * set.
00077  * This solves the major problems, but there are other issues to
00078  * be addressed:
00079  * + How do we ensure that the operations are eventually executed?
00080  * + How do we simplify the execution of the locking protocol for
00081  * clients of this class?
00082  * + How do we minimize overhead for single threaded execution?
00083  * + How do we minimize the overhead for the cases where the
00084  * threads dispatching events don't post changes to the
00085  * collection?
00086  */
00087 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00088 class TAO_ESF_Delayed_Changes : public TAO_ESF_Proxy_Collection<PROXY>
00089 {
00090 public:
00091   TAO_ESF_Delayed_Changes (void);
00092   TAO_ESF_Delayed_Changes (const COLLECTION &collection);
00093 
00094   int busy (void);
00095   int idle (void);
00096   int execute_delayed_operations (void);
00097 
00098   void connected_i (PROXY *proxy
00099                     ACE_ENV_ARG_DECL);
00100   void reconnected_i (PROXY *proxy
00101                     ACE_ENV_ARG_DECL);
00102   void disconnected_i (PROXY *proxy
00103                      ACE_ENV_ARG_DECL);
00104   void shutdown_i (ACE_ENV_SINGLE_ARG_DECL);
00105 
00106   typedef TAO_ESF_Connected_Command<TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>,PROXY> Connected_Command;
00107   typedef TAO_ESF_Reconnected_Command<TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>,PROXY> Reconnected_Command;
00108   typedef TAO_ESF_Disconnected_Command<TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>,PROXY> Disconnected_Command;
00109   typedef TAO_ESF_Shutdown_Command<TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE> > Shutdown_Command;
00110 
00111   // = The TAO_ESF_Proxy methods
00112   virtual void for_each (TAO_ESF_Worker<PROXY> *worker
00113                          ACE_ENV_ARG_DECL);
00114   virtual void connected (PROXY *proxy
00115                           ACE_ENV_ARG_DECL);
00116   virtual void reconnected (PROXY *proxy
00117                             ACE_ENV_ARG_DECL);
00118   virtual void disconnected (PROXY *proxy
00119                              ACE_ENV_ARG_DECL);
00120   virtual void shutdown (ACE_ENV_SINGLE_ARG_DECL);
00121 
00122 private:
00123   COLLECTION collection_;
00124 
00125   typedef TAO_ESF_Busy_Lock_Adapter<TAO_ESF_Delayed_Changes<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE> > Busy_Lock;
00126 
00127   Busy_Lock lock_;
00128 
00129   ACE_SYNCH_MUTEX_T busy_lock_;
00130 
00131   ACE_SYNCH_CONDITION_T busy_cond_;
00132 
00133   CORBA::ULong busy_count_;
00134 
00135   CORBA::ULong write_delay_count_;
00136 
00137   /// Control variables for the concurrency policies.
00138   CORBA::ULong busy_hwm_;
00139   CORBA::ULong max_write_delay_;
00140 
00141   ACE_Unbounded_Queue<ACE_Command_Base*> command_queue_;
00142 };
00143 
00144 // ****************************************************************
00145 
00146 TAO_END_VERSIONED_NAMESPACE_DECL
00147 
00148 #if defined (__ACE_INLINE__)
00149 #include "orbsvcs/ESF/ESF_Delayed_Changes.i"
00150 #endif /* __ACE_INLINE__ */
00151 
00152 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00153 #include "orbsvcs/ESF/ESF_Delayed_Changes.cpp"
00154 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00155 
00156 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00157 #pragma implementation ("ESF_Delayed_Changes.cpp")
00158 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00159 
00160 #endif /* TAO_ESF_DELAYED_CHANGES_H */

Generated on Thu Nov 9 13:08:13 2006 for TAO_ESF by doxygen 1.3.6