00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Future_Set.h 00006 * 00007 * $Id: Future_Set.h 77114 2007-02-14 08:55:20Z johnnyw $ 00008 * 00009 * @author John Tucker <jtucker@infoglide.com> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_FUTURE_SET_H 00014 #define ACE_FUTURE_SET_H 00015 #include /**/ "ace/pre.h" 00016 00017 #include "ace/Thread.h" 00018 #include "ace/Message_Queue.h" 00019 #include "ace/Future.h" 00020 #include "ace/Hash_Map_Manager_T.h" 00021 #include "ace/Null_Mutex.h" 00022 00023 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00024 #pragma once 00025 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00026 00027 #if defined (ACE_HAS_THREADS) 00028 00029 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00030 00031 /** 00032 * @class ACE_Future_Set 00033 * 00034 * @brief This class implements a mechanism which allows the values of 00035 * a collection of ACE_Future objects to be accessed by reader threads 00036 * as they become available. The caller(s) provide the ACE_Future_Set 00037 * (i.e. the observer...) with the collection of ACE_Future objects 00038 * (i.e. the subjects...) that are to be observed using the 00039 * the ACE_Future_Set::insert() method. The caller(s) may then iterate 00040 * over the collection in the order in which they become readable using 00041 * the ACE_Future_Set::next_readable() method. 00042 */ 00043 template <class T> 00044 class ACE_Future_Set : public ACE_Future_Observer<T> 00045 { 00046 public: 00047 // = Initialization and termination methods. 00048 00049 /// Constructor. 00050 ACE_Future_Set (ACE_Message_Queue<ACE_SYNCH> *future_notification_queue_ = 0); 00051 00052 /// Destructor. 00053 ~ACE_Future_Set (void); 00054 00055 /** 00056 * Return 1 if their are no ACE_Future objects left on its queue and 00057 * 0 otherwise. 00058 * 00059 * When an ACE_Future_Set has no ACE_Future>subjects to observe it is 00060 * empty. The ACE_Future_Set is in the empty state when either the caller(s) 00061 * have retrieved every readable ACE_Future subject assigned the 00062 * ACE_Future_Set via the ACE_Future_Set::next_readable() method, 00063 * or when the ACE_Future_Set has not been assigned any subjects. 00064 */ 00065 int is_empty (void) const; 00066 00067 /** 00068 * Enqueus the given ACE_Future into this objects queue when it is 00069 * readable. 00070 * 00071 * Returns 0 if the future is successfully inserted, 1 if the 00072 * future is already inserted, and -1 if failures occur. 00073 */ 00074 int insert (ACE_Future<T> &future); 00075 00076 /** 00077 * Wait up to @a tv time to get the @a value. Note that @a tv must be 00078 * specified in absolute time rather than relative time.); get the 00079 * next ACE_Future that is readable. If @a tv = 0, the will block 00080 * forever. 00081 * 00082 * If a readable future becomes available, then the input 00083 * ACE_Future object param will be assigned with it and 1 will 00084 * be returned. If the ACE_Future_Set is empty (i.e. see definition 00085 * of ACE_Future_Set::is_empty()), then 0 is returned. 00086 * 00087 * When a readable ACE_Future object is retrieved via the 00088 * ACE_Future_Set::next_readable() method, the ACE_Future_Set will 00089 * remove that ACE_Future object from its list of subjects. 00090 */ 00091 int next_readable (ACE_Future<T> &result, 00092 ACE_Time_Value *tv = 0); 00093 00094 /// Called by the ACE_Future subject in which we are subscribed to 00095 /// when its value is written to. 00096 virtual void update (const ACE_Future<T> &future); 00097 00098 /// Declare the dynamic allocation hooks. 00099 ACE_ALLOC_HOOK_DECLARE; 00100 00101 private: 00102 // = Disallow these operations. 00103 ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Future_Set<T> &)) 00104 ACE_UNIMPLEMENTED_FUNC (ACE_Future_Set (const ACE_Future_Set<T> &)) 00105 00106 typedef ACE_Future<T> FUTURE; 00107 00108 typedef ACE_Future_Rep<T> FUTURE_REP; 00109 00110 typedef ACE_Future_Holder<T> FUTURE_HOLDER; 00111 00112 typedef ACE_Pointer_Hash<FUTURE_REP *> FUTURE_REP_HASH; 00113 00114 typedef ACE_Equal_To<FUTURE_REP *> FUTURE_REP_COMPARE; 00115 00116 typedef ACE_Hash_Map_Manager_Ex<FUTURE_REP *, 00117 FUTURE_HOLDER *, 00118 FUTURE_REP_HASH, 00119 FUTURE_REP_COMPARE, 00120 ACE_Null_Mutex> FUTURE_HASH_MAP; 00121 00122 /// Map of <ACE_Futures>, subjects, which have not been written to by 00123 /// client's writer thread. 00124 FUTURE_HASH_MAP future_map_; 00125 00126 /// Message queue for notifying the reader thread of <ACE_Futures> which 00127 /// have been written to by client's writer thread. 00128 ACE_Message_Queue<ACE_SYNCH> *future_notification_queue_; 00129 00130 /// Keeps track of whether we need to delete the message queue. 00131 int delete_queue_; 00132 }; 00133 00134 ACE_END_VERSIONED_NAMESPACE_DECL 00135 00136 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00137 #include "ace/Future_Set.cpp" 00138 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00139 00140 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00141 #pragma implementation ("Future_Set.cpp") 00142 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00143 00144 #endif /* ACE_HAS_THREADS */ 00145 #include /**/ "ace/post.h" 00146 #endif /* ACE_FUTURE_SET_H */