Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_Future_Rep Class Template Reference

#include <Future.h>

Collaboration diagram for ACE_Future_Rep:

Collaboration graph
[legend]
List of all members.

Private Types

typedef ACE_Future_Observer<
T > 
OBSERVER
typedef ACE_Unbounded_Set<
OBSERVER * > 
OBSERVER_COLLECTION

Private Methods

int set (const T &r, ACE_Future< T > &caller)
int get (T &value, ACE_Time_Value *tv) const
 Wait up to <tv> time to get the <value>. Note that <tv> must be specified in absolute time rather than relative time. More...

int attach (ACE_Future_Observer< T > *observer, ACE_Future< T > &caller)
int detach (ACE_Future_Observer< T > *observer)
 operator T ()
void dump (void) const
 Dump the state of an object. More...

int ready (void) const
 Is result available? More...

 ACE_Future_Rep (void)
 ~ACE_Future_Rep (void)

Static Private Methods

ACE_Future_Rep< T > * internal_create (void)
 Allocate a new ACE_Future_Rep<T> instance, returning NULL if it cannot be created. More...

ACE_Future_Rep< T > * create (void)
 Create a ACE_Future_Rep<T> and initialize the reference count. More...

ACE_Future_Rep< T > * attach (ACE_Future_Rep< T > *&rep)
void detach (ACE_Future_Rep< T > *&rep)
void assign (ACE_Future_Rep< T > *&rep, ACE_Future_Rep< T > *new_rep)

Private Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks. More...

T * value_
 Pointer to the result. More...

int ref_count_
 Reference count. More...

OBSERVER_COLLECTION observer_collection_
 Keep a list of ACE_Future_Observers unread by client's reader thread. More...

ACE_Thread_Mutex value_ready_mutex_
ACE_Condition_Thread_Mutex value_ready_

Friends

class ACE_Future< T >

template<class T>
class ACE_Future_Rep< T >


Member Typedef Documentation

template<class T>
typedef ACE_Future_Observer<T> ACE_Future_Rep::OBSERVER [private]
 

Definition at line 207 of file Future.h.

template<class T>
typedef ACE_Unbounded_Set<OBSERVER *> ACE_Future_Rep::OBSERVER_COLLECTION [private]
 

Definition at line 210 of file Future.h.


Constructor & Destructor Documentation

template<class T>
ACE_Future_Rep< T >::ACE_Future_Rep void    [private]
 

Definition at line 139 of file Future.cpp.

00140   : value_ (0),
00141     ref_count_ (0),
00142     value_ready_ (this->value_ready_mutex_)
00143 {
00144 }

template<class T>
ACE_Future_Rep< T >::~ACE_Future_Rep void    [private]
 

Definition at line 147 of file Future.cpp.

References value_.

00148 {
00149   delete this->value_;
00150 }


Member Function Documentation

template<class T>
void ACE_Future_Rep< T >::assign ACE_Future_Rep< T > *&    rep,
ACE_Future_Rep< T > *    new_rep
[static, private]
 

Decreases the rep's reference count and deletes rep if there are no more references to rep. Then assigns new_rep to rep.

Precondition (rep != 0 && new_rep != 0)

Definition at line 117 of file Future.cpp.

References ACE_ASSERT, ACE_GUARD, ACE_MT, ref_count_, and value_ready_mutex_.

00118 {
00119   ACE_ASSERT (rep != 0);
00120   ACE_ASSERT (new_rep != 0);
00121   // Use value_ready_mutex_ for both condition and ref count management
00122   ACE_MT (ACE_GUARD (ACE_Thread_Mutex, r_mon, rep->value_ready_mutex_));
00123 
00124   ACE_Future_Rep<T>* old = rep;
00125   rep = new_rep;
00126 
00127   // detached old last for exception safety
00128   if (old->ref_count_-- == 0)
00129     {
00130       ACE_MT (r_mon.release ());
00131       // We do not need the lock when deleting the representation.
00132       // There should be no side effects from deleting rep and we don
00133       // not want to release a deleted mutex.
00134       delete old;
00135     }
00136 }

