25
25
#include < kj/memory.h>
26
26
#include < map>
27
27
#include < memory>
28
- #include < mutex>
29
28
#include < optional>
30
29
#include < stdexcept>
31
30
#include < string>
@@ -305,15 +304,15 @@ bool EventLoop::done() const
305
304
return m_num_clients == 0 && m_async_fns->empty ();
306
305
}
307
306
308
- std::tuple<ConnThread, bool > SetThread (ConnThreads& threads, std::mutex& mutex , Connection* connection, const std::function<Thread::Client()>& make_thread)
307
+ std::tuple<ConnThread, bool > SetThread (GuardedRef< ConnThreads> threads, Connection* connection, const std::function<Thread::Client()>& make_thread)
309
308
{
310
- const std::unique_lock<std::mutex> lock (mutex);
311
- auto thread = threads.find (connection);
312
- if (thread != threads.end ()) return {thread, false };
313
- thread = threads.emplace (
309
+ const Lock lock (threads. mutex );
310
+ auto thread = threads.ref . find (connection);
311
+ if (thread != threads.ref . end ()) return {thread, false };
312
+ thread = threads.ref . emplace (
314
313
std::piecewise_construct, std::forward_as_tuple (connection),
315
314
std::forward_as_tuple (make_thread (), connection, /* destroy_connection= */ false )).first ;
316
- thread->second .setDisconnectCallback ([& threads, &mutex , thread] {
315
+ thread->second .setDisconnectCallback ([threads, thread] {
317
316
// Note: it is safe to use the `thread` iterator in this cleanup
318
317
// function, because the iterator would only be invalid if the map entry
319
318
// was removed, and if the map entry is removed the ProxyClient<Thread>
@@ -323,9 +322,9 @@ std::tuple<ConnThread, bool> SetThread(ConnThreads& threads, std::mutex& mutex,
323
322
// thread client m_disconnect_cb member so thread client destructor does not
324
323
// try to unregister this callback after connection is destroyed.
325
324
// Remove connection pointer about to be destroyed from the map
326
- const std::unique_lock<std::mutex> lock (mutex);
325
+ const Lock lock (threads. mutex );
327
326
thread->second .m_disconnect_cb .reset ();
328
- threads.erase (thread);
327
+ threads.ref . erase (thread);
329
328
});
330
329
return {thread, true };
331
330
}
@@ -364,7 +363,7 @@ ProxyServer<Thread>::~ProxyServer()
364
363
assert (m_thread_context.waiter .get ());
365
364
std::unique_ptr<Waiter> waiter;
366
365
{
367
- const std::unique_lock<std::mutex> lock (m_thread_context.waiter ->m_mutex );
366
+ const Lock lock (m_thread_context.waiter ->m_mutex );
368
367
// ! Reset thread context waiter pointer, as shutdown signal for done
369
368
// ! lambda passed as waiter->wait() argument in makeThread code below.
370
369
waiter = std::move (m_thread_context.waiter );
@@ -398,7 +397,7 @@ kj::Promise<void> ProxyServer<ThreadMap>::makeThread(MakeThreadContext context)
398
397
g_thread_context.thread_name = ThreadName (m_connection.m_loop ->m_exe_name ) + " (from " + from + " )" ;
399
398
g_thread_context.waiter = std::make_unique<Waiter>();
400
399
thread_context.set_value (&g_thread_context);
401
- std::unique_lock<std::mutex> lock (g_thread_context.waiter ->m_mutex );
400
+ Lock lock (g_thread_context.waiter ->m_mutex );
402
401
// Wait for shutdown signal from ProxyServer<Thread> destructor (signal
403
402
// is just waiter getting set to null.)
404
403
g_thread_context.waiter ->wait (lock, [] { return !g_thread_context.waiter ; });
0 commit comments