1818#include < sys/types.h>
1919#include < sys/uio.h>
2020#include < unistd.h>
21- #include < thread>
2221
2322using namespace lldb ;
2423using namespace lldb_private ;
@@ -95,41 +94,6 @@ bool LLDBServerPluginMockGPU::HandleEventFileDescriptorEvent(int fd) {
9594 return false ;
9695}
9796
98- void LLDBServerPluginMockGPU::AcceptAndMainLoopThread (
99- std::unique_ptr<TCPSocket> listen_socket_up) {
100- Log *log = GetLog (GDBRLog::Plugin);
101- LLDB_LOGF (log, " %s spawned" , __PRETTY_FUNCTION__);
102- Socket *socket = nullptr ;
103- Status error = listen_socket_up->Accept (std::chrono::seconds (30 ), socket);
104- // Scope for lock guard.
105- {
106- // Protect access to m_is_listening and m_is_connected.
107- std::lock_guard<std::mutex> guard (m_connect_mutex);
108- m_is_listening = false ;
109- if (error.Fail ()) {
110- LLDB_LOGF (log, " %s error returned from Accept(): %s" , __PRETTY_FUNCTION__,
111- error.AsCString ());
112- return ;
113- }
114- m_is_connected = true ;
115- }
116-
117- LLDB_LOGF (log, " %s initializing connection" , __PRETTY_FUNCTION__);
118- std::unique_ptr<Connection> connection_up (
119- new ConnectionFileDescriptor (std::unique_ptr<Socket>(socket)));
120- m_gdb_server->InitializeConnection (std::move (connection_up));
121- LLDB_LOGF (log, " %s running main loop" , __PRETTY_FUNCTION__);
122- m_main_loop_status = m_main_loop.Run ();
123- LLDB_LOGF (log, " %s main loop exited!" , __PRETTY_FUNCTION__);
124- if (m_main_loop_status.Fail ()) {
125- LLDB_LOGF (log, " %s main loop exited with an error: %s" , __PRETTY_FUNCTION__,
126- m_main_loop_status.AsCString ());
127-
128- }
129- // Protect access to m_is_connected.
130- std::lock_guard<std::mutex> guard (m_connect_mutex);
131- m_is_connected = false ;
132- }
13397
13498std::optional<GPUPluginConnectionInfo> LLDBServerPluginMockGPU::CreateConnection () {
13599 std::lock_guard<std::mutex> guard (m_connect_mutex);
@@ -153,9 +117,20 @@ std::optional<GPUPluginConnectionInfo> LLDBServerPluginMockGPU::CreateConnection
153117 connection_info.connect_url = llvm::formatv (" connect://localhost:{}" ,
154118 listen_port);
155119 LLDB_LOGF (log, " %s listening to %u" , __PRETTY_FUNCTION__, listen_port);
156- std::thread t (&LLDBServerPluginMockGPU::AcceptAndMainLoopThread, this ,
157- std::move (*sock));
158- t.detach ();
120+
121+ m_listen_socket = std::move (*sock);
122+ m_is_connected = false ;
123+ auto res = m_listen_socket->Accept (m_main_loop, [this ](std::unique_ptr<Socket> socket) {
124+ std::unique_ptr<Connection> connection_up (new ConnectionFileDescriptor (std::move (socket)));
125+ this ->m_gdb_server ->InitializeConnection (std::move (connection_up));
126+ m_is_connected = true ;
127+ });
128+ if (res) {
129+ m_read_handles = std::move (*res);
130+ } else {
131+ LLDB_LOGF (log, " MockGPU failed to accept: %s" , llvm::toString (res.takeError ()).c_str ());
132+ }
133+
159134 return connection_info;
160135 } else {
161136 std::string error = llvm::toString (sock.takeError ());
0 commit comments