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"
|
2018-03-31 12:03:55 +00:00
|
|
|
#include "../common/TracyAlign.hpp"
|
2017-10-04 13:41:02 +00:00
|
|
|
#include "TracyProfiler.hpp"
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
class LockableCtx
|
2017-10-04 13:41:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline LockableCtx( const SourceLocationData* srcloc )
|
2019-02-19 18:33:37 +00:00
|
|
|
: m_id( GetLockCounter().fetch_add( 1, std::memory_order_relaxed ) )
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
, m_lockCount( 0 )
|
|
|
|
, m_active( false )
|
|
|
|
#endif
|
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
|
|
|
|
2020-07-26 11:45:53 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockAnnounce );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockAnnounce.id, m_id );
|
2018-12-16 19:33:18 +00:00
|
|
|
MemWrite( &item->lockAnnounce.time, Profiler::GetTime() );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockAnnounce.lckloc, (uint64_t)srcloc );
|
|
|
|
MemWrite( &item->lockAnnounce.type, LockType::Lockable );
|
2018-07-11 10:24:58 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().DeferItem( *item );
|
2018-07-10 18:42:30 +00:00
|
|
|
#endif
|
2020-07-26 11:45:53 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-10-04 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
LockableCtx( const LockableCtx& ) = delete;
|
|
|
|
LockableCtx& operator=( const LockableCtx& ) = delete;
|
2017-10-04 13:41:02 +00:00
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline ~LockableCtx()
|
2018-12-16 19:37:48 +00:00
|
|
|
{
|
2020-07-26 11:45:53 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockTerminate );
|
2018-12-16 19:37:48 +00:00
|
|
|
MemWrite( &item->lockTerminate.id, m_id );
|
|
|
|
MemWrite( &item->lockTerminate.time, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->lockTerminate.type, LockType::Lockable );
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().DeferItem( *item );
|
2018-12-16 19:37:48 +00:00
|
|
|
#endif
|
2020-07-26 11:45:53 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2018-12-16 19:37:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline bool BeforeLock()
|
2017-10-04 13:41:02 +00:00
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
bool queue = false;
|
|
|
|
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
|
|
|
|
const auto active = m_active.load( std::memory_order_relaxed );
|
|
|
|
if( locks == 0 || active )
|
|
|
|
{
|
2019-02-19 17:38:08 +00:00
|
|
|
const bool connected = GetProfiler().IsConnected();
|
2018-07-11 12:02:41 +00:00
|
|
|
if( active != connected ) m_active.store( connected, std::memory_order_relaxed );
|
|
|
|
if( connected ) queue = true;
|
|
|
|
}
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !queue ) return false;
|
2018-07-11 12:02:41 +00:00
|
|
|
#endif
|
2017-10-04 14:45:46 +00:00
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockWait );
|
|
|
|
MemWrite( &item->lockWait.thread, GetThreadHandle() );
|
|
|
|
MemWrite( &item->lockWait.id, m_id );
|
|
|
|
MemWrite( &item->lockWait.time, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->lockWait.type, LockType::Lockable );
|
|
|
|
Profiler::QueueSerialFinish();
|
|
|
|
return true;
|
2017-10-04 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterLock()
|
2017-10-04 13:41:02 +00:00
|
|
|
{
|
2019-09-21 12:55:14 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockObtain );
|
|
|
|
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
|
|
|
|
MemWrite( &item->lockObtain.id, m_id );
|
|
|
|
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
|
|
|
|
Profiler::QueueSerialFinish();
|
|
|
|
}
|
2017-10-04 14:45:46 +00:00
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterUnlock()
|
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
m_lockCount.fetch_sub( 1, std::memory_order_relaxed );
|
|
|
|
if( !m_active.load( std::memory_order_relaxed ) ) return;
|
2019-02-19 17:38:08 +00:00
|
|
|
if( !GetProfiler().IsConnected() )
|
2018-07-11 12:02:41 +00:00
|
|
|
{
|
|
|
|
m_active.store( false, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-12 11:51:01 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::LockRelease );
|
2019-08-12 11:51:01 +00:00
|
|
|
MemWrite( &item->lockRelease.thread, GetThreadHandle() );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockRelease.id, m_id );
|
|
|
|
MemWrite( &item->lockRelease.time, Profiler::GetTime() );
|
2019-08-12 11:51:01 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-10-04 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterTryLock( bool acquired )
|
2017-10-04 13:41:02 +00:00
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !acquired ) return;
|
2018-07-11 12:02:41 +00:00
|
|
|
|
|
|
|
bool queue = false;
|
|
|
|
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
|
|
|
|
const auto active = m_active.load( std::memory_order_relaxed );
|
|
|
|
if( locks == 0 || active )
|
|
|
|
{
|
2019-02-19 17:38:08 +00:00
|
|
|
const bool connected = GetProfiler().IsConnected();
|
2018-07-11 12:02:41 +00:00
|
|
|
if( active != connected ) m_active.store( connected, std::memory_order_relaxed );
|
|
|
|
if( connected ) queue = true;
|
|
|
|
}
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !queue ) return;
|
2018-07-11 12:02:41 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
if( acquired )
|
2017-10-04 14:45:46 +00:00
|
|
|
{
|
2019-08-12 11:51:01 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::LockObtain );
|
2019-08-12 11:51:01 +00:00
|
|
|
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
|
2018-03-31 12:13:46 +00:00
|
|
|
MemWrite( &item->lockObtain.id, m_id );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
|
2019-08-12 11:51:01 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-10-04 14:45:46 +00:00
|
|
|
}
|
2017-10-04 13:41:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 22:34:04 +00:00
|
|
|
tracy_force_inline void Mark( const SourceLocationData* srcloc )
|
2017-10-06 14:32:32 +00:00
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
const auto active = m_active.load( std::memory_order_relaxed );
|
|
|
|
if( !active ) return;
|
2019-02-19 17:38:08 +00:00
|
|
|
const auto connected = GetProfiler().IsConnected();
|
2018-07-11 12:02:41 +00:00
|
|
|
if( !connected )
|
|
|
|
{
|
|
|
|
if( active ) m_active.store( false, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-12 11:51:01 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::LockMark );
|
2019-08-12 11:51:01 +00:00
|
|
|
MemWrite( &item->lockMark.thread, GetThreadHandle() );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockMark.id, m_id );
|
|
|
|
MemWrite( &item->lockMark.srcloc, (uint64_t)srcloc );
|
2019-08-12 11:51:01 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-10-06 14:32:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 12:47:38 +00:00
|
|
|
tracy_force_inline void CustomName( const char* name, size_t size )
|
|
|
|
{
|
2020-07-25 23:22:09 +00:00
|
|
|
assert( size < std::numeric_limits<uint16_t>::max() );
|
|
|
|
auto ptr = (char*)tracy_malloc( size );
|
2020-03-08 12:47:38 +00:00
|
|
|
memcpy( ptr, name, size );
|
2020-07-26 11:45:53 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockName );
|
2020-07-25 23:22:09 +00:00
|
|
|
MemWrite( &item->lockNameFat.id, m_id );
|
|
|
|
MemWrite( &item->lockNameFat.name, (uint64_t)ptr );
|
|
|
|
MemWrite( &item->lockNameFat.size, (uint16_t)size );
|
2020-03-08 12:47:38 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
GetProfiler().DeferItem( *item );
|
|
|
|
#endif
|
2020-07-26 11:45:53 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2020-03-08 12:47:38 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 13:41:02 +00:00
|
|
|
private:
|
2017-10-13 18:05:38 +00:00
|
|
|
uint32_t m_id;
|
2018-07-11 12:02:41 +00:00
|
|
|
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
std::atomic<uint32_t> m_lockCount;
|
|
|
|
std::atomic<bool> m_active;
|
|
|
|
#endif
|
2017-10-04 13:41:02 +00:00
|
|
|
};
|
|
|
|
|
2017-12-10 20:49:45 +00:00
|
|
|
template<class T>
|
2019-09-21 12:55:14 +00:00
|
|
|
class Lockable
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline Lockable( const SourceLocationData* srcloc )
|
|
|
|
: m_ctx( srcloc )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Lockable( const Lockable& ) = delete;
|
|
|
|
Lockable& operator=( const Lockable& ) = delete;
|
|
|
|
|
|
|
|
tracy_force_inline void lock()
|
|
|
|
{
|
|
|
|
const auto runAfter = m_ctx.BeforeLock();
|
|
|
|
m_lockable.lock();
|
|
|
|
if( runAfter ) m_ctx.AfterLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void unlock()
|
|
|
|
{
|
|
|
|
m_lockable.unlock();
|
|
|
|
m_ctx.AfterUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline bool try_lock()
|
|
|
|
{
|
|
|
|
const auto acquired = m_lockable.try_lock();
|
|
|
|
m_ctx.AfterTryLock( acquired );
|
|
|
|
return acquired;
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void Mark( const SourceLocationData* srcloc )
|
|
|
|
{
|
|
|
|
m_ctx.Mark( srcloc );
|
|
|
|
}
|
|
|
|
|
2020-03-08 12:47:38 +00:00
|
|
|
tracy_force_inline void CustomName( const char* name, size_t size )
|
|
|
|
{
|
|
|
|
m_ctx.CustomName( name, size );
|
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
private:
|
|
|
|
T m_lockable;
|
|
|
|
LockableCtx m_ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SharedLockableCtx
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
tracy_force_inline SharedLockableCtx( const SourceLocationData* srcloc )
|
2019-02-19 18:33:37 +00:00
|
|
|
: m_id( GetLockCounter().fetch_add( 1, std::memory_order_relaxed ) )
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
, m_lockCount( 0 )
|
|
|
|
, m_active( false )
|
|
|
|
#endif
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
|
|
|
assert( m_id != std::numeric_limits<uint32_t>::max() );
|
|
|
|
|
2020-07-26 11:45:53 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockAnnounce );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockAnnounce.id, m_id );
|
2018-12-16 19:33:18 +00:00
|
|
|
MemWrite( &item->lockAnnounce.time, Profiler::GetTime() );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockAnnounce.lckloc, (uint64_t)srcloc );
|
|
|
|
MemWrite( &item->lockAnnounce.type, LockType::SharedLockable );
|
2018-07-11 10:24:58 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().DeferItem( *item );
|
2018-07-10 18:42:30 +00:00
|
|
|
#endif
|
2020-07-26 11:45:53 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-12-10 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
SharedLockableCtx( const SharedLockableCtx& ) = delete;
|
|
|
|
SharedLockableCtx& operator=( const SharedLockableCtx& ) = delete;
|
2017-12-10 20:49:45 +00:00
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline ~SharedLockableCtx()
|
2018-12-16 19:37:48 +00:00
|
|
|
{
|
2020-07-26 11:45:53 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockTerminate );
|
2018-12-16 19:37:48 +00:00
|
|
|
MemWrite( &item->lockTerminate.id, m_id );
|
|
|
|
MemWrite( &item->lockTerminate.time, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->lockTerminate.type, LockType::SharedLockable );
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-02-19 17:38:08 +00:00
|
|
|
GetProfiler().DeferItem( *item );
|
2018-12-16 19:37:48 +00:00
|
|
|
#endif
|
2020-07-26 11:45:53 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2018-12-16 19:37:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline bool BeforeLock()
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
bool queue = false;
|
|
|
|
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
|
|
|
|
const auto active = m_active.load( std::memory_order_relaxed );
|
|
|
|
if( locks == 0 || active )
|
|
|
|
{
|
2019-02-19 17:38:08 +00:00
|
|
|
const bool connected = GetProfiler().IsConnected();
|
2018-07-11 12:02:41 +00:00
|
|
|
if( active != connected ) m_active.store( connected, std::memory_order_relaxed );
|
|
|
|
if( connected ) queue = true;
|
|
|
|
}
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !queue ) return false;
|
2018-07-11 12:02:41 +00:00
|
|
|
#endif
|
2019-08-12 11:51:01 +00:00
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockWait );
|
|
|
|
MemWrite( &item->lockWait.thread, GetThreadHandle() );
|
|
|
|
MemWrite( &item->lockWait.id, m_id );
|
|
|
|
MemWrite( &item->lockWait.time, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->lockWait.type, LockType::SharedLockable );
|
|
|
|
Profiler::QueueSerialFinish();
|
|
|
|
return true;
|
2017-12-10 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterLock()
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
2019-09-21 12:55:14 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockObtain );
|
|
|
|
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
|
|
|
|
MemWrite( &item->lockObtain.id, m_id );
|
|
|
|
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
|
|
|
|
Profiler::QueueSerialFinish();
|
|
|
|
}
|
2017-12-10 20:49:45 +00:00
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterUnlock()
|
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
m_lockCount.fetch_sub( 1, std::memory_order_relaxed );
|
|
|
|
if( !m_active.load( std::memory_order_relaxed ) ) return;
|
2019-02-19 17:38:08 +00:00
|
|
|
if( !GetProfiler().IsConnected() )
|
2018-07-11 12:02:41 +00:00
|
|
|
{
|
|
|
|
m_active.store( false, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-12 11:51:01 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
2018-03-31 12:19:45 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::LockRelease );
|
2019-08-12 11:51:01 +00:00
|
|
|
MemWrite( &item->lockRelease.thread, GetThreadHandle() );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockRelease.id, m_id );
|
|
|
|
MemWrite( &item->lockRelease.time, Profiler::GetTime() );
|
2019-08-12 11:51:01 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-12-10 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterTryLock( bool acquired )
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !acquired ) return;
|
2018-07-11 12:02:41 +00:00
|
|
|
|
|
|
|
bool queue = false;
|
|
|
|
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
|
|
|
|
const auto active = m_active.load( std::memory_order_relaxed );
|
|
|
|
if( locks == 0 || active )
|
|
|
|
{
|
2019-02-19 17:38:08 +00:00
|
|
|
const bool connected = GetProfiler().IsConnected();
|
2018-07-11 12:02:41 +00:00
|
|
|
if( active != connected ) m_active.store( connected, std::memory_order_relaxed );
|
|
|
|
if( connected ) queue = true;
|
|
|
|
}
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !queue ) return;
|
2018-07-11 12:02:41 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
if( acquired )
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
2019-08-12 11:51:01 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::LockObtain );
|
2019-08-12 11:51:01 +00:00
|
|
|
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
|
2018-03-31 12:13:46 +00:00
|
|
|
MemWrite( &item->lockObtain.id, m_id );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
|
2019-08-12 11:51:01 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-12-10 20:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline bool BeforeLockShared()
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
bool queue = false;
|
|
|
|
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
|
|
|
|
const auto active = m_active.load( std::memory_order_relaxed );
|
|
|
|
if( locks == 0 || active )
|
|
|
|
{
|
2019-02-19 17:38:08 +00:00
|
|
|
const bool connected = GetProfiler().IsConnected();
|
2018-07-11 12:02:41 +00:00
|
|
|
if( active != connected ) m_active.store( connected, std::memory_order_relaxed );
|
|
|
|
if( connected ) queue = true;
|
|
|
|
}
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !queue ) return false;
|
2018-07-11 12:02:41 +00:00
|
|
|
#endif
|
2017-12-10 20:56:19 +00:00
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockSharedWait );
|
|
|
|
MemWrite( &item->lockWait.thread, GetThreadHandle() );
|
|
|
|
MemWrite( &item->lockWait.id, m_id );
|
|
|
|
MemWrite( &item->lockWait.time, Profiler::GetTime() );
|
|
|
|
MemWrite( &item->lockWait.type, LockType::SharedLockable );
|
|
|
|
Profiler::QueueSerialFinish();
|
|
|
|
return true;
|
2017-12-10 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterLockShared()
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
2019-09-21 12:55:14 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockSharedObtain );
|
|
|
|
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
|
|
|
|
MemWrite( &item->lockObtain.id, m_id );
|
|
|
|
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
|
|
|
|
Profiler::QueueSerialFinish();
|
|
|
|
}
|
2017-12-10 20:56:19 +00:00
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterUnlockShared()
|
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
m_lockCount.fetch_sub( 1, std::memory_order_relaxed );
|
|
|
|
if( !m_active.load( std::memory_order_relaxed ) ) return;
|
2019-02-19 17:38:08 +00:00
|
|
|
if( !GetProfiler().IsConnected() )
|
2018-07-11 12:02:41 +00:00
|
|
|
{
|
|
|
|
m_active.store( false, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-12 11:51:01 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::LockSharedRelease );
|
2019-08-12 11:51:01 +00:00
|
|
|
MemWrite( &item->lockRelease.thread, GetThreadHandle() );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockRelease.id, m_id );
|
|
|
|
MemWrite( &item->lockRelease.time, Profiler::GetTime() );
|
2019-08-12 11:51:01 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-12-10 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
tracy_force_inline void AfterTryLockShared( bool acquired )
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !acquired ) return;
|
2018-07-11 12:02:41 +00:00
|
|
|
|
|
|
|
bool queue = false;
|
|
|
|
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
|
|
|
|
const auto active = m_active.load( std::memory_order_relaxed );
|
|
|
|
if( locks == 0 || active )
|
|
|
|
{
|
2019-02-19 17:38:08 +00:00
|
|
|
const bool connected = GetProfiler().IsConnected();
|
2018-07-11 12:02:41 +00:00
|
|
|
if( active != connected ) m_active.store( connected, std::memory_order_relaxed );
|
|
|
|
if( connected ) queue = true;
|
|
|
|
}
|
2019-09-21 12:55:14 +00:00
|
|
|
if( !queue ) return;
|
2018-07-11 12:02:41 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
if( acquired )
|
2017-12-10 20:56:19 +00:00
|
|
|
{
|
2019-08-12 11:51:01 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::LockSharedObtain );
|
2019-08-12 11:51:01 +00:00
|
|
|
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
|
2018-03-31 12:13:46 +00:00
|
|
|
MemWrite( &item->lockObtain.id, m_id );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
|
2019-08-12 11:51:01 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-12-10 20:56:19 +00:00
|
|
|
}
|
2017-12-10 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 22:34:04 +00:00
|
|
|
tracy_force_inline void Mark( const SourceLocationData* srcloc )
|
2017-12-10 20:49:45 +00:00
|
|
|
{
|
2018-07-11 12:02:41 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
const auto active = m_active.load( std::memory_order_relaxed );
|
|
|
|
if( !active ) return;
|
2019-02-19 17:38:08 +00:00
|
|
|
const auto connected = GetProfiler().IsConnected();
|
2018-07-11 12:02:41 +00:00
|
|
|
if( !connected )
|
|
|
|
{
|
|
|
|
if( active ) m_active.store( false, std::memory_order_relaxed );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-12 11:51:01 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->hdr.type, QueueType::LockMark );
|
2019-08-12 11:51:01 +00:00
|
|
|
MemWrite( &item->lockMark.thread, GetThreadHandle() );
|
2018-03-31 12:03:55 +00:00
|
|
|
MemWrite( &item->lockMark.id, m_id );
|
|
|
|
MemWrite( &item->lockMark.srcloc, (uint64_t)srcloc );
|
2019-08-12 11:51:01 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2017-12-10 20:49:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 12:47:38 +00:00
|
|
|
tracy_force_inline void CustomName( const char* name, size_t size )
|
|
|
|
{
|
2020-07-25 23:22:09 +00:00
|
|
|
assert( size < std::numeric_limits<uint16_t>::max() );
|
|
|
|
auto ptr = (char*)tracy_malloc( size );
|
2020-03-08 12:47:38 +00:00
|
|
|
memcpy( ptr, name, size );
|
2020-07-26 11:45:53 +00:00
|
|
|
auto item = Profiler::QueueSerial();
|
|
|
|
MemWrite( &item->hdr.type, QueueType::LockName );
|
2020-07-25 23:22:09 +00:00
|
|
|
MemWrite( &item->lockNameFat.id, m_id );
|
|
|
|
MemWrite( &item->lockNameFat.name, (uint64_t)ptr );
|
|
|
|
MemWrite( &item->lockNameFat.size, (uint16_t)size );
|
2020-03-08 12:47:38 +00:00
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
GetProfiler().DeferItem( *item );
|
|
|
|
#endif
|
2020-07-26 11:45:53 +00:00
|
|
|
Profiler::QueueSerialFinish();
|
2020-03-08 12:47:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 20:49:45 +00:00
|
|
|
private:
|
|
|
|
uint32_t m_id;
|
2018-07-11 12:02:41 +00:00
|
|
|
|
|
|
|
#ifdef TRACY_ON_DEMAND
|
|
|
|
std::atomic<uint32_t> m_lockCount;
|
|
|
|
std::atomic<bool> m_active;
|
|
|
|
#endif
|
2017-12-10 20:49:45 +00:00
|
|
|
};
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
template<class T>
|
|
|
|
class SharedLockable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
tracy_force_inline SharedLockable( const SourceLocationData* srcloc )
|
|
|
|
: m_ctx( srcloc )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedLockable( const SharedLockable& ) = delete;
|
|
|
|
SharedLockable& operator=( const SharedLockable& ) = delete;
|
|
|
|
|
|
|
|
tracy_force_inline void lock()
|
|
|
|
{
|
|
|
|
const auto runAfter = m_ctx.BeforeLock();
|
|
|
|
m_lockable.lock();
|
|
|
|
if( runAfter ) m_ctx.AfterLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void unlock()
|
|
|
|
{
|
|
|
|
m_lockable.unlock();
|
|
|
|
m_ctx.AfterUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline bool try_lock()
|
|
|
|
{
|
|
|
|
const auto acquired = m_lockable.try_lock();
|
|
|
|
m_ctx.AfterTryLock( acquired );
|
|
|
|
return acquired;
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void lock_shared()
|
|
|
|
{
|
|
|
|
const auto runAfter = m_ctx.BeforeLockShared();
|
|
|
|
m_lockable.lock_shared();
|
|
|
|
if( runAfter ) m_ctx.AfterLockShared();
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void unlock_shared()
|
|
|
|
{
|
|
|
|
m_lockable.unlock_shared();
|
|
|
|
m_ctx.AfterUnlockShared();
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline bool try_lock_shared()
|
|
|
|
{
|
|
|
|
const auto acquired = m_lockable.try_lock_shared();
|
|
|
|
m_ctx.AfterTryLockShared( acquired );
|
|
|
|
return acquired;
|
|
|
|
}
|
|
|
|
|
|
|
|
tracy_force_inline void Mark( const SourceLocationData* srcloc )
|
|
|
|
{
|
|
|
|
m_ctx.Mark( srcloc );
|
|
|
|
}
|
|
|
|
|
2020-03-08 12:47:38 +00:00
|
|
|
tracy_force_inline void CustomName( const char* name, size_t size )
|
|
|
|
{
|
2020-03-08 13:19:08 +00:00
|
|
|
m_ctx.CustomName( name, size );
|
2020-03-08 12:47:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 12:55:14 +00:00
|
|
|
private:
|
|
|
|
T m_lockable;
|
|
|
|
SharedLockableCtx m_ctx;
|
|
|
|
};
|
|
|
|
|
2017-12-10 20:49:45 +00:00
|
|
|
|
2020-05-10 13:32:39 +00:00
|
|
|
}
|
2017-10-04 13:41:02 +00:00
|
|
|
|
|
|
|
#endif
|