Non-Legato Thread Initialization

Summary

Is there any way to detect if a non-legato thread has already been initialized?

Issue

I am using Go so all concurrency is performed via Goroutines, as such the runtime routine scheduler handles thread lifetimes. This makes registering threads that use legato api’s a challenge. I have found a work around for Goroutines directly launched by me via the below:

	go func() {
		runtime.LockOSThread()
		tname := C.CString("sig_monitoring_thread")
		C.le_thread_InitLegatoThreadData(tname)
		defer C.le_thread_CleanupLegatoThreadData()
		C.free(unsafe.Pointer(tname))
		C.le_mrc_ConnectService()
		//le_,rc can be used now

	}()

This does not account for my code being launched by a Goroutine from another framework. I originally tried to just call le_thread_InitLegatoThreadData more often, but I found that if this is called by a thread that is already registered, legato kills the process.

Right now I am hoping there is a way to detect if a thread is registered to legato and only if it is not, call le_thread_InitLegatoThreadData. If you have another strategy, i am also open to it (short of recompiling go with legato baked in).

have you tried to check the thread detail by the method here?

I don’t think that helps. Legato will stop my program if le_thread_InitLegatoThreadData is called more than once:

#if LE_CONFIG_THREAD_NAMES_ENABLED
void le_thread_InitLegatoThreadData
(
    const char* name    ///< [IN] A name for the thread (will be copied, so can be temporary).
)
{
    LE_FATAL_IF(ThreadPool == NULL,
                "Legato C Runtime Library (liblegato) has not been initialized!");

    LE_FATAL_IF(pthread_getspecific(ThreadLocalDataKey) != NULL,
                "Legato thread-specific data initialized more than once!");

under the hood, it is using thread memory to store Legato specific information. I don’t have access to ThreadLocalDataKey (just being used as a UUID) so I cannot use pthread_getspecific directly.

After looking into thread.c and le_thread.h, the only function that seem does not crash the program for an edge case is le_thread_GetMyName.

I’ll just have to use that for now.