Skip to content

[Brl.Threads] Add Thread naming concerns/issues #360

@GWRon

Description

@GWRon

Heya,

I started to add support to set/get thread names. I started with Mac/Linux etc - so posix "oriented" things.
It works.

On windows it seems that prior Windows10 this was only doable via "exceptions" and from Windows10 on there are 2 separate API commands provided.
Should I - for simplicity - only support these commands on Windows10 and newer or should I branch things?

For what is thread naming useful at all?

See this gdb screenshot:

Image

PulseAudio etc named their threads - so easy to identify what is what - but BlitzMax threads just inherit the application name.
So when running multiple threads in BlitzMax - and some race condition is met (aka thread1 resets and thread2 accesses) then it is hard to identify which thread it really was.

Thread 24 "TVTower.debug" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffaabff640 (LWP 418866)]
__avl_balance (new=0x7fffa8b697b0, new@entry=0x7fffa8b695a0, root=root@entry=0x7fffcb73f508) at /BlitzMaxNG/mod/brl.mod/blitz.mod/tree/tree.c:601
601                __rotate_right((struct tree_root *)child,
(gdb) bt
#0  __avl_balance (new=0x7fffa8b697b0, new@entry=0x7fffa8b695a0, root=root@entry=0x7fffcb73f508) at /BlitzMaxNG/mod/brl.mod/blitz.mod/tree/tree.c:601
#1  0x00000000015a47eb in avl_map (new=new@entry=0x7fffa8b695a0, compare=compare@entry=0x152e960 <compare_intmap_nodes>, root=root@entry=0x7fffcb73f508)
    at /BlitzMaxNG/mod/brl.mod/blitz.mod/tree/tree.c:640
#2  0x000000000152eade in bmx_map_intmap_insert (key=107870, value=0x7fffc9343d20, root=0x7fffcb73f508) at /BlitzMaxNG/mod/brl.mod/map.mod/map.c:42
#3  0x0000000001523393 in _brl_map_intmap_TIntMap_Insert_iTObject (o=<optimized out>, bbt_key=<optimized out>, bbt_value=<optimized out>)
    at /BlitzMaxNG/mod/brl.mod/map.mod/.bmx/intmap.bmx.debug.linux.x64.c:188

Cool... thread 24 ..

I would have used something like this for Windows:

#if defined(_WIN32)
#include <windows.h>
void SetThreadName(BBString *name ) {
	char *p=(char*)bbStringToCString( name );

	const DWORD MS_VC_EXCEPTION = 0x406D1388;
	#pragma pack(push,8)
	typedef struct tagTHREADNAME_INFO {
		DWORD dwType;
		LPCSTR szName;
		DWORD dwThreadID;
		DWORD dwFlags;
	} THREADNAME_INFO;
	#pragma pack(pop)
	THREADNAME_INFO info;
	info.dwType = 0x1000;
	info.szName = p;
	info.dwThreadID = GetCurrentThreadId();
	info.dwFlags = 0;
	__try {
		RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
	} __except(EXCEPTION_EXECUTE_HANDLER) {
	}
	bbMemFree( p );
}

But my current "threads.c" just looks like this:

#ifdef _WIN32
void threads_SetThreadName( BBThread *thread, BBString *name ){
}

BBString *threads_GetThreadName( BBThread *thread ){
}

//Linux and Mac
#elif defined(__linux__) || defined(__APPLE__)
#include <pthread.h>
#include <errno.h>
void threads_SetThreadName( BBThread *thread, BBString *name ){
	char *p=(char*)bbStringToCString( name );

	// pthread_setname_np limit is 16 bytes (including null terminator)
	char shortName[16];
	// Copy only first 15 chars and Ensure null termination
	strncpy(shortName, p, 15);
	shortName[15] = '\0';

	pthread_setname_np(thread->handle, shortName);

	bbMemFree( p );
}

BBString *threads_GetThreadName( BBThread *thread ){
	// https://linux.die.net/man/3/pthread_getname_np
	// The thread name is a meaningful C language string, whose length
	// is restricted to 16 characters, including the terminating null byte
    char name[16];
    if (pthread_getname_np(thread->handle, name, sizeof(name)) == 0) {
        return bbStringFromCString(name);
    }
    return &bbEmptyString;
}
#else
void threads_SetThreadName( BBThread *thread, BBString *name ){
//
}

BBString *threads_GetThreadName( BBThread *thread ){
//
}
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions