123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #pragma once
- #include "../C/Baselib_ReentrantLock.h"
- #include "Time.h"
- namespace baselib
- {
- BASELIB_CPP_INTERFACE
- {
-
-
-
-
-
-
-
-
-
-
- class ReentrantLock
- {
- public:
-
- ReentrantLock(const ReentrantLock& other) = delete;
- ReentrantLock& operator=(const ReentrantLock& other) = delete;
-
- ReentrantLock(ReentrantLock&& other) = delete;
- ReentrantLock& operator=(ReentrantLock&& other) = delete;
-
-
- ReentrantLock() : m_ReentrantLockData(Baselib_ReentrantLock_Create())
- {
- }
-
-
-
-
- ~ReentrantLock()
- {
- Baselib_ReentrantLock_Free(&m_ReentrantLockData);
- }
-
-
-
-
-
-
-
- inline void Acquire()
- {
- return Baselib_ReentrantLock_Acquire(&m_ReentrantLockData);
- }
-
-
-
-
-
-
-
- COMPILER_WARN_UNUSED_RESULT
- FORCE_INLINE bool TryAcquire()
- {
- return Baselib_ReentrantLock_TryAcquire(&m_ReentrantLockData);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- COMPILER_WARN_UNUSED_RESULT
- FORCE_INLINE bool TryTimedAcquire(const timeout_ms timeoutInMilliseconds)
- {
- return Baselib_ReentrantLock_TryTimedAcquire(&m_ReentrantLockData, timeoutInMilliseconds.count());
- }
-
-
-
-
-
-
-
- FORCE_INLINE void Release()
- {
- return Baselib_ReentrantLock_Release(&m_ReentrantLockData);
- }
-
-
-
-
-
-
-
-
-
- template<class FunctionType>
- FORCE_INLINE void AcquireScoped(const FunctionType& func)
- {
- ReleaseOnDestroy releaseScope(*this);
- Acquire();
- func();
- }
-
-
-
-
-
-
-
-
-
-
-
-
- template<class FunctionType>
- FORCE_INLINE bool TryAcquireScoped(const FunctionType& func)
- {
- if (TryAcquire())
- {
- ReleaseOnDestroy releaseScope(*this);
- func();
- return true;
- }
- return false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class FunctionType>
- FORCE_INLINE bool TryTimedAcquireScoped(const timeout_ms timeoutInMilliseconds, const FunctionType& func)
- {
- if (TryTimedAcquire(timeoutInMilliseconds))
- {
- ReleaseOnDestroy releaseScope(*this);
- func();
- return true;
- }
- return false;
- }
- private:
- class ReleaseOnDestroy
- {
- public:
- FORCE_INLINE ReleaseOnDestroy(ReentrantLock& lockReference) : m_LockReference(lockReference) {}
- FORCE_INLINE ~ReleaseOnDestroy() { m_LockReference.Release(); }
- private:
- ReentrantLock& m_LockReference;
- };
- Baselib_ReentrantLock m_ReentrantLockData;
- };
- }
- }
|