diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index ab045991322863..5eb1544b72e43d 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -2252,15 +2252,6 @@ SetThreadDescription( IN PCWSTR lpThreadDescription ); -PALIMPORT -HRESULT -PALAPI -GetThreadDescription( - IN HANDLE hThread, - IN size_t length, - OUT char* name -); - #define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF) PALIMPORT diff --git a/src/coreclr/pal/src/thread/thread.cpp b/src/coreclr/pal/src/thread/thread.cpp index 46ffb6d4bc371e..f3bbaa0db61819 100644 --- a/src/coreclr/pal/src/thread/thread.cpp +++ b/src/coreclr/pal/src/thread/thread.cpp @@ -1586,34 +1586,6 @@ GetThreadTimes( return (retval); } -HRESULT -PALAPI -GetThreadDescription( - IN HANDLE hThread, - IN size_t length, - OUT char* name) -{ - PAL_ERROR palError = NO_ERROR; - CPalThread *pCurrentThread; - CPalThread *pTargetThread; - IPalObject *pobjThread = NULL; - pCurrentThread = InternalGetCurrentThread(); - - palError = InternalGetThreadDataFromHandle( - pCurrentThread, - hThread, - &pTargetThread, - &pobjThread - ); - - if (palError == NO_ERROR) - { - pthread_getname_np(pTargetThread->GetPThreadSelf(), name, length); - } - - return HRESULT_FROM_WIN32(palError); -} - HRESULT PALAPI SetThreadDescription( @@ -1661,7 +1633,7 @@ CorUnix::InternalSetThreadDescription( char *nameBuf = NULL; // The exact API of pthread_setname_np varies very wildly depending on OS. -// For now, only Linux and MacOS are implemented. +// For now, only Linux and macOS are implemented. #if defined(__linux__) || defined(__APPLE__) palError = InternalGetThreadDataFromHandle( @@ -1710,7 +1682,7 @@ CorUnix::InternalSetThreadDescription( // Null terminate early. // pthread_setname_np only accepts up to 16 chars on Linux and - // 64 chars on MacOS. + // 64 chars on macOS. if (nameSize > MAX_THREAD_NAME_SIZE) { nameBuf[MAX_THREAD_NAME_SIZE] = '\0'; @@ -1721,7 +1693,7 @@ CorUnix::InternalSetThreadDescription( #endif #if defined(__APPLE__) - // on MacOS, pthread_setname_np only works for the calling thread. + // on macOS, pthread_setname_np only works for the calling thread. if (PlatformGetCurrentThreadId() == pTargetThread->GetThreadId()) { error = pthread_setname_np(nameBuf); diff --git a/src/coreclr/pal/tests/palsuite/CMakeLists.txt b/src/coreclr/pal/tests/palsuite/CMakeLists.txt index e5feab9abc1d07..8c1ce95289bd56 100644 --- a/src/coreclr/pal/tests/palsuite/CMakeLists.txt +++ b/src/coreclr/pal/tests/palsuite/CMakeLists.txt @@ -32,6 +32,13 @@ if(FEATURE_EVENT_TRACE) add_subdirectory(eventprovider) endif(FEATURE_EVENT_TRACE) +if (CLR_CMAKE_TARGET_OSX OR (CLR_CMAKE_TARGET_LINUX AND NOT CLR_CMAKE_TARGET_ALPINE_LINUX)) + set(PALSUITE_THREAD_NAME_SOURCES + threading/SetThreadDescription/test1/pthread_helpers.cpp + threading/SetThreadDescription/test1/test1.cpp + ) + endif() + _add_executable(paltests paltests.cpp common/palsuite.cpp @@ -893,7 +900,6 @@ _add_executable(paltests threading/SetEvent/test2/test2.cpp threading/SetEvent/test3/test3.cpp threading/SetEvent/test4/test4.cpp - threading/SetThreadDescription/test1/test1.cpp threading/SignalObjectAndWait/SignalObjectAndWaitTest.cpp threading/Sleep/test1/Sleep.cpp threading/Sleep/test2/sleep.cpp @@ -921,7 +927,7 @@ _add_executable(paltests threading/WaitForSingleObject/WFSOSemaphoreTest/WFSOSemaphoreTest.cpp threading/WaitForSingleObject/WFSOThreadTest/WFSOThreadTest.cpp threading/YieldProcessor/test1/test1.cpp - + ${PALSUITE_THREAD_NAME_SOURCES} ) add_dependencies(paltests coreclrpal) diff --git a/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/pthread_helpers.cpp b/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/pthread_helpers.cpp new file mode 100644 index 00000000000000..39601406019374 --- /dev/null +++ b/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/pthread_helpers.cpp @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include +#include + +#define NAMELEN 256 +char* GetThreadName() +{ + char* threadName = (char*)malloc(sizeof(char) * NAMELEN); + pthread_t thread = pthread_self(); + int rc = pthread_getname_np(thread, threadName, NAMELEN); + if (rc != 0) + { + free(threadName); + return NULL; + } + + return threadName; +} \ No newline at end of file diff --git a/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/pthread_helpers.hpp b/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/pthread_helpers.hpp new file mode 100644 index 00000000000000..b2bfdc2ee2657c --- /dev/null +++ b/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/pthread_helpers.hpp @@ -0,0 +1,4 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +char* GetThreadName(); \ No newline at end of file diff --git a/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/test1.cpp b/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/test1.cpp index c3db596fd61e7c..391c0b1db60f0e 100644 --- a/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/test1.cpp +++ b/src/coreclr/pal/tests/palsuite/threading/SetThreadDescription/test1/test1.cpp @@ -12,24 +12,24 @@ **=========================================================*/ #include +#include "pthread_helpers.hpp" -char * threadName = NULL; -char * expectedThreadName = NULL; -char * actualThreadName = new char[256]; +char * threadName; +char * expectedThreadName; +char * actualThreadName; DWORD PALAPI SetThreadDescriptionTestThread(LPVOID lpParameter) { - HRESULT result; HANDLE palThread = GetCurrentThread(); WCHAR wideThreadName[256]; MultiByteToWideChar(CP_ACP, 0, threadName, strlen(threadName)+1, wideThreadName, 256); SetThreadDescription(palThread, wideThreadName); - result = GetThreadDescription(palThread, 256, actualThreadName); + actualThreadName = GetThreadName(); + return 0; } - BOOL SetThreadDescriptionTest(char* name, char* expected) { BOOL bResult = FALSE; @@ -65,29 +65,24 @@ BOOL SetThreadDescriptionTests() if (!SetThreadDescriptionTest("Hello, World", "Hello, World")) { Trace("Setting thread name failed"); - return false; + return FAIL; } - // verify that thread name truncations works correctly on linux on macos - char * threadName; + // verify that thread name truncations works correctly on linux on macOS. + char * threadName = "aaaaaaa_15chars_aaaaaaa_31chars_aaaaaaaaaaaaaaaaaaaaaaa_63chars_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; char * expected; - #if defined(__linux__) - threadName = "linuxstring15characters"; - expected = "linuxstring15ch"; - #endif - #if defined(__APPLE__) - threadName = "appplestring63charactersappplestring63charactersappplestring63characters"; - expected = "appplestring63charactersappplestring63charactersappplestring63c"; + expected = "aaaaaaa_15chars_aaaaaaa_31chars_aaaaaaaaaaaaaaaaaaaaaaa_63chars"; + #else + expected = "aaaaaaa_15chars"; #endif - + if (!SetThreadDescriptionTest(threadName, expected)) { - Trace("Setting thread name truncation failed"); - return false; + return PASS; } - return true; + return FAIL; } PALTEST(threading_SetThreadDescription_test1_paltest_setthreaddescription_test1, "threading/SetThreadDescription/test1/paltest_setthreaddescription_test1") @@ -97,13 +92,13 @@ PALTEST(threading_SetThreadDescription_test1_paltest_setthreaddescription_test1, return FAIL; } - if (!SetThreadDescriptionTests()) + BOOL result = SetThreadDescriptionTests(); + if(actualThreadName) free(actualThreadName); + if (!result) { - delete[] actualThreadName; Fail("Test Failed"); } - delete[] actualThreadName; PAL_Terminate(); return PASS; }