JobControl_s.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include "ThirdPartyHeadersBegin.h"
  3. #if defined _WIN32
  4. #include <windows.h>
  5. #else
  6. #include <pthread.h>
  7. #if defined __APPLE__
  8. #include <sys/types.h>
  9. #include <sys/sysctl.h>
  10. #endif
  11. #endif
  12. #include "ThirdPartyHeadersEnd.h"
  13. #include "MASTER.h"
  14. #include "GLOBAL.h"
  15. #include "Mutex_s.h"
  16. #if defined _WIN32
  17. typedef HANDLE pthread_t;
  18. #endif
  19. struct ___2122
  20. {
  21. ___2664 ___2494;
  22. std::vector<pthread_t> ___2648;
  23. static int ___2827();
  24. ___2122() { ___2494 = new ___2665(); }
  25. ~___2122() { delete ___2494; }
  26. struct ThreadJobData
  27. {
  28. ___4160 m_job;
  29. ___90 m_jobData;
  30. ThreadJobData(___4160 ___2118, ___90 ___2123) : m_job(___2118), m_jobData(___2123) {}
  31. };
  32. #if defined _WIN32
  33. static DWORD WINAPI ___4162(LPVOID data);
  34. #else
  35. static void *___4162(void *data);
  36. #endif
  37. void addJob(___4160 ___2118, ___90 ___2123);
  38. void wait();
  39. void lock() { ___2494->lock(); }
  40. void unlock() { ___2494->unlock(); }
  41. };
  42. inline int ___2122::___2827()
  43. {
  44. int ___2828 = 0;
  45. #if defined _WIN32
  46. SYSTEM_INFO sysinfo;
  47. GetSystemInfo(&sysinfo);
  48. ___2828 = static_cast<int>(sysinfo.dwNumberOfProcessors);
  49. #elif defined __APPLE__
  50. int nm[2];
  51. size_t len = 4;
  52. uint32_t count;
  53. nm[0] = CTL_HW;
  54. nm[1] = HW_AVAILCPU;
  55. sysctl(nm, 2, &count, &len, NULL, 0);
  56. if (count < 1)
  57. {
  58. nm[1] = HW_NCPU;
  59. sysctl(nm, 2, &count, &len, NULL, 0);
  60. if (count < 1)
  61. count = 1;
  62. }
  63. ___2828 = static_cast<int>(count);
  64. #else
  65. ___2828 = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
  66. #endif
  67. return ___2828;
  68. }
  69. #if defined _WIN32
  70. inline DWORD WINAPI ___2122::___4162(LPVOID data)
  71. #else
  72. inline void *___2122::___4162(void *data)
  73. #endif
  74. {
  75. ThreadJobData *___2123 = reinterpret_cast<ThreadJobData *>(data);
  76. ___2123->m_job(___2123->m_jobData);
  77. delete ___2123;
  78. return NULL;
  79. }
  80. inline void ___2122::addJob(___4160 ___2118, ___90 ___2123)
  81. {
  82. lock();
  83. ___2122::ThreadJobData *threadJobData = new ThreadJobData(___2118, ___2123);
  84. #if defined _WIN32
  85. ___2648.push_back(CreateThread(NULL, 0, ___4162, threadJobData, 0, NULL));
  86. #else
  87. pthread_t thread;
  88. pthread_create(&thread, NULL, ___4162, (void *)threadJobData);
  89. ___2648.push_back(thread);
  90. #endif
  91. unlock();
  92. }
  93. inline void ___2122::wait()
  94. {
  95. size_t i;
  96. for (i = 0; i < ___2648.size(); ++i)
  97. {
  98. lock();
  99. pthread_t thr = ___2648[i];
  100. unlock();
  101. #if defined _WIN32
  102. WaitForSingleObject(thr, INFINITE);
  103. #else
  104. pthread_join(thr, NULL);
  105. #endif
  106. }
  107. lock();
  108. ___2648.erase(___2648.begin(), ___2648.begin() + i);
  109. unlock();
  110. }