Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Sep 22, 2023
1 parent 96f3146 commit 497fc96
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 130 deletions.
23 changes: 11 additions & 12 deletions src/engine/allocators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,16 @@ namespace Lumix

BaseProxyAllocator::BaseProxyAllocator(IAllocator& source)
: m_source(source)
, m_allocation_count(0)
{
m_allocation_count = 0;
}

BaseProxyAllocator::~BaseProxyAllocator() { ASSERT(m_allocation_count == 0); }


void* BaseProxyAllocator::allocate(size_t size, size_t align)
{
atomicIncrement(&m_allocation_count);
m_allocation_count.inc();
return m_source.allocate(size, align);
}

Expand All @@ -233,30 +233,29 @@ void BaseProxyAllocator::deallocate(void* ptr)
{
if(ptr)
{
atomicDecrement(&m_allocation_count);
m_allocation_count.dec();
m_source.deallocate(ptr);
}
}


void* BaseProxyAllocator::reallocate(void* ptr, size_t new_size, size_t old_size, size_t align)
{
if (!ptr) atomicIncrement(&m_allocation_count);
if (new_size == 0) atomicDecrement(&m_allocation_count);
if (!ptr) m_allocation_count.inc();
if (new_size == 0) m_allocation_count.dec();
return m_source.reallocate(ptr, new_size, old_size, align);
}

LinearAllocator::LinearAllocator(u32 reserved) {
m_end = 0;
m_commited_bytes = 0;
LinearAllocator::LinearAllocator(u32 reserved)
{
m_reserved = reserved;
m_mem = (u8*)os::memReserve(reserved);
}

LinearAllocator::~LinearAllocator() {
ASSERT(m_end == 0);
os::memRelease(m_mem, m_reserved);
atomicSubtract(&g_total_commited_bytes, m_commited_bytes);
g_total_commited_bytes.subtract(m_commited_bytes);
}

void LinearAllocator::reset() {
Expand All @@ -274,7 +273,7 @@ void* LinearAllocator::allocate(size_t size, size_t align) {
for (;;) {
const u32 end = m_end;
start = roundUp(end, (u32)align);
if (compareAndExchange(&m_end, u32(start + size), end)) break;
if (m_end.compareExchange(u32(start + size), end)) break;
}

if (start + size <= m_commited_bytes) return m_mem + start;
Expand All @@ -285,13 +284,13 @@ void* LinearAllocator::allocate(size_t size, size_t align) {
const u32 commited = roundUp(start + (u32)size, 4096);
ASSERT(commited < m_reserved);
os::memCommit(m_mem + m_commited_bytes, commited - m_commited_bytes);
atomicAdd(&g_total_commited_bytes, commited - m_commited_bytes);
g_total_commited_bytes.add(commited - m_commited_bytes);
m_commited_bytes = commited;

return m_mem + start;
}

volatile i64 LinearAllocator::g_total_commited_bytes = 0;
AtomicI64 LinearAllocator::g_total_commited_bytes = 0;

void LinearAllocator::deallocate(void* ptr) { /*everything should be "deallocated" with reset()*/ }
void* LinearAllocator::reallocate(void* ptr, size_t new_size, size_t old_size, size_t align) {
Expand Down
9 changes: 5 additions & 4 deletions src/engine/allocators.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "allocator.h"
#include "atomic.h"
#include "crt.h"
#include "sync.h"

Expand Down Expand Up @@ -58,7 +59,7 @@ struct LUMIX_ENGINE_API BaseProxyAllocator final : IAllocator {

private:
IAllocator& m_source;
volatile i32 m_allocation_count;
AtomicI32 m_allocation_count;
};

// allocations in a row one after another, deallocate everything at once
Expand All @@ -76,13 +77,13 @@ struct LUMIX_ENGINE_API LinearAllocator : IAllocator {
static size_t getTotalCommitedBytes() { return g_total_commited_bytes; }

private:
u32 m_commited_bytes;
u32 m_commited_bytes = 0;
u32 m_reserved;
volatile i32 m_end;
AtomicI32 m_end = 0;
u8* m_mem;
Mutex m_mutex;

static volatile i64 g_total_commited_bytes;
static AtomicI64 g_total_commited_bytes;
};

// one allocation from local memory backing (m_mem), use fallback allocator otherwise
Expand Down
46 changes: 35 additions & 11 deletions src/engine/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,41 @@
namespace Lumix
{

LUMIX_ENGINE_API i64 atomicIncrement(i64 volatile* value);
LUMIX_ENGINE_API i32 atomicIncrement(i32 volatile* value);
// returns the resulting value
LUMIX_ENGINE_API i32 atomicDecrement(i32 volatile* value);
// returns the initial value
LUMIX_ENGINE_API i32 atomicAdd(i32 volatile* addend, i32 value);
LUMIX_ENGINE_API i64 atomicAdd(i64 volatile* addend, i64 value);
LUMIX_ENGINE_API i32 atomicSubtract(i32 volatile* addend, i32 value);
LUMIX_ENGINE_API i64 atomicSubtract(i64 volatile* addend, i64 value);
LUMIX_ENGINE_API bool compareAndExchange(i32 volatile* dest, i32 exchange, i32 comperand);
LUMIX_ENGINE_API bool compareAndExchange64(i64 volatile* dest, i64 exchange, i64 comperand);
LUMIX_ENGINE_API struct AtomicI32 {
AtomicI32(i32 v) : value(v) {}

void operator =(i32 v);
operator i32() const;

// returns initial value of the variable
i32 inc();
i32 dec();
i32 add(i32 v);
i32 subtract(i32 v);

bool compareExchange(i32 exchange, i32 comperand);
private:
volatile i32 value;
};

LUMIX_ENGINE_API struct AtomicI64 {
AtomicI64(i64 v) : value(v) {}

void operator =(i64 v);
operator i64() const;

// returns initial value of the variable
i64 inc();
i64 dec();
i64 add(i64 v);
i64 subtract(i64 v);

bool compareExchange(i64 exchange, i64 comperand);
private:
volatile i64 value;
};

LUMIX_ENGINE_API bool compareExchangePtr(volatile void** value, void* exchange, void* comperand);
LUMIX_ENGINE_API void memoryBarrier();

} // namespace Lumix
3 changes: 2 additions & 1 deletion src/engine/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


#include "engine/allocator.h"
#include "engine/atomic.h"
#include "engine/lumix.h"
#include "engine/sync.h"

Expand Down Expand Up @@ -41,7 +42,7 @@ struct LUMIX_ENGINE_API StackTree

private:
StackNode* m_root;
static i32 s_instances;
static AtomicI32 s_instances;
};

#ifdef _WIN32
Expand Down
17 changes: 8 additions & 9 deletions src/engine/job_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct System {

static Local<System> g_system;

static volatile i32 g_generation = 0;
static AtomicI32 g_generation = 0;
static thread_local WorkerTask* g_worker = nullptr;

#ifndef _WIN32
Expand Down Expand Up @@ -166,9 +166,9 @@ LUMIX_FORCE_INLINE static bool trigger(Signal* signal)
signal->counter = 0;
}
else {
--signal->counter;
ASSERT(signal->counter >= 0);
if (signal->counter > 0) return false;
i32 counter = signal->counter.dec();
ASSERT(counter > 0);
if (counter > 1) return false;
}

waitor = signal->waitor;
Expand Down Expand Up @@ -223,9 +223,9 @@ void enableBackupWorker(bool enable)
LUMIX_FORCE_INLINE static bool setRedEx(Signal* signal) {
ASSERT(signal);
ASSERT(signal->counter <= 1);
bool res = compareAndExchange(&signal->counter, 1, 0);
bool res = signal->counter.compareExchange(1, 0);
if (res) {
signal->generation = atomicIncrement(&g_generation);
signal->generation = g_generation.inc();
}
return res;
}
Expand Down Expand Up @@ -260,9 +260,8 @@ void runEx(void* data, void(*task)(void*), Signal* on_finished, u8 worker_index)

if (on_finished) {
Lumix::MutexGuard guard(g_system->m_sync);
++on_finished->counter;
if (on_finished->counter == 1) {
on_finished->generation = atomicIncrement(&g_generation);
if (on_finished->counter.inc() == 0) {
on_finished->generation = g_generation.inc();
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/engine/job_system.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once
#include "lumix.h"
#ifndef _WIN32
#include "atomic.h"
#endif
#include "atomic.h"

namespace Lumix {

Expand Down Expand Up @@ -64,7 +62,7 @@ struct Signal {
~Signal() { ASSERT(!waitor); ASSERT(!counter); }

struct Waitor* waitor = nullptr;
volatile i32 counter = 0;
AtomicI32 counter = 0;
i32 generation; // identify different red-green pairs on the same signal, used by profiler
};

Expand Down Expand Up @@ -95,11 +93,11 @@ void forEach(i32 count, i32 step, const F& f)
return;
}

volatile i32 offset = 0;
AtomicI32 offset = 0;

jobs::runOnWorkers([&](){
for(;;) {
const i32 idx = atomicAdd(&offset, step);
const i32 idx = offset.add(step);
if (idx >= count) break;
i32 to = idx + step;
to = to > count ? count : to;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/page_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void PageAllocator::unlock()

void* PageAllocator::allocate(bool lock)
{
atomicIncrement(&allocated_count);
allocated_count.inc();

void* p;
if (free_pages.pop(p)) return p;
Expand All @@ -65,7 +65,7 @@ void* PageAllocator::allocate(bool lock)

void PageAllocator::deallocate(void* mem, bool lock)
{
atomicDecrement(&allocated_count);
allocated_count.dec();
free_pages.push(mem, lock ? &mutex : nullptr);
}

Expand Down
5 changes: 3 additions & 2 deletions src/engine/page_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct LUMIX_ENGINE_API PageAllocator final
void unlock();

private:
volatile i32 allocated_count = 0;
AtomicI32 allocated_count = 0;
u32 reserved_count = 0;
RingBuffer<void*, 512> free_pages;
Mutex mutex;
Expand All @@ -47,10 +47,11 @@ struct PagedListIterator
for (;;) {
volatile T* tmp = value;
if(!tmp) return nullptr;
if (compareAndExchange64((volatile i64*)&value, (i64)tmp->header.next, (i64)tmp)) return (T*)tmp;
if (compareExchangePtr((volatile void**)&value, (void*)tmp->header.next, (void*)tmp)) return (T*)tmp;
}
}

private:
volatile T* value;
};

Expand Down
15 changes: 8 additions & 7 deletions src/engine/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <evntcons.h>
#endif

#include "engine/atomic.h"
#include "engine/array.h"
#include "engine/crt.h"
#include "engine/hash_map.h"
Expand Down Expand Up @@ -182,7 +183,7 @@ static struct Instance
u64 paused_time = 0;
u64 last_frame_duration = 0;
u64 last_frame_time = 0;
volatile i32 fiber_wait_id = 0;
AtomicI32 fiber_wait_id = 0;
TraceTask trace_task;
ThreadContext global_context;
} g_instance;
Expand Down Expand Up @@ -363,18 +364,18 @@ void blockColor(u8 r, u8 g, u8 b)
write(*ctx, EventType::BLOCK_COLOR, color);
}

static volatile i32 last_block_id = 0;

static void continueBlock(i32 block_id) {
ThreadContext* ctx = g_instance.getThreadContext();
ctx->open_blocks.push(block_id);
write(*ctx, EventType::CONTINUE_BLOCK, block_id);
}

static AtomicI32 last_block_id = 0;

void beginBlock(const char* name)
{
BlockRecord r;
r.id = atomicIncrement(&last_block_id);
r.id = last_block_id.inc();
r.name = name;
ThreadContext* ctx = g_instance.getThreadContext();
ctx->open_blocks.push(r.id);
Expand Down Expand Up @@ -402,8 +403,8 @@ void endGPUBlock(u64 timestamp)

i64 createNewLinkID()
{
static i64 counter = 0;
return atomicIncrement(&counter);
AtomicI64 counter = 0;
return counter.inc();
}


Expand Down Expand Up @@ -447,7 +448,7 @@ void signalTriggered(i32 job_system_signal) {
FiberSwitchData beginFiberWait(i32 job_system_signal, bool is_mutex)
{
FiberWaitRecord r;
r.id = atomicIncrement(&g_instance.fiber_wait_id);
r.id = g_instance.fiber_wait_id.inc();
r.job_system_signal = job_system_signal;
r.is_mutex = is_mutex;

Expand Down
8 changes: 4 additions & 4 deletions src/engine/ring_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct RingBuffer {
return false;
}
else if (seq == pos + 1) {
if (compareAndExchange(&rd, pos + 1, pos)) break;
if (rd.compareExchange(pos + 1, pos)) break;
}
else {
pos = rd;
Expand All @@ -58,7 +58,7 @@ struct RingBuffer {
}
else if (seq == pos) {
// we can try to push
if (compareAndExchange(&wr, pos + 1, pos)) break;
if (wr.compareExchange(pos + 1, pos)) break;
}
else {
// somebody pushed before us, try again
Expand All @@ -77,8 +77,8 @@ struct RingBuffer {
}

Item objects[CAPACITY];
volatile i32 rd = 0;
volatile i32 wr = 0;
AtomicI32 rd = 0;
AtomicI32 wr = 0;
Array<T> m_fallback;
};

Expand Down
Loading

0 comments on commit 497fc96

Please sign in to comment.