Skip to content

Commit

Permalink
Fix GetCurrentThreadId
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleejordan committed Oct 14, 2016
1 parent 24d0ce3 commit e630c6f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ internal static bool NonWindowsIsExecutable(string path)

internal static uint NonWindowsGetThreadId()
{
// TODO:PSL clean this up
return 0;
return Unix.NativeMethods.GetCurrentThreadId();
}

// Unix specific implementations of required functionality
Expand Down Expand Up @@ -552,6 +551,9 @@ internal static class NativeMethods
[return: MarshalAs(UnmanagedType.I1)]
internal static extern bool IsExecutable([MarshalAs(UnmanagedType.LPStr)]string filePath);

[DllImport(psLib, CharSet = CharSet.Ansi)]
internal static extern uint GetCurrentThreadId();

[DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.LPStr)]
internal static extern string GetFullyQualifiedName();
Expand Down
16 changes: 4 additions & 12 deletions src/System.Management.Automation/utils/PsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,19 +624,11 @@ internal static string WinGetHostName()

internal static uint GetNativeThreadId()
{
if (Platform.IsWindows)
{
return WinGetNativeThreadId();
}
else
{
return Platform.NonWindowsGetThreadId();
}
}

internal static uint WinGetNativeThreadId()
{
#if UNIX
return Platform.NonWindowsGetThreadId();
#else
return NativeMethods.GetCurrentThreadId();
#endif
}

private static class NativeMethods
Expand Down
1 change: 1 addition & 0 deletions src/libpsl-native/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(psl-native SHARED
getpwuid.cpp
getuserfrompid.cpp
getfileowner.cpp
getcurrentthreadid.cpp
getcurrentprocessorid.cpp
getusername.cpp
getcomputername.cpp
Expand Down
11 changes: 9 additions & 2 deletions src/libpsl-native/src/getcurrentthreadid.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include "getcurrentthreadid.h"

#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>

pid_t GetCurrentThreadId()
{
return pthread_self();
pid_t tid = 0;
#if defined(__linux__)
tid = syscall(SYS_gettid);
#elif defined(__APPLE__) && defined(__MACH__)
tid = syscall(SYS_thread_selfid);
#endif
return tid;
}
1 change: 0 additions & 1 deletion src/libpsl-native/src/getcurrentthreadid.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ PAL_BEGIN_EXTERNC
pid_t GetCurrentThreadId();

PAL_END_EXTERNC

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Describe "Stream writer tests" -Tags "CI" {
# redirect the streams is sufficient
$result = Write-Information "Test Message" *>&1
$result.GetType().Fullname | Should be "System.Management.Automation.InformationRecord"
$result.NativeThreadId | Should Not Be 0
"$result" | Should be "Test Message"
}
}
Expand Down

0 comments on commit e630c6f

Please sign in to comment.