tracy/client/TracyLock.hpp

103 lines
2.7 KiB
C++
Raw Normal View History

2017-10-04 13:41:02 +00:00
#ifndef __TRACYLOCK_HPP__
#define __TRACYLOCK_HPP__
#include <atomic>
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
{
static std::atomic<uint64_t> s_lockCounter( 0 );
2017-10-04 13:41:02 +00:00
template<class T>
class Lockable
{
public:
Lockable( const SourceLocation* srcloc )
: m_id( s_lockCounter.fetch_add( 1, std::memory_order_relaxed ) )
2017-10-04 13:41:02 +00:00
{
2017-10-04 14:16:40 +00:00
Magic magic;
auto& token = s_token;
auto item = s_queue.enqueue_begin( token, magic );
item->hdr.type = QueueType::LockAnnounce;
item->lockAnnounce.id = m_id;
2017-10-04 14:16:40 +00:00
item->lockAnnounce.srcloc = (uint64_t)srcloc;
s_queue.enqueue_finish( token, magic );
2017-10-04 13:41:02 +00:00
}
Lockable( const Lockable& ) = delete;
Lockable& operator=( const Lockable& ) = delete;
void lock()
{
2017-10-04 14:45:46 +00:00
int8_t cpu;
const auto thread = GetThreadHandle();
{
Magic magic;
auto& token = s_token;
auto item = s_queue.enqueue_begin( token, magic );
item->hdr.type = QueueType::LockWait;
item->lockWait.id = m_id;
2017-10-04 14:45:46 +00:00
item->lockWait.thread = thread;
2017-10-05 01:07:26 +00:00
item->lockWait.time = Profiler::GetTime( cpu );
2017-10-04 14:45:46 +00:00
s_queue.enqueue_finish( token, magic );
}
2017-10-04 13:41:02 +00:00
m_lockable.lock();
2017-10-04 14:45:46 +00:00
{
Magic magic;
auto& token = s_token;
auto item = s_queue.enqueue_begin( token, magic );
item->hdr.type = QueueType::LockObtain;
item->lockObtain.id = m_id;
2017-10-04 14:45:46 +00:00
item->lockObtain.thread = thread;
2017-10-05 01:07:26 +00:00
item->lockObtain.time = Profiler::GetTime( cpu );
2017-10-04 14:45:46 +00:00
s_queue.enqueue_finish( token, magic );
}
2017-10-04 13:41:02 +00:00
}
void unlock()
{
m_lockable.unlock();
2017-10-04 14:45:46 +00:00
int8_t cpu;
Magic magic;
auto& token = s_token;
auto item = s_queue.enqueue_begin( token, magic );
item->hdr.type = QueueType::LockRelease;
item->lockRelease.id = m_id;
2017-10-04 14:45:46 +00:00
item->lockRelease.thread = GetThreadHandle();
2017-10-05 01:07:26 +00:00
item->lockRelease.time = Profiler::GetTime( cpu );
2017-10-04 14:45:46 +00:00
s_queue.enqueue_finish( token, magic );
2017-10-04 13:41:02 +00:00
}
bool try_lock()
{
2017-10-04 14:45:46 +00:00
const auto ret = m_lockable.try_lock();
if( ret )
{
int8_t cpu;
Magic magic;
auto& token = s_token;
auto item = s_queue.enqueue_begin( token, magic );
item->hdr.type = QueueType::LockObtain;
item->lockObtain.id = (uint64_t)&m_lockable;
item->lockObtain.thread = GetThreadHandle();
2017-10-05 01:07:26 +00:00
item->lockObtain.time = Profiler::GetTime( cpu );
2017-10-04 14:45:46 +00:00
s_queue.enqueue_finish( token, magic );
}
return ret;
2017-10-04 13:41:02 +00:00
}
private:
T m_lockable;
uint64_t m_id;
2017-10-04 13:41:02 +00:00
};
};
#endif