Core: Add thread extension functions
This commit is contained in:
56
src/Nazara/Core/Posix/ThreadImpl.cpp
Normal file
56
src/Nazara/Core/Posix/ThreadImpl.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Posix/ThreadImpl.hpp>
|
||||
#include <array>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
// https://stackoverflow.com/questions/2369738/how-to-set-the-name-of-a-thread-in-linux-pthreads
|
||||
|
||||
namespace Nz::PlatformImpl
|
||||
{
|
||||
pthread_t GetCurrentThreadHandle()
|
||||
{
|
||||
return ::pthread_self();
|
||||
}
|
||||
|
||||
std::string GetThreadName(pthread_t threadHandle)
|
||||
{
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
std::array<char, 16> name;
|
||||
::pthread_getname_np(threadHandle, &name[0], name.size());
|
||||
|
||||
return std::string(&name[0]);
|
||||
#else
|
||||
return "<unsupported>";
|
||||
#endif
|
||||
}
|
||||
|
||||
void SetCurrentThreadName(const char* threadName)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
::pthread_setname_np(::pthread_self(), threadName);
|
||||
#elif defined(__APPLE__)
|
||||
::pthread_setname_np(threadName);
|
||||
#elif defined(__FreeBSD__)
|
||||
::pthread_set_name_np(::pthread_self(), threadName);
|
||||
#else
|
||||
NazaraWarning("setting current thread name is not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
void SetThreadName(pthread_t threadHandle, const char* threadName)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
::pthread_setname_np(threadHandle, threadName);
|
||||
#elif defined(__APPLE__)
|
||||
NazaraWarning("only current thread name can be set on MacOS X");
|
||||
#elif defined(__FreeBSD__)
|
||||
::pthread_set_name_np(threadHandle, threadName);
|
||||
#else
|
||||
NazaraWarning("setting a thread name is not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
25
src/Nazara/Core/Posix/ThreadImpl.hpp
Normal file
25
src/Nazara/Core/Posix/ThreadImpl.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_CORE_POSIX_THREADIMPL_HPP
|
||||
#define NAZARA_CORE_POSIX_THREADIMPL_HPP
|
||||
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <string>
|
||||
#include <pthread.h>
|
||||
|
||||
namespace Nz::PlatformImpl
|
||||
{
|
||||
using ThreadHandle = pthread_t;
|
||||
|
||||
pthread_t GetCurrentThreadHandle();
|
||||
std::string GetCurrentThreadName();
|
||||
std::string GetThreadName(pthread_t threadHandle);
|
||||
void SetCurrentThreadName(const char* threadName);
|
||||
void SetThreadName(pthread_t threadHandle, const char* threadName);
|
||||
}
|
||||
|
||||
#endif // NAZARA_CORE_POSIX_THREADIMPL_HPP
|
||||
Reference in New Issue
Block a user