3 //=============================================================================
5 * @file Remote_Tokens.h
7 * @author Douglas C. Schmidt (d.schmidt@vanderbilt.edu)
8 * @author Tim Harrison (harrison@cs.wustl.edu)
10 //=============================================================================
12 #ifndef ACE_REMOTE_MUTEX_H
13 #define ACE_REMOTE_MUTEX_H
15 #include /**/ "ace/pre.h"
17 #include "ace/INET_Addr.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 #include "ace/SOCK_Connector.h"
24 #include "ace/SOCK_Stream.h"
25 #include "ace/Synch_Options.h"
26 #include "ace/Local_Tokens.h"
27 #include "ace/Token_Request_Reply.h"
29 #if defined (ACE_HAS_TOKENS_LIBRARY)
31 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
34 * @class ACE_Remote_Token_Proxy
36 * @brief Proxy for acquiring, renewing, and releasing a distributed
37 * synchronization token.
39 * The Remote_Token_Proxy class implements the mechanisms for
40 * distributed token operations. It is similar to the
43 * Distributed sleep_hooks have not been implemented. <owner_id>
46 class ACE_Export ACE_Remote_Token_Proxy
: public ACE_Token_Proxy
49 /// Null construction.
50 ACE_Remote_Token_Proxy ();
53 virtual ~ACE_Remote_Token_Proxy ();
56 * Same as Token_Proxy. @a name is the string uniquely identifying
57 * the token. @a ignore_deadlock can be 1 to disable deadlock
58 * notifications. @a debug prints debug messages.
60 int open (const ACE_TCHAR
*name
,
61 int ignore_deadlock
= 0,
66 * Open a connection with the token server. This only need be used
67 * when the user wishes to explicitly open a connection to check if
68 * the server exists. Connections are stored in the
69 * ACE_Token_Connections singleton as thread-specific data. That
70 * is, every thread has only one connection that is used for all
73 int initiate_connection ();
76 * Acquire the distributed token. If notify is specified and the
77 * token is already held, the owner is notified. options contains
78 * the timeout value for the acquire call. The timer is kept at the
79 * token server. Asynchronous operations are not supported.
80 * Returns 0 on success, -1 on failure with @c errno == problem.
82 virtual int acquire (int notify
= 0,
83 void (*sleep_hook
)(void *) = 0,
84 ACE_Synch_Options
&options
=
85 ACE_Synch_Options::synch
);
88 * Try to acquire the distributed token. If the token is already
89 * held, the call returns without queuing the caller as a waiter.
90 * Returns 0 on success (the token was acquired), and -1 with
91 * EWOULDBLOCK if the token was already held.
93 virtual int tryacquire (void (*sleep_hook
)(void *) = 0);
96 * Renew the token by offering to release it if there are any other
97 * waiters, otherwise get the token back immediately. This renew
98 * has the same semantics as ACE_Local_Mutex release. It is
99 * semantically equivalent to release() followed by acquire(), but
100 * it is faster. options contains the timeout value used if renew
101 * blocks. As with acquire, the timer is maintained at the token
102 * server. If there are waiters and requeue_position == -1, the
103 * caller is queued at the rear of the waiter list. Otherwise,
104 * requeue_position specifies the number of waiters to "let by"
105 * before reacquiring the token (effectively, the position in the
108 virtual int renew (int requeue_position
= 0,
109 ACE_Synch_Options
&options
=
110 ACE_Synch_Options::synch
);
113 * Release the distributed token. Similar to ACE_Local_Mutex, if the
114 * caller is not the owner, it is removed from the waiter list (if
115 * applicable.) Returns 0 on success, -1 on failure with @c errno ==
118 virtual int release (ACE_Synch_Options
&options
=
119 ACE_Synch_Options::synch
);
121 /// Become interface compliant for our guards. This has no
123 virtual int remove (ACE_Synch_Options
&options
= ACE_Synch_Options::synch
);
125 /// Override the default to do nothing.
126 virtual void token_acquired (ACE_TPQ_Entry
*);
128 /// The client id of the current token holder
129 virtual const ACE_TCHAR
* owner_id ();
132 * Sets the server address for all instances of ACE_Remote_Token_Proxy
133 * If this isn't called, the environment variable TOKEN_SERVER is
134 * checked for the server address. If that is not specified, all
135 * ACE_Remote_** operations will fail.
137 static void set_server_address (const ACE_INET_Addr
&server_address
);
139 /// Dump the state of the class.
143 /// If shadows report deadlock, go remote anyway
144 int ignore_shadow_deadlock_
;
146 /// Perform the request and wait for the reply.
147 int request_reply (ACE_Token_Request
&request
,
148 ACE_Synch_Options
&options
);
152 * @class ACE_Remote_Mutex
154 * @brief Proxy for acquiring, renewing, and releasing a distributed
157 * This is the remote equivalent to ACE_Local_Mutex. The
158 * Remote_Mutex class offers methods for acquiring, renewing, and
159 * releasing a distributed synchronization mutex. Similar to
160 * ACE_Local_Mutex, ACE_Remote_Token_Proxy offers recursive
161 * acquisition, FIFO waiter ordering, and deadlock detection. It
162 * depends on the Token Server for its distributed synchronization
165 class ACE_Export ACE_Remote_Mutex
: public ACE_Remote_Token_Proxy
168 /// Null creation. Remote_Token_Proxy::open must be called.
171 /// Calls Remote_Token_Proxy::open for you.
172 ACE_Remote_Mutex (const ACE_TCHAR
*token_name
,
173 int ignore_deadlock
= 0,
176 /// Dump the state of the class.
179 /// Return deep copy.
180 virtual ACE_Token_Proxy
*clone () const;
183 /// Make the correct type of ACE_Tokens.
184 /// This is called by the ACE_Token_Manager.
185 virtual ACE_Tokens
*create_token (const ACE_TCHAR
*name
);
189 * @class ACE_Remote_RLock
191 * @brief Proxy for acquiring, renewing, and releasing a distributed
194 * This is the remote equivalent to ACE_Local_RLock. Multiple
195 * readers can hold the lock simultaneously when no writers have
196 * the lock. Alternatively, when a writer holds the lock, no other
197 * participants (readers or writers) may hold the lock.
198 * ACE_Remote_RLock depends on the ACE Token Server for its
199 * distributed synchronization semantics.
201 class ACE_Export ACE_Remote_RLock
: public ACE_Remote_Token_Proxy
206 ACE_Remote_RLock (const ACE_TCHAR
*token_name
,
207 int ignore_deadlock
= 0,
210 ACE_Remote_RLock (const ACE_Remote_RLock
&mutex
);
212 /// Dump the state of the class.
215 /// Returns ACE_RW_Token::RLOCK;
216 virtual int type () const;
218 /// Return deep copy.
219 virtual ACE_Token_Proxy
*clone () const;
222 /// Make the correct type of ACE_Tokens. This is called by the Token
224 virtual ACE_Tokens
*create_token (const ACE_TCHAR
*name
);
228 * @class ACE_Remote_WLock
230 * @brief Proxy for acquiring, renewing, and releasing a distributed
233 * Shields applications from details of interacting with the
234 * ACE_Token_Server. The token_name_ is just the string that the
235 * Token Server uses to identify the token. The client_id_ (also
236 * used by the Token Server,) identifies the owner of the token and
237 * is used for deadlock detection.
239 class ACE_Export ACE_Remote_WLock
: public ACE_Remote_Token_Proxy
244 ACE_Remote_WLock (const ACE_TCHAR
*token_name
,
245 int ignore_deadlock
= 0,
248 ACE_Remote_WLock (const ACE_Remote_WLock
&mutex
);
250 /// Dump the state of the class.
253 /// Returns ACE_RW_Token::WLOCK;
254 virtual int type () const;
256 /// Return deep copy.
257 virtual ACE_Token_Proxy
*clone () const;
260 /// Make the correct type of ACE_Tokens. This is called by the Token
262 virtual ACE_Tokens
*create_token (const ACE_TCHAR
*name
);
266 * @class ACE_TSS_Connection
268 * @brief Class for providing a connection per thread.
270 * ACE_TSS_Connection provides a single access point for all
271 * threads to access thread-specific connections. This prevents
272 * resource-sharing problems such as thread serialization.
274 class ACE_Export ACE_TSS_Connection
: public ACE_TSS
<ACE_SOCK_Stream
>
277 // Necessary to make some compilers work...
278 ACE_TSS_Connection ();
279 ~ACE_TSS_Connection ();
281 /// Retrieve the thread's connection
282 ACE_SOCK_Stream
*get_connection ();
284 /// Factory Method that creates a new SOCK Stream.
285 virtual ACE_SOCK_Stream
*make_TSS_TYPE () const;
287 /// Inheritance and operator overloading don't mix. Redefine this
288 /// from ACE_TSS so that we can use it.
289 operator ACE_SOCK_Stream
*();
291 /// Set the server address.
292 static void set_server_address (const ACE_INET_Addr
&server_address
);
294 /// Dump the state of the class.
298 /// The address of the Token Server used by all instances of
300 static ACE_INET_Addr server_address_
;
303 ACE_TSS_Connection (const ACE_TSS_Connection
&) = delete;
304 void operator= (const ACE_TSS_Connection
&) = delete;
307 ACE_END_VERSIONED_NAMESPACE_DECL
309 #endif /* ACE_HAS_TOKENS_LIBRARY */
311 #if defined (__ACE_INLINE__)
312 #include "ace/Remote_Tokens.inl"
313 #endif /* __ACE_INLINE__ */
315 #include /**/ "ace/post.h"
317 #endif /* ACE_REMOTE_TOKEN_H */