提交 35defdff 编写于 作者: Y youngwolf 提交者: youngwolf

Introduce macro ASCS_USE_DISPATCH_IN_IO to force to use dispatch_strand in IO operations.

For recv_msg, go back to dispatch_strand if macro ASCS_PASSIVE_RECV is not defined because it
 cannot be invoked concurrently without the macro.
上级 501a4c47
Subproject commit 5302adea05c8a1c5d3923fa93afd7cf437df5937
Subproject commit 9902fe7d1d6b5321f4ab2f4ddbfd19cf39160891
......@@ -811,6 +811,8 @@
* Graceful shutdown does not support sync mode anymore.
* Use post_strand instead of dispatch_strand in send_msg and recv_msg, because we don't synchronize socket's member variable sending and reading,
* there's still a race condition even in the same strand because of memory synchronization.
* But we can difine macro ASCS_USE_DISPATCH_IN_IO to use dispatch_strand back, because dispatch_strand can be more efficient in specific cases.
* The macro will be removed after I confirmed that dispatch_strand indeed introduces race condition.
*
* HIGHLIGHT:
* Make shutdown thread safe.
......
......@@ -168,20 +168,32 @@ public:
#ifdef ASCS_PASSIVE_RECV
bool is_reading() const {return reading;}
#ifdef ASCS_USE_DISPATCH_IN_IO
void recv_msg() {if (!reading && is_ready()) dispatch_in_io_strand([this]() {this->do_recv_msg();});}
#else
void recv_msg() {if (!reading && is_ready()) post_in_io_strand([this]() {this->do_recv_msg();});}
#endif
#else
private:
void recv_msg() {post_in_io_strand([this]() {this->do_recv_msg();});}
void recv_msg() {dispatch_in_io_strand([this]() {this->do_recv_msg();});}
public:
#endif
#ifndef ASCS_EXPOSE_SEND_INTERFACE
protected:
#endif
#ifdef ASCS_USE_DISPATCH_IN_IO
#ifdef ASCS_ARBITRARY_SEND
void send_msg() {dispatch_in_io_strand([this]() {this->do_send_msg();});}
#else
void send_msg() {if (!sending && is_ready()) dispatch_in_io_strand([this]() {this->do_send_msg();});}
#endif
#else
#ifdef ASCS_ARBITRARY_SEND
void send_msg() {post_in_io_strand([this]() {this->do_send_msg();});}
#else
void send_msg() {if (!sending && is_ready()) post_in_io_strand([this]() {this->do_send_msg();});}
#endif
#endif
public:
void start_heartbeat(int interval, int max_absence = ASCS_HEARTBEAT_MAX_ABSENCE)
......@@ -664,7 +676,7 @@ private:
return false;
}
//do not use dispatch_strand at here, because the handler (do_dispatch_msg) may call this function, which can lead stack overflow.
//do not use dispatch_strand/dispatch_in_dis_strand at here, because the handler (do_dispatch_msg) may call this function, which can lead stack overflow.
void dispatch_msg() {if (!dispatching) post_in_dis_strand([this]() {this->do_dispatch_msg();});}
void do_dispatch_msg()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册