OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
7 | 7 |
8 #include "bin/dbg_connection.h" | 8 #include "bin/dbg_connection.h" |
9 | 9 |
10 #include "bin/eventhandler.h" | 10 #include "bin/eventhandler.h" |
11 | 11 #include "bin/lockers.h" |
12 #include "bin/log.h" | |
13 #include "bin/thread.h" | |
12 | 14 |
13 namespace dart { | 15 namespace dart { |
14 namespace bin { | 16 namespace bin { |
15 | 17 |
18 Monitor* DebuggerConnectionImpl::handler_monitor_ = new Monitor(); | |
19 ThreadId DebuggerConnectionImpl::handler_thread_id_ = Thread::kInvalidThreadId; | |
20 bool DebuggerConnectionImpl::handler_thread_running_ = false; | |
21 | |
22 | |
23 void DebuggerConnectionImpl::NotifyThreadStarted() { | |
24 MonitorLocker ml(handler_monitor_); | |
Ivan Posva
2015/08/25 22:21:03
Assert that thread is not running and that the id
zra
2015/08/26 05:24:39
Done.
| |
25 handler_thread_running_ = true; | |
26 handler_thread_id_ = Thread::GetCurrentThreadId(); | |
27 ml.Notify(); | |
28 } | |
29 | |
30 | |
31 void DebuggerConnectionImpl::WaitForThreadStarted() { | |
32 MonitorLocker ml(handler_monitor_); | |
33 while (!handler_thread_running_) { | |
34 ml.Wait(); | |
35 } | |
36 ASSERT(handler_thread_id_ != Thread::kInvalidThreadId); | |
37 } | |
38 | |
39 | |
40 void DebuggerConnectionImpl::NotifyThreadFinished() { | |
41 MonitorLocker ml(handler_monitor_); | |
42 handler_thread_running_ = false; | |
Ivan Posva
2015/08/25 22:21:03
Assert that thread is running beforehand and that
zra
2015/08/26 05:24:39
Done.
| |
43 ml.Notify(); | |
44 } | |
45 | |
46 | |
47 void DebuggerConnectionImpl::WaitForThreadFinished() { | |
48 MonitorLocker ml(handler_monitor_); | |
49 while (handler_thread_running_) { | |
50 ml.Wait(); | |
51 } | |
52 ASSERT(handler_thread_id_ != Thread::kInvalidThreadId); | |
53 Thread::Join(handler_thread_id_); | |
54 handler_thread_id_ = Thread::kInvalidThreadId; | |
55 } | |
56 | |
57 | |
16 void DebuggerConnectionImpl::ThreadEntry(uword args) { | 58 void DebuggerConnectionImpl::ThreadEntry(uword args) { |
59 NotifyThreadStarted(); | |
17 ListenSocket* listen_socket = | 60 ListenSocket* listen_socket = |
18 reinterpret_cast<ListenSocket*>(DebuggerConnectionHandler::listener_fd_); | 61 reinterpret_cast<ListenSocket*>(DebuggerConnectionHandler::listener_fd_); |
19 SOCKET client_socket = accept(listen_socket->socket(), NULL, NULL); | 62 SOCKET client_socket = accept(listen_socket->socket(), NULL, NULL); |
20 if (client_socket == INVALID_SOCKET) { | 63 if (client_socket == INVALID_SOCKET) { |
21 FATAL("Accepting new debugger connection failed.\n"); | 64 FATAL("Accepting new debugger connection failed.\n"); |
22 } | 65 } |
23 ClientSocket* socket = new ClientSocket(client_socket); | 66 ClientSocket* socket = new ClientSocket(client_socket); |
24 DebuggerConnectionHandler::AcceptDbgConnection( | 67 DebuggerConnectionHandler::AcceptDbgConnection( |
25 reinterpret_cast<intptr_t>(socket)); | 68 reinterpret_cast<intptr_t>(socket)); |
69 NotifyThreadFinished(); | |
26 } | 70 } |
27 | 71 |
28 | 72 |
29 void DebuggerConnectionImpl::StartHandler(int port_number) { | 73 void DebuggerConnectionImpl::StartHandler(int port_number) { |
30 ASSERT(DebuggerConnectionHandler::listener_fd_ != -1); | 74 ASSERT(DebuggerConnectionHandler::listener_fd_ != -1); |
31 int result = Thread::Start(&DebuggerConnectionImpl::ThreadEntry, 0); | 75 int result = Thread::Start(&DebuggerConnectionImpl::ThreadEntry, 0); |
32 if (result != 0) { | 76 if (result != 0) { |
33 FATAL1("Failed to start debugger connection handler thread: %d\n", result); | 77 FATAL1("Failed to start debugger connection handler thread: %d\n", result); |
34 } | 78 } |
79 WaitForThreadStarted(); | |
35 } | 80 } |
36 | 81 |
37 | 82 |
83 void DebuggerConnectionImpl::StopHandler(intptr_t debug_fd) { | |
84 Send(debug_fd, NULL, 0); | |
85 WaitForThreadFinished(); | |
86 } | |
87 | |
88 | |
38 intptr_t DebuggerConnectionImpl::Send(intptr_t socket, | 89 intptr_t DebuggerConnectionImpl::Send(intptr_t socket, |
39 const char* buf, | 90 const char* buf, |
40 int len) { | 91 int len) { |
41 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(socket); | 92 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(socket); |
42 return send(client_socket->socket(), buf, len, 0); | 93 return send(client_socket->socket(), buf, len, 0); |
43 } | 94 } |
44 | 95 |
45 | 96 |
46 intptr_t DebuggerConnectionImpl::Receive(intptr_t socket, char* buf, int len) { | 97 intptr_t DebuggerConnectionImpl::Receive(intptr_t socket, char* buf, int len) { |
47 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(socket); | 98 ClientSocket* client_socket = reinterpret_cast<ClientSocket*>(socket); |
48 return recv(client_socket->socket(), buf, len, 0); | 99 return recv(client_socket->socket(), buf, len, 0); |
49 } | 100 } |
50 | 101 |
51 } // namespace bin | 102 } // namespace bin |
52 } // namespace dart | 103 } // namespace dart |
53 | 104 |
54 #endif // defined(TARGET_OS_WINDOWS) | 105 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |