Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add BSNonReentrantSpinLock #170

Merged
merged 2 commits into from
Oct 19, 2023
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
69 changes: 22 additions & 47 deletions CommonLibSF/include/RE/B/BSLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@

namespace RE
{
class BSReadWriteLock
class BSNonReentrantSpinLock
{
public:
void lock_read()
{
using func_t = decltype(&BSReadWriteLock::lock_read);
REL::Relocation<func_t> func{ REL::ID(178605) };
return func(this);
}
void lock();
[[nodiscard]] bool try_lock();
void unlock();

void lock_write()
{
using func_t = decltype(&BSReadWriteLock::lock_write);
REL::Relocation<func_t> func{ REL::ID(34125) };
return func(this);
}

void unlock_read()
{
using func_t = decltype(&BSReadWriteLock::unlock_read);
REL::Relocation<func_t> func{ REL::ID(178609) };
return func(this);
}
private:
// members
volatile std::uint32_t _lock{ 0 }; // 0
};
static_assert(sizeof(BSNonReentrantSpinLock) == 0x4);

void unlock_write()
{
using func_t = decltype(&BSReadWriteLock::unlock_write);
REL::Relocation<func_t> func{ REL::ID(34257) };
return func(this);
}
class BSReadWriteLock
{
public:
void lock_read();
void lock_write();
void unlock_read();
void unlock_write();

private:
// members
Expand All @@ -43,27 +33,12 @@ namespace RE
class BSSpinLock
{
public:
void lock()
{
using func_t = decltype(&BSSpinLock::lock);
REL::Relocation<func_t> func{ REL::ID(178543) };
return func(this);
}

[[nodiscard]] bool try_lock()
{
using func_t = decltype(&BSSpinLock::try_lock);
REL::Relocation<func_t> func{ REL::ID(178545) };
return func(this);
}

void unlock()
{
using func_t = decltype(&BSSpinLock::unlock);
REL::Relocation<func_t> func{ REL::ID(178544) };
return func(this);
}
void lock();
[[nodiscard]] bool try_lock();
void unlock();

private:
// members
std::uint32_t _owningThread{ 0 }; // 0
volatile std::uint32_t _lock{ 0 }; // 4
};
Expand Down Expand Up @@ -128,7 +103,7 @@ namespace RE
// members
mutex_type* _lock{ nullptr }; // 00
};
static_assert(sizeof(BSAutoLock<BSSpinLock>) == 0x8);
static_assert(sizeof(BSAutoLock<void*>) == 0x8);

template <class Mutex>
BSAutoLock(Mutex&) -> BSAutoLock<Mutex>;
Expand Down
74 changes: 74 additions & 0 deletions CommonLibSF/src/RE/B/BSLock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "RE/B/BSLock.h"

namespace RE
{
void BSNonReentrantSpinLock::lock()
{
using func_t = decltype(&BSNonReentrantSpinLock::lock);
REL::Relocation<func_t> func{ REL::ID(73879) };
return func(this);
}

bool BSNonReentrantSpinLock::try_lock()
{
using func_t = decltype(&BSNonReentrantSpinLock::try_lock);
REL::Relocation<func_t> func{ REL::ID(74235) };
return func(this);
}

void BSNonReentrantSpinLock::unlock()
{
using func_t = decltype(&BSNonReentrantSpinLock::unlock);
REL::Relocation<func_t> func{ REL::ID(73895) };
return func(this);
}

void BSReadWriteLock::lock_read()
{
using func_t = decltype(&BSReadWriteLock::lock_read);
REL::Relocation<func_t> func{ REL::ID(178605) };
return func(this);
}

void BSReadWriteLock::lock_write()
{
using func_t = decltype(&BSReadWriteLock::lock_write);
REL::Relocation<func_t> func{ REL::ID(34125) };
return func(this);
}

void BSReadWriteLock::unlock_read()
{
using func_t = decltype(&BSReadWriteLock::unlock_read);
REL::Relocation<func_t> func{ REL::ID(178609) };
return func(this);
}

void BSReadWriteLock::unlock_write()
{
using func_t = decltype(&BSReadWriteLock::unlock_write);
REL::Relocation<func_t> func{ REL::ID(34257) };
return func(this);
}

void BSSpinLock::lock()
{
using func_t = decltype(&BSSpinLock::lock);
REL::Relocation<func_t> func{ REL::ID(178543) };
return func(this);
}

bool BSSpinLock::try_lock()
{
using func_t = decltype(&BSSpinLock::try_lock);
REL::Relocation<func_t> func{ REL::ID(178545) };
return func(this);
}

void BSSpinLock::unlock()
{
using func_t = decltype(&BSSpinLock::unlock);
REL::Relocation<func_t> func{ REL::ID(178544) };
return func(this);
}
}