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

Protect against crashes when MANUAL_LIFETIME is used #690

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
92 changes: 80 additions & 12 deletions public/client/TracyLock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ namespace tracy
class LockableCtx
{
public:
tracy_force_inline LockableCtx( const SourceLocationData* srcloc )
: m_id( GetLockCounter().fetch_add( 1, std::memory_order_relaxed ) )
tracy_force_inline LockableCtx( const SourceLocationData* srcloc ) :
#ifdef TRACY_ON_DEMAND
, m_lockCount( 0 )
, m_active( false )
#else
m_active(TracyIsStarted)
#endif
{
#if !TRACY_ON_DEMAND
plekakis marked this conversation as resolved.
Show resolved Hide resolved
if (!m_active) return;
#endif

m_id = GetLockCounter().fetch_add(1, std::memory_order_relaxed);

assert( m_id != (std::numeric_limits<uint32_t>::max)() );

auto item = Profiler::QueueSerial();
Expand All @@ -40,6 +47,9 @@ class LockableCtx

tracy_force_inline ~LockableCtx()
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::LockTerminate );
MemWrite( &item->lockTerminate.id, m_id );
Expand All @@ -52,6 +62,9 @@ class LockableCtx

tracy_force_inline bool BeforeLock()
{
#if !TRACY_ON_DEMAND
if (!m_active) return false;
#endif
#ifdef TRACY_ON_DEMAND
bool queue = false;
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
Expand All @@ -76,6 +89,9 @@ class LockableCtx

tracy_force_inline void AfterLock()
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::LockObtain );
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
Expand All @@ -86,6 +102,9 @@ class LockableCtx

tracy_force_inline void AfterUnlock()
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
#ifdef TRACY_ON_DEMAND
m_lockCount.fetch_sub( 1, std::memory_order_relaxed );
if( !m_active.load( std::memory_order_relaxed ) ) return;
Expand All @@ -105,6 +124,9 @@ class LockableCtx

tracy_force_inline void AfterTryLock( bool acquired )
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
#ifdef TRACY_ON_DEMAND
if( !acquired ) return;

Expand Down Expand Up @@ -133,6 +155,9 @@ class LockableCtx

tracy_force_inline void Mark( const SourceLocationData* srcloc )
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
#ifdef TRACY_ON_DEMAND
const auto active = m_active.load( std::memory_order_relaxed );
if( !active ) return;
Expand All @@ -154,6 +179,9 @@ class LockableCtx

tracy_force_inline void CustomName( const char* name, size_t size )
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
assert( size < (std::numeric_limits<uint16_t>::max)() );
auto ptr = (char*)tracy_malloc( size );
memcpy( ptr, name, size );
Expand All @@ -173,8 +201,9 @@ class LockableCtx

#ifdef TRACY_ON_DEMAND
std::atomic<uint32_t> m_lockCount;
std::atomic<bool> m_active;
#endif

std::atomic<bool> m_active;
};

template<class T>
Expand Down Expand Up @@ -229,22 +258,28 @@ class SharedLockableCtx
{
public:
tracy_force_inline SharedLockableCtx( const SourceLocationData* srcloc )
: m_id( GetLockCounter().fetch_add( 1, std::memory_order_relaxed ) )
:
#ifdef TRACY_ON_DEMAND
, m_lockCount( 0 )
, m_active( false )
#else
m_active(TracyIsStarted)
#endif
{
assert( m_id != (std::numeric_limits<uint32_t>::max)() );
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
m_id = GetLockCounter().fetch_add(1, std::memory_order_relaxed);
assert(m_id != (std::numeric_limits<uint32_t>::max)());

auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::LockAnnounce );
MemWrite( &item->lockAnnounce.id, m_id );
MemWrite( &item->lockAnnounce.time, Profiler::GetTime() );
MemWrite( &item->lockAnnounce.lckloc, (uint64_t)srcloc );
MemWrite( &item->lockAnnounce.type, LockType::SharedLockable );
MemWrite(&item->hdr.type, QueueType::LockAnnounce);
MemWrite(&item->lockAnnounce.id, m_id);
MemWrite(&item->lockAnnounce.time, Profiler::GetTime());
MemWrite(&item->lockAnnounce.lckloc, (uint64_t)srcloc);
MemWrite(&item->lockAnnounce.type, LockType::SharedLockable);
#ifdef TRACY_ON_DEMAND
GetProfiler().DeferItem( *item );
GetProfiler().DeferItem(*item);
#endif
Profiler::QueueSerialFinish();
}
Expand All @@ -254,6 +289,9 @@ class SharedLockableCtx

