Skip to content

Commit 0a8a742

Browse files
committed
using synchronized_value in output code + sync fixes
1 parent e3db4b3 commit 0a8a742

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

libmamba/src/core/output.cpp

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "mamba/core/tasksync.hpp"
2626
#include "mamba/core/thread_utils.hpp"
2727
#include "mamba/core/util.hpp"
28+
#include "mamba/util/synchronized_value.hpp"
2829
#include "mamba/specs/conda_url.hpp"
2930
#include "mamba/util/string.hpp"
3031
#include "mamba/util/url_manip.hpp"
@@ -283,15 +284,21 @@ namespace mamba
283284

284285
const Context& m_context;
285286

286-
std::mutex m_mutex;
287-
std::unique_ptr<ProgressBarManager> p_progress_bar_manager;
288287

289288
std::string json_hier;
290289
unsigned int json_index;
291290
nlohmann::json json_log;
292291
bool is_json_print_cancelled = false;
293292

294-
std::vector<std::string> m_buffer;
293+
using Buffer = std::vector<std::string>;
294+
295+
struct Data
296+
{
297+
std::unique_ptr<ProgressBarManager> progress_bar_manager;
298+
Buffer buffer;
299+
};
300+
301+
util::synchronized_value<Data> m_synched_data;
295302

296303
TaskSynchronizer m_tasksync;
297304
};
@@ -346,11 +353,11 @@ namespace mamba
346353
{
347354
if (force_print || !(context().output_params.quiet || context().output_params.json))
348355
{
349-
const std::lock_guard<std::mutex> lock(p_data->m_mutex);
356+
auto synched_data = p_data->m_synched_data.synchronize();
350357

351-
if (p_data->p_progress_bar_manager && p_data->p_progress_bar_manager->started())
358+
if (synched_data->progress_bar_manager && synched_data->progress_bar_manager->started())
352359
{
353-
p_data->m_buffer.push_back(hide_secrets(str));
360+
synched_data->buffer.push_back(hide_secrets(str));
354361
}
355362
else
356363
{
@@ -362,12 +369,8 @@ namespace mamba
362369
void Console::print_buffer(std::ostream& ostream)
363370
{
364371
auto& data = instance().p_data;
365-
decltype(data->m_buffer) tmp;
366-
367-
{
368-
const std::lock_guard<std::mutex> lock(data->m_mutex);
369-
data->m_buffer.swap(tmp);
370-
}
372+
ConsoleData::Buffer tmp;
373+
data->m_synched_data->buffer.swap(tmp);
371374

372375
for (const auto& message : tmp)
373376
{
@@ -436,7 +439,7 @@ namespace mamba
436439
}
437440
else
438441
{
439-
return p_data->p_progress_bar_manager->add_progress_bar(
442+
return p_data->m_synched_data->progress_bar_manager->add_progress_bar(
440443
name,
441444
{
442445
/* .graphics = */ context().graphics_params,
@@ -449,31 +452,35 @@ namespace mamba
449452

450453
void Console::clear_progress_bars()
451454
{
452-
return p_data->p_progress_bar_manager->clear_progress_bars();
455+
return p_data->m_synched_data->progress_bar_manager->clear_progress_bars();
453456
}
454457

455458
ProgressBarManager& Console::init_progress_bar_manager(ProgressBarMode mode)
456459
{
457-
p_data->p_progress_bar_manager = make_progress_bar_manager(mode);
458-
p_data->p_progress_bar_manager->register_print_hook(Console::print_buffer);
459-
p_data->p_progress_bar_manager->register_print_hook(MessageLogger::print_buffer);
460-
p_data->p_progress_bar_manager->register_pre_start_hook(MessageLogger::activate_buffer);
461-
p_data->p_progress_bar_manager->register_post_stop_hook(MessageLogger::deactivate_buffer);
460+
auto new_progress_bar_manager = make_progress_bar_manager(mode);
461+
new_progress_bar_manager->register_print_hook(Console::print_buffer);
462+
new_progress_bar_manager->register_print_hook(MessageLogger::print_buffer);
463+
new_progress_bar_manager->register_pre_start_hook(MessageLogger::activate_buffer);
464+
new_progress_bar_manager->register_post_stop_hook(MessageLogger::deactivate_buffer);
465+
466+
auto synched_data = p_data->m_synched_data.synchronize();
467+
synched_data->progress_bar_manager = std::move(new_progress_bar_manager);
462468

463-
return *(p_data->p_progress_bar_manager);
469+
return *(synched_data->progress_bar_manager); // unsafe!
464470
}
465471

466472
void Console::terminate_progress_bar_manager()
467473
{
468-
if (p_data->p_progress_bar_manager)
474+
auto synched_data = p_data->m_synched_data.synchronize();
475+
if (synched_data->progress_bar_manager)
469476
{
470-
p_data->p_progress_bar_manager->terminate();
477+
synched_data->progress_bar_manager->terminate();
471478
}
472479
}
473480

474481
ProgressBarManager& Console::progress_bar_manager()
475482
{
476-
return *(p_data->p_progress_bar_manager);
483+
return *(p_data->m_synched_data->progress_bar_manager);
477484
}
478485

479486
void Console::json_print()
@@ -543,12 +550,10 @@ namespace mamba
543550
* MessageLogger *
544551
*****************/
545552

546-
struct MessageLoggerData
547-
{
548-
static std::mutex m_mutex;
549-
static bool use_buffer;
550-
static std::vector<std::pair<std::string, log_level>> m_buffer;
551-
};
553+
static std::atomic<bool> message_logger_use_buffer;
554+
555+
using MessageLoggerBuffer = std::vector<std::pair<std::string, log_level>>;
556+
static util::synchronized_value<MessageLoggerBuffer> message_logger_buffer;
552557

553558
MessageLogger::MessageLogger(log_level level)
554559
: m_level(level)
@@ -558,14 +563,13 @@ namespace mamba
558563

559564
MessageLogger::~MessageLogger()
560565
{
561-
if (!MessageLoggerData::use_buffer && Console::is_available())
566+
if (!message_logger_use_buffer && Console::is_available())
562567
{
563568
emit(m_stream.str(), m_level);
564569
}
565570
else
566571
{
567-
const std::lock_guard<std::mutex> lock(MessageLoggerData::m_mutex);
568-
MessageLoggerData::m_buffer.push_back({ m_stream.str(), m_level });
572+
message_logger_buffer->push_back({ m_stream.str(), m_level });
569573
}
570574
}
571575

@@ -608,22 +612,18 @@ namespace mamba
608612

609613
void MessageLogger::activate_buffer()
610614
{
611-
MessageLoggerData::use_buffer = true;
615+
message_logger_use_buffer = true;
612616
}
613617

614618
void MessageLogger::deactivate_buffer()
615619
{
616-
MessageLoggerData::use_buffer = false;
620+
message_logger_use_buffer = false;
617621
}
618622

619623
void MessageLogger::print_buffer(std::ostream& /*ostream*/)
620624
{
621-
decltype(MessageLoggerData::m_buffer) tmp;
622-
623-
{
624-
const std::lock_guard<std::mutex> lock(MessageLoggerData::m_mutex);
625-
MessageLoggerData::m_buffer.swap(tmp);
626-
}
625+
MessageLoggerBuffer tmp;
626+
message_logger_buffer->swap(tmp);
627627

628628
for (const auto& [msg, level] : tmp)
629629
{

0 commit comments

Comments
 (0)