1370b324cSopenharmony_ci// Windows/System.h
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#ifndef ZIP7_INC_WINDOWS_SYSTEM_H
4370b324cSopenharmony_ci#define ZIP7_INC_WINDOWS_SYSTEM_H
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#ifndef _WIN32
7370b324cSopenharmony_ci// #include <sched.h>
8370b324cSopenharmony_ci#include "../../C/Threads.h"
9370b324cSopenharmony_ci#endif
10370b324cSopenharmony_ci
11370b324cSopenharmony_ci#include "../Common/MyTypes.h"
12370b324cSopenharmony_ci#include "../Common/MyWindows.h"
13370b324cSopenharmony_ci
14370b324cSopenharmony_cinamespace NWindows {
15370b324cSopenharmony_cinamespace NSystem {
16370b324cSopenharmony_ci
17370b324cSopenharmony_ci#ifdef _WIN32
18370b324cSopenharmony_ci
19370b324cSopenharmony_ciUInt32 CountAffinity(DWORD_PTR mask);
20370b324cSopenharmony_ci
21370b324cSopenharmony_cistruct CProcessAffinity
22370b324cSopenharmony_ci{
23370b324cSopenharmony_ci  // UInt32 numProcessThreads;
24370b324cSopenharmony_ci  // UInt32 numSysThreads;
25370b324cSopenharmony_ci  DWORD_PTR processAffinityMask;
26370b324cSopenharmony_ci  DWORD_PTR systemAffinityMask;
27370b324cSopenharmony_ci
28370b324cSopenharmony_ci  void InitST()
29370b324cSopenharmony_ci  {
30370b324cSopenharmony_ci    // numProcessThreads = 1;
31370b324cSopenharmony_ci    // numSysThreads = 1;
32370b324cSopenharmony_ci    processAffinityMask = 1;
33370b324cSopenharmony_ci    systemAffinityMask = 1;
34370b324cSopenharmony_ci  }
35370b324cSopenharmony_ci
36370b324cSopenharmony_ci  void CpuZero()
37370b324cSopenharmony_ci  {
38370b324cSopenharmony_ci    processAffinityMask = 0;
39370b324cSopenharmony_ci  }
40370b324cSopenharmony_ci
41370b324cSopenharmony_ci  void CpuSet(unsigned cpuIndex)
42370b324cSopenharmony_ci  {
43370b324cSopenharmony_ci    processAffinityMask |= ((DWORD_PTR)1 << cpuIndex);
44370b324cSopenharmony_ci  }
45370b324cSopenharmony_ci
46370b324cSopenharmony_ci  UInt32 GetNumProcessThreads() const { return CountAffinity(processAffinityMask); }
47370b324cSopenharmony_ci  UInt32 GetNumSystemThreads() const { return CountAffinity(systemAffinityMask); }
48370b324cSopenharmony_ci
49370b324cSopenharmony_ci  BOOL Get();
50370b324cSopenharmony_ci
51370b324cSopenharmony_ci  BOOL SetProcAffinity() const
52370b324cSopenharmony_ci  {
53370b324cSopenharmony_ci    return SetProcessAffinityMask(GetCurrentProcess(), processAffinityMask);
54370b324cSopenharmony_ci  }
55370b324cSopenharmony_ci};
56370b324cSopenharmony_ci
57370b324cSopenharmony_ci
58370b324cSopenharmony_ci#else // WIN32
59370b324cSopenharmony_ci
60370b324cSopenharmony_cistruct CProcessAffinity
61370b324cSopenharmony_ci{
62370b324cSopenharmony_ci  UInt32 numSysThreads;
63370b324cSopenharmony_ci
64370b324cSopenharmony_ci  UInt32 GetNumSystemThreads() const { return (UInt32)numSysThreads; }
65370b324cSopenharmony_ci  BOOL Get();
66370b324cSopenharmony_ci
67370b324cSopenharmony_ci  #ifdef Z7_AFFINITY_SUPPORTED
68370b324cSopenharmony_ci
69370b324cSopenharmony_ci  CCpuSet cpu_set;
70370b324cSopenharmony_ci
71370b324cSopenharmony_ci  void InitST()
72370b324cSopenharmony_ci  {
73370b324cSopenharmony_ci    numSysThreads = 1;
74370b324cSopenharmony_ci    CpuSet_Zero(&cpu_set);
75370b324cSopenharmony_ci    CpuSet_Set(&cpu_set, 0);
76370b324cSopenharmony_ci  }
77370b324cSopenharmony_ci
78370b324cSopenharmony_ci  UInt32 GetNumProcessThreads() const { return (UInt32)CPU_COUNT(&cpu_set); }
79370b324cSopenharmony_ci  void CpuZero()              { CpuSet_Zero(&cpu_set); }
80370b324cSopenharmony_ci  void CpuSet(unsigned cpuIndex)   { CpuSet_Set(&cpu_set, cpuIndex); }
81370b324cSopenharmony_ci  int IsCpuSet(unsigned cpuIndex) const { return CpuSet_IsSet(&cpu_set, cpuIndex); }
82370b324cSopenharmony_ci  // void CpuClr(int cpuIndex) { CPU_CLR(cpuIndex, &cpu_set); }
83370b324cSopenharmony_ci
84370b324cSopenharmony_ci  BOOL SetProcAffinity() const
85370b324cSopenharmony_ci  {
86370b324cSopenharmony_ci    return sched_setaffinity(0, sizeof(cpu_set), &cpu_set) == 0;
87370b324cSopenharmony_ci  }
88370b324cSopenharmony_ci
89370b324cSopenharmony_ci  #else // Z7_AFFINITY_SUPPORTED
90370b324cSopenharmony_ci
91370b324cSopenharmony_ci  void InitST()
92370b324cSopenharmony_ci  {
93370b324cSopenharmony_ci    numSysThreads = 1;
94370b324cSopenharmony_ci  }
95370b324cSopenharmony_ci
96370b324cSopenharmony_ci  UInt32 GetNumProcessThreads() const
97370b324cSopenharmony_ci  {
98370b324cSopenharmony_ci    return numSysThreads;
99370b324cSopenharmony_ci    /*
100370b324cSopenharmony_ci    UInt32 num = 0;
101370b324cSopenharmony_ci    for (unsigned i = 0; i < sizeof(cpu_set) * 8; i++)
102370b324cSopenharmony_ci      num += (UInt32)((cpu_set >> i) & 1);
103370b324cSopenharmony_ci    return num;
104370b324cSopenharmony_ci    */
105370b324cSopenharmony_ci  }
106370b324cSopenharmony_ci
107370b324cSopenharmony_ci  void CpuZero() { }
108370b324cSopenharmony_ci  void CpuSet(unsigned cpuIndex) { UNUSED_VAR(cpuIndex); }
109370b324cSopenharmony_ci  int IsCpuSet(unsigned cpuIndex) const { return (cpuIndex < numSysThreads) ? 1 : 0; }
110370b324cSopenharmony_ci
111370b324cSopenharmony_ci  BOOL SetProcAffinity() const
112370b324cSopenharmony_ci  {
113370b324cSopenharmony_ci    errno = ENOSYS;
114370b324cSopenharmony_ci    return FALSE;
115370b324cSopenharmony_ci  }
116370b324cSopenharmony_ci
117370b324cSopenharmony_ci  #endif // Z7_AFFINITY_SUPPORTED
118370b324cSopenharmony_ci};
119370b324cSopenharmony_ci
120370b324cSopenharmony_ci#endif // _WIN32
121370b324cSopenharmony_ci
122370b324cSopenharmony_ci
123370b324cSopenharmony_ciUInt32 GetNumberOfProcessors();
124370b324cSopenharmony_ci
125370b324cSopenharmony_cibool GetRamSize(UInt64 &size); // returns false, if unknown ram size
126370b324cSopenharmony_ci
127370b324cSopenharmony_ciunsigned long Get_File_OPEN_MAX();
128370b324cSopenharmony_ciunsigned Get_File_OPEN_MAX_Reduced_for_3_tasks();
129370b324cSopenharmony_ci
130370b324cSopenharmony_ci}}
131370b324cSopenharmony_ci
132370b324cSopenharmony_ci#endif
133