2017-10-16 19:13:03 +00:00
|
|
|
#ifndef __TRACYTHREAD_HPP__
|
|
|
|
#define __TRACYTHREAD_HPP__
|
|
|
|
|
2021-10-07 21:28:40 +00:00
|
|
|
#if defined _WIN32
|
2017-10-16 19:13:03 +00:00
|
|
|
# include <windows.h>
|
|
|
|
#else
|
|
|
|
# include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2020-07-19 11:04:51 +00:00
|
|
|
#ifdef TRACY_MANUAL_LIFETIME
|
|
|
|
# include "tracy_rpmalloc.hpp"
|
|
|
|
#endif
|
|
|
|
|
2017-10-16 19:13:03 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2022-04-05 16:12:05 +00:00
|
|
|
#ifdef TRACY_MANUAL_LIFETIME
|
|
|
|
extern thread_local bool RpThreadInitDone;
|
|
|
|
#endif
|
|
|
|
|
2020-07-19 01:24:49 +00:00
|
|
|
class ThreadExitHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~ThreadExitHandler()
|
|
|
|
{
|
|
|
|
#ifdef TRACY_MANUAL_LIFETIME
|
|
|
|
rpmalloc_thread_finalize();
|
2022-04-05 16:12:05 +00:00
|
|
|
RpThreadInitDone = false;
|
2020-07-19 01:24:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-07 21:28:40 +00:00
|
|
|
#if defined _WIN32
|
2017-10-16 19:13:03 +00:00
|
|
|
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Thread( void(*func)( void* ptr ), void* ptr )
|
|
|
|
: m_func( func )
|
|
|
|
, m_ptr( ptr )
|
|
|
|
, m_hnd( CreateThread( nullptr, 0, Launch, this, 0, nullptr ) )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~Thread()
|
|
|
|
{
|
|
|
|
WaitForSingleObject( m_hnd, INFINITE );
|
|
|
|
CloseHandle( m_hnd );
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE Handle() const { return m_hnd; }
|
|
|
|
|
|
|
|
private:
|
2017-10-30 20:46:50 +00:00
|
|
|
static DWORD WINAPI Launch( void* ptr ) { ((Thread*)ptr)->m_func( ((Thread*)ptr)->m_ptr ); return 0; }
|
2017-10-16 19:13:03 +00:00
|
|
|
|
|
|
|
void(*m_func)( void* ptr );
|
|
|
|
void* m_ptr;
|
|
|
|
HANDLE m_hnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Thread( void(*func)( void* ptr ), void* ptr )
|
|
|
|
: m_func( func )
|
|
|
|
, m_ptr( ptr )
|
|
|
|
{
|
|
|
|
pthread_create( &m_thread, nullptr, Launch, this );
|
|
|
|
}
|
|
|
|
|
|
|
|
~Thread()
|
|
|
|
{
|
|
|
|
pthread_join( m_thread, nullptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_t Handle() const { return m_thread; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void* Launch( void* ptr ) { ((Thread*)ptr)->m_func( ((Thread*)ptr)->m_ptr ); return nullptr; }
|
|
|
|
void(*m_func)( void* ptr );
|
|
|
|
void* m_ptr;
|
|
|
|
pthread_t m_thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|