Skip to content

Commit

Permalink
removing GetThreadDescription from PAL API
Browse files Browse the repository at this point in the history
  • Loading branch information
esoros committed Jan 28, 2021
1 parent f9236f1 commit 584d9bf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 65 deletions.
9 changes: 0 additions & 9 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 3 additions & 31 deletions src/coreclr/pal/src/thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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';
Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/coreclr/pal/tests/palsuite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#include <pthread.h>

#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;
}
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
**=========================================================*/

#include <palsuite.h>
#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;
Expand Down Expand Up @@ -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")
Expand All @@ -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;
}
Expand Down

0 comments on commit 584d9bf

Please sign in to comment.