template<class T>
ACE_Future_Rep< T > * ACE_Future_Rep< T >::attach ACE_Future_Rep< T > *&    rep [static, private]
 

Increase the reference count and return argument. Uses the attribute "value_ready_mutex_" to synchronize reference count updating.

Precondition (rep != 0).

Definition at line 90 of file Future.cpp.

References ACE_ASSERT, ACE_MT, ref_count_, and value_ready_mutex_.

00091 {
00092   ACE_ASSERT (rep != 0);
00093   // Use value_ready_mutex_ for both condition and ref count management
00094   ACE_MT (ACE_Guard<ACE_Thread_Mutex> r_mon (rep->value_ready_mutex_));
00095   ++rep->ref_count_;
00096   return rep;
00097 }

template<class T>
int ACE_Future_Rep< T >::attach ACE_Future_Observer< T > *    observer,
ACE_Future< T > &    caller
[private]
 

Attaches the specified observer to a subject (i.e., the <ACE_Future_Rep>). The update method of the specified subject will be invoked with a copy of the written-to <ACE_Future> as input when the result gets set.

Returns 0 if the observer is successfully attached, 1 if the observer is already attached, and -1 if failures occur.

Definition at line 225 of file Future.cpp.

References ACE_GUARD_RETURN, ACE_MT, ACE_Unbounded_Set< OBSERVER * >::insert, observer_collection_, ACE_Future_Observer::update, and value_.

Referenced by ACE_Future::attach.

00227 {
00228   ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->value_ready_mutex_, -1));
00229 
00230   // Otherwise, create a new result value.  Note the use of the
00231   // Double-checked locking pattern to avoid corrupting the list.
00232 
00233   int result = 1;
00234 
00235   // If the value is already produced, then notify observer
00236   if (this->value_ == 0)
00237     result = this->observer_collection_.insert (observer);
00238   else
00239       observer->update (caller);
00240 
00241   return result;
00242 }

template<class T>
ACE_Future_Rep< T > * ACE_Future_Rep< T >::create void    [static, private]
 

Create a ACE_Future_Rep<T> and initialize the reference count.

Definition at line 75 of file Future.cpp.

References ACE_ASSERT, ACE_throw_bad_alloc, and internal_create.

00076 {
00077   // Yes set ref count to zero.
00078   ACE_Future_Rep<T> *temp = internal_create ();
00079 #if defined (ACE_NEW_THROWS_EXCEPTIONS)
00080   if (temp == 0)
00081     ACE_throw_bad_alloc;
00082 #else
00083   ACE_ASSERT (temp != 0);
00084 #endif /* ACE_NEW_THROWS_EXCEPTIONS */
00085    return temp;
00086  }

template<class T>
void ACE_Future_Rep< T >::detach ACE_Future_Rep< T > *&    rep [static, private]
 

Decreases the reference count and deletes rep if there are no more references to rep.

Precondition (rep != 0)

Definition at line 100 of file Future.cpp.

References ACE_ASSERT, ACE_GUARD, ACE_MT, ref_count_, and value_ready_mutex_.

00101 {
00102   ACE_ASSERT (rep != 0);
00103   // Use value_ready_mutex_ for both condition and ref count management
00104   ACE_MT (ACE_GUARD (ACE_Thread_Mutex, r_mon, rep->value_ready_mutex_));
00105 
00106   if (rep->ref_count_-- == 0)
00107     {
00108       ACE_MT (r_mon.release ());
00109       // We do not need the lock when deleting the representation.
00110       // There should be no side effects from deleting rep and we don
00111       // not want to release a deleted mutex.
00112       delete rep;
00113     }
00114 }

template<class T>
int ACE_Future_Rep< T >::detach ACE_Future_Observer< T > *    observer [private]
 

Detaches the specified observer from a subject (i.e., the <ACE_Future_Rep>). The update method of the specified subject will not be invoked when the <ACE_Future_Rep>s result gets set. Returns 1 if the specified observer was actually attached to the subject prior to this call and 0 if was not.

Returns 0 if the observer was successfully detached, and -1 if the observer was not attached in the first place.

