提交 ea101e3a 编写于 作者: cdy816's avatar cdy816

增加Linux下,指定线程使用指定CPU

上级 a2848781
......@@ -61,7 +61,7 @@ namespace Cdy.Tag
}
else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
SetThreadAffinityMaskLinux(MaskFromIds(cpus));
}
else
{
......@@ -81,7 +81,9 @@ namespace Cdy.Tag
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
//ulong re = 0;
//LinuxNative.pthread_getaffinity_np(LinuxNative.pthread_self(), ref re);
//return (uint)re;
}
else
{
......@@ -115,6 +117,16 @@ namespace Cdy.Tag
//return SetThreadAffinityMask(Win32Native.GetCurrentThread(), mask);
}
/// <summary>
///
/// </summary>
/// <param name="mask"></param>
public static void SetThreadAffinityMaskLinux(ulong mask)
{
LinuxNative.pthread_setaffinity_np(LinuxNative.pthread_self(),8,ref mask);
}
/// <summary>
/// Masks from ids.
/// </summary>
......@@ -166,6 +178,38 @@ namespace Cdy.Tag
#endregion ...Interfaces...
}
public static class LinuxNative
{
private const string pthread = "libpthread.so.0";
/// <summary>
///
/// </summary>
/// <param name="threadHandel"></param>
/// <param name="cpusize"></param>
/// <param name="cpuset"></param>
/// <returns></returns>
[DllImport(pthread, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int pthread_setaffinity_np(IntPtr threadHandel, int cpusize,ref UInt64 cpuset);
/// <summary>
///
/// </summary>
/// <param name="threadHandel"></param>
/// <param name="cpuset"></param>
/// <returns></returns>
[DllImport(pthread, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int pthread_getaffinity_np(IntPtr threadHandel,ref UInt64 cpuset);
/// <summary>
///
/// </summary>
/// <returns></returns>
[DllImport(pthread, CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr pthread_self();
}
/// <summary>
/// Win32Native Class
/// </summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册