Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
option(LOGIT_EMSCRIPTEN "Build for Emscripten" ON)
endif()
option(LOGIT_FORCE_ASYNC_OFF "Force disable async logging" OFF)
option(LOGIT_USE_MPSC_RING "Enable lock-free TaskExecutor queue" ON)
option(LOGIT_ENABLE_DROP_OLDEST_SLOWPATH "Enable TaskExecutor DropOldest slow-path" ON)

if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
Expand Down Expand Up @@ -42,6 +44,14 @@ target_include_directories(log-it-cpp INTERFACE

target_link_libraries(log-it-cpp INTERFACE time_shield::time_shield)

if(LOGIT_USE_MPSC_RING)
target_compile_definitions(log-it-cpp INTERFACE LOGIT_USE_MPSC_RING=1)
endif()

if(LOGIT_ENABLE_DROP_OLDEST_SLOWPATH)
target_compile_definitions(log-it-cpp INTERFACE LOGIT_ENABLE_DROP_OLDEST_SLOWPATH=1)
endif()

if(LOGIT_EMSCRIPTEN)
set(LOGIT_WITH_SYSLOG OFF CACHE BOOL "" FORCE)
set(LOGIT_WITH_WIN_EVENT_LOG OFF CACHE BOOL "" FORCE)
Expand Down
50 changes: 44 additions & 6 deletions include/logit_cpp/logit/detail/MpscRingAny.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,39 @@ namespace logit { namespace detail {

/// \brief Destroy remaining elements, if any.
~MpscRingAny() {
T tmp;
while (try_pop(tmp)) {
// Element destroyed via move-from tmp
}
destroy_all_();
}

MpscRingAny(const MpscRingAny&) = delete;
MpscRingAny& operator=(const MpscRingAny&) = delete;

/// \brief Move construct ring, adopting producer/consumer indices.
MpscRingAny(MpscRingAny&& other) noexcept
: m_cap(other.m_cap),
m_cells(std::move(other.m_cells)),
m_enqueue_pos(other.m_enqueue_pos.load(std::memory_order_relaxed)),
m_dequeue_pos(other.m_dequeue_pos.load(std::memory_order_relaxed)) {
other.m_cap = 0;
other.m_enqueue_pos.store(0, std::memory_order_relaxed);
other.m_dequeue_pos.store(0, std::memory_order_relaxed);
}

/// \brief Move assign ring, draining existing payload.
MpscRingAny& operator=(MpscRingAny&& other) noexcept {
if (this != &other) {
destroy_all_();
m_cap = other.m_cap;
m_cells = std::move(other.m_cells);
m_enqueue_pos.store(other.m_enqueue_pos.load(std::memory_order_relaxed),
std::memory_order_relaxed);
m_dequeue_pos.store(other.m_dequeue_pos.load(std::memory_order_relaxed),
std::memory_order_relaxed);
other.m_cap = 0;
other.m_enqueue_pos.store(0, std::memory_order_relaxed);
other.m_dequeue_pos.store(0, std::memory_order_relaxed);
}
return *this;
}

/// \brief Capacity of the ring.
std::size_t capacity() const noexcept { return m_cap; }
Expand Down Expand Up @@ -117,15 +142,28 @@ namespace logit { namespace detail {

/// \brief Lightweight emptiness check for current consumer position.
bool empty() const noexcept {
if (!m_cells || m_cap == 0) {
return true;
}
std::size_t pos = m_dequeue_pos.load(std::memory_order_acquire);
const Cell& c = m_cells[pos % m_cap];
std::size_t seq = c.m_seq.load(std::memory_order_acquire);
std::intptr_t diff =
static_cast<std::intptr_t>(seq) - static_cast<std::intptr_t>(pos + 1);
return diff != 0;
}

private:
void destroy_all_() noexcept {
if (!m_cells || m_cap == 0) {
return;
}
T tmp;
while (try_pop(tmp)) {
// Element destroyed via move-from tmp
}
}

std::size_t m_cap;
std::unique_ptr<Cell[]> m_cells;
alignas(64) std::atomic<std::size_t> m_enqueue_pos;
Expand Down
Loading
Loading