The open source OpenXR runtime
1#ifndef __TRACYTHREAD_HPP__
2#define __TRACYTHREAD_HPP__
3
4#if defined _WIN32
5# include <windows.h>
6#else
7# include <pthread.h>
8#endif
9
10#ifdef TRACY_MANUAL_LIFETIME
11# include "tracy_rpmalloc.hpp"
12#endif
13
14namespace tracy
15{
16
17#ifdef TRACY_MANUAL_LIFETIME
18extern thread_local bool RpThreadInitDone;
19#endif
20
21class ThreadExitHandler
22{
23public:
24 ~ThreadExitHandler()
25 {
26#ifdef TRACY_MANUAL_LIFETIME
27 rpmalloc_thread_finalize( 1 );
28 RpThreadInitDone = false;
29#endif
30 }
31};
32
33#if defined _WIN32
34
35class Thread
36{
37public:
38 Thread( void(*func)( void* ptr ), void* ptr )
39 : m_func( func )
40 , m_ptr( ptr )
41 , m_hnd( CreateThread( nullptr, 0, Launch, this, 0, nullptr ) )
42 {}
43
44 ~Thread()
45 {
46 WaitForSingleObject( m_hnd, INFINITE );
47 CloseHandle( m_hnd );
48 }
49
50 HANDLE Handle() const { return m_hnd; }
51
52private:
53 static DWORD WINAPI Launch( void* ptr ) { ((Thread*)ptr)->m_func( ((Thread*)ptr)->m_ptr ); return 0; }
54
55 void(*m_func)( void* ptr );
56 void* m_ptr;
57 HANDLE m_hnd;
58};
59
60#else
61
62class Thread
63{
64public:
65 Thread( void(*func)( void* ptr ), void* ptr )
66 : m_func( func )
67 , m_ptr( ptr )
68 {
69 pthread_create( &m_thread, nullptr, Launch, this );
70 }
71
72 ~Thread()
73 {
74 pthread_join( m_thread, nullptr );
75 }
76
77 pthread_t Handle() const { return m_thread; }
78
79private:
80 static void* Launch( void* ptr ) { ((Thread*)ptr)->m_func( ((Thread*)ptr)->m_ptr ); return nullptr; }
81 void(*m_func)( void* ptr );
82 void* m_ptr;
83 pthread_t m_thread;
84};
85
86#endif
87
88}
89
90#endif