3 //=============================================================================
5 * @file Token_Invariants.h
7 * $Id: Token_Invariants.h 80826 2008-03-04 14:51:23Z wotte $
9 * @author Tim Harrison (harrison@cs.wustl.edu)
11 * Allows applications to test that invariants are always
12 * satisfied. Can test mutexes and readers/writer locks. Does
13 * not test recursive acquisition.
17 //=============================================================================
19 #ifndef ACE_TOKEN_INVARIANTS_H
20 #define ACE_TOKEN_INVARIANTS_H
21 #include /**/ "ace/pre.h"
23 #include /**/ "ace/config-all.h"
25 #if !defined (ACE_LACKS_PRAGMA_ONCE)
27 #endif /* ACE_LACKS_PRAGMA_ONCE */
29 #if defined (ACE_HAS_TOKENS_LIBRARY)
31 #include "ace/Map_Manager.h"
32 #include "ace/Local_Tokens.h"
33 #include "ace/Null_Mutex.h"
35 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
38 * @class ACE_Mutex_Invariants
40 * @brief Mutex Invariants
42 * 1. Only one owner at a time.
44 class ACE_Export ACE_Mutex_Invariants
47 /// Default construction.
48 ACE_Mutex_Invariants (void);
50 /// Returns 1 on success, 0 when an invariant has been violated and
54 /// Updates internal database.
55 void releasing (void);
57 // = Map_Manager operations.
59 /// Copy construction.
60 ACE_Mutex_Invariants (const ACE_Mutex_Invariants
&rhs
);
63 void operator= (const ACE_Mutex_Invariants
&rhs
);
65 /// Dump the state of the class.
66 void dump (void) const;
69 /// Number of owners. This had better be 0 >= owners_ <= 1;
74 * @class ACE_RWLock_Invariants
76 * @brief RWLock Invariants
78 * Preserve the following invariants:
79 * -# Only one writer at a time.
80 * -# If there is an owning writer, there are no owning readers.
82 class ACE_Export ACE_RWLock_Invariants
85 /// Default construction.
86 ACE_RWLock_Invariants (void);
88 /// Returns 1 on success, 0 when an invariant has been violated and
90 int writer_acquired (void);
92 /// Returns 1 on success, 0 when an invariant has been violated and
94 int reader_acquired (void);
96 /// Updates internal database.
97 void releasing (void);
99 // = Map_Manager operations.
101 /// Copy construction.
102 ACE_RWLock_Invariants (const ACE_RWLock_Invariants
&rhs
);
105 void operator= (const ACE_RWLock_Invariants
&rhs
);
107 /// Dump the state of the class.
108 void dump (void) const;
111 /// Number of owning writers.
114 /// Number of owning readers.
119 * @class ACE_Token_Invariant_Manager
121 * @brief Token Invariants
123 * The Token Invariant Manager allows applications to test that
124 * invariants are always satisfied. Currently, Token_Invariants
125 * can test mutexes and readers/writer locks. Does not test
126 * recursive acquisition.
127 * Note that this class does not ever clean its database. Until
128 * destroyed, it's size will forever increase.
130 class ACE_Export ACE_Token_Invariant_Manager
: public ACE_Cleanup
134 /// Singleton access point.
135 static ACE_Token_Invariant_Manager
*instance (void);
137 // = Polymorphic methods. Just pass in the proxy and the method
138 // figures out the type of the token.
140 /// Returns 1 on success, 0 when an invariant has been violated and
142 int acquired (const ACE_Token_Proxy
*proxy
);
144 /// Updates internal database.
145 void releasing (const ACE_Token_Proxy
*proxy
);
147 // = Explicit methods. These to not require actual proxies in order
148 // to test a scenario.
150 /// Returns 1 on success, 0 when an invariant has been violated and
152 int mutex_acquired (const ACE_TCHAR
*token_name
);
154 /// Updates internal database.
155 void mutex_releasing (const ACE_TCHAR
*token_name
);
157 /// Returns 1 on success, 0 when an invariant has been violated and
159 int reader_acquired (const ACE_TCHAR
*token_name
);
161 /// Returns 1 on success, 0 when an invariant has been violated and
163 int writer_acquired (const ACE_TCHAR
*token_name
);
165 /// Updates internal database.
166 void rwlock_releasing (const ACE_TCHAR
*token_name
);
168 /// Dump the state of the class.
169 void dump (void) const;
171 // = The following two method should be in the protected part of the
172 // class. Bugs with certain compilers preclude this.
173 /// Prevent non-singleton construction.
174 ACE_Token_Invariant_Manager (void);
177 virtual ~ACE_Token_Invariant_Manager (void);
180 /// Return or create.
181 int get_mutex (const ACE_TCHAR
*token_name
,
182 ACE_Mutex_Invariants
*&inv
);
184 /// Return or create.
185 int get_rwlock (const ACE_TCHAR
*token_name
,
186 ACE_RWLock_Invariants
*&inv
);
188 /// ACE_Mutex_Token used to lock internal data structures.
189 ACE_TOKEN_CONST::MUTEX lock_
;
191 /// This may be changed to a template type.
192 typedef ACE_Token_Name TOKEN_NAME
;
194 /// COLLECTION maintains a mapping from token names to mutexes.
195 typedef ACE_Map_Manager
<TOKEN_NAME
, ACE_Mutex_Invariants
*, ACE_Null_Mutex
>
198 /// Allows iterations through collection.
200 * @deprecated Deprecated typedef. Use MUTEX_COLLECTION::ITERATOR trait
203 typedef MUTEX_COLLECTION::ITERATOR MUTEX_COLLECTION_ITERATOR
;
205 /// Allows iterations through collection.
207 * @deprecated Deprecated typedef. Use MUTEX_COLLECTION::ENTRY trait
210 typedef MUTEX_COLLECTION::ENTRY MUTEX_COLLECTION_ENTRY
;
212 /// MUTEX_COLLECTION maintains a mapping from token names to mutexes.
213 MUTEX_COLLECTION mutex_collection_
;
215 /// COLLECTION maintains a mapping from token names to mutexes.
216 typedef ACE_Map_Manager
<TOKEN_NAME
, ACE_RWLock_Invariants
*, ACE_Null_Mutex
>
219 /// Allows iterations through collection.
221 * @deprecated Deprecated typedef. Use RWLOCK_COLLECTION::ITERATOR trait
224 typedef RWLOCK_COLLECTION::ITERATOR RWLOCK_COLLECTION_ITERATOR
;
226 /// Allows iterations through collection.
228 * @deprecated Deprecated typedef. Use RWLOCK_COLLECTION::ENTRY trait
231 typedef RWLOCK_COLLECTION::ENTRY RWLOCK_COLLECTION_ENTRY
;
233 /// MUTEX_COLLECTION maintains a mapping from token names to mutexes.
234 RWLOCK_COLLECTION rwlock_collection_
;
236 /// Singleton pointer.
237 static ACE_Token_Invariant_Manager
*instance_
;
240 ACE_END_VERSIONED_NAMESPACE_DECL
242 #endif /* ACE_HAS_TOKENS_LIBRARY */
244 #include /**/ "ace/post.h"
245 #endif /* ACE_TOKEN_INVARIANTS_H */