Token_Invariants.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Token_Invariants.h
00006  *
00007  *  Token_Invariants.h,v 4.24 2005/10/28 23:55:10 ossama Exp
00008  *
00009  *  @author Tim Harrison (harrison@cs.wustl.edu)
00010  *
00011  *   Allows applications to test that invariants are always
00012  *   satisfied.  Can test mutexes and readers/writer locks.  Does
00013  *   not test recursive acquisition.
00014  *
00015  *
00016  */
00017 //=============================================================================
00018 
00019 #ifndef ACE_TOKEN_INVARIANTS_H
00020 #define ACE_TOKEN_INVARIANTS_H
00021 #include /**/ "ace/pre.h"
00022 
00023 #include "ace/config-all.h"
00024 
00025 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00026 # pragma once
00027 #endif /* ACE_LACKS_PRAGMA_ONCE */
00028 
00029 #if defined (ACE_HAS_TOKENS_LIBRARY)
00030 
00031 #include "ace/Map_Manager.h"
00032 #include "ace/Local_Tokens.h"
00033 #include "ace/Null_Mutex.h"
00034 
00035 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00036 
00037 /**
00038  * @class ACE_Mutex_Invariants
00039  *
00040  * @brief Mutex Invariants
00041  * = INVARIANTS
00042  * 1. Only one owner at a time.
00043  */
00044 class ACE_Export ACE_Mutex_Invariants
00045 {
00046 public:
00047   /// Default construction.
00048   ACE_Mutex_Invariants (void);
00049 
00050   /// Returns 1 on success, 0 when an invariant has been violated and
00051   /// -1 on error.
00052   int acquired (void);
00053 
00054   /// Updates internal database.
00055   void releasing (void);
00056 
00057   // = Map_Manager operations.
00058 
00059   /// Copy construction.
00060   ACE_Mutex_Invariants (const ACE_Mutex_Invariants &rhs);
00061 
00062   /// Copy.
00063   void operator= (const ACE_Mutex_Invariants &rhs);
00064 
00065   /// Dump the state of the class.
00066   void dump (void) const;
00067 
00068 private:
00069   /// Number of owners.  This had better be 0 >= owners_ <= 1;
00070   int owners_;
00071 };
00072 
00073 /**
00074  * @class ACE_RWLock_Invariants
00075  *
00076  * @brief RWLock Invariants
00077  *
00078  * Preserve the following invariants:
00079  * -# Only one writer at a time.
00080  * -# If there is an owning writer, there are no owning readers.
00081  */
00082 class ACE_Export ACE_RWLock_Invariants
00083 {
00084 public:
00085   /// Default construction.
00086   ACE_RWLock_Invariants (void);
00087 
00088   /// Returns 1 on success, 0 when an invariant has been violated and
00089   /// -1 on error.
00090   int writer_acquired (void);
00091 
00092   /// Returns 1 on success, 0 when an invariant has been violated and
00093   /// -1 on error.
00094   int reader_acquired (void);
00095 
00096   /// Updates internal database.
00097   void releasing (void);
00098 
00099   // = Map_Manager operations.
00100 
00101   /// Copy construction.
00102   ACE_RWLock_Invariants (const ACE_RWLock_Invariants &rhs);
00103 
00104   /// Copy.
00105   void operator= (const ACE_RWLock_Invariants &rhs);
00106 
00107   /// Dump the state of the class.
00108   void dump (void) const;
00109 
00110 private:
00111   /// Number of owning writers.
00112   int writers_;
00113 
00114   /// Number of owning readers.
00115   int readers_;
00116 };
00117 
00118 /**
00119  * @class ACE_Token_Invariant_Manager
00120  *
00121  * @brief Token Invariants
00122  *
00123  * The Token Invariant Manager allows applications to test that
00124  * invariants are always satisfied.  Currently, Token_Invariants
00125  * can test mutexes and readers/writer locks.  Does not test
00126  * recursive acquisition.
00127  * Note that this class does not ever clean its database.  Until
00128  * destroyed, it's size will forever increase.
00129  */
00130 class ACE_Export ACE_Token_Invariant_Manager : public ACE_Cleanup
00131 {
00132 public:
00133 
00134   /// Singleton access point.
00135   static ACE_Token_Invariant_Manager *instance (void);
00136 
00137   // = Polymorphic methods.  Just pass in the proxy and the method
00138   // figures out the type of the token.
00139 
00140   /// Returns 1 on success, 0 when an invariant has been violated and
00141   /// -1 on error.
00142   int acquired (const ACE_Token_Proxy *proxy);
00143 
00144   /// Updates internal database.
00145   void releasing (const ACE_Token_Proxy *proxy);
00146 
00147   // = Explicit methods.  These to not require actual proxies in order
00148   // to test a scenario.
00149 
00150   /// Returns 1 on success, 0 when an invariant has been violated and
00151   /// -1 on error.
00152   int mutex_acquired (const ACE_TCHAR *token_name);
00153 
00154   /// Updates internal database.
00155   void mutex_releasing (const ACE_TCHAR *token_name);
00156 
00157   /// Returns 1 on success, 0 when an invariant has been violated and
00158   /// -1 on error.
00159   int reader_acquired (const ACE_TCHAR *token_name);
00160 
00161   /// Returns 1 on success, 0 when an invariant has been violated and
00162   /// -1 on error.
00163   int writer_acquired (const ACE_TCHAR *token_name);
00164 
00165   /// Updates internal database.
00166   void rwlock_releasing (const ACE_TCHAR *token_name);
00167 
00168   /// Dump the state of the class.
00169   void dump (void) const;
00170 
00171   // = The following two method should be in the protected part of the
00172   //   class.  Bugs with certain compilers preclude this.
00173   /// Prevent non-singleton construction.
00174   ACE_Token_Invariant_Manager (void);
00175 
00176   /// Destruction.
00177   virtual ~ACE_Token_Invariant_Manager (void);
00178 
00179 protected:
00180   /// Return or create.
00181   int get_mutex (const ACE_TCHAR *token_name,
00182                  ACE_Mutex_Invariants *&inv);
00183 
00184   /// Return or create.
00185   int get_rwlock (const ACE_TCHAR *token_name,
00186                   ACE_RWLock_Invariants *&inv);
00187 
00188   /// ACE_Mutex_Token used to lock internal data structures.
00189   ACE_TOKEN_CONST::MUTEX lock_;
00190 
00191   /// This may be changed to a template type.
00192   typedef ACE_Token_Name TOKEN_NAME;
00193 
00194   /// COLLECTION maintains a mapping from token names to mutexes.
00195   typedef ACE_Map_Manager<TOKEN_NAME, ACE_Mutex_Invariants *, ACE_Null_Mutex>
00196     MUTEX_COLLECTION;
00197 
00198   /// Allows iterations through collection.
00199   /**
00200    * @deprecated Deprecated typedef.  Use MUTEX_COLLECTION::ITERATOR trait
00201    * instead.
00202    */
00203   typedef MUTEX_COLLECTION::ITERATOR MUTEX_COLLECTION_ITERATOR;
00204 
00205   /// Allows iterations through collection.
00206   /**
00207    * @deprecated Deprecated typedef.  Use MUTEX_COLLECTION::ENTRY trait
00208    * instead.
00209    */
00210   typedef MUTEX_COLLECTION::ENTRY MUTEX_COLLECTION_ENTRY;
00211 
00212   /// MUTEX_COLLECTION maintains a mapping from token names to mutexes.
00213   MUTEX_COLLECTION mutex_collection_;
00214 
00215   /// COLLECTION maintains a mapping from token names to mutexes.
00216   typedef ACE_Map_Manager<TOKEN_NAME, ACE_RWLock_Invariants *, ACE_Null_Mutex>
00217     RWLOCK_COLLECTION;
00218 
00219   /// Allows iterations through collection.
00220   /**
00221    * @deprecated Deprecated typedef.  Use RWLOCK_COLLECTION::ITERATOR trait
00222    * instead.
00223    */
00224   typedef RWLOCK_COLLECTION::ITERATOR RWLOCK_COLLECTION_ITERATOR;
00225 
00226   /// Allows iterations through collection.
00227   /**
00228    * @deprecated Deprecated typedef.  Use RWLOCK_COLLECTION::ENTRY trait
00229    * instead.
00230    */
00231   typedef RWLOCK_COLLECTION::ENTRY RWLOCK_COLLECTION_ENTRY;
00232 
00233   /// MUTEX_COLLECTION maintains a mapping from token names to mutexes.
00234   RWLOCK_COLLECTION rwlock_collection_;
00235 
00236   /// Singleton pointer.
00237   static ACE_Token_Invariant_Manager *instance_;
00238 };
00239 
00240 ACE_END_VERSIONED_NAMESPACE_DECL
00241 
00242 #endif /* ACE_HAS_TOKENS_LIBRARY */
00243 
00244 #include /**/ "ace/post.h"
00245 #endif /* ACE_TOKEN_INVARIANTS_H */

Generated on Thu Nov 9 09:42:08 2006 for ACE by doxygen 1.3.6