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

Token_Manager.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Token_Manager.cpp,v 1.1.1.3.40.1 2003/03/13 19:44:23 chad Exp $
00003 
00004 #include "ace/Token_Manager.h"
00005 #include "ace/Object_Manager.h"
00006 
00007 #if !defined (__ACE_INLINE__)
00008 #include "ace/Token_Manager.i"
00009 #endif /* __ACE_INLINE__ */
00010 
00011 #if defined (ACE_HAS_TOKENS_LIBRARY)
00012 
00013 ACE_RCSID(ace, Token_Manager, "$Id: Token_Manager.cpp,v 1.1.1.3.40.1 2003/03/13 19:44:23 chad Exp $")
00014 
00015 // singleton token manager
00016 ACE_Token_Manager *ACE_Token_Manager::token_manager_ = 0;
00017 
00018 ACE_Token_Manager::ACE_Token_Manager ()
00019 {
00020   ACE_TRACE ("ACE_Token_Manager::ACE_Token_Manager");
00021 }
00022 
00023 ACE_Token_Manager::~ACE_Token_Manager ()
00024 {
00025   ACE_TRACE ("ACE_Token_Manager::~ACE_Token_Manager");
00026 
00027   COLLECTION_ITERATOR iterator (collection_);
00028 
00029   for (COLLECTION_ENTRY *temp = 0;
00030        iterator.next (temp) != 0;
00031        iterator.advance ())
00032     {
00033       // @ should I be doing an unbind here?
00034       delete temp->int_id_;
00035       // The ext_id_'s delete themselves when the array of
00036       // COLLECTION_ENTRYs goes away.
00037     }
00038 }
00039 
00040 ACE_Token_Manager *
00041 ACE_Token_Manager::instance (void)
00042 {
00043   ACE_TRACE ("ACE_Token_Manager::instance");
00044 
00045   // This first check is to avoid acquiring the mutex in the common
00046   // case.  Double-Check pattern rules.
00047   if (token_manager_ == 0)
00048     {
00049 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
00050       ACE_TOKEN_CONST::MUTEX *lock =
00051         ACE_Managed_Object<ACE_TOKEN_CONST::MUTEX>::get_preallocated_object
00052           (ACE_Object_Manager::ACE_TOKEN_MANAGER_CREATION_LOCK);
00053       ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX, ace_mon, *lock, 0);
00054 #endif /* ACE_MT_SAFE */
00055 
00056       if (token_manager_ == 0)
00057         {
00058           ACE_NEW_RETURN (token_manager_,
00059                           ACE_Token_Manager,
00060                           0);
00061           // Register for destruction with ACE_Object_Manager.
00062           ACE_Object_Manager::at_exit (token_manager_);
00063         }
00064     }
00065 
00066   return token_manager_;
00067 }
00068 
00069 void
00070 ACE_Token_Manager::get_token (ACE_Token_Proxy *proxy,
00071                               const ACE_TCHAR *token_name)
00072 {
00073   ACE_TRACE ("ACE_Token_Manager::get_token");
00074   // Hmm.  I think this makes sense.  We perform our own locking here
00075   // (see safe_acquire.)  We have to make sure that only one thread
00076   // uses the collection at a time.
00077 
00078   ACE_GUARD (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_);
00079 
00080   TOKEN_NAME name (token_name);
00081 
00082   if (collection_.find (name, proxy->token_) == -1)
00083     // We did not find one in the collection.
00084     {
00085       // Make one.
00086       proxy->token_ = proxy->create_token (token_name);
00087 
00088       // Put it in the collection.
00089       if (collection_.bind (name, proxy->token_) == -1)
00090         {
00091           delete proxy->token_;
00092           proxy->token_ = 0;
00093         }
00094     }
00095 
00096   if (proxy->token_ != 0)
00097     proxy->token_->inc_reference ();
00098 
00099   // We may be returning proxy->token_ == 0 if new failed, caller must
00100   // check.
00101 }
00102 
00103 // 0. check_deadlock (TOKEN)
00104 // 1. if TOKEN->visited (), return 0.
00105 // 2. mark TOKEN visited.
00106 // 3. get ALL_OWNERS
00107 // 4. if CLIENT in ALL_OWNERS, return *DEADLOCK*.
00108 // 5. for each OWNER in ALL_OWNERS,
00109 // 6.    if OWNER is not waiting for a NEW_TOKEN, continue.
00110 // 7.    else, if check_deadlock (NEW_TOKEN) == 1, return *DEADLOCK*
00111 // 8. return 0.
00112 
00113 int
00114 ACE_Token_Manager::check_deadlock (ACE_Token_Proxy *proxy)
00115 {
00116   ACE_TRACE ("ACE_Token_Manager::check_deadlock");
00117 
00118   // Start the recursive deadlock detection algorithm.
00119   int result = this->check_deadlock (proxy->token_, proxy);
00120 
00121   // Whether or not we detect deadlock, we have to unmark all tokens
00122   // for the next time.
00123   COLLECTION_ITERATOR iterator (collection_);
00124   for (COLLECTION_ENTRY *temp = 0;
00125        iterator.next (temp) != 0;
00126        iterator.advance ())
00127       temp->int_id_->visit (0);
00128 
00129   return result;
00130 }
00131 
00132 int
00133 ACE_Token_Manager::check_deadlock (ACE_Tokens *token, ACE_Token_Proxy *proxy)
00134 {
00135   ACE_TRACE ("ACE_Token_Manager::check_deadlock");
00136 
00137   if (token->visited ())
00138     return 0;
00139 
00140   token->visit (1);
00141 
00142   ACE_Tokens::OWNER_STACK owners;
00143 
00144   int is_owner = token->owners (owners, proxy->client_id ());
00145 
00146   switch (is_owner)
00147     {
00148     case -1:
00149       // Error.
00150       return -1;
00151     case 1:
00152       // The caller is an owner, so we have a deadlock situation.
00153       if (debug_)
00154         {
00155           ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("(%t) Deadlock detected.\n")));
00156           ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("%s owns %s and is waiting for %s.\n"),
00157                       proxy->client_id (),
00158                       token->name (),
00159                       proxy->token_->name ()));
00160         }
00161 
00162       return 1;
00163     case 0:
00164     default:
00165       // Recurse on each owner.
00166       while (!owners.is_empty ())
00167         {
00168           ACE_TPQ_Entry *e;
00169           owners.pop (e);
00170           // If the owner is waiting on another token, recurse.
00171           ACE_Tokens *twf = this->token_waiting_for (e->client_id ());
00172           if ((twf != 0) &&
00173               (this->check_deadlock (twf, proxy) == 1))
00174             {
00175               if (debug_)
00176                 {
00177                   ACE_DEBUG ((LM_DEBUG,
00178                               ACE_LIB_TEXT ("%s owns %s and is waiting for %s.\n"),
00179                               e->client_id (),
00180                               token->name (),
00181                               twf->name ()));
00182                 }
00183               return 1;
00184             }
00185           // else, check the next owner.
00186         }
00187 
00188       // We've checked all the owners and found no deadlock.
00189       return 0;
00190     }
00191 }
00192 
00193 
00194 ACE_Tokens *
00195 ACE_Token_Manager::token_waiting_for (const ACE_TCHAR *client_id)
00196 {
00197   COLLECTION_ITERATOR iterator (collection_);
00198   for (COLLECTION_ENTRY *temp = 0;
00199        iterator.next (temp) != 0;
00200        iterator.advance ())
00201     {
00202       if (temp->int_id_->is_waiting_for (client_id))
00203         return temp->int_id_;
00204     }
00205 
00206   // nothing was found, return NULL.
00207   return 0;
00208 }
00209 
00210 // Notify the token manager that a token is has been released.  If
00211 // as a result, there is no owner of the token, the token is
00212 // deleted.
00213 void
00214 ACE_Token_Manager::release_token (ACE_Tokens *&token)
00215 {
00216   ACE_TRACE ("ACE_Token_Manager::release_token");
00217   // again, let's perform our own locking here.
00218 
00219   ACE_GUARD (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_);
00220 
00221   if (token->dec_reference () == 0)
00222     {
00223       // No one has the token, so we can delete it and remove it from
00224       // our collection.  First, let's get it from the collection.
00225       TOKEN_NAME token_name (token->name ());
00226 
00227       ACE_Tokens *temp;
00228 
00229       if (collection_.unbind (token_name, temp) == -1)
00230         // we did not find one in the collection
00231         {
00232           errno = ENOENT;
00233           ACE_ERROR ((LM_ERROR, ACE_LIB_TEXT ("Token Manager could not release %s:%d\n"),
00234                       token->name (), token->type ()));
00235           // @@ bad
00236         }
00237       else
00238         // we found it
00239         {
00240           // sanity pointer comparison.  The token referenced by the
00241           // proxy better be the one we found in the list.
00242           ACE_ASSERT (token == temp);
00243           delete token;  // or delete temp
00244           // we set their token to zero.  if the calling proxy is
00245           // still going to be used, it had better check it's token
00246           // value before calling a method on it!
00247           token = 0;
00248         }
00249     }
00250   // else
00251   // someone is still interested in the token, so keep it around.
00252 }
00253 
00254 void
00255 ACE_Token_Manager::dump (void) const
00256 {
00257   ACE_TRACE ("ACE_Token_Manager::dump");
00258   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00259   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("ACE_Token_Manager::dump:\n")));
00260   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("lock_\n")));
00261   lock_.dump ();
00262   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("collection_\n")));
00263   collection_.dump ();
00264   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00265 }
00266 
00267 #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
00268 template class ACE_Map_Manager <ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>;
00269 template class ACE_Map_Iterator_Base<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>;
00270 template class ACE_Map_Iterator<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>;
00271 template class ACE_Map_Reverse_Iterator<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>;
00272 template class ACE_Map_Entry <ACE_Token_Name, ACE_Tokens *>;
00273 #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
00274 #pragma instantiate ACE_Map_Manager <ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>
00275 #pragma instantiate ACE_Map_Iterator_Base<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>
00276 #pragma instantiate ACE_Map_Iterator<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>
00277 #pragma instantiate ACE_Map_Reverse_Iterator<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>
00278 #pragma instantiate ACE_Map_Entry <ACE_Token_Name, ACE_Tokens *>
00279 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
00280 
00281 #endif /* ACE_HAS_TOKENS_LIBRARY */

Generated on Mon Jun 16 11:21:58 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002