tracy/client/TracyLock.hpp

518 lines
17 KiB
C++
Raw Normal View History

2017-10-04 13:41:02 +00:00
#ifndef __TRACYLOCK_HPP__
#define __TRACYLOCK_HPP__
#include <atomic>
#include <limits>
2017-10-04 14:45:46 +00:00
#include "../common/TracySystem.hpp"
#include "../common/TracyAlign.hpp"
2017-10-04 13:41:02 +00:00
#include "TracyProfiler.hpp"
namespace tracy
{
template<class T>
class Lockable
{
public:
tracy_force_inline Lockable( 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
{
assert( m_id != std::numeric_limits<uint32_t>::max() );
2017-12-10 20:37:39 +00:00
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-12-10 20:37:39 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockAnnounce );
MemWrite( &item->lockAnnounce.id, m_id );
2018-12-16 19:33:18 +00:00
MemWrite( &item->lockAnnounce.time, Profiler::GetTime() );
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 );
#endif
2018-07-11 10:24:58 +00:00
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;
2018-12-16 19:37:48 +00:00
~Lockable()
{
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2018-12-16 19:37:48 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
2018-12-16 19:37:48 +00:00
MemWrite( &item->hdr.type, QueueType::LockTerminate );
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
tail.store( magic + 1, std::memory_order_release );
}
2017-10-06 14:33:11 +00:00
tracy_force_inline void lock()
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;
}
if( !queue )
{
m_lockable.lock();
return;
}
#endif
2017-10-04 14:45:46 +00:00
const auto thread = GetThreadHandle();
{
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-10-10 23:27:22 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockWait );
MemWrite( &item->lockWait.id, m_id );
MemWrite( &item->lockWait.thread, thread );
MemWrite( &item->lockWait.time, Profiler::GetTime() );
MemWrite( &item->lockWait.type, LockType::Lockable );
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;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-10-10 23:27:22 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockObtain );
MemWrite( &item->lockObtain.id, m_id );
MemWrite( &item->lockObtain.thread, thread );
MemWrite( &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
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
2017-10-04 14:45:46 +00:00
Magic magic;
2019-06-24 17:38:44 +00:00
const auto thread = GetThreadHandle();
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-10-10 23:27:22 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockRelease );
MemWrite( &item->lockRelease.id, m_id );
2019-06-24 17:38:44 +00:00
MemWrite( &item->lockRelease.thread, thread );
MemWrite( &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();
2018-07-11 12:02:41 +00:00
#ifdef TRACY_ON_DEMAND
if( !ret ) return ret;
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;
}
2018-07-13 18:12:39 +00:00
if( !queue ) return ret;
2018-07-11 12:02:41 +00:00
#endif
2017-10-04 14:45:46 +00:00
if( ret )
{
Magic magic;
2019-06-24 17:38:44 +00:00
const auto thread = GetThreadHandle();
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-10-10 23:27:22 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockObtain );
2018-03-31 12:13:46 +00:00
MemWrite( &item->lockObtain.id, m_id );
2019-06-24 17:38:44 +00:00
MemWrite( &item->lockObtain.thread, thread );
MemWrite( &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
}
2018-07-11 12:02:41 +00:00
2017-10-04 14:45:46 +00:00
return ret;
2017-10-04 13:41:02 +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
2017-10-06 14:32:32 +00:00
Magic magic;
2019-06-24 17:38:44 +00:00
const auto thread = GetThreadHandle();
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-10-10 23:27:22 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockMark );
MemWrite( &item->lockMark.id, m_id );
2019-06-24 17:38:44 +00:00
MemWrite( &item->lockMark.thread, thread );
MemWrite( &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;
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>
class SharedLockable
{
public:
tracy_force_inline SharedLockable( 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() );
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-12-10 20:49:45 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockAnnounce );
MemWrite( &item->lockAnnounce.id, m_id );
2018-12-16 19:33:18 +00:00
MemWrite( &item->lockAnnounce.time, Profiler::GetTime() );
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 );
#endif
2018-07-11 10:24:58 +00:00
tail.store( magic + 1, std::memory_order_release );
2017-12-10 20:49:45 +00:00
}
SharedLockable( const SharedLockable& ) = delete;
SharedLockable& operator=( const SharedLockable& ) = delete;
2018-12-16 19:37:48 +00:00
~SharedLockable()
{
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2018-12-16 19:37:48 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
2018-12-16 19:37:48 +00:00
MemWrite( &item->hdr.type, QueueType::LockTerminate );
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
tail.store( magic + 1, std::memory_order_release );
}
2017-12-10 20:49:45 +00:00
tracy_force_inline void lock()
{
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;
}
if( !queue )
{
m_lockable.lock();
return;
}
#endif
2017-12-10 20:49:45 +00:00
const auto thread = GetThreadHandle();
{
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-12-10 20:49:45 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockWait );
MemWrite( &item->lockWait.id, m_id );
MemWrite( &item->lockWait.thread, thread );
MemWrite( &item->lockWait.time, Profiler::GetTime() );
MemWrite( &item->lockWait.type, LockType::SharedLockable );
2017-12-10 20:49:45 +00:00
tail.store( magic + 1, std::memory_order_release );
}
m_lockable.lock();
{
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-12-10 20:49:45 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockObtain );
MemWrite( &item->lockObtain.id, m_id );
MemWrite( &item->lockObtain.thread, thread );
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
2017-12-10 20:49:45 +00:00
tail.store( magic + 1, std::memory_order_release );
}
}
tracy_force_inline void unlock()
{
m_lockable.unlock();
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
2017-12-10 20:49:45 +00:00
Magic magic;
2019-06-24 17:38:44 +00:00
const auto thread = GetThreadHandle();
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-12-10 20:49:45 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
2018-03-31 12:19:45 +00:00
MemWrite( &item->hdr.type, QueueType::LockRelease );
MemWrite( &item->lockRelease.id, m_id );
2019-06-24 17:38:44 +00:00
MemWrite( &item->lockRelease.thread, thread );
MemWrite( &item->lockRelease.time, Profiler::GetTime() );
2017-12-10 20:49:45 +00:00
tail.store( magic + 1, std::memory_order_release );
}
tracy_force_inline bool try_lock()
{
const auto ret = m_lockable.try_lock();
2018-07-11 12:02:41 +00:00
#ifdef TRACY_ON_DEMAND
if( !ret ) return ret;
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;
}
if( !queue ) return ret;
#endif
2017-12-10 20:49:45 +00:00
if( ret )
{
Magic magic;
2019-06-24 17:38:44 +00:00
const auto thread = GetThreadHandle();
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-12-10 20:49:45 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockObtain );
2018-03-31 12:13:46 +00:00
MemWrite( &item->lockObtain.id, m_id );
2019-06-24 17:38:44 +00:00
MemWrite( &item->lockObtain.thread, thread );
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
2017-12-10 20:49:45 +00:00
tail.store( magic + 1, std::memory_order_release );
}
2018-07-11 12:02:41 +00:00
2017-12-10 20:49:45 +00:00
return ret;
}
tracy_force_inline void lock_shared()
{
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;
}
if( !queue )
{
2018-07-11 12:43:38 +00:00
m_lockable.lock_shared();
2018-07-11 12:02:41 +00:00
return;
}
#endif
const auto thread = GetThreadHandle();
{
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockSharedWait );
MemWrite( &item->lockWait.id, m_id );
MemWrite( &item->lockWait.thread, thread );
MemWrite( &item->lockWait.time, Profiler::GetTime() );
MemWrite( &item->lockWait.type, LockType::SharedLockable );
tail.store( magic + 1, std::memory_order_release );
}
2017-12-10 20:49:45 +00:00
m_lockable.lock_shared();
{
Magic magic;
2019-02-19 17:27:00 +00:00
auto token = GetToken();
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockSharedObtain );
MemWrite( &item->lockObtain.id, m_id );
MemWrite( &item->lockObtain.thread, thread );
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
tail.store( magic + 1, std::memory_order_release );
}
2017-12-10 20:49:45 +00:00
}
tracy_force_inline void unlock_shared()
{
m_lockable.unlock_shared();
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
Magic magic;
2019-06-24 17:38:44 +00:00
const auto thread = GetThreadHandle();
2019-02-19 17:27:00 +00:00
auto token = GetToken();
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockSharedRelease );
MemWrite( &item->lockRelease.id, m_id );
2019-06-24 17:38:44 +00:00
MemWrite( &item->lockRelease.thread, thread );
MemWrite( &item->lockRelease.time, Profiler::GetTime() );
tail.store( magic + 1, std::memory_order_release );
2017-12-10 20:49:45 +00:00
}
tracy_force_inline bool try_lock_shared()
{
const auto ret = m_lockable.try_lock_shared();
2018-07-11 12:02:41 +00:00
#ifdef TRACY_ON_DEMAND
if( !ret ) return ret;
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;
}
if( !queue ) return ret;
#endif
if( ret )
{
Magic magic;
2019-06-24 17:38:44 +00:00
const auto thread = GetThreadHandle();
2019-02-19 17:27:00 +00:00
auto token = GetToken();
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockSharedObtain );
2018-03-31 12:13:46 +00:00
MemWrite( &item->lockObtain.id, m_id );
2019-06-24 17:38:44 +00:00
MemWrite( &item->lockObtain.thread, thread );
MemWrite( &item->lockObtain.time, Profiler::GetTime() );
tail.store( magic + 1, std::memory_order_release );
}
2018-07-11 12:02:41 +00:00
2017-12-10 20:49:45 +00:00
return ret;
}
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
2017-12-10 20:49:45 +00:00
Magic magic;
2019-06-24 17:38:44 +00:00
const auto thread = GetThreadHandle();
2019-02-19 17:27:00 +00:00
auto token = GetToken();
2017-12-10 20:49:45 +00:00
auto& tail = token->get_tail_index();
2019-07-29 20:13:16 +00:00
auto item = token->enqueue_begin( magic );
MemWrite( &item->hdr.type, QueueType::LockMark );
MemWrite( &item->lockMark.id, m_id );
2019-06-24 17:38:44 +00:00
MemWrite( &item->lockMark.thread, thread );
MemWrite( &item->lockMark.srcloc, (uint64_t)srcloc );
2017-12-10 20:49:45 +00:00
tail.store( magic + 1, std::memory_order_release );
}
private:
T m_lockable;
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
};
2017-10-04 13:41:02 +00:00
};
#endif