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:
parent
381f79f63c
commit
c509b6ccc5
@ -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 {};
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user