PipeConnection: Avoid deadlock when calling is_connected

Signed-off-by: Haomai Wang <haomaiwang@gmail.com>
This commit is contained in:
Haomai Wang 2014-12-07 12:27:38 +08:00
parent 9783a5cb64
commit 20ea086722
4 changed files with 28 additions and 6 deletions

View File

@ -92,6 +92,13 @@ public:
return NULL;
}
/**
* Used to judge whether this connection is ready to send. Usually, the
* implementation need to build a own shakehand or sesson then it can be
* ready to send.
*
* @return true if ready to send, or false otherwise
*/
virtual bool is_connected() = 0;
Messenger *get_messenger() {

View File

@ -67,11 +67,9 @@ void PipeConnection::reset_pipe(Pipe *p)
pipe = p->get();
}
bool PipeConnection::is_connected() {
Mutex::Locker l(lock);
if (pipe)
return pipe->is_connected();
return false;
bool PipeConnection::is_connected()
{
return static_cast<SimpleMessenger*>(msgr)->is_connected(this);
}
int PipeConnection::send_message(Message *m)

View File

@ -266,7 +266,19 @@ void SimpleMessenger::queue_reap(Pipe *pipe)
lock.Unlock();
}
bool SimpleMessenger::is_connected(Connection *con)
{
bool r = false;
if (con) {
Pipe *p = static_cast<Pipe *>(static_cast<PipeConnection*>(con)->get_pipe());
if (p) {
assert(p->msgr == this);
r = p->is_connected();
p->put();
}
}
return r;
}
int SimpleMessenger::bind(const entity_addr_t &bind_addr)
{

View File

@ -409,6 +409,11 @@ public:
* ready to be torn down.
*/
void queue_reap(Pipe *pipe);
/**
* Used to get whether this connection ready to send
*/
bool is_connected(Connection *con);
/**
* @} // SimpleMessenger Internals
*/