Definition at line 245 of file Future.cpp.

References ACE_GUARD_RETURN, ACE_MT, observer_collection_, and ACE_Unbounded_Set< OBSERVER * >::remove.

Referenced by ACE_Future::detach.

00246 {
00247   ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->value_ready_mutex_, -1));
00248 
00249   // Remove all occurrences of the specified observer from this
00250   // objects hash map.
00251   return this->observer_collection_.remove (observer);
00252 }

template<class T>
void ACE_Future_Rep< T >::dump void    const [private]
 

Dump the state of an object.

Definition at line 45 of file Future.cpp.

References ACE_BEGIN_DUMP, ACE_DEBUG, ACE_END_DUMP, ACE_LIB_TEXT, ACE_Thread_Mutex::dump, ACE_Condition_Thread_Mutex::dump, LM_DEBUG, LM_INFO, value_, value_ready_, and value_ready_mutex_.

Referenced by ACE_Future::dump.

00046 {
00047   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00048   ACE_DEBUG ((LM_DEBUG,
00049               "ref_count_ = %d\n",
00050  (int) this->ref_count_));
00051   ACE_DEBUG ((LM_INFO,"value_: \n"));
00052   if (this->value_)
00053     ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT (" (NON-NULL)\n")));
00054   else
00055     ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT (" (NULL)\n")));
00056 
00057   ACE_DEBUG ((LM_INFO,"value_ready_: \n"));
00058   this->value_ready_.dump ();
00059   ACE_DEBUG ((LM_INFO,"value_ready_mutex_: \n"));
00060   this->value_ready_mutex_.dump ();
00061   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00062 }

template<class T>
int ACE_Future_Rep< T >::get T &    value,
ACE_Time_Value   tv
const [private]
 

Wait up to <tv> time to get the <value>. Note that <tv> must be specified in absolute time rather than relative time.

Definition at line 200 of file Future.cpp.

References ACE_GUARD_RETURN, ACE_MT, and value_.

Referenced by ACE_Future::get.

00202 {
00203   // If the value is already produced, return it.
00204   if (this->value_ == 0)
00205     {
00206       ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon,
00207                                 ACE_const_cast (ACE_Thread_Mutex &, this->value_ready_mutex_),
00208                                 -1));
00209       // If the value is not yet defined we must block until the
00210       // producer writes to it.
00211 
00212       while (this->value_ == 0)
00213         // Perform a timed wait.
00214         if ((ACE_const_cast (ACE_Condition_Thread_Mutex &, this->value_ready_)).wait (tv) == -1)
00215           return -1;
00216 
00217       // Destructor releases the lock.
00218     }
00219 
00220   value = *this->value_;
00221   return 0;
00222 }

template<class T>
ACE_Future_Rep< T > * ACE_Future_Rep< T >::internal_create void    [static, private]
 

Allocate a new ACE_Future_Rep<T> instance, returning NULL if it cannot be created.

Definition at line 65 of file Future.cpp.

References ACE_NEW_RETURN.

Referenced by create.

00066 {
00067   ACE_Future_Rep<T> *temp = 0;
00068   ACE_NEW_RETURN (temp,
00069                   ACE_Future_Rep<T> (),
00070                   0);
00071   return temp;
00072 }

template<class T>
ACE_Future_Rep< T >::operator T   [private]
 

Type conversion. will block forever until the result is available. Note that this method is going away in a subsequent release since it doesn't distinguish between failure results and success results (exceptions should be used, but they aren't portable...). The <get> method should be used instead since it separates the error value from the result, and also permits timeouts.

Definition at line 255 of file Future.cpp.

References ACE_GUARD_RETURN, ACE_MT, value_, value_ready_, and ACE_Condition_Thread_Mutex::wait.

00256 {
00257   // If the value is already produced, return it.
00258   if (this->value_ == 0)
00259     {
00260       // Constructor of ace_mon acquires the mutex.
00261       ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->value_ready_mutex_, 0));
00262 
00263       // If the value is not yet defined we must block until the
00264       // producer writes to it.
00265 
00266       // Wait ``forever.''
00267 
00268       while (this->value_ == 0)
00269         if (this->value_ready_.wait () == -1)
00270           // What to do in this case since we've got to indicate
00271           // failure somehow?  Exceptions would be nice, but they're
00272           // not portable...
00273           return 0;
00274 
00275       // Destructor releases the mutex
00276     }
00277 
00278   return *this->value_;
00279 }

