44#include " env.h"
55#include " env-inl.h"
66#include " node.h"
7+ #include " node_mutex.h"
78#include " node_version.h"
89#include " v8-platform.h"
910#include " util.h"
@@ -189,9 +190,9 @@ class AgentImpl {
189190 void Write (const std::string& message);
190191
191192 uv_sem_t start_sem_;
192- uv_cond_t pause_cond_;
193- uv_mutex_t queue_lock_ ;
194- uv_mutex_t pause_lock_ ;
193+ ConditionVariable pause_cond_;
194+ Mutex pause_lock_ ;
195+ Mutex queue_lock_ ;
195196 uv_thread_t thread_;
196197 uv_loop_t child_loop_;
197198
@@ -290,9 +291,10 @@ class V8NodeInspector : public blink::V8Inspector {
290291 terminated_ = false ;
291292 running_nested_loop_ = true ;
292293 do {
293- uv_mutex_lock (&agent_->pause_lock_ );
294- uv_cond_wait (&agent_->pause_cond_ , &agent_->pause_lock_ );
295- uv_mutex_unlock (&agent_->pause_lock_ );
294+ {
295+ Mutex::ScopedLock scoped_lock (agent_->pause_lock_ );
296+ agent_->pause_cond_ .Wait (scoped_lock);
297+ }
296298 while (v8::platform::PumpMessageLoop (platform_, isolate_))
297299 {}
298300 } while (!terminated_);
@@ -321,19 +323,14 @@ AgentImpl::AgentImpl(Environment* env) : port_(0),
321323 inspector_(nullptr ),
322324 platform_(nullptr ),
323325 dispatching_messages_(false ) {
324- int err;
325- err = uv_sem_init (&start_sem_, 0 );
326- CHECK_EQ (err, 0 );
326+ CHECK_EQ (0 , uv_sem_init (&start_sem_, 0 ));
327327 memset (&data_written_, 0 , sizeof (data_written_));
328328 memset (&io_thread_req_, 0 , sizeof (io_thread_req_));
329329}
330330
331331AgentImpl::~AgentImpl () {
332332 if (!inspector_)
333333 return ;
334- uv_mutex_destroy (&queue_lock_);
335- uv_mutex_destroy (&pause_lock_);
336- uv_cond_destroy (&pause_cond_);
337334 uv_close (reinterpret_cast <uv_handle_t *>(&data_written_), nullptr );
338335}
339336
@@ -349,12 +346,6 @@ void AgentImpl::Start(v8::Platform* platform, int port, bool wait) {
349346 CHECK_EQ (err, 0 );
350347 err = uv_async_init (env->event_loop (), &data_written_, nullptr );
351348 CHECK_EQ (err, 0 );
352- err = uv_mutex_init (&queue_lock_);
353- CHECK_EQ (err, 0 );
354- err = uv_mutex_init (&pause_lock_);
355- CHECK_EQ (err, 0 );
356- err = uv_cond_init (&pause_cond_);
357- CHECK_EQ (err, 0 );
358349
359350 uv_unref (reinterpret_cast <uv_handle_t *>(&data_written_));
360351
@@ -441,6 +432,7 @@ void AgentImpl::OnRemoteDataIO(uv_stream_t* stream,
441432 const uv_buf_t * b) {
442433 inspector_socket_t * socket = static_cast <inspector_socket_t *>(stream->data );
443434 AgentImpl* agent = static_cast <AgentImpl*>(socket->data );
435+ Mutex::ScopedLock scoped_lock (agent->pause_lock_ );
444436 if (read > 0 ) {
445437 std::string str (b->base , read);
446438 agent->PushPendingMessage (&agent->message_queue_ , str);
@@ -470,21 +462,19 @@ void AgentImpl::OnRemoteDataIO(uv_stream_t* stream,
470462 }
471463 DisconnectAndDisposeIO (socket);
472464 }
473- uv_cond_broadcast (& agent->pause_cond_ );
465+ agent->pause_cond_ . Broadcast (scoped_lock );
474466}
475467
476468void AgentImpl::PushPendingMessage (std::vector<std::string>* queue,
477- const std::string& message) {
478- uv_mutex_lock (& queue_lock_);
469+ const std::string& message) {
470+ Mutex::ScopedLock scoped_lock ( queue_lock_);
479471 queue->push_back (message);
480- uv_mutex_unlock (&queue_lock_);
481472}
482473
483474void AgentImpl::SwapBehindLock (std::vector<std::string> AgentImpl::*queue,
484- std::vector<std::string>* output) {
485- uv_mutex_lock (& queue_lock_);
475+ std::vector<std::string>* output) {
476+ Mutex::ScopedLock scoped_lock ( queue_lock_);
486477 (this ->*queue).swap (*output);
487- uv_mutex_unlock (&queue_lock_);
488478}
489479
490480// static
0 commit comments