tracy_force_inline ~SharedLockableCtx()
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::LockTerminate );
MemWrite( &item->lockTerminate.id, m_id );
Expand All @@ -266,6 +304,9 @@ class SharedLockableCtx

tracy_force_inline bool BeforeLock()
{
#if !TRACY_ON_DEMAND
if (!m_active) return false;
#endif
#ifdef TRACY_ON_DEMAND
bool queue = false;
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
Expand All @@ -290,6 +331,9 @@ class SharedLockableCtx

tracy_force_inline void AfterLock()
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::LockObtain );
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
Expand All @@ -300,6 +344,9 @@ class SharedLockableCtx

tracy_force_inline void AfterUnlock()
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
#ifdef TRACY_ON_DEMAND
m_lockCount.fetch_sub( 1, std::memory_order_relaxed );
if( !m_active.load( std::memory_order_relaxed ) ) return;
Expand All @@ -319,6 +366,9 @@ class SharedLockableCtx

tracy_force_inline void AfterTryLock( bool acquired )
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
#ifdef TRACY_ON_DEMAND
if( !acquired ) return;

Expand Down Expand Up @@ -347,6 +397,9 @@ class SharedLockableCtx

tracy_force_inline bool BeforeLockShared()
{
#if !TRACY_ON_DEMAND
if (!m_active) return false;
#endif
#ifdef TRACY_ON_DEMAND
bool queue = false;
const auto locks = m_lockCount.fetch_add( 1, std::memory_order_relaxed );
Expand All @@ -371,6 +424,9 @@ class SharedLockableCtx

tracy_force_inline void AfterLockShared()
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
auto item = Profiler::QueueSerial();
MemWrite( &item->hdr.type, QueueType::LockSharedObtain );
MemWrite( &item->lockObtain.thread, GetThreadHandle() );
Expand All @@ -381,6 +437,9 @@ class SharedLockableCtx

tracy_force_inline void AfterUnlockShared()
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
#ifdef TRACY_ON_DEMAND
m_lockCount.fetch_sub( 1, std::memory_order_relaxed );
if( !m_active.load( std::memory_order_relaxed ) ) return;
Expand All @@ -401,6 +460,9 @@ class SharedLockableCtx

tracy_force_inline void AfterTryLockShared( bool acquired )
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
#ifdef TRACY_ON_DEMAND
if( !acquired ) return;

Expand Down Expand Up @@ -429,6 +491,9 @@ class SharedLockableCtx

tracy_force_inline void Mark( const SourceLocationData* srcloc )
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
#ifdef TRACY_ON_DEMAND
const auto active = m_active.load( std::memory_order_relaxed );
if( !active ) return;
Expand All @@ -450,6 +515,9 @@ class SharedLockableCtx

tracy_force_inline void CustomName( const char* name, size_t size )
{
#if !TRACY_ON_DEMAND
if (!m_active) return;
#endif
assert( size < (std::numeric_limits<uint16_t>::max)() );
auto ptr = (char*)tracy_malloc( size );
memcpy( ptr, name, size );
Expand All @@ -469,8 +537,8 @@ class SharedLockableCtx

#ifdef TRACY_ON_DEMAND
std::atomic<uint32_t> m_lockCount;
std::atomic<bool> m_active;
#endif
std::atomic<bool> m_active;
};

template<class T>
Expand Down
Loading