Skip to content

Commit

Permalink
xrCore/Threading/Lock.hpp: add move constructor and move assign operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed May 25, 2024
1 parent 8c6b60e commit d127ae2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/xrCore/Threading/Lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ Lock::Lock() : impl(xr_new<LockImpl>()), lockCounter(0) {}

Lock::~Lock() { xr_delete(impl); }

Lock::Lock(Lock&& other) noexcept(false)
{
xr_delete(impl);
impl = other.impl;
lockCounter.store(other.lockCounter.load(std::memory_order_acquire), std::memory_order_release);
other.impl = xr_new<LockImpl>();
other.lockCounter.store(0, std::memory_order_release);
}

Lock& Lock::operator=(Lock&& other) noexcept(false)
{
xr_delete(impl);
impl = other.impl;
lockCounter.store(other.lockCounter.load(std::memory_order_acquire), std::memory_order_release);
other.impl = xr_new<LockImpl>();
other.lockCounter.store(0, std::memory_order_release);
return *this;
}

void Lock::Enter()
{
impl->Lock();
Expand Down
11 changes: 9 additions & 2 deletions src/xrCore/Threading/Lock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ void XRCORE_API set_add_profile_portion(add_profile_portion_callback callback);
#define MUTEX_PROFILE_ID(a) MACRO_TO_STRING(CONCATENIZE(MUTEX_PROFILE_PREFIX_ID, a))
#endif // CONFIG_PROFILE_LOCKS

class XRCORE_API Lock : Noncopyable
class XRCORE_API Lock
{
struct LockImpl* impl;
struct LockImpl* impl{};

public:
#ifdef CONFIG_PROFILE_LOCKS
Lock(const char* id);
Expand All @@ -23,6 +24,12 @@ class XRCORE_API Lock : Noncopyable
#endif
~Lock();

Lock(Lock& other) = delete;
Lock& operator=(Lock& other) = delete;

Lock(Lock&& other) noexcept(false);
Lock& operator=(Lock&& other) noexcept(false);

#ifdef CONFIG_PROFILE_LOCKS
void Enter();
#else
Expand Down

0 comments on commit d127ae2

Please sign in to comment.