Skip to content

Commit 8112e10

Browse files
committed
fixed: MainExecutor must use recursive mutexes
1 parent 659153c commit 8112e10

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

libmamba/include/mamba/core/execution.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ namespace mamba
144144
private:
145145

146146
std::atomic<bool> is_open{ true };
147-
util::synchronized_value<std::vector<std::thread>> threads;
148-
util::synchronized_value<std::vector<on_close_handler>> close_handlers;
147+
using Threads = std::vector<std::thread>;
148+
using CloseHandlers = std::vector<on_close_handler>;
149+
util::synchronized_value<Threads, std::recursive_mutex> threads;
150+
util::synchronized_value<CloseHandlers, std::recursive_mutex> close_handlers;
149151

150152
void invoke_close_handlers();
151153
};

libmamba/src/core/singletons.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <mutex>
1010

1111
#include "mamba/util/build.hpp"
12+
#include "mamba/util/synchronized_value.hpp"
1213

1314
extern "C"
1415
{
@@ -105,19 +106,18 @@ namespace mamba
105106

106107
static std::atomic<MainExecutor*> main_executor{ nullptr };
107108

108-
static std::unique_ptr<MainExecutor> default_executor;
109-
static std::mutex default_executor_mutex; // TODO: replace by synchronized_value once available
109+
static util::synchronized_value<std::unique_ptr<MainExecutor>> default_executor;
110110

111111
MainExecutor& MainExecutor::instance()
112112
{
113113
if (!main_executor)
114114
{
115115
// When no MainExecutor was created before we create a static one.
116-
std::scoped_lock lock{ default_executor_mutex };
116+
auto synched_default_executor = default_executor.synchronize();
117117
if (!main_executor) // double check necessary to avoid data race
118118
{
119-
default_executor = std::make_unique<MainExecutor>();
120-
assert(main_executor == default_executor.get());
119+
*synched_default_executor = std::make_unique<MainExecutor>();
120+
assert(main_executor == synched_default_executor->get());
121121
}
122122
}
123123

@@ -126,8 +126,7 @@ namespace mamba
126126

127127
void MainExecutor::stop_default()
128128
{
129-
std::scoped_lock lock{ default_executor_mutex };
130-
default_executor.reset();
129+
default_executor->reset();
131130
}
132131

133132
MainExecutor::MainExecutor()

0 commit comments

Comments
 (0)