ProcessUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "../Core/Mutex.h"
  24. #include "../Core/ProcessUtils.h"
  25. #include "../Math/MathDefs.h"
  26. #include <cstdio>
  27. #include <fcntl.h>
  28. #ifdef __APPLE__
  29. #include "TargetConditionals.h"
  30. #endif
  31. #if defined(IOS)
  32. #include <mach/mach_host.h>
  33. #elif !defined(ANDROID) && !defined(RPI)
  34. #include <LibCpuId/src/libcpuid.h>
  35. #endif
  36. #ifdef WIN32
  37. #include <windows.h>
  38. #include <io.h>
  39. #else
  40. #include <unistd.h>
  41. #endif
  42. #if defined(_MSC_VER)
  43. #include <float.h>
  44. #elif !defined(ANDROID) && !defined(IOS) && !defined(RPI)
  45. // From http://stereopsis.com/FPU.html
  46. #define FPU_CW_PREC_MASK 0x0300
  47. #define FPU_CW_PREC_SINGLE 0x0000
  48. #define FPU_CW_PREC_DOUBLE 0x0200
  49. #define FPU_CW_PREC_EXTENDED 0x0300
  50. #define FPU_CW_ROUND_MASK 0x0c00
  51. #define FPU_CW_ROUND_NEAR 0x0000
  52. #define FPU_CW_ROUND_DOWN 0x0400
  53. #define FPU_CW_ROUND_UP 0x0800
  54. #define FPU_CW_ROUND_CHOP 0x0c00
  55. inline unsigned GetFPUState()
  56. {
  57. unsigned control = 0;
  58. __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
  59. return control;
  60. }
  61. inline void SetFPUState(unsigned control)
  62. {
  63. __asm__ __volatile__ ("fldcw %0" : : "m" (control));
  64. }
  65. #endif
  66. #include "../DebugNew.h"
  67. namespace Atomic
  68. {
  69. #ifdef WIN32
  70. static bool consoleOpened = false;
  71. #endif
  72. static String currentLine;
  73. static Vector<String> arguments;
  74. #if defined(IOS)
  75. static void GetCPUData(host_basic_info_data_t* data)
  76. {
  77. mach_msg_type_number_t infoCount;
  78. infoCount = HOST_BASIC_INFO_COUNT;
  79. host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)data, &infoCount);
  80. }
  81. #elif !defined(ANDROID) && !defined(RPI)
  82. static void GetCPUData(struct cpu_id_t* data)
  83. {
  84. if (cpu_identify(0, data) < 0)
  85. {
  86. data->num_logical_cpus = 1;
  87. data->num_cores = 1;
  88. }
  89. }
  90. #endif
  91. void InitFPU()
  92. {
  93. #if !defined(ATOMIC_LUAJIT) && !defined(ANDROID) && !defined(IOS) && !defined(RPI) && !defined(__x86_64__) && !defined(_M_AMD64)
  94. // Make sure FPU is in round-to-nearest, single precision mode
  95. // This ensures Direct3D and OpenGL behave similarly, and all threads behave similarly
  96. #ifdef _MSC_VER
  97. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  98. #else
  99. unsigned control = GetFPUState();
  100. control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
  101. control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
  102. SetFPUState(control);
  103. #endif
  104. #endif
  105. }
  106. void ErrorDialog(const String& title, const String& message)
  107. {
  108. #ifdef WIN32
  109. MessageBoxW(0, WString(message).CString(), WString(title).CString(), 0);
  110. #else
  111. PrintLine(message, true);
  112. #endif
  113. }
  114. void ErrorExit(const String& message, int exitCode)
  115. {
  116. if (!message.Empty())
  117. PrintLine(message, true);
  118. exit(exitCode);
  119. }
  120. void OpenConsoleWindow()
  121. {
  122. #ifdef WIN32
  123. if (consoleOpened)
  124. return;
  125. AllocConsole();
  126. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  127. int hCrt = _open_osfhandle((size_t)hOut, _O_TEXT);
  128. FILE* outFile = _fdopen(hCrt, "w");
  129. setvbuf(outFile, NULL, _IONBF, 1);
  130. *stdout = *outFile;
  131. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  132. hCrt = _open_osfhandle((size_t)hIn, _O_TEXT);
  133. FILE* inFile = _fdopen(hCrt, "r");
  134. setvbuf(inFile, NULL, _IONBF, 128);
  135. *stdin = *inFile;
  136. consoleOpened = true;
  137. #endif
  138. }
  139. void PrintUnicode(const String& str, bool error)
  140. {
  141. #if !defined(ANDROID) && !defined(IOS)
  142. #ifdef WIN32
  143. HANDLE stream = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
  144. if (stream == INVALID_HANDLE_VALUE)
  145. return;
  146. WString strW(str);
  147. DWORD charsWritten;
  148. WriteConsoleW(stream, strW.CString(), strW.Length(), &charsWritten, 0);
  149. #else
  150. fprintf(error ? stderr : stdout, "%s", str.CString());
  151. #endif
  152. #endif
  153. }
  154. void PrintUnicodeLine(const String& str, bool error)
  155. {
  156. PrintUnicode(str + '\n', error);
  157. }
  158. void PrintLine(const String& str, bool error)
  159. {
  160. #if !defined(ANDROID) && !defined(IOS)
  161. fprintf(error ? stderr: stdout, "%s\n", str.CString());
  162. #endif
  163. }
  164. const Vector<String>& ParseArguments(const String& cmdLine, bool skipFirstArgument)
  165. {
  166. arguments.Clear();
  167. unsigned cmdStart = 0, cmdEnd = 0;
  168. bool inCmd = false;
  169. bool inQuote = false;
  170. for (unsigned i = 0; i < cmdLine.Length(); ++i)
  171. {
  172. if (cmdLine[i] == '\"')
  173. inQuote = !inQuote;
  174. if (cmdLine[i] == ' ' && !inQuote)
  175. {
  176. if (inCmd)
  177. {
  178. inCmd = false;
  179. cmdEnd = i;
  180. // Do not store the first argument (executable name)
  181. if (!skipFirstArgument)
  182. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  183. skipFirstArgument = false;
  184. }
  185. }
  186. else
  187. {
  188. if (!inCmd)
  189. {
  190. inCmd = true;
  191. cmdStart = i;
  192. }
  193. }
  194. }
  195. if (inCmd)
  196. {
  197. cmdEnd = cmdLine.Length();
  198. if (!skipFirstArgument)
  199. arguments.Push(cmdLine.Substring(cmdStart, cmdEnd - cmdStart));
  200. }
  201. // Strip double quotes from the arguments
  202. for (unsigned i = 0; i < arguments.Size(); ++i)
  203. arguments[i].Replace("\"", "");
  204. return arguments;
  205. }
  206. const Vector<String>& ParseArguments(const char* cmdLine)
  207. {
  208. return ParseArguments(String(cmdLine));
  209. }
  210. const Vector<String>& ParseArguments(const WString& cmdLine)
  211. {
  212. return ParseArguments(String(cmdLine));
  213. }
  214. const Vector<String>& ParseArguments(const wchar_t* cmdLine)
  215. {
  216. return ParseArguments(String(cmdLine));
  217. }
  218. const Vector<String>& ParseArguments(int argc, char** argv)
  219. {
  220. String cmdLine;
  221. for (int i = 0; i < argc; ++i)
  222. cmdLine.AppendWithFormat("\"%s\" ", (const char*)argv[i]);
  223. return ParseArguments(cmdLine);
  224. }
  225. const Vector<String>& GetArguments()
  226. {
  227. return arguments;
  228. }
  229. String GetConsoleInput()
  230. {
  231. String ret;
  232. #ifdef ATOMIC_TESTING
  233. // When we are running automated tests, reading the console may block. Just return empty in that case
  234. return ret;
  235. #endif
  236. #ifdef WIN32
  237. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  238. HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
  239. if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
  240. return ret;
  241. // Use char-based input
  242. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  243. INPUT_RECORD record;
  244. DWORD events = 0;
  245. DWORD readEvents = 0;
  246. if (!GetNumberOfConsoleInputEvents(input, &events))
  247. return ret;
  248. while (events--)
  249. {
  250. ReadConsoleInputW(input, &record, 1, &readEvents);
  251. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  252. {
  253. unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
  254. if (c)
  255. {
  256. if (c == '\b')
  257. {
  258. PrintUnicode("\b \b");
  259. int length = currentLine.LengthUTF8();
  260. if (length)
  261. currentLine = currentLine.SubstringUTF8(0, length - 1);
  262. }
  263. else if (c == '\r')
  264. {
  265. PrintUnicode("\n");
  266. ret = currentLine;
  267. currentLine.Clear();
  268. return ret;
  269. }
  270. else
  271. {
  272. // We have disabled echo, so echo manually
  273. wchar_t out = c;
  274. DWORD charsWritten;
  275. WriteConsoleW(output, &out, 1, &charsWritten, 0);
  276. currentLine.AppendUTF8(c);
  277. }
  278. }
  279. }
  280. }
  281. #elif !defined(ANDROID) && !defined(IOS)
  282. int flags = fcntl(STDIN_FILENO, F_GETFL);
  283. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  284. for (;;)
  285. {
  286. int ch = fgetc(stdin);
  287. if (ch >= 0 && ch != '\n')
  288. ret += (char)ch;
  289. else
  290. break;
  291. }
  292. #endif
  293. return ret;
  294. }
  295. String GetPlatform()
  296. {
  297. #if defined(ANDROID)
  298. return "Android";
  299. #elif defined(IOS)
  300. return "iOS";
  301. #elif defined(WIN32)
  302. return "Windows";
  303. #elif defined(__APPLE__)
  304. return "Mac OS X";
  305. #elif defined(RPI)
  306. return "Raspberry Pi";
  307. #elif defined(__linux__)
  308. return "Linux";
  309. #else
  310. return String::EMPTY;
  311. #endif
  312. }
  313. #ifdef ANDROID
  314. static unsigned GetAndroidCPUCount()
  315. {
  316. FILE* fp;
  317. int res, i = -1, j = -1;
  318. fp = fopen("/sys/devices/system/cpu/present", "r");
  319. // If failed, return at least 1
  320. if (fp == 0)
  321. return 1;
  322. res = fscanf(fp, "%d-%d", &i, &j);
  323. fclose(fp);
  324. if (res == 1 && i == 0)
  325. return 1;
  326. else if (res == 2 && i == 0)
  327. return j + 1;
  328. // If failed, return at least 1
  329. return 1;
  330. }
  331. #endif
  332. unsigned GetNumPhysicalCPUs()
  333. {
  334. #if defined(IOS)
  335. host_basic_info_data_t data;
  336. GetCPUData(&data);
  337. #if defined(TARGET_IPHONE_SIMULATOR)
  338. // Hardcoded to dual-core on simulator mode even if the host has more
  339. return Min(2, data.physical_cpu);
  340. #else
  341. return data.physical_cpu;
  342. #endif
  343. #elif defined(ANDROID)
  344. return GetAndroidCPUCount();
  345. #elif !defined(ANDROID) && !defined(RPI)
  346. struct cpu_id_t data;
  347. GetCPUData(&data);
  348. return data.num_cores;
  349. #else
  350. /// \todo Implement properly
  351. return 1;
  352. #endif
  353. }
  354. unsigned GetNumLogicalCPUs()
  355. {
  356. #if defined(IOS)
  357. host_basic_info_data_t data;
  358. GetCPUData(&data);
  359. #if defined(TARGET_IPHONE_SIMULATOR)
  360. return Min(2, data.logical_cpu);
  361. #else
  362. return data.logical_cpu;
  363. #endif
  364. #elif defined(ANDROID)
  365. return GetAndroidCPUCount();
  366. #elif !defined(ANDROID) && !defined(RPI)
  367. struct cpu_id_t data;
  368. GetCPUData(&data);
  369. return data.num_logical_cpus;
  370. #else
  371. /// \todo Implement properly
  372. return 1;
  373. #endif
  374. }
  375. }