1 // Windows/System.h 2 3 #ifndef ZIP7_INC_WINDOWS_SYSTEM_H 4 #define ZIP7_INC_WINDOWS_SYSTEM_H 5 6 #ifndef _WIN32 7 // #include <sched.h> 8 #include "../../C/Threads.h" 9 #endif 10 11 #include "../Common/MyTypes.h" 12 #include "../Common/MyWindows.h" 13 14 namespace NWindows { 15 namespace NSystem { 16 17 #ifdef _WIN32 18 19 UInt32 CountAffinity(DWORD_PTR mask); 20 21 struct CProcessAffinity 22 { 23 // UInt32 numProcessThreads; 24 // UInt32 numSysThreads; 25 DWORD_PTR processAffinityMask; 26 DWORD_PTR systemAffinityMask; 27 InitSTCProcessAffinity28 void InitST() 29 { 30 // numProcessThreads = 1; 31 // numSysThreads = 1; 32 processAffinityMask = 1; 33 systemAffinityMask = 1; 34 } 35 CpuZeroCProcessAffinity36 void CpuZero() 37 { 38 processAffinityMask = 0; 39 } 40 CpuSetCProcessAffinity41 void CpuSet(unsigned cpuIndex) 42 { 43 processAffinityMask |= ((DWORD_PTR)1 << cpuIndex); 44 } 45 GetNumProcessThreadsCProcessAffinity46 UInt32 GetNumProcessThreads() const { return CountAffinity(processAffinityMask); } GetNumSystemThreadsCProcessAffinity47 UInt32 GetNumSystemThreads() const { return CountAffinity(systemAffinityMask); } 48 49 BOOL Get(); 50 SetProcAffinityCProcessAffinity51 BOOL SetProcAffinity() const 52 { 53 return SetProcessAffinityMask(GetCurrentProcess(), processAffinityMask); 54 } 55 }; 56 57 58 #else // WIN32 59 60 struct CProcessAffinity 61 { 62 UInt32 numSysThreads; 63 64 UInt32 GetNumSystemThreads() const { return (UInt32)numSysThreads; } 65 BOOL Get(); 66 67 #ifdef Z7_AFFINITY_SUPPORTED 68 69 CCpuSet cpu_set; 70 71 void InitST() 72 { 73 numSysThreads = 1; 74 CpuSet_Zero(&cpu_set); 75 CpuSet_Set(&cpu_set, 0); 76 } 77 78 UInt32 GetNumProcessThreads() const { return (UInt32)CPU_COUNT(&cpu_set); } 79 void CpuZero() { CpuSet_Zero(&cpu_set); } 80 void CpuSet(unsigned cpuIndex) { CpuSet_Set(&cpu_set, cpuIndex); } 81 int IsCpuSet(unsigned cpuIndex) const { return CpuSet_IsSet(&cpu_set, cpuIndex); } 82 // void CpuClr(int cpuIndex) { CPU_CLR(cpuIndex, &cpu_set); } 83 84 BOOL SetProcAffinity() const 85 { 86 return sched_setaffinity(0, sizeof(cpu_set), &cpu_set) == 0; 87 } 88 89 #else // Z7_AFFINITY_SUPPORTED 90 91 void InitST() 92 { 93 numSysThreads = 1; 94 } 95 96 UInt32 GetNumProcessThreads() const 97 { 98 return numSysThreads; 99 /* 100 UInt32 num = 0; 101 for (unsigned i = 0; i < sizeof(cpu_set) * 8; i++) 102 num += (UInt32)((cpu_set >> i) & 1); 103 return num; 104 */ 105 } 106 107 void CpuZero() { } 108 void CpuSet(unsigned cpuIndex) { UNUSED_VAR(cpuIndex); } 109 int IsCpuSet(unsigned cpuIndex) const { return (cpuIndex < numSysThreads) ? 1 : 0; } 110 111 BOOL SetProcAffinity() const 112 { 113 errno = ENOSYS; 114 return FALSE; 115 } 116 117 #endif // Z7_AFFINITY_SUPPORTED 118 }; 119 120 #endif // _WIN32 121 122 123 UInt32 GetNumberOfProcessors(); 124 125 bool GetRamSize(UInt64 &size); // returns false, if unknown ram size 126 127 unsigned long Get_File_OPEN_MAX(); 128 unsigned Get_File_OPEN_MAX_Reduced_for_3_tasks(); 129 130 }} 131 132 #endif 133