Skip to content

Commit

Permalink
Make shared pointer std atomic
Browse files Browse the repository at this point in the history
Removes use of deprecated atomic operations
  • Loading branch information
ikbuibui committed Jul 24, 2024
1 parent def0c50 commit 32c7c3d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
5 changes: 3 additions & 2 deletions redGrapes/TaskFreeCtx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <vector>

Expand All @@ -36,10 +37,10 @@ namespace redGrapes
inline memory::ChunkedBumpAlloc<memory::HwlocAlloc>& get_alloc(WorkerId worker_id)
{
assert(worker_id < allocs.size());
return allocs[worker_id];
return *allocs[worker_id];
}

std::vector<memory::ChunkedBumpAlloc<memory::HwlocAlloc>> allocs;
std::vector<std::unique_ptr<memory::ChunkedBumpAlloc<memory::HwlocAlloc>>> allocs;
};

struct TaskFreeCtx
Expand Down
9 changes: 6 additions & 3 deletions redGrapes/dispatch/thread/worker_pool.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "redGrapes/memory/hwloc_alloc.hpp"
#include "redGrapes/util/trace.hpp"

#include <memory>

namespace redGrapes
{
namespace dispatch
Expand Down Expand Up @@ -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::ChunkedBumpAlloc<memory::HwlocAlloc>>(
memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj),
REDGRAPES_ALLOC_CHUNKSIZE));

auto worker = memory::alloc_shared_bind<WorkerThread<Worker>>(
worker_id,
Expand Down
6 changes: 0 additions & 6 deletions redGrapes/memory/chunked_bump_alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
Expand Down
5 changes: 3 additions & 2 deletions redGrapes/scheduler/thread_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::ChunkedBumpAlloc<memory::HwlocAlloc>>(
memory::HwlocAlloc(TaskFreeCtx::hwloc_ctx, obj),
REDGRAPES_ALLOC_CHUNKSIZE));

m_worker_thread
= memory::alloc_shared_bind<dispatch::thread::WorkerThread<Worker>>(m_base_id, obj, m_base_id);
Expand Down
24 changes: 12 additions & 12 deletions redGrapes/util/atomic_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ namespace redGrapes
struct ItemControlBlock
{
bool volatile deleted;
std::shared_ptr<ItemControlBlock> prev;
std::atomic<std::shared_ptr<ItemControlBlock>> 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
Expand Down Expand Up @@ -76,11 +76,11 @@ namespace redGrapes
*/
void skip_deleted_prev()
{
std::shared_ptr<ItemControlBlock> p = std::atomic_load(&prev);
std::shared_ptr<ItemControlBlock> 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
Expand All @@ -90,7 +90,7 @@ namespace redGrapes
};

Allocator alloc;
std::shared_ptr<ItemControlBlock> head;
std::atomic<std::shared_ptr<ItemControlBlock>> head;
size_t const chunk_capacity;

/* keeps a single, predefined pointer
Expand Down Expand Up @@ -256,7 +256,7 @@ namespace redGrapes
*/
MutBackwardIterator rbegin() const
{
return MutBackwardIterator{std::atomic_load(&head)};
return MutBackwardIterator{head.load()};
}

MutBackwardIterator rend() const
Expand All @@ -266,7 +266,7 @@ namespace redGrapes

ConstBackwardIterator crbegin() const
{
return ConstBackwardIterator{std::atomic_load(&head)};
return ConstBackwardIterator{head.load()};
}

ConstBackwardIterator crend() const
Expand Down Expand Up @@ -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};
Expand All @@ -312,7 +312,7 @@ namespace redGrapes

std::shared_ptr<ItemControlBlock> expected(nullptr);
std::shared_ptr<ItemControlBlock> const& desired = new_head;
return std::atomic_compare_exchange_strong(&head, &expected, desired);
return head.compare_exchange_strong(expected, desired);
}
};

Expand Down

0 comments on commit 32c7c3d

Please sign in to comment.