Don't deadlock during nested evaluation in print context

Lock for stdout should only hold when things are ready to be printed.
This prevents any double locking issues
This commit is contained in:
Robert Bendun 2022-11-24 01:23:31 +01:00
parent 381f79f63c
commit c509b6ccc5

View File

@ -191,12 +191,19 @@ struct Runner
Env::global->force_define("print", +[](Interpreter &interpreter, std::vector<Value> args) -> Result<Value> {
static std::mutex stdio_mutex;
std::lock_guard guard{stdio_mutex};
for (auto it = args.begin(); it != args.end(); ++it) {
std::cout << Try(format(interpreter, *it));
if (std::next(it) != args.end())
{
auto result = Try(format(interpreter, *it));
std::lock_guard guard{stdio_mutex};
std::cout << result;
}
if (std::next(it) != args.end()) {
std::lock_guard guard{stdio_mutex};
std::cout << ' ';
}
}
std::lock_guard guard{stdio_mutex};
std::cout << std::endl;
return {};
});