#include "perfetto/base/build_config.h"
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
#include <Windows.h>
#include "perfetto/ext/base/thread_task_runner.h"
#include "perfetto/tracing/internal/tracing_tls.h"
#include "perfetto/tracing/platform.h"
#ifdef _WIN64
#pragma comment(linker, "/INCLUDE:_tls_used")
#pragma comment(linker, "/INCLUDE:perfetto_thread_callback_base")
#else
#pragma comment(linker, "/INCLUDE:__tls_used")
#pragma comment(linker, "/INCLUDE:_perfetto_thread_callback_base")
#endif
namespace perfetto {
namespace {
class PlatformWindows : public Platform {
public:
static PlatformWindows* instance;
PlatformWindows();
~PlatformWindows() override;
ThreadLocalObject* GetOrCreateThreadLocalObject() override;
std::unique_ptr<base::TaskRunner> CreateTaskRunner(
const CreateTaskRunnerArgs&) override;
std::string GetCurrentProcessName() override;
void OnThreadExit();
private:
DWORD tls_key_{};
};
using ThreadLocalObject = Platform::ThreadLocalObject;
PlatformWindows* PlatformWindows::instance = nullptr;
PlatformWindows::PlatformWindows() {
instance = this;
tls_key_ = ::TlsAlloc();
PERFETTO_CHECK(tls_key_ != TLS_OUT_OF_INDEXES);
}
PlatformWindows::~PlatformWindows() {
::TlsFree(tls_key_);
instance = nullptr;
}
void PlatformWindows::OnThreadExit() {
auto tls = static_cast<ThreadLocalObject*>(::TlsGetValue(tls_key_));
if (tls) {
delete tls;
}
}
ThreadLocalObject* PlatformWindows::GetOrCreateThreadLocalObject() {
void* tls_ptr = ::TlsGetValue(tls_key_);
auto* tls = static_cast<ThreadLocalObject*>(tls_ptr);
if (!tls) {
tls = ThreadLocalObject::CreateInstance().release();
::TlsSetValue(tls_key_, tls);
}
return tls;
}
std::unique_ptr<base::TaskRunner> PlatformWindows::CreateTaskRunner(
const CreateTaskRunnerArgs& args) {
return std::unique_ptr<base::TaskRunner>(new base::ThreadTaskRunner(
base::ThreadTaskRunner::CreateAndStart(args.name_for_debugging)));
}
std::string PlatformWindows::GetCurrentProcessName() {
char buf[MAX_PATH];
auto len = ::GetModuleFileNameA(nullptr , buf, sizeof(buf));
std::string name(buf, static_cast<size_t>(len));
size_t sep = name.find_last_of('\\');
if (sep != std::string::npos)
name = name.substr(sep + 1);
return name;
}
}
Platform* Platform::GetDefaultPlatform() {
static PlatformWindows* thread_safe_init_instance = new PlatformWindows();
return thread_safe_init_instance;
}
}
extern "C" {
void NTAPI PerfettoOnThreadExit(PVOID, DWORD, PVOID);
void NTAPI PerfettoOnThreadExit(PVOID, DWORD reason, PVOID) {
if (reason == DLL_THREAD_DETACH || reason == DLL_PROCESS_DETACH) {
if (perfetto::PlatformWindows::instance)
perfetto::PlatformWindows::instance->OnThreadExit();
}
}
#ifdef _WIN64
#pragma const_seg(".CRT$XLP")
extern const PIMAGE_TLS_CALLBACK perfetto_thread_callback_base;
const PIMAGE_TLS_CALLBACK perfetto_thread_callback_base = PerfettoOnThreadExit;
#pragma const_seg()
#else
#pragma data_seg(".CRT$XLP")
PIMAGE_TLS_CALLBACK perfetto_thread_callback_base = PerfettoOnThreadExit;
#pragma data_seg()
#endif
}
#endif