template<class T>
int ACE_Future_Rep< T >::ready void    const [private]
 

Is result available?

Definition at line 153 of file Future.cpp.

References value_.

Referenced by ACE_Future::ready.

00154 {
00155   return this->value_ != 0;
00156 }

template<class T>
int ACE_Future_Rep< T >::set const T &    r,
ACE_Future< T > &    caller
[private]
 

Set the result value. The specified <caller> represents the future that invoked this <set> method, which is used to notify the list of future observers. Returns 0 for success, -1 on error. This function only has an effect the first time it is called for the object. Subsequent calls return 0 (success) but have no effect.

Definition at line 159 of file Future.cpp.

References ACE_GUARD_RETURN, ACE_MT, ACE_NEW_RETURN, ACE_TYPENAME, ACE_Unbounded_Set< OBSERVER * >::begin, ACE_Condition_Thread_Mutex::broadcast, ACE_Unbounded_Set< OBSERVER * >::end, observer_collection_, ACE_Future_Observer::update, value_, and value_ready_.

Referenced by ACE_Future::ACE_Future, ACE_Future::cancel, and ACE_Future::set.

00161 {
00162   // If the value is already produced, ignore it...
00163   if (this->value_ == 0)
00164     {
00165       ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex,
00166                                 ace_mon,
00167                                 this->value_ready_mutex_,
00168                                 -1));
00169       // Otherwise, create a new result value.  Note the use of the
00170       // Double-checked locking pattern to avoid multiple allocations.
00171 
00172       if (this->value_ == 0)       // Still no value, so proceed
00173         {
00174           ACE_NEW_RETURN (this->value_,
00175                           T (r),
00176                           -1);
00177 
00178           // Remove and notify all subscribed observers.
00179           ACE_TYPENAME OBSERVER_COLLECTION::iterator iterator =
00180             this->observer_collection_.begin ();
00181 
00182           ACE_TYPENAME OBSERVER_COLLECTION::iterator end =
00183             this->observer_collection_.end ();
00184 
00185           while (iterator != end)
00186             {
00187               OBSERVER *observer = *iterator++;
00188               observer->update (caller);
00189             }
00190 
00191           // Signal all the waiting threads.
00192           return this->value_ready_.broadcast ();
00193         }
00194       // Destructor releases the lock.
00195     }
00196   return 0;
00197 }


Friends And Related Function Documentation

template<class T>
friend class ACE_Future< T > [friend]
 

Definition at line 103 of file Future.h.


Member Data Documentation

template<class T>
ACE_Future_Rep::ACE_ALLOC_HOOK_DECLARE [private]
 

Declare the dynamic allocation hooks.

Definition at line 157 of file Future.h.

template<class T>
OBSERVER_COLLECTION ACE_Future_Rep::observer_collection_ [private]
 

Keep a list of ACE_Future_Observers unread by client's reader thread.

Definition at line 213 of file Future.h.

Referenced by attach, detach, and set.

template<class T>
int ACE_Future_Rep::ref_count_ [private]
 

Reference count.

Definition at line 204 of file Future.h.

Referenced by assign, attach, and detach.

template<class T>
T* ACE_Future_Rep::value_ [private]
 

Pointer to the result.

Definition at line 201 of file Future.h.

Referenced by attach, dump, get, operator T, ready, set, and ~ACE_Future_Rep.

template<class T>
ACE_Condition_Thread_Mutex ACE_Future_Rep::value_ready_ [private]
 

Definition at line 217 of file Future.h.

Referenced by dump, operator T, and set.

template<class T>
ACE_Thread_Mutex ACE_Future_Rep::value_ready_mutex_ [private]
 

Definition at line 216 of file Future.h.

Referenced by assign, attach, detach, and dump.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:48:12 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002