@@ -46,17 +46,18 @@ class MTModule : public Module {
4646 s_next_mt_id_mutex.unlock ();
4747 state.is_executed = false ;
4848 state.request = data;
49- m_mt_states_mutex.lock ();
50- ASSERT ( m_mt_states.find ( mt_id ) == m_mt_states.end (), " duplicate mt_id" );
51- m_mt_states[ mt_id ] = state;
52- m_mt_states_mutex.unlock ();
49+ {
50+ std::lock_guard guard ( m_mt_states_mutex );
51+ ASSERT ( m_mt_states.find ( mt_id ) == m_mt_states.end (), " duplicate mt_id" );
52+ m_mt_states[ mt_id ] = state;
53+ }
5354 // Log( "MT Request " + to_string( mt_id ) + " created" );
5455 return mt_id;
5556 }
5657
5758 const RESPONSE_TYPE MT_GetResponse ( const mt_id_t mt_id ) {
59+ std::lock_guard guard ( m_mt_states_mutex );
5860 RESPONSE_TYPE response = {};
59- m_mt_states_mutex.lock ();
6061 auto it = m_mt_states.find ( mt_id );
6162 if ( it == m_mt_states.end () ) {
6263 return response;
@@ -67,8 +68,6 @@ class MTModule : public Module {
6768 m_mt_states.erase ( it );
6869 // Log( "MT Request " + to_string( mt_id ) + " result returned" );
6970 }
70- m_mt_states_mutex.unlock ();
71-
7271 return response;
7372 }
7473
@@ -78,7 +77,7 @@ class MTModule : public Module {
7877 }
7978
8079 void MT_Cancel ( const mt_id_t mt_id ) {
81- m_mt_states_mutex. lock ( );
80+ std::lock_guard guard ( m_mt_states_mutex );
8281 auto it = m_mt_states.find ( mt_id );
8382 if ( it == m_mt_states.end () ) {
8483 return ; // already gone
@@ -91,7 +90,7 @@ class MTModule : public Module {
9190 Log ( " Waiting for MT Request " + std::to_string ( mt_id ) + " to finish" );
9291 while ( it->second .is_processing ) {
9392 m_mt_states_mutex.unlock ();
94- std::this_thread::sleep_for ( std::chrono::milliseconds ( 1 ) );
93+ std::this_thread::sleep_for ( std::chrono::milliseconds ( 10 ) );
9594 m_mt_states_mutex.lock ();
9695 }
9796 }
@@ -100,7 +99,6 @@ class MTModule : public Module {
10099 DestroyResponse ( it->second .response );
101100 m_mt_states.erase ( it );
102101 }
103- m_mt_states_mutex.unlock ();
104102 }
105103
106104protected:
@@ -116,17 +114,21 @@ class MTModule : public Module {
116114 // Log( "MT Processing " + to_string( requests.size() ) + " requests" );
117115 mt_response_map_t responses = {};
118116 for ( auto & request : requests ) {
119- m_mt_states_mutex.lock ();
120- const mt_id_t previous_request_id = m_current_request_id;
121- const bool was_canceled = m_is_canceled;
122- m_current_request_id = request.first ;
123- m_is_canceled = false ;
124- m_mt_states_mutex.unlock ();
117+ mt_id_t previous_request_id = 0 ;
118+ bool was_canceled = false ;
119+ {
120+ std::lock_guard guard ( m_mt_states_mutex );
121+ previous_request_id = m_current_request_id;
122+ was_canceled = m_is_canceled;
123+ m_current_request_id = request.first ;
124+ m_is_canceled = false ;
125+ }
125126 responses[ request.first ] = ProcessRequest ( request.second , m_is_canceled );
126- m_mt_states_mutex.lock ();
127- m_current_request_id = previous_request_id;
128- m_is_canceled = was_canceled;
129- m_mt_states_mutex.unlock ();
127+ {
128+ std::lock_guard guard ( m_mt_states_mutex );
129+ m_current_request_id = previous_request_id;
130+ m_is_canceled = was_canceled;
131+ }
130132 }
131133 if ( !responses.empty () ) {
132134 MT_SetResponses ( responses );
@@ -146,21 +148,20 @@ class MTModule : public Module {
146148 typedef std::map< mt_id_t , RESPONSE_TYPE > mt_response_map_t ;
147149
148150 mt_request_map_t MT_GetRequests () {
151+ std::lock_guard guard ( m_mt_states_mutex );
149152 mt_request_map_t result = {};
150- m_mt_states_mutex.lock ();
151153 for ( auto & it : m_mt_states ) {
152154 if ( !it.second .is_executed && !it.second .is_processing ) {
153155 it.second .is_processing = true ;
154156 result[ it.first ] = it.second .request ;
155157 }
156158 }
157- m_mt_states_mutex.unlock ();
158159 return result;
159160 }
160161
161162 void MT_SetResponses ( const mt_response_map_t & responses ) {
162163 if ( !responses.empty () ) {
163- m_mt_states_mutex. lock ( );
164+ std::lock_guard guard ( m_mt_states_mutex );
164165 for ( auto & response : responses ) {
165166 auto it = m_mt_states.find ( response.first );
166167 ASSERT ( it != m_mt_states.end (), " invalid response mt_id" );
@@ -170,7 +171,6 @@ class MTModule : public Module {
170171 it->second .is_processing = false ;
171172 // Log( "MT Request " + to_string( response.first ) + " executed" );
172173 }
173- m_mt_states_mutex.unlock ();
174174 }
175175 }
176176
0 commit comments