! set the affinity of a given thread */
| 32 | { |
| 33 | /*! set the affinity of a given thread */ |
| 34 | void setAffinity(HANDLE thread, ssize_t affinity) |
| 35 | { |
| 36 | typedef WORD (WINAPI *GetActiveProcessorGroupCountFunc)(); |
| 37 | typedef DWORD (WINAPI *GetActiveProcessorCountFunc)(WORD); |
| 38 | typedef BOOL (WINAPI *SetThreadGroupAffinityFunc)(HANDLE, const GROUP_AFFINITY *, PGROUP_AFFINITY); |
| 39 | typedef BOOL (WINAPI *SetThreadIdealProcessorExFunc)(HANDLE, PPROCESSOR_NUMBER, PPROCESSOR_NUMBER); |
| 40 | HMODULE hlib = LoadLibrary("Kernel32"); |
| 41 | GetActiveProcessorGroupCountFunc pGetActiveProcessorGroupCount = (GetActiveProcessorGroupCountFunc)GetProcAddress(hlib, "GetActiveProcessorGroupCount"); |
| 42 | GetActiveProcessorCountFunc pGetActiveProcessorCount = (GetActiveProcessorCountFunc)GetProcAddress(hlib, "GetActiveProcessorCount"); |
| 43 | SetThreadGroupAffinityFunc pSetThreadGroupAffinity = (SetThreadGroupAffinityFunc)GetProcAddress(hlib, "SetThreadGroupAffinity"); |
| 44 | SetThreadIdealProcessorExFunc pSetThreadIdealProcessorEx = (SetThreadIdealProcessorExFunc)GetProcAddress(hlib, "SetThreadIdealProcessorEx"); |
| 45 | if (pGetActiveProcessorGroupCount && pGetActiveProcessorCount && pSetThreadGroupAffinity && pSetThreadIdealProcessorEx) |
| 46 | { |
| 47 | int groups = pGetActiveProcessorGroupCount(); |
| 48 | int totalProcessors = 0, group = 0, number = 0; |
| 49 | for (int i = 0; i<groups; i++) { |
| 50 | int processors = pGetActiveProcessorCount(i); |
| 51 | if (totalProcessors + processors > affinity) { |
| 52 | group = i; |
| 53 | number = (int)affinity - totalProcessors; |
| 54 | break; |
| 55 | } |
| 56 | totalProcessors += processors; |
| 57 | } |
| 58 | |
| 59 | GROUP_AFFINITY groupAffinity; |
| 60 | groupAffinity.Group = (WORD)group; |
| 61 | groupAffinity.Mask = (KAFFINITY)(uint64_t(1) << number); |
| 62 | groupAffinity.Reserved[0] = 0; |
| 63 | groupAffinity.Reserved[1] = 0; |
| 64 | groupAffinity.Reserved[2] = 0; |
| 65 | if (!pSetThreadGroupAffinity(thread, &groupAffinity, nullptr)) |
| 66 | WARNING("SetThreadGroupAffinity failed"); // on purpose only a warning |
| 67 | |
| 68 | PROCESSOR_NUMBER processorNumber; |
| 69 | processorNumber.Group = group; |
| 70 | processorNumber.Number = number; |
| 71 | processorNumber.Reserved = 0; |
| 72 | if (!pSetThreadIdealProcessorEx(thread, &processorNumber, nullptr)) |
| 73 | WARNING("SetThreadIdealProcessorEx failed"); // on purpose only a warning |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | if (!SetThreadAffinityMask(thread, DWORD_PTR(uint64_t(1) << affinity))) |
| 78 | WARNING("SetThreadAffinityMask failed"); // on purpose only a warning |
| 79 | if (SetThreadIdealProcessor(thread, (DWORD)affinity) == (DWORD)-1) |
| 80 | WARNING("SetThreadIdealProcessor failed"); // on purpose only a warning |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /*! set affinity of the calling thread */ |
| 85 | void setAffinity(ssize_t affinity) { |
no outgoing calls
no test coverage detected