Replace verify_no_send_in_progress with no_parallel_execution_guard

This commit is contained in:
Sandip Patel 2019-12-02 12:04:28 +05:30
parent c49ce31201
commit ffbae79a48
2 changed files with 23 additions and 15 deletions

View file

@ -264,13 +264,13 @@ namespace graphene { namespace net
fc::future<void> accept_or_connect_task_done; fc::future<void> accept_or_connect_task_done;
firewall_check_state_data *firewall_check_state = nullptr; firewall_check_state_data *firewall_check_state = nullptr;
#ifndef NDEBUG
private: private:
#ifndef NDEBUG
fc::thread* _thread = nullptr; fc::thread* _thread = nullptr;
unsigned _send_message_queue_tasks_running = 0; // temporary debugging unsigned _send_message_queue_tasks_running = 0; // temporary debugging
#endif #endif
bool _currently_handling_message = false; // true while we're in the middle of handling a message from the remote system bool _currently_handling_message = false; // true while we're in the middle of handling a message from the remote system
private:
peer_connection(peer_connection_delegate* delegate); peer_connection(peer_connection_delegate* delegate);
void destroy(); void destroy();
public: public:

View file

@ -62,7 +62,8 @@ namespace graphene { namespace net {
fc::time_point _last_message_received_time; fc::time_point _last_message_received_time;
fc::time_point _last_message_sent_time; fc::time_point _last_message_sent_time;
bool _send_message_in_progress; std::atomic_bool _send_message_in_progress;
std::atomic_bool _read_loop_in_progress;
#ifndef NDEBUG #ifndef NDEBUG
fc::thread* _thread; fc::thread* _thread;
#endif #endif
@ -98,7 +99,8 @@ namespace graphene { namespace net {
_delegate(delegate), _delegate(delegate),
_bytes_received(0), _bytes_received(0),
_bytes_sent(0), _bytes_sent(0),
_send_message_in_progress(false) _send_message_in_progress(false),
_read_loop_in_progress(false)
#ifndef NDEBUG #ifndef NDEBUG
,_thread(&fc::thread::current()) ,_thread(&fc::thread::current())
#endif #endif
@ -138,6 +140,21 @@ namespace graphene { namespace net {
_sock.bind(local_endpoint); _sock.bind(local_endpoint);
} }
class no_parallel_execution_guard final
{
std::atomic_bool* _flag;
public:
explicit no_parallel_execution_guard(std::atomic_bool* flag) : _flag(flag)
{
bool expected = false;
FC_ASSERT( flag->compare_exchange_strong( expected, true ), "Only one thread at time can visit it");
}
~no_parallel_execution_guard()
{
*_flag = false;
}
};
void message_oriented_connection_impl::read_loop() void message_oriented_connection_impl::read_loop()
{ {
VERIFY_CORRECT_THREAD(); VERIFY_CORRECT_THREAD();
@ -145,6 +162,7 @@ namespace graphene { namespace net {
const int LEFTOVER = BUFFER_SIZE - sizeof(message_header); const int LEFTOVER = BUFFER_SIZE - sizeof(message_header);
static_assert(BUFFER_SIZE >= sizeof(message_header), "insufficient buffer"); static_assert(BUFFER_SIZE >= sizeof(message_header), "insufficient buffer");
no_parallel_execution_guard guard( &_read_loop_in_progress );
_connected_time = fc::time_point::now(); _connected_time = fc::time_point::now();
fc::oexception exception_to_rethrow; fc::oexception exception_to_rethrow;
@ -241,17 +259,7 @@ namespace graphene { namespace net {
} send_message_scope_logger(remote_endpoint); } send_message_scope_logger(remote_endpoint);
#endif #endif
#endif #endif
struct verify_no_send_in_progress { no_parallel_execution_guard guard( &_send_message_in_progress );
bool& var;
verify_no_send_in_progress(bool& var) : var(var)
{
if (var)
elog("Error: two tasks are calling message_oriented_connection::send_message() at the same time");
assert(!var);
var = true;
}
~verify_no_send_in_progress() { var = false; }
} _verify_no_send_in_progress(_send_message_in_progress);
try try
{ {