2020#include < dplx/dp/macros.hpp>
2121#include < dplx/dp/streams/memory_output_stream.hpp>
2222#include < dplx/make.hpp>
23+ #include < dplx/predef/os.h>
2324#include < dplx/scope_guard.hpp>
2425
2526#include < dplx/dlog/concepts.hpp>
27+ #include < dplx/dlog/config.hpp>
2628#include < dplx/dlog/core/file_database.hpp>
2729#include < dplx/dlog/core/log_clock.hpp>
2830#include < dplx/dlog/core/strong_types.hpp>
3133#include < dplx/dlog/log_fabric.hpp>
3234#include < dplx/dlog/source/record_output_buffer.hpp>
3335
36+ #if DPLX_DLOG_USE_BOOST_ATOMIC_REF
37+ #include < boost/atomic/atomic_ref.hpp>
38+ #endif
39+
3440namespace dplx ::dlog::detail
3541{
3642
43+ #if DPLX_DLOG_USE_BOOST_ATOMIC_REF
44+ template <typename T>
45+ using atomic_ref = boost::atomic_ref<T>;
46+ using memory_order = boost::memory_order;
47+ #else
48+ template <typename T>
49+ using atomic_ref = std::atomic_ref<T>;
50+ using memory_order = std::memory_order;
51+ #endif
52+
3753auto hashed_this_thread_id () noexcept -> std::uint32_t;
3854
3955constexpr auto hash_to_index (std::uint32_t h, std::uint32_t buckets) noexcept
@@ -64,11 +80,11 @@ using mpsc_bus_info = mpsc_bus_info_v00;
6480
6581struct mpsc_bus_region_ctrl
6682{
67- alignas (std ::atomic_ref<std::uint32_t >::required_alignment)
83+ alignas (detail ::atomic_ref<std::uint32_t >::required_alignment)
6884 std::uint32_t read_ptr;
69- alignas (std ::atomic_ref<std::uint32_t >::required_alignment)
85+ alignas (detail ::atomic_ref<std::uint32_t >::required_alignment)
7086 std::uint32_t alloc_ptr;
71- alignas (std ::atomic_ref<std::uint64_t >::required_alignment) std::uint64_t
87+ alignas (detail ::atomic_ref<std::uint64_t >::required_alignment) std::uint64_t
7288 span_prng_ctr;
7389
7490 std::uint8_t padding[48 ]; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
@@ -174,7 +190,7 @@ class mpsc_bus_handle
174190 = static_cast <std::uint32_t >(sizeof (region_ctrl));
175191
176192 static constexpr std::uint32_t write_alignment
177- = std ::atomic_ref<std::uint32_t >::required_alignment;
193+ = detail ::atomic_ref<std::uint32_t >::required_alignment;
178194 static constexpr std::uint32_t block_size = write_alignment;
179195 static constexpr std::uint32_t message_header_size = block_size;
180196
@@ -209,8 +225,8 @@ class mpsc_bus_handle
209225 return errc::bad;
210226 }
211227
212- std ::atomic_ref<std::uint32_t >(*mMsgCtrl ).fetch_and (
213- max_message_size, std ::memory_order::release);
228+ detail ::atomic_ref<std::uint32_t >(*mMsgCtrl ).fetch_and (
229+ max_message_size, detail ::memory_order::release);
214230 // reset();
215231 mMsgCtrl = nullptr ;
216232 return outcome::success ();
@@ -238,11 +254,12 @@ class mpsc_bus_handle
238254 auto *const blockData = region_data (regionId);
239255 writable_bytes block (blockData, mRegionSize - region_ctrl_overhead);
240256
241- std ::atomic_ref<std::uint32_t > const readPtr (ctx->read_ptr );
242- std ::atomic_ref<std::uint32_t > const allocPtr (ctx->alloc_ptr );
257+ detail ::atomic_ref<std::uint32_t > const readPtr (ctx->read_ptr );
258+ detail ::atomic_ref<std::uint32_t > const allocPtr (ctx->alloc_ptr );
243259
244- auto const originalReadPos = readPtr.load (std::memory_order::relaxed);
245- auto const allocPos = allocPtr.load (std::memory_order::relaxed);
260+ auto const originalReadPos
261+ = readPtr.load (detail::memory_order::relaxed);
262+ auto const allocPos = allocPtr.load (detail::memory_order::relaxed);
246263 if (allocPos == originalReadPos)
247264 {
248265 // nothing to see here
@@ -253,7 +270,7 @@ class mpsc_bus_handle
253270 scope_guard readUpdateGuard = [&readPos, &originalReadPos, &readPtr] {
254271 if (readPos != originalReadPos)
255272 {
256- readPtr.store (readPos, std ::memory_order::release);
273+ readPtr.store (readPos, detail ::memory_order::release);
257274 }
258275 };
259276
@@ -276,8 +293,8 @@ class mpsc_bus_handle
276293 blockData + readPos);
277294
278295 auto const msgHead
279- = std ::atomic_ref<std::uint32_t >{*msgHeadPtr}.load (
280- std ::memory_order::acquire);
296+ = detail ::atomic_ref<std::uint32_t >{*msgHeadPtr}.load (
297+ detail ::memory_order::acquire);
281298 if ((msgHead & message_lock_flag) != 0U )
282299 {
283300 break ;
@@ -312,9 +329,9 @@ class mpsc_bus_handle
312329
313330 for (std::size_t i = 0U ; i < batchSize; ++i)
314331 {
315- std ::atomic_ref<std::uint32_t >{*infos[i].head }.fetch_or (
332+ detail ::atomic_ref<std::uint32_t >{*infos[i].head }.fetch_or (
316333 message_lock_flag | message_consumed_flag,
317- std ::memory_order::relaxed);
334+ detail ::memory_order::relaxed);
318335 std::memset (infos[i].content .data (),
319336 static_cast <int >(unused_block_content),
320337 infos[i].content .size ());
@@ -379,12 +396,13 @@ class mpsc_bus_handle
379396 auto const regionEnd = mRegionSize - region_ctrl_overhead;
380397 auto const allocSize = cncr::round_up_p2 (payloadSize, block_size);
381398
382- std ::atomic_ref<std::uint32_t > const sharedReadHand (ctx->read_ptr );
383- std ::atomic_ref<std::uint32_t > const sharedAllocHand (ctx->alloc_ptr );
399+ detail ::atomic_ref<std::uint32_t > const sharedReadHand (ctx->read_ptr );
400+ detail ::atomic_ref<std::uint32_t > const sharedAllocHand (ctx->alloc_ptr );
384401
385- auto const readHand = sharedReadHand.load (std::memory_order::acquire);
402+ auto const readHand
403+ = sharedReadHand.load (detail::memory_order::acquire);
386404 std::uint32_t allocHand
387- = sharedAllocHand.load (std ::memory_order::relaxed);
405+ = sharedAllocHand.load (detail ::memory_order::relaxed);
388406 // NOLINTNEXTLINE(cppcoreguidelines-init-variables)
389407 std::uint32_t payloadPosition;
390408 for (;;)
@@ -412,7 +430,7 @@ class mpsc_bus_handle
412430 }
413431
414432 if (sharedAllocHand.compare_exchange_weak (
415- allocHand, payloadEnd, std ::memory_order::relaxed))
433+ allocHand, payloadEnd, detail ::memory_order::relaxed))
416434 [[likely]]
417435 {
418436 break ;
@@ -424,8 +442,8 @@ class mpsc_bus_handle
424442 auto *ctrl = reinterpret_cast <std::uint32_t *>(regionData + allocHand);
425443 auto *data = regionData + payloadPosition;
426444 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
427- std ::atomic_ref<std::uint32_t >(*ctrl).store (
428- payloadSize | message_lock_flag, std ::memory_order::relaxed);
445+ detail ::atomic_ref<std::uint32_t >(*ctrl).store (
446+ payloadSize | message_lock_flag, detail ::memory_order::relaxed);
429447 out.reset (data, allocSize);
430448 out.mMsgCtrl = ctrl;
431449 return errc::success;
0 commit comments