3 //=============================================================================
5 * @file Token_Invariants.h
7 * @author Tim Harrison (harrison@cs.wustl.edu)
9 * Allows applications to test that invariants are always
10 * satisfied. Can test mutexes and readers/writer locks. Does
11 * not test recursive acquisition.
13 //=============================================================================
15 #ifndef ACE_TOKEN_INVARIANTS_H
16 #define ACE_TOKEN_INVARIANTS_H
17 #include /**/ "ace/pre.h"
19 #include /**/ "ace/config-all.h"
21 #if !defined (ACE_LACKS_PRAGMA_ONCE)
23 #endif /* ACE_LACKS_PRAGMA_ONCE */
25 #if defined (ACE_HAS_TOKENS_LIBRARY)
27 #include "ace/Map_Manager.h"
28 #include "ace/Local_Tokens.h"
29 #include "ace/Null_Mutex.h"
31 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
34 * @class ACE_Mutex_Invariants
36 * @brief Mutex Invariants
38 * 1. Only one owner at a time.
40 class ACE_Export ACE_Mutex_Invariants
43 /// Default construction.
44 ACE_Mutex_Invariants () = default;
46 /// Returns 1 on success, 0 when an invariant has been violated and
50 /// Updates internal database.
53 // = Map_Manager operations.
55 /// Copy construction.
56 ACE_Mutex_Invariants (const ACE_Mutex_Invariants
&rhs
) = default;
59 void operator= (const ACE_Mutex_Invariants
&rhs
) = default;
61 /// Dump the state of the class.
65 /// Number of owners. This had better be 0 >= owners_ <= 1;
70 * @class ACE_RWLock_Invariants
72 * @brief RWLock Invariants
74 * Preserve the following invariants:
75 * -# Only one writer at a time.
76 * -# If there is an owning writer, there are no owning readers.
78 class ACE_Export ACE_RWLock_Invariants
81 /// Default construction.
82 ACE_RWLock_Invariants () = default;
84 /// Returns 1 on success, 0 when an invariant has been violated and
86 int writer_acquired ();
88 /// Returns 1 on success, 0 when an invariant has been violated and
90 int reader_acquired ();
92 /// Updates internal database.
95 // = Map_Manager operations.
97 /// Copy construction.
98 ACE_RWLock_Invariants (const ACE_RWLock_Invariants
&rhs
) = default;
101 void operator= (const ACE_RWLock_Invariants
&rhs
) = default;
103 /// Dump the state of the class.
107 /// Number of owning writers.
110 /// Number of owning readers.
115 * @class ACE_Token_Invariant_Manager
117 * @brief Token Invariants
119 * The Token Invariant Manager allows applications to test that
120 * invariants are always satisfied. Currently, Token_Invariants
121 * can test mutexes and readers/writer locks. Does not test
122 * recursive acquisition.
123 * Note that this class does not ever clean its database. Until
124 * destroyed, it's size will forever increase.
126 class ACE_Export ACE_Token_Invariant_Manager
: public ACE_Cleanup
129 /// Singleton access point.
130 static ACE_Token_Invariant_Manager
*instance ();
132 // = Polymorphic methods. Just pass in the proxy and the method
133 // figures out the type of the token.
135 /// Returns 1 on success, 0 when an invariant has been violated and
137 int acquired (const ACE_Token_Proxy
*proxy
);
139 /// Updates internal database.
140 void releasing (const ACE_Token_Proxy
*proxy
);
142 // = Explicit methods. These to not require actual proxies in order
143 // to test a scenario.
145 /// Returns 1 on success, 0 when an invariant has been violated and
147 int mutex_acquired (const ACE_TCHAR
*token_name
);
149 /// Updates internal database.
150 void mutex_releasing (const ACE_TCHAR
*token_name
);
152 /// Returns 1 on success, 0 when an invariant has been violated and
154 int reader_acquired (const ACE_TCHAR
*token_name
);
156 /// Returns 1 on success, 0 when an invariant has been violated and
158 int writer_acquired (const ACE_TCHAR
*token_name
);
160 /// Updates internal database.
161 void rwlock_releasing (const ACE_TCHAR
*token_name
);
163 /// Dump the state of the class.
166 // = The following two method should be in the protected part of the
167 // class. Bugs with certain compilers preclude this.
168 /// Prevent non-singleton construction.
169 ACE_Token_Invariant_Manager ();
172 virtual ~ACE_Token_Invariant_Manager ();
175 /// Return or create.
176 int get_mutex (const ACE_TCHAR
*token_name
,
177 ACE_Mutex_Invariants
*&inv
);
179 /// Return or create.
180 int get_rwlock (const ACE_TCHAR
*token_name
,
181 ACE_RWLock_Invariants
*&inv
);
183 /// ACE_Mutex_Token used to lock internal data structures.
184 ACE_TOKEN_CONST::MUTEX lock_
;
186 /// This may be changed to a template type.
187 typedef ACE_Token_Name TOKEN_NAME
;
189 /// COLLECTION maintains a mapping from token names to mutexes.
190 typedef ACE_Map_Manager
<TOKEN_NAME
, ACE_Mutex_Invariants
*, ACE_Null_Mutex
>
193 /// MUTEX_COLLECTION maintains a mapping from token names to mutexes.
194 MUTEX_COLLECTION mutex_collection_
;
196 /// COLLECTION maintains a mapping from token names to mutexes.
197 typedef ACE_Map_Manager
<TOKEN_NAME
, ACE_RWLock_Invariants
*, ACE_Null_Mutex
>
200 /// MUTEX_COLLECTION maintains a mapping from token names to mutexes.
201 RWLOCK_COLLECTION rwlock_collection_
;
203 /// Singleton pointer.
204 static ACE_Token_Invariant_Manager
*instance_
;
207 ACE_END_VERSIONED_NAMESPACE_DECL
209 #endif /* ACE_HAS_TOKENS_LIBRARY */
211 #include /**/ "ace/post.h"
212 #endif /* ACE_TOKEN_INVARIANTS_H */