Future_Set.cpp

Go to the documentation of this file.
00001 // Future_Set.cpp,v 4.14 2005/10/28 16:14:52 ossama Exp
00002 
00003 #ifndef ACE_FUTURE_SET_CPP
00004 #define ACE_FUTURE_SET_CPP
00005 
00006 #include "ace/Future_Set.h"
00007 
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 #pragma once
00010 #endif /* ACE_LACKS_PRAGMA_ONCE */
00011 
00012 #if defined (ACE_HAS_THREADS)
00013 
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 template <class T>
00017 ACE_Future_Set<T>::ACE_Future_Set (ACE_Message_Queue<ACE_SYNCH> *new_queue)
00018   : delete_queue_ (0)
00019 {
00020   if (new_queue)
00021     this->future_notification_queue_ = new_queue;
00022   else
00023     {
00024       ACE_NEW (this->future_notification_queue_,
00025                ACE_Message_Queue<ACE_SYNCH>);
00026       this->delete_queue_ = 1;
00027     }
00028 }
00029 
00030 template <class T>
00031 ACE_Future_Set<T>::~ACE_Future_Set (void)
00032 {
00033   // Detach ourselves from all remaining futures, if any, in our map.
00034   ACE_TYPENAME FUTURE_HASH_MAP::iterator iterator =
00035     this->future_map_.begin ();
00036 
00037   ACE_TYPENAME FUTURE_HASH_MAP::iterator end =
00038     this->future_map_.end ();
00039 
00040   for (;
00041        iterator != end;
00042        ++iterator)
00043     {
00044       FUTURE_HOLDER *future_holder = (*iterator).int_id_;
00045       future_holder->item_.detach (this);
00046       delete future_holder;
00047     }
00048 
00049   if (this->delete_queue_ != 0)
00050     delete this->future_notification_queue_;
00051 }
00052 
00053 template <class T> int
00054 ACE_Future_Set<T>::is_empty () const
00055 {
00056   return (((ACE_Future_Set<T>*)this)->future_map_.current_size () == 0 );
00057 }
00058 
00059 template <class T> int
00060 ACE_Future_Set<T>::insert (ACE_Future<T> &future)
00061 {
00062   FUTURE_HOLDER *future_holder;
00063   ACE_NEW_RETURN (future_holder,
00064                   FUTURE_HOLDER (future),
00065                   -1);
00066 
00067   FUTURE_REP *future_rep = future.get_rep ();
00068   int result = this->future_map_.bind (future_rep,
00069                                        future_holder);
00070 
00071   // If a new map entry was created, then attach to the future,
00072   // otherwise we were already attached to the future or some error
00073   // occurred so just delete the future holder.
00074   if ( result == 0 )
00075     // Attach ourself to the ACE_Futures list of observer
00076     future.attach (this);
00077   else
00078     delete future_holder;
00079 
00080   return result;
00081 }
00082 
00083 template <class T> void
00084 ACE_Future_Set<T>::update (const ACE_Future<T> &future)
00085 {
00086   ACE_Message_Block *mb;
00087   FUTURE &local_future = const_cast<ACE_Future<T> &> (future);
00088 
00089   ACE_NEW (mb,
00090            ACE_Message_Block ((char *) local_future.get_rep (), 0));
00091 
00092   // Enqueue in priority order.
00093   this->future_notification_queue_->enqueue (mb, 0);
00094 }
00095 
00096 template <class T> int
00097 ACE_Future_Set<T>::next_readable (ACE_Future<T> &future,
00098                                   ACE_Time_Value *tv)
00099 {
00100   if (this->is_empty ())
00101     return 0;
00102 
00103   ACE_Message_Block *mb = 0;
00104   FUTURE_REP *future_rep = 0;
00105 
00106   // Wait for a "readable future" signal from the message queue.
00107   if (this->future_notification_queue_->dequeue_head (mb,
00108                                                       tv) != -1)
00109     {
00110       // Extract future rep from the message block.
00111       future_rep = reinterpret_cast<FUTURE_REP *> (mb->base ());
00112 
00113       // Delete the message block.
00114       mb->release ();
00115     }
00116   else
00117     return 0;
00118 
00119   // Remove the hash map entry with the specified future rep from our map.
00120   FUTURE_HOLDER *future_holder;
00121   if (this->future_map_.find (future_rep,
00122                               future_holder) != -1)
00123     {
00124       future = future_holder->item_;
00125       this->future_map_.unbind (future_rep);
00126       delete future_holder;
00127       return 1;
00128     }
00129 
00130   return 0;
00131 }
00132 
00133 ACE_END_VERSIONED_NAMESPACE_DECL
00134 
00135 #endif /* ACE_HAS_THREADS */
00136 #endif /* ACE_FUTURE_SET_CPP */

Generated on Thu Nov 9 09:41:51 2006 for ACE by doxygen 1.3.6