3 //=============================================================================
5 * @file Transport_Cache_Manager_T.h
7 * @author Balachandran Natarajan <bala@cs.wustl.edu>
9 //=============================================================================
11 #ifndef TAO_CONNECTION_CACHE_MANAGER_T_H
12 #define TAO_CONNECTION_CACHE_MANAGER_T_H
14 #include /**/ "ace/pre.h"
15 #include "ace/Null_Mutex.h"
16 #include "ace/Thread_Mutex.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #define ACE_LACKS_PRAGMA_ONCE
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/Hash_Map_Manager_T.h"
24 #include "tao/Cache_Entries_T.h"
25 #include "tao/orbconf.h"
27 #if defined (TAO_HAS_MONITOR_POINTS) && (TAO_HAS_MONITOR_POINTS == 1)
28 #include "ace/Monitor_Size.h"
29 #endif /* TAO_HAS_MONITOR_POINTS==1 */
31 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
33 template <class T
> class ACE_Unbounded_Set
;
34 template <class T
> class ACE_Unbounded_Set_Iterator
;
35 ACE_END_VERSIONED_NAMESPACE_DECL
37 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
39 class TAO_Connection_Handler
;
40 class TAO_Resource_Factory
;
42 template <class ACE_COND_MUTEX
> class TAO_Condition
;
46 typedef ACE_Unbounded_Set
<TAO_Connection_Handler
*> Connection_Handler_Set
;
48 * @brief The Transport Cache Manager for TAO
50 * This class provides interfaces associating a TAO_Cache_ExtId
51 * & TAO_Cache_IntId. This class is wrapper around the
52 * ACE_Hash_Map_Manager class which is used as a container to Cache
53 * the connections. This class protects the entries with a lock. The
54 * map is updated only by holding the lock. The more compeling reason
55 * to have the lock in this class and not in the Hash_Map is that, we
56 * do quite a bit of work in this class for which we need a lock.
58 template <typename TT
, typename TRDT
, typename PSTRAT
>
59 class Transport_Cache_Manager_T
62 typedef TT transport_type
;
63 typedef TRDT transport_descriptor_type
;
64 typedef PSTRAT purging_strategy
;
70 CACHE_FOUND_CONNECTING
,
75 typedef Cache_ExtId_T
<transport_descriptor_type
> Cache_ExtId
;
76 typedef Cache_IntId_T
<transport_type
> Cache_IntId
;
78 // Some useful typedef's
79 typedef ACE_Hash_Map_Manager_Ex
<Cache_ExtId
,
81 ACE_Hash
<Cache_ExtId
>,
82 ACE_Equal_To
<Cache_ExtId
>,
86 typedef typename
HASH_MAP::iterator HASH_MAP_ITER
;
88 typedef ACE_Hash_Map_Entry
<Cache_ExtId
,
92 typedef TAO_Condition
<TAO_SYNCH_MUTEX
> CONDITION
;
96 Transport_Cache_Manager_T (
98 purging_strategy
* purging_strategy
,
104 ~Transport_Cache_Manager_T ();
106 /// Add the transport to the cache.
108 * The transport has the property definition based on which caching
109 * can be done. This method sets the cache entry status. By
110 * default the status is set to <CODE>ENTRY_IDLE_AND_PURGABLE</CODE>
112 int cache_transport (transport_descriptor_type
*prop
,
113 transport_type
*transport
,
114 Cache_Entries_State state
=
115 ENTRY_IDLE_AND_PURGABLE
);
117 /// Check the Transport Cache to check whether the connection exists
118 /// in the Cache and return the connection
119 Find_Result
find_transport (
120 transport_descriptor_type
*prop
,
121 transport_type
*&transport
,
122 size_t & busy_count
);
124 /// Remove entries from the cache depending upon the strategy.
127 /// Purge the entry from the Cache Map
128 int purge_entry (HASH_MAP_ENTRY
*& entry
);
130 /// Mark the entry as connected.
131 void mark_connected (HASH_MAP_ENTRY
*& entry
, bool state
);
133 /// Make the entry idle and ready for use.
134 int make_idle (HASH_MAP_ENTRY
*&entry
);
136 /// Modify the state setting on the provided entry.
137 void set_entry_state (HASH_MAP_ENTRY
*&entry
,
138 TAO::Cache_Entries_State state
);
140 /// Mark the entry as touched. This call updates the purging
141 /// strategy policy information.
142 int update_entry (HASH_MAP_ENTRY
*&entry
);
144 /// Close the underlying hash map manager and return any handlers
146 int close (Connection_Handler_Set
&handlers
);
148 /// Return a set of connection handlers that belong to transports
149 /// that have a RW wait strategy.
151 * This call is used for a specific use case by the ORB_Core
152 * during shutdown. The only way the ORB can wake up threads
153 * waiting on these sockets for replies is to iterate over
154 * these blockable transports and close the socket
155 * handles. Without these the threads will continue to wait there
158 bool blockable_client_transports (Connection_Handler_Set
&handlers
);
160 /// Return the current size of the cache.
161 size_t current_size () const;
163 /// Return the total size of the cache.
164 size_t total_size () const;
166 /// Return the underlying cache map
170 /// Lookup entry<key,value> in the cache. Grabs the lock and calls the
171 /// implementation function find_i.
173 transport_descriptor_type
*prop
,
174 transport_type
*&transport
,
175 size_t & busy_count
);
178 * Non-Locking version and actual implementation of bind ()
179 * call. Calls bind on the Hash_Map_Manager that it holds. If the
180 * bind succeeds, it adds the Hash_Map_Entry in to the
181 * Transport for its reference.
183 int bind_i (Cache_ExtId
&ext_id
, Cache_IntId
&int_id
);
186 * Non-locking version and actual implementation of find ()
187 * call. This calls the find () on the underlying
188 * Hash_Map_Manager. If the find succeeds, it calls the
189 * get_idle_transport ().
192 transport_descriptor_type
*prop
,
193 transport_type
*&transport
,
194 size_t & busy_count
);
196 /// Non-locking version and actual implementation of make_idle ().
197 int make_idle_i (HASH_MAP_ENTRY
*entry
);
199 /// Non-locking version and actual implementation of close ()
200 int close_i (Connection_Handler_Set
&handlers
);
202 /// Purge the entry from the Cache Map
203 int purge_entry_i (HASH_MAP_ENTRY
*entry
);
207 * Tries to find if the @c int_id_ in @a entry is available for use.
209 bool is_entry_available_i (const HASH_MAP_ENTRY
&entry
);
212 * Tries to find if the @c int_id_ in @a entry is connect pending
214 bool is_entry_connecting_i (const HASH_MAP_ENTRY
&entry
);
217 * Tries to find if the @c int_id_ in @a entry is purgable
219 bool is_entry_purgable_i (HASH_MAP_ENTRY
&entry
);
221 #if !defined(ACE_LACKS_QSORT)
223 static int cpscmp(const void* a
, const void* b
);
226 typedef HASH_MAP_ENTRY
** DESCRIPTOR_SET
;
228 /// Sort the list of entries
229 void sort_set (DESCRIPTOR_SET
& entries
, int size
);
231 /// Fill sorted_set in with the transport_descriptor_type's in
233 int fill_set_i (DESCRIPTOR_SET
& sorted_set
);
235 /// Non-locking version of blockable_client_transports ().
236 bool blockable_client_transports_i (Connection_Handler_Set
&handlers
);
239 /// The percentage of the cache to purge at one time
242 /// The underlying connection purging strategy
243 purging_strategy
*purging_strategy_
;
245 /// The hash map that has the connections
248 TAO_SYNCH_MUTEX cache_map_mutex_
;
250 /// The lock that is used by the cache map
251 ACE_Lock
*cache_lock_
;
253 /// Maximum size of the cache
254 size_t cache_maximum_
;
256 #if defined (TAO_HAS_MONITOR_POINTS) && (TAO_HAS_MONITOR_POINTS == 1)
257 /// Connection cache purge monitor.
258 ACE::Monitor_Control::Size_Monitor
*purge_monitor_
;
260 /// Connection cache size monitor.
261 ACE::Monitor_Control::Size_Monitor
*size_monitor_
;
262 #endif /* TAO_HAS_MONITOR_POINTS==1 */
266 TAO_END_VERSIONED_NAMESPACE_DECL
268 #if defined (__ACE_INLINE__)
269 # include "tao/Transport_Cache_Manager_T.inl"
270 #endif /* __ACE_INLINE__ */
272 #include "tao/Transport_Cache_Manager_T.cpp"
274 #include /**/ "ace/post.h"
276 #endif /*TAO_CONNECTION_CACHE_MANAGER_T_H*/