Skip to content

Commit 2a15121

Browse files
committed
use synchronized_value in MainExecutor
1 parent 9c09c64 commit 2a15121

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

libmamba/include/mamba/core/execution.hpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <vector>
1515

1616
#include "mamba/core/error_handling.hpp"
17+
#include "mamba/util/synchronized_value.hpp"
1718

1819
namespace mamba
1920
{
@@ -71,10 +72,10 @@ namespace mamba
7172
return;
7273
}
7374

74-
std::scoped_lock lock{ threads_mutex };
75+
auto synched_threads = threads.synchronize();
7576
if (is_open) // Double check necessary for correctness
7677
{
77-
threads.emplace_back(std::forward<Task>(task), std::forward<Args>(args)...);
78+
synched_threads->emplace_back(std::forward<Task>(task), std::forward<Args>(args)...);
7879
}
7980
}
8081

@@ -92,10 +93,10 @@ namespace mamba
9293
return;
9394
}
9495

95-
std::scoped_lock lock{ threads_mutex };
96+
auto synched_threads = threads.synchronize();
9697
if (is_open) // Double check necessary for correctness
9798
{
98-
threads.push_back(std::move(thread));
99+
synched_threads->push_back(std::move(thread));
99100
}
100101
}
101102

@@ -116,12 +117,12 @@ namespace mamba
116117

117118
invoke_close_handlers();
118119

119-
std::scoped_lock lock{ threads_mutex };
120-
for (auto&& t : threads)
120+
auto synched_threads = threads.synchronize();
121+
for (auto&& t : *synched_threads)
121122
{
122123
t.join();
123124
}
124-
threads.clear();
125+
synched_threads->clear();
125126
}
126127

127128
using on_close_handler = std::function<void()>;
@@ -133,21 +134,18 @@ namespace mamba
133134
return;
134135
}
135136

136-
std::scoped_lock lock{ handlers_mutex };
137+
auto handlers = close_handlers.synchronize();
137138
if (is_open) // Double check needed to avoid adding new handles while closing.
138139
{
139-
close_handlers.push_back(std::move(handler));
140+
handlers->push_back(std::move(handler));
140141
}
141142
}
142143

143144
private:
144145

145146
std::atomic<bool> is_open{ true };
146-
std::vector<std::thread> threads;
147-
std::recursive_mutex threads_mutex; // TODO: replace by synchronized_value once available
148-
149-
std::vector<on_close_handler> close_handlers;
150-
std::recursive_mutex handlers_mutex; // TODO: replace by synchronized_value once available
147+
util::synchronized_value<std::vector<std::thread>> threads;
148+
util::synchronized_value<std::vector<on_close_handler>> close_handlers;
151149

152150
void invoke_close_handlers();
153151
};

libmamba/src/core/execution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace mamba
88

99
void MainExecutor::invoke_close_handlers()
1010
{
11-
std::scoped_lock lock{ handlers_mutex };
12-
for (auto&& handler : close_handlers)
11+
auto synched_handlers = close_handlers.synchronize();
12+
for (auto&& handler : *synched_handlers)
1313
{
1414
const auto result = safe_invoke(handler);
1515
if (!result)

0 commit comments

Comments
 (0)