Skip to content

Commit 2ad8337

Browse files
committed
Use std::optional in ConnThreads to allow shortening locks
This is just a refactoring changing the ConnThreads data type. The optional value is not actually left in an unset state until the next commit.
1 parent 35112b9 commit 2ad8337

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

include/mp/proxy-io.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,10 @@ void ProxyServerBase<Interface, Impl>::invokeDestroy()
537537
//! Map from Connection to local or remote thread handle which will be used over
538538
//! that connection. This map will typically only contain one entry, but can
539539
//! contain multiple if a single thread makes IPC calls over multiple
540-
//! connections.
541-
using ConnThreads = std::map<Connection*, ProxyClient<Thread>>;
540+
//! connections. A std::optional value type is used to avoid the map needing to
541+
//! be locked while ProxyClient<Thread> objects are constructed, see
542+
//! ThreadContext "Synchronization note" below.
543+
using ConnThreads = std::map<Connection*, std::optional<ProxyClient<Thread>>>;
542544
using ConnThread = ConnThreads::iterator;
543545

544546
// Retrieve ProxyClient<Thread> object associated with this connection from a

include/mp/type-context.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ void CustomBuildField(TypeList<>,
4747
&connection, make_request_thread)};
4848

4949
auto context = output.init();
50-
context.setThread(request_thread->second.m_client);
51-
context.setCallbackThread(callback_thread->second.m_client);
50+
context.setThread(request_thread->second->m_client);
51+
context.setCallbackThread(callback_thread->second->m_client);
5252
}
5353

5454
//! PassField override for mp.Context arguments. Return asynchronously and call

src/mp/proxy.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,10 @@ std::tuple<ConnThread, bool> SetThread(GuardedRef<ConnThreads> threads, Connecti
311311
if (thread != threads.ref.end()) return {thread, false};
312312
thread = threads.ref.emplace(
313313
std::piecewise_construct, std::forward_as_tuple(connection),
314-
std::forward_as_tuple(make_thread(), connection, /* destroy_connection= */ false)).first;
315-
thread->second.setDisconnectCallback([threads, thread] {
314+
std::forward_as_tuple()
315+
).first;
316+
thread->second.emplace(make_thread(), connection, /* destroy_connection= */ false);
317+
thread->second->setDisconnectCallback([threads, thread] {
316318
// Note: it is safe to use the `thread` iterator in this cleanup
317319
// function, because the iterator would only be invalid if the map entry
318320
// was removed, and if the map entry is removed the ProxyClient<Thread>
@@ -323,7 +325,7 @@ std::tuple<ConnThread, bool> SetThread(GuardedRef<ConnThreads> threads, Connecti
323325
// try to unregister this callback after connection is destroyed.
324326
// Remove connection pointer about to be destroyed from the map
325327
const Lock lock(threads.mutex);
326-
thread->second.m_disconnect_cb.reset();
328+
thread->second->m_disconnect_cb.reset();
327329
threads.ref.erase(thread);
328330
});
329331
return {thread, true};

0 commit comments

Comments
 (0)