From 32c7c3d32e4a079a442f23eb8eee0997414197c1 Mon Sep 17 00:00:00 2001 From: Tapish Date: Wed, 24 Jul 2024 15:18:54 +0200 Subject: [PATCH] Make shared pointer std atomic Removes use of deprecated atomic operations --- redGrapes/TaskFreeCtx.hpp | 5 +++-- redGrapes/dispatch/thread/worker_pool.tpp | 9 ++++++--- redGrapes/memory/chunked_bump_alloc.hpp | 6 ------ redGrapes/scheduler/thread_scheduler.hpp | 5 +++-- redGrapes/util/atomic_list.hpp | 24 +++++++++++------------ 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/redGrapes/TaskFreeCtx.hpp b/redGrapes/TaskFreeCtx.hpp index 73d4cb4b..892a527f 100644 --- a/redGrapes/TaskFreeCtx.hpp +++ b/redGrapes/TaskFreeCtx.hpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -36,10 +37,10 @@ namespace redGrapes inline memory::ChunkedBumpAlloc& get_alloc(WorkerId worker_id) { assert(worker_id < allocs.size()); - return allocs[worker_id]; + return *allocs[worker_id]; } - std::vector> allocs; + std::vector>> allocs; }; struct TaskFreeCtx diff --git a/redGrapes/dispatch/thread/worker_pool.tpp b/redGrapes/dispatch/thread/worker_pool.tpp index c2503cc9..eb9f7af9 100644 --- a/redGrapes/dispatch/thread/worker_pool.tpp +++ b/redGrapes/dispatch/thread/worker_pool.tpp @@ -15,6 +15,8 @@ #include "redGrapes/memory/hwloc_alloc.hpp" #include "redGrapes/util/trace.hpp" +#include + namespace redGrapes { namespace dispatch @@ -45,9 +47,10 @@ namespace redGrapes WorkerId pu_id = worker_id % TaskFreeCtx::n_pus; // allocate worker with id `i` on arena `i`, hwloc_obj_t obj = hwloc_get_obj_by_type(TaskFreeCtx::hwloc_ctx.topology, HWLOC_OBJ_PU, pu_id); - TaskFreeCtx::worker_alloc_pool.allocs.emplace_back( - memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj), - REDGRAPES_ALLOC_CHUNKSIZE); + TaskFreeCtx::worker_alloc_pool.allocs.push_back( + std::make_unique>( + memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj), + REDGRAPES_ALLOC_CHUNKSIZE)); auto worker = memory::alloc_shared_bind>( worker_id, diff --git a/redGrapes/memory/chunked_bump_alloc.hpp b/redGrapes/memory/chunked_bump_alloc.hpp index 66b3a369..5d960044 100644 --- a/redGrapes/memory/chunked_bump_alloc.hpp +++ b/redGrapes/memory/chunked_bump_alloc.hpp @@ -47,12 +47,6 @@ namespace redGrapes { } - ChunkedBumpAlloc(ChunkedBumpAlloc&& other) - : chunk_size(other.chunk_size) - , bump_allocators(other.bump_allocators) - { - } - static inline size_t roundup_to_poweroftwo(size_t s) { s--; diff --git a/redGrapes/scheduler/thread_scheduler.hpp b/redGrapes/scheduler/thread_scheduler.hpp index bd34b649..f9a1bad8 100644 --- a/redGrapes/scheduler/thread_scheduler.hpp +++ b/redGrapes/scheduler/thread_scheduler.hpp @@ -96,8 +96,9 @@ namespace redGrapes // allocate worker with id `i` on arena `i`, hwloc_obj_t obj = hwloc_get_obj_by_type(TaskFreeCtx::hwloc_ctx.topology, HWLOC_OBJ_PU, pu_id); TaskFreeCtx::worker_alloc_pool.allocs.emplace_back( - memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj), - REDGRAPES_ALLOC_CHUNKSIZE); + std::make_unique>( + memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj), + REDGRAPES_ALLOC_CHUNKSIZE)); m_worker_thread = memory::alloc_shared_bind>(m_base_id, obj, m_base_id); diff --git a/redGrapes/util/atomic_list.hpp b/redGrapes/util/atomic_list.hpp index 0ff7b4b7..fbf30f97 100644 --- a/redGrapes/util/atomic_list.hpp +++ b/redGrapes/util/atomic_list.hpp @@ -45,10 +45,10 @@ namespace redGrapes struct ItemControlBlock { bool volatile deleted; - std::shared_ptr prev; + std::atomic> prev; uintptr_t item_data_ptr; - ItemControlBlock(memory::Block blk) : deleted(false), item_data_ptr(blk.ptr) + ItemControlBlock(memory::Block blk) : deleted(false), prev(nullptr), item_data_ptr(blk.ptr) { /* put Item at front and initialize it * with the remaining memory region @@ -76,11 +76,11 @@ namespace redGrapes */ void skip_deleted_prev() { - std::shared_ptr p = std::atomic_load(&prev); + std::shared_ptr p = prev.load(); while(p && p->deleted) - p = std::atomic_load(&p->prev); + p = (p->prev).load(); - std::atomic_store(&prev, p); + prev.store(p); } Item* get() const @@ -90,7 +90,7 @@ namespace redGrapes }; Allocator alloc; - std::shared_ptr head; + std::atomic> head; size_t const chunk_capacity; /* keeps a single, predefined pointer @@ -256,7 +256,7 @@ namespace redGrapes */ MutBackwardIterator rbegin() const { - return MutBackwardIterator{std::atomic_load(&head)}; + return MutBackwardIterator{head.load()}; } MutBackwardIterator rend() const @@ -266,7 +266,7 @@ namespace redGrapes ConstBackwardIterator crbegin() const { - return ConstBackwardIterator{std::atomic_load(&head)}; + return ConstBackwardIterator{head.load()}; } ConstBackwardIterator crend() const @@ -297,9 +297,9 @@ namespace redGrapes bool append_successful = false; while(!append_successful) { - old_head = std::atomic_load(&head); - std::atomic_store(&new_head->prev, old_head); - append_successful = std::atomic_compare_exchange_strong(&head, &old_head, new_head); + old_head = head.load(); + (new_head->prev).store(old_head); + append_successful = head.compare_exchange_strong(old_head, new_head); } return MutBackwardIterator{old_head}; @@ -312,7 +312,7 @@ namespace redGrapes std::shared_ptr expected(nullptr); std::shared_ptr const& desired = new_head; - return std::atomic_compare_exchange_strong(&head, &expected, desired); + return head.compare_exchange_strong(expected, desired); } };