2017-10-04 13:41:02 +00:00
|
|
|
#ifndef __TRACYLOCK_HPP__
|
|
|
|
#define __TRACYLOCK_HPP__
|
|
|
|
|
2017-10-04 14:51:51 +00:00
|
|
|
#include <atomic>
|
2017-10-13 18:05:38 +00:00
|
|
|
#include <limits>
|
2017-10-04 14:51:51 +00:00
|
|
|
|
2017-10-04 14:45:46 +00:00
|
|
|
#include "../common/TracySystem.hpp"
|
2017-10-04 13:41:02 +00:00
|
|
|
#include "TracyProfiler.hpp"
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2017-10-24 20:02:49 +00:00
|
|
|
extern std::atomic<uint32_t> s_lockCounter;
|
2017-10-04 14:51:51 +00:00
|
|
|
|
2017-10-04 13:41:02 +00:00
|
|
|
template<class T>
|
|
|
|
class Lockable
|
|
|
|
{
|
|
|
|
public:
|
2017-10-06 14:33:11 +00:00
|
|
|
tracy_force_inline Lockable( const SourceLocation* srcloc )
|
2017-10-04 14:51:51 +00:00
|
|
|
: m_id( s_lockCounter.fetch_add( 1, std::memory_order_relaxed ) )
|
2017-10-04 13:41:02 +00:00
|
|
|
{
|
2017-10-13 18:05:38 +00:00
|
|
|
assert( m_id != std::numeric_limits<uint32_t>::max() );
|
2017-12-10 20:37:39 +00:00
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
|
|
|
item->hdr.type = QueueType::LockAnnounce;
|
|
|
|
item->lockAnnounce.id = m_id;
|
|
|
|
item->lockAnnounce.lckloc = (uint64_t)srcloc;
|
|
|
|
item->lockAnnounce.type = LockType::Lockable;
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-04 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Lockable( const Lockable& ) = delete;
|
|
|
|
Lockable& operator=( const Lockable& ) = delete;
|
|
|
|
|
2017-10-06 14:33:11 +00:00
|
|
|
tracy_force_inline void lock()
|
2017-10-04 13:41:02 +00:00
|
|
|
{
|
2017-10-04 14:45:46 +00:00
|
|
|
const auto thread = GetThreadHandle();
|
|
|
|
{
|
|
|
|
Magic magic;
|
2017-10-10 23:44:35 +00:00
|
|
|
auto& token = s_token.ptr;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2017-10-04 14:45:46 +00:00
|
|
|
item->hdr.type = QueueType::LockWait;
|
2017-10-04 14:51:51 +00:00
|
|
|
item->lockWait.id = m_id;
|
2017-10-04 14:45:46 +00:00
|
|
|
item->lockWait.thread = thread;
|
2017-10-29 15:12:16 +00:00
|
|
|
item->lockWait.time = Profiler::GetTime();
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-04 14:45:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 13:41:02 +00:00
|
|
|
m_lockable.lock();
|
2017-10-04 14:45:46 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
Magic magic;
|
2017-10-10 23:44:35 +00:00
|
|
|
auto& token = s_token.ptr;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2017-10-04 14:45:46 +00:00
|
|
|
item->hdr.type = QueueType::LockObtain;
|
2017-10-04 14:51:51 +00:00
|
|
|
item->lockObtain.id = m_id;
|
2017-10-04 14:45:46 +00:00
|
|
|
item->lockObtain.thread = thread;
|
2017-10-29 15:12:16 +00:00
|
|
|
item->lockObtain.time = Profiler::GetTime();
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-04 14:45:46 +00:00
|
|
|
}
|
2017-10-04 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 14:33:11 +00:00
|
|
|
tracy_force_inline void unlock()
|
2017-10-04 13:41:02 +00:00
|
|
|
{
|
|
|
|
m_lockable.unlock();
|
2017-10-04 14:45:46 +00:00
|
|
|
|
|
|
|
Magic magic;
|
2017-10-10 23:44:35 +00:00
|
|
|
auto& token = s_token.ptr;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2017-10-04 14:45:46 +00:00
|
|
|
item->hdr.type = QueueType::LockRelease;
|
2017-10-04 14:51:51 +00:00
|
|
|
item->lockRelease.id = m_id;
|
2017-10-04 14:45:46 +00:00
|
|
|
item->lockRelease.thread = GetThreadHandle();
|
2017-10-29 15:12:16 +00:00
|
|
|
item->lockRelease.time = Profiler::GetTime();
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-04 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 14:33:11 +00:00
|
|
|
tracy_force_inline bool try_lock()
|
2017-10-04 13:41:02 +00:00
|
|
|
{
|
2017-10-04 14:45:46 +00:00
|
|
|
const auto ret = m_lockable.try_lock();
|
|
|
|
if( ret )
|
|
|
|
{
|
|
|
|
Magic magic;
|
2017-10-10 23:44:35 +00:00
|
|
|
auto& token = s_token.ptr;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2017-10-04 14:45:46 +00:00
|
|
|
item->hdr.type = QueueType::LockObtain;
|
|
|
|
item->lockObtain.id = (uint64_t)&m_lockable;
|
|
|
|
item->lockObtain.thread = GetThreadHandle();
|
2017-10-29 15:12:16 +00:00
|
|
|
item->lockObtain.time = Profiler::GetTime();
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-04 14:45:46 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2017-10-04 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 15:14:57 +00:00
|
|
|
tracy_force_inline void Mark( const SourceLocation* srcloc ) const
|
2017-10-06 14:32:32 +00:00
|
|
|
{
|
|
|
|
Magic magic;
|
2017-10-10 23:44:35 +00:00
|
|
|
auto& token = s_token.ptr;
|
2017-10-10 23:27:22 +00:00
|
|
|
auto& tail = token->get_tail_index();
|
2017-10-10 23:04:21 +00:00
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
2017-10-06 14:32:32 +00:00
|
|
|
item->hdr.type = QueueType::LockMark;
|
|
|
|
item->lockMark.id = m_id;
|
|
|
|
item->lockMark.thread = GetThreadHandle();
|
|
|
|
item->lockMark.srcloc = (uint64_t)srcloc;
|
2017-10-10 23:27:22 +00:00
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
2017-10-06 14:32:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 13:41:02 +00:00
|
|
|
private:
|
|
|
|
T m_lockable;
|
2017-10-13 18:05:38 +00:00
|
|
|
uint32_t m_id;
|
2017-10-04 13:41:02 +00:00
|
|
|
};
|
|
|
|
|
2017-12-10 20:49:45 +00:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class SharedLockable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
tracy_force_inline SharedLockable( const SourceLocation* srcloc )
|
|
|
|
: m_id( s_lockCounter.fetch_add( 1, std::memory_order_relaxed ) )
|
|
|
|
{
|
|
|
|
assert( m_id != std::numeric_limits<uint32_t>::max() );
|
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
|
|
|
item->hdr.type = QueueType::LockAnnounce;
|
|
|
|
item->lockAnnounce.id = m_id;
|
|
|
|
item->lockAnnounce.lckloc = (uint64_t)srcloc;
|
|
|
|
item->lockAnnounce.type = LockType::SharedLockable;
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedLockable( const SharedLockable& ) = delete;
|
|
|
|
SharedLockable& operator=( const SharedLockable& ) = delete;
|
|
|
|
|
|
|
|
tracy_force_inline void lock()
|
|
|
|
{
|
|
|
|
const auto thread = GetThreadHandle();
|
|
|
|
{
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
|
|
|
item->hdr.type = QueueType::LockWait;
|
|
|
|
item->lockWait.id = m_id;
|
|
|
|
item->lockWait.thread = thread;
|
|
|
|
item->lockWait.time = Profiler::GetTime();
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_lockable.lock();
|
|
|
|
|
|
|
|
{
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
|
|
|
item->hdr.type = QueueType::LockObtain;
|
|
|
|
item->lockObtain.id = m_id;
|
|
|
|
item->lockObtain.thread = thread;
|
|
|
|
item->lockObtain.time = Profiler::GetTime();
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void unlock()
|
|
|
|
{
|
|
|
|
m_lockable.unlock();
|
|
|
|
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
|
|
|
item->hdr.type = QueueType::LockRelease;
|
|
|
|
item->lockRelease.id = m_id;
|
|
|
|
item->lockRelease.thread = GetThreadHandle();
|
|
|
|
item->lockRelease.time = Profiler::GetTime();
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline bool try_lock()
|
|
|
|
{
|
|
|
|
const auto ret = m_lockable.try_lock();
|
|
|
|
if( ret )
|
|
|
|
{
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
|
|
|
item->hdr.type = QueueType::LockObtain;
|
|
|
|
item->lockObtain.id = (uint64_t)&m_lockable;
|
|
|
|
item->lockObtain.thread = GetThreadHandle();
|
|
|
|
item->lockObtain.time = Profiler::GetTime();
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void lock_shared()
|
|
|
|
{
|
|
|
|
m_lockable.lock_shared();
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void unlock_shared()
|
|
|
|
{
|
|
|
|
m_lockable.unlock_shared();
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline bool try_lock_shared()
|
|
|
|
{
|
|
|
|
const auto ret = m_lockable.try_lock_shared();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void Mark( const SourceLocation* srcloc ) const
|
|
|
|
{
|
|
|
|
Magic magic;
|
|
|
|
auto& token = s_token.ptr;
|
|
|
|
auto& tail = token->get_tail_index();
|
|
|
|
auto item = token->enqueue_begin<moodycamel::CanAlloc>( magic );
|
|
|
|
item->hdr.type = QueueType::LockMark;
|
|
|
|
item->lockMark.id = m_id;
|
|
|
|
item->lockMark.thread = GetThreadHandle();
|
|
|
|
item->lockMark.srcloc = (uint64_t)srcloc;
|
|
|
|
tail.store( magic + 1, std::memory_order_release );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_lockable;
|
|
|
|
uint32_t m_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-10-04 13:41:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|