1 #include "ace/Remote_Tokens.h"
3 #if defined (ACE_HAS_TOKENS_LIBRARY)
5 #include "ace/Singleton.h"
7 #if !defined (__ACE_INLINE__)
8 #include "ace/Remote_Tokens.inl"
9 #endif /* __ACE_INLINE__ */
11 #if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
12 #define ACE_TSS_CONNECTION_MUTEX ACE_Thread_Mutex
14 #define ACE_TSS_CONNECTION_MUTEX ACE_Null_Mutex
15 #endif /* ACE_MT_SAFE */
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 // Make a typedef to simplify access to the Singleton below.
20 typedef ACE_Singleton
<ACE_TSS_Connection
, ACE_TSS_CONNECTION_MUTEX
> ACE_Token_Connections
;
22 // Initialize the statics from ACE_TSS_Connection;
23 ACE_INET_Addr
ACE_TSS_Connection::server_address_
;
25 // ************************************************************
28 ACE_TSS_Connection::set_server_address (const ACE_INET_Addr
&server_address
)
30 ACE_TRACE ("ACE_TSS_Connection::set_server_address");
31 server_address_
= server_address
;
34 // Necessary to make some compilers work...
35 ACE_TSS_Connection::ACE_TSS_Connection ()
37 ACE_TRACE ("ACE_TSS_Connection::ACE_TSS_Connection");
40 ACE_TSS_Connection::~ACE_TSS_Connection ()
42 ACE_TRACE ("ACE_TSS_Connection::~ACE_TSS_Connection");
46 ACE_TSS_Connection::get_connection ()
48 return ACE_TSS
<ACE_SOCK_Stream
>::operator-> ();
52 ACE_TSS_Connection::make_TSS_TYPE () const
54 ACE_TRACE ("ACE_TSS_Connection::make_TSS_TYPE");
56 ACE_SOCK_Connector connector
;
57 ACE_SOCK_Stream
*stream
= 0;
59 ACE_NEW_RETURN (stream
,
63 if (connector
.connect (*stream
, server_address_
) == -1)
70 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("ACE_TSS_Connection new connection\n")));
74 ACE_TSS_Connection::operator ACE_SOCK_Stream
*()
76 return this->get_connection ();
80 ACE_TSS_Connection::dump () const
82 #if defined (ACE_HAS_DUMP)
83 ACE_TRACE ("ACE_TSS_Connection::dump");
84 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
85 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("ACE_TSS_Connection::dump:\n")));
86 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("server_address_\n")));
87 server_address_
.dump ();
88 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("base:\n")));
89 ACE_TSS
<ACE_SOCK_Stream
>::dump ();
90 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
91 #endif /* ACE_HAS_DUMP */
94 ACE_Remote_Token_Proxy::ACE_Remote_Token_Proxy ()
96 ACE_TRACE ("ACE_Remote_Token_Proxy::ACE_Remote_Token_Proxy");
99 ACE_Remote_Token_Proxy::~ACE_Remote_Token_Proxy ()
101 ACE_TRACE ("ACE_Remote_Token_Proxy::~ACE_Remote_Token_Proxy");
105 ACE_Remote_Token_Proxy::open (const ACE_TCHAR
*name
,
109 ACE_TRACE ("ACE_Remote_Token_Proxy::open");
110 ignore_shadow_deadlock_
= ignore_deadlock
;
111 return ACE_Token_Proxy::open (name
, 0, debug
);
115 ACE_Remote_Token_Proxy::set_server_address (const ACE_INET_Addr
&server_address
)
117 ACE_TRACE ("ACE_Remote_Token_Proxy::set_server_address");
118 ACE_Token_Connections::instance ()->set_server_address (server_address
);
122 ACE_Remote_Token_Proxy::initiate_connection ()
124 ACE_TRACE ("ACE_Remote_Token_Proxy::initiate_connection");
128 ACELIB_ERROR_RETURN ((LM_ERROR
,
129 ACE_TEXT ("ACE_Remote_Token_Proxy not open.\n")), -1);
132 ACE_SOCK_Stream
*peer
= ACE_Token_Connections::instance ()->get_connection ();
133 return peer
== 0 ? 0 : 1;
136 // Do the work of sending a request and getting a reply.
139 ACE_Remote_Token_Proxy::request_reply (ACE_Token_Request
&request
,
142 ACE_TRACE ("ACE_Remote_Token_Proxy::request_reply");
146 if ((length
= request
.encode (buffer
)) == -1)
147 ACELIB_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("encode failed")), -1);
149 ACE_SOCK_Stream
*peer
= ACE_Token_Connections::instance ()->get_connection ();
152 ACELIB_ERROR_RETURN ((LM_ERROR
,
153 ACE_TEXT("(%P|%t) %p\n"),
154 ACE_TEXT("BIG PROBLEMS with get_connection")), -1);
156 // Transmit request via a blocking send.
158 if (peer
->send_n (buffer
, length
) != length
)
159 ACELIB_ERROR_RETURN ((LM_ERROR
,
161 ACE_TEXT ("send_n failed")), -1);
164 ACE_Token_Reply reply
;
166 // Receive reply via blocking read.
168 if (peer
->recv (&reply
, sizeof reply
) != sizeof reply
)
169 ACELIB_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("recv failed")), -1);
171 if (reply
.decode () == -1)
172 ACELIB_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("decode failed")), -1);
174 errno
= int (reply
.errnum ());
183 ACE_Remote_Token_Proxy::acquire (int notify
,
184 void (*sleep_hook
)(void *),
185 ACE_Synch_Options
&options
)
187 ACE_TRACE ("ACE_Remote_Token_Proxy::acquire");
189 // First grab the local shadow mutex.
190 if (ACE_Token_Proxy::acquire (notify
,
192 ACE_Synch_Options::asynch
) == -1)
194 // Acquire failed, deal with it...
198 // Whoah, we detected wouldblock via the shadow mutex!
200 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) shadow: acquire will block, owner is %s\n"),
201 this->token_
->owner_id ()));
202 // No error, but would block,
207 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) shadow: deadlock detected\n")));
209 if (ignore_shadow_deadlock_
)
218 ACELIB_ERROR_RETURN ((LM_ERROR
,
219 ACE_TEXT ("(%t) %p shadow acquire failed\n"),
220 ACE_TEXT ("ACE_Remote_Token_Proxy")),
225 ACE_Token_Request
request (token_
->type (),
227 ACE_Token_Request::ACQUIRE
,
232 request
.notify (notify
);
234 int result
= this->request_reply (request
, options
);
238 // Update the local shadow copy.
239 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("error on remote acquire, releasing shadow mutex.\n")));
240 ACE_Token_Proxy::release ();
244 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) acquired %s remotely.\n"), this->name ()));
245 // Our shadow call may have failed. However, it's still a race
246 // to the remote server. If we beat the client which holds the
247 // local token, we need to fix things locally to reflect the
248 // actual ownership. All that should happen is that our waiter
249 // is moved to the front of the waiter list.
250 token_
->make_owner (waiter_
);
257 ACE_Remote_Token_Proxy::tryacquire (void (*sleep_hook
)(void *))
259 ACE_TRACE ("ACE_Remote_Token_Proxy::tryacquire");
261 // If we can detect locally that the tryacquire will fail, there is
262 // no need to go remote.
263 if (ACE_Token_Proxy::tryacquire (sleep_hook
) == -1)
267 // Save/restore errno.
268 ACE_Errno_Guard
error (errno
);
269 ACELIB_DEBUG ((LM_DEBUG
,
270 ACE_TEXT ("shadow try acquire failed\n")));
276 ACE_Token_Request
request (token_
->type (),
278 ACE_Token_Request::TRY_ACQUIRE
,
281 ACE_Synch_Options::synch
);
283 return this->request_reply (request
,
284 ACE_Synch_Options::synch
);
288 ACE_Remote_Token_Proxy::renew (int requeue_position
,
289 ACE_Synch_Options
&options
)
291 ACE_TRACE ("ACE_Remote_Token_Proxy::renew");
293 if (ACE_Token_Proxy::renew (requeue_position
,
294 ACE_Synch_Options::asynch
) == -1)
297 if (errno
!= EWOULDBLOCK
)
300 ACELIB_DEBUG ((LM_DEBUG
,
301 ACE_TEXT ("(%t) shadow: renew would block. owner %s.\n"),
302 this->token_
->owner_id ()));
305 ACE_Token_Request
request (token_
->type (),
307 ACE_Token_Request::RENEW
,
312 request
.requeue_position (requeue_position
);
314 int result
= this->request_reply (request
, options
);
319 // Save/restore errno.
320 ACE_Errno_Guard
error (errno
);
321 ACE_Token_Proxy::release ();
323 ACELIB_ERROR_RETURN ((LM_ERROR
,
324 ACE_TEXT ("%p error on remote renew, releasing shadow mutex.\n"),
325 ACE_TEXT ("ACE_Remote_Token_Proxy")), -1);
330 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) renewed %s remotely.\n"), this->name ()));
331 // Make sure that the local shadow reflects our new ownership.
332 token_
->make_owner (waiter_
);
338 ACE_Remote_Token_Proxy::release (ACE_Synch_Options
&options
)
340 ACE_TRACE ("ACE_Remote_Token_Proxy::release");
342 ACE_Token_Request
request (token_
->type (),
344 ACE_Token_Request::RELEASE
,
349 int result
= this->request_reply (request
, options
);
351 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) released %s remotely.\n"), this->name ()));
353 // whether success or failure, we're going to release the shadow.
354 // If race conditions exist such that we are no longer the owner,
355 // this release will perform a remove.
356 if (ACE_Token_Proxy::release () == -1)
357 ACELIB_ERROR ((LM_ERROR
, ACE_TEXT ("(%t) shadow: release failed\n")));
363 ACE_Remote_Token_Proxy::remove (ACE_Synch_Options
&)
365 ACE_TRACE ("ACE_Remote_Token_Proxy::remove");
370 ACE_Remote_Token_Proxy::token_acquired (ACE_TPQ_Entry
*)
372 ACE_TRACE ("ACE_Remote_Token_Proxy::token_acquired");
373 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("(%t) %s shadow token %s acquired\n"),
376 // ACE_Token_Proxy::token_acquired (vp);
380 ACE_Remote_Token_Proxy::owner_id ()
382 ACE_TRACE ("ACE_Remote_Token_Proxy::owner_id");
383 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("owner_id called\n")));
384 // @@ special operation
389 ACE_Remote_Token_Proxy::dump () const
391 #if defined (ACE_HAS_DUMP)
392 ACE_TRACE ("ACE_Remote_Token_Proxy::dump");
393 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
394 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("ACE_Tokens::dump:\n")
395 ACE_TEXT (" ignore_shadow_deadlock_ = %d\n"),
396 ignore_shadow_deadlock_
));
397 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("base:\n")));
398 ACE_Token_Proxy::dump ();
399 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
400 #endif /* ACE_HAS_DUMP */
404 ACE_Remote_Mutex::clone () const
406 ACE_Token_Proxy
*temp
;
407 ACE_NEW_RETURN (temp
,
408 ACE_Remote_Mutex (this->name (),
416 ACE_Remote_Mutex::create_token (const ACE_TCHAR
*name
)
419 ACE_NEW_RETURN (temp
,
420 ACE_Mutex_Token (name
),
426 ACE_Remote_Mutex::dump () const
428 #if defined (ACE_HAS_DUMP)
429 ACE_TRACE ("ACE_Remote_Mutex::dump");
430 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
431 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("ACE_Remote_Mutex::dump:\n")));
432 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("base:\n")));
433 ACE_Remote_Token_Proxy::dump ();
434 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
435 #endif /* ACE_HAS_DUMP */
439 ACE_Remote_RLock::create_token (const ACE_TCHAR
*name
)
441 ACE_Tokens
*temp
= 0;
442 ACE_NEW_RETURN (temp
,
449 ACE_Remote_RLock::type () const
451 return ACE_RW_Token::READER
;
455 ACE_Remote_RLock::clone () const
457 ACE_Token_Proxy
*temp
= 0;
458 ACE_NEW_RETURN (temp
,
459 ACE_Remote_RLock (this->name (),
467 ACE_Remote_RLock::dump () const
469 #if defined (ACE_HAS_DUMP)
470 ACE_TRACE ("ACE_Remote_RLock::dump");
471 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
472 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("ACE_Remote_RLock::dump:\n")));
473 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("base:\n")));
474 ACE_Remote_Token_Proxy::dump ();
475 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
476 #endif /* ACE_HAS_DUMP */
481 ACE_Remote_WLock::create_token (const ACE_TCHAR
*name
)
483 ACE_Tokens
*temp
= 0;
484 ACE_NEW_RETURN (temp
,
491 ACE_Remote_WLock::type () const
493 return ACE_RW_Token::WRITER
;
497 ACE_Remote_WLock::clone () const
499 ACE_Token_Proxy
*temp
= 0;
500 ACE_NEW_RETURN (temp
,
501 ACE_Remote_WLock (this->name (),
509 ACE_Remote_WLock::dump () const
511 #if defined (ACE_HAS_DUMP)
512 ACE_TRACE ("ACE_Remote_WLock::dump");
513 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
514 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("ACE_Remote_WLock::dump:\n")));
515 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("base:\n")));
516 ACE_Remote_Token_Proxy::dump ();
517 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
518 #endif /* ACE_HAS_DUMP */
521 ACE_END_VERSIONED_NAMESPACE_DECL
523 #endif /* ACE_HAS_TOKENS_LIBRARY */