Dreamine.Threading.Windows 1.0.1
이 패키지는 다른 어셈블리에서 `DMContainer`를 partial class로 확장하지 않습니다. 등록은 별도의 Registration 클래스를 통해 제공합니다.
로딩중...
검색중...
일치하는것 없음
WindowsThreadAffinityService.cs
이 파일의 문서화 페이지로 가기
1using System.ComponentModel;
2using Dreamine.Threading.Interfaces;
4
6
15public sealed class WindowsThreadAffinityService : IThreadAffinityService
16{
57 public void ApplyToCurrentThread(int coreIndex)
58 {
59 if (coreIndex < 0)
60 {
61 throw new ArgumentOutOfRangeException(nameof(coreIndex), "CPU core index cannot be negative.");
62 }
63
64 if (coreIndex >= IntPtr.Size * 8)
65 {
66 throw new ArgumentOutOfRangeException(
67 nameof(coreIndex),
68 $"CPU core index {coreIndex} exceeds the affinity mask width.");
69 }
70
71 var mask = new UIntPtr(1UL << coreIndex);
72 var currentThread = Kernel32NativeMethods.GetCurrentThread();
73
74 var previousMask = Kernel32NativeMethods.SetThreadAffinityMask(currentThread, mask);
75
76 if (previousMask == UIntPtr.Zero)
77 {
78 throw new Win32Exception(
79 System.Runtime.InteropServices.Marshal.GetLastWin32Error(),
80 $"Failed to set thread affinity. CoreIndex={coreIndex}");
81 }
82 }
83
101 {
102 /*
103 * Windows SetThreadAffinityMask requires a valid mask.
104 * Restoring the original process-wide scheduling behavior requires the previous
105 * affinity mask to be stored per thread.
106 *
107 * The current Core interface does not expose a restore token, so this method is
108 * intentionally a no-op for now.
109 *
110 * Future improvement:
111 * - Change IThreadAffinityService.ApplyToCurrentThread to return a restore handle.
112 * - Restore the previous affinity mask in Clear/Dispose.
113 */
114 }
115}