Add automatically-generated names to threads

This commit is contained in:
RichardG867
2024-01-09 20:13:16 -03:00
parent ce342400eb
commit 67c84f1ae6
9 changed files with 107 additions and 6 deletions

View File

@@ -51,6 +51,7 @@
#include <86box/path.h>
#define GLOBAL
#include <86box/plat.h>
#include <86box/plat_dynld.h>
#include <86box/thread.h>
#include <86box/ui.h>
#ifdef USE_VNC
@@ -1276,6 +1277,35 @@ plat_get_cpu_string(char *outbuf, uint8_t len) {
strncpy(outbuf, cpu_string, len);
}
void
plat_set_thread_name(void *thread, const char *name)
{
/* SetThreadDescription was added in 14393. Revisit if we ever start requiring 10. */
static void *kernel32_handle = NULL;
static HRESULT(WINAPI *pSetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription) = NULL;
static dllimp_t kernel32_imports[] = {
// clang-format off
{ "SetThreadDescription", &pSetThreadDescription },
{ NULL, NULL }
// clang-format on
};
if (!kernel32_handle) {
kernel32_handle = dynld_module("kernel32.dll", kernel32_imports);
if (!kernel32_handle) {
kernel32_handle = kernel32_imports; /* dummy pointer to store that we tried */
pSetThreadDescription = NULL;
}
}
if (pSetThreadDescription) {
size_t len = strlen(name) + 1;
wchar_t wname[len];
mbstowcs(wname, name, len);
pSetThreadDescription(thread ? (HANDLE) thread : GetCurrentThread(), wname);
}
}
void
take_screenshot(void)
{

View File

@@ -37,9 +37,10 @@ typedef struct {
} win_event_t;
thread_t *
thread_create(void (*func)(void *param), void *param)
thread_create(void (*func)(void *param), void *param, const char *name)
{
uintptr_t bt = _beginthread(func, 0, param);
plat_set_thread_name(bt, name);
return ((thread_t *) bt);
}