Skip to content

Commit 691fa8d

Browse files
committed
homebrew workaround attempt
1 parent b780e7c commit 691fa8d

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

libmamba/include/mamba/util/synchronized_value.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,7 @@ namespace mamba::util
546546
///////////////////////////////////////////////////////////////////////////////////////////
547547

548548
template <std::default_initializable T, Mutex M>
549-
constexpr synchronized_value<T, M>::synchronized_value(
550-
) noexcept(std::is_nothrow_default_constructible_v<T>)
551-
{
552-
// NOTE: non-defaulted because of homebrew/clang compiler not liking the defaulted version
553-
// for some reason
554-
}
549+
constexpr synchronized_value<T, M>::synchronized_value() noexcept(std::is_nothrow_default_constructible_v<T>) = default;
555550

556551
template <std::default_initializable T, Mutex M>
557552
synchronized_value<T, M>::synchronized_value(T value) noexcept

libmamba/src/core/logging.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,33 @@ namespace mamba::logging
128128
{
129129
constinit std::atomic<bool> message_logger_use_buffer;
130130

131-
using MessageLoggerBuffer = std::vector<LogRecord>;
131+
// NOTE: this looks complicated because it's a workaround `std::vector` implementations
132+
// which are not `constexpr` (required by c++20), we defer the vector creation to the moment it's needed.
133+
// Constexpr constructor is required for a type which is usable in a `constinit` declaration,
134+
// which is required to avoid the static-initialization-fiasco (at least for initialization).
135+
struct MessageLoggerBuffer
136+
{
137+
using buffer = std::vector<LogRecord>;
138+
139+
constexpr MessageLoggerBuffer() = default;
140+
141+
template<class T>
142+
auto push_back(T&& record)
143+
{
144+
return ready_records().push_back(std::forward<T>(record));
145+
}
146+
147+
auto ready_records() -> buffer&
148+
{
149+
if (not records)
150+
{
151+
records = buffer{};
152+
}
153+
return *records;
154+
}
155+
156+
std::optional<buffer> records;
157+
};
132158
constinit util::synchronized_value<MessageLoggerBuffer> message_logger_buffer;
133159

134160
auto
@@ -193,8 +219,8 @@ namespace mamba::logging
193219

194220
void MessageLogger::print_buffer(std::ostream& /*ostream*/)
195221
{
196-
MessageLoggerBuffer tmp;
197-
message_logger_buffer->swap(tmp);
222+
MessageLoggerBuffer::buffer tmp;
223+
message_logger_buffer->ready_records().swap(tmp);
198224

199225
for (auto& log_record : tmp)
200226
{

0 commit comments

Comments
 (0)