ProcessUtils.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Mutex.h"
  25. #include "ProcessUtils.h"
  26. #include <cstdio>
  27. #include <cstdlib>
  28. #include <fcntl.h>
  29. #ifdef WIN32
  30. #include <windows.h>
  31. #include <io.h>
  32. #else
  33. #include <unistd.h>
  34. #endif
  35. #ifdef _MSC_VER
  36. #include <float.h>
  37. #include <intrin.h>
  38. #else
  39. // From http://stereopsis.com/FPU.html
  40. #define FPU_CW_PREC_MASK 0x0300
  41. #define FPU_CW_PREC_SINGLE 0x0000
  42. #define FPU_CW_PREC_DOUBLE 0x0200
  43. #define FPU_CW_PREC_EXTENDED 0x0300
  44. #define FPU_CW_ROUND_MASK 0x0c00
  45. #define FPU_CW_ROUND_NEAR 0x0000
  46. #define FPU_CW_ROUND_DOWN 0x0400
  47. #define FPU_CW_ROUND_UP 0x0800
  48. #define FPU_CW_ROUND_CHOP 0x0c00
  49. inline unsigned GetFPUState()
  50. {
  51. unsigned control = 0;
  52. __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
  53. return control;
  54. }
  55. inline void SetFPUState(unsigned control)
  56. {
  57. __asm__ __volatile__ ("fldcw %0" : : "m" (control));
  58. }
  59. #endif
  60. void CpuID(int i, int regs[4]) {
  61. #ifdef _MSC_VER
  62. __cpuid(regs, i);
  63. #else
  64. __asm__ __volatile__ ("cpuid" : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3]) : "a" (i), "c" (0));
  65. #endif
  66. }
  67. #include "DebugNew.h"
  68. #ifdef WIN32
  69. static bool consoleOpened = false;
  70. #endif
  71. static String currentLine;
  72. static Vector<String> arguments;
  73. static Mutex staticMutex;
  74. void InitFPU()
  75. {
  76. // Make sure FPU is in round-to-nearest, single precision mode
  77. // This is needed for ODE to behave predictably in float mode
  78. #ifdef _MSC_VER
  79. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  80. #else
  81. unsigned control = GetFPUState();
  82. control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
  83. control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
  84. SetFPUState(control);
  85. #endif
  86. }
  87. void ErrorDialog(const char* title, const char* message)
  88. {
  89. #ifdef WIN32
  90. MessageBox(0, message, title, 0);
  91. #else
  92. printf("%s\n", message);
  93. #endif
  94. }
  95. void ErrorExit(const String& message, int exitCode)
  96. {
  97. printf("%s", message.CString());
  98. exit(exitCode);
  99. }
  100. void OpenConsoleWindow()
  101. {
  102. #ifdef WIN32
  103. if (consoleOpened)
  104. return;
  105. AllocConsole();
  106. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  107. int hCrt = _open_osfhandle((long)hOut, _O_TEXT);
  108. FILE* outFile = _fdopen(hCrt, "w");
  109. setvbuf(outFile, NULL, _IONBF, 1);
  110. *stdout = *outFile;
  111. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  112. hCrt = _open_osfhandle((long)hIn, _O_TEXT);
  113. FILE* inFile = _fdopen(hCrt, "r");
  114. setvbuf(inFile, NULL, _IONBF, 128);
  115. *stdin = *inFile;
  116. consoleOpened = true;
  117. #endif
  118. }
  119. void PrintLine(const String& str)
  120. {
  121. printf("%s\n", str.CString());
  122. }
  123. void PrintLine(const char* str)
  124. {
  125. printf("%s\n", str);
  126. }
  127. const Vector<String>& ParseArguments(const char* cmdLine)
  128. {
  129. arguments.Clear();
  130. if (!cmdLine)
  131. return arguments;
  132. String cmdStr(cmdLine);
  133. unsigned cmdStart = 0, cmdEnd = 0;
  134. bool inCmd = false;
  135. bool inQuote = false;
  136. for (unsigned i = 0; i < cmdStr.Length(); ++i)
  137. {
  138. if (cmdStr[i] == '\"')
  139. inQuote = !inQuote;
  140. if (cmdStr[i] == ' ' && !inQuote)
  141. {
  142. if (inCmd)
  143. {
  144. inCmd = false;
  145. cmdEnd = i;
  146. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  147. }
  148. }
  149. else
  150. {
  151. if (!inCmd)
  152. {
  153. inCmd = true;
  154. cmdStart = i;
  155. }
  156. }
  157. }
  158. if (inCmd)
  159. {
  160. cmdEnd = cmdStr.Length();
  161. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  162. }
  163. return arguments;
  164. }
  165. const Vector<String>& GetArguments()
  166. {
  167. return arguments;
  168. }
  169. String GetConsoleInput()
  170. {
  171. String ret;
  172. #ifdef WIN32
  173. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  174. if (input == INVALID_HANDLE_VALUE)
  175. return ret;
  176. // Use char-based input
  177. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  178. INPUT_RECORD record;
  179. DWORD events = 0;
  180. DWORD readEvents = 0;
  181. if (!GetNumberOfConsoleInputEvents(input, &events))
  182. return ret;
  183. while (events--)
  184. {
  185. ReadConsoleInput(input, &record, 1, &readEvents);
  186. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  187. {
  188. char c = record.Event.KeyEvent.uChar.AsciiChar;
  189. if (c)
  190. {
  191. if (c == '\b')
  192. {
  193. printf("\b \b");
  194. int length = currentLine.Length();
  195. if (length)
  196. currentLine.Resize(length - 1);
  197. }
  198. else if (c == '\r')
  199. {
  200. printf("\n");
  201. ret = currentLine;
  202. currentLine.Clear();
  203. return ret;
  204. }
  205. else
  206. {
  207. // We have disabled echo, so echo manually
  208. printf("%c", c);
  209. currentLine += c;
  210. }
  211. }
  212. }
  213. }
  214. #else
  215. int flags = fcntl(STDIN_FILENO, F_GETFL);
  216. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  217. for (;;)
  218. {
  219. int ch = fgetc(stdin);
  220. if (ch >= 0 && ch != '\n')
  221. ret += (char)ch;
  222. else
  223. break;
  224. }
  225. #endif
  226. return ret;
  227. }
  228. unsigned GetNumCPUCores()
  229. {
  230. // Get number of CPU cores without counting hyperthreaded cores, as creating a worker thread for each threaded core results
  231. // in extra time spent synchronizing. Code based on
  232. // http://stackoverflow.com/questions/2901694/programatically-detect-number-of-physical-processors-cores-or-if-hyper-threading
  233. int regs[4];
  234. // Get vendor
  235. char vendor[12];
  236. CpuID(0, regs);
  237. ((unsigned*)vendor)[0] = regs[1]; // EBX
  238. ((unsigned*)vendor)[1] = regs[3]; // EDX
  239. ((unsigned*)vendor)[2] = regs[2]; // ECX
  240. String cpuVendor = String(vendor, 12);
  241. // Get CPU features
  242. CpuID(1, regs);
  243. unsigned cpuFeatures = regs[3]; // EDX
  244. // Logical core count per CPU
  245. CpuID(1, regs);
  246. unsigned logical = (regs[1] >> 16) & 0xff; // EBX[23:16]
  247. unsigned cores = logical;
  248. if (cpuVendor == "GenuineIntel")
  249. {
  250. // Get DCP cache info
  251. CpuID(4, regs);
  252. cores = ((regs[0] >> 26) & 0x3f) + 1; // EAX[31:26] + 1
  253. }
  254. else if (cpuVendor == "AuthenticAMD")
  255. {
  256. // Get NC: Number of CPU cores - 1
  257. CpuID(0x80000008, regs);
  258. cores = ((unsigned)(regs[2] & 0xff)) + 1; // ECX[7:0] + 1
  259. }
  260. return cores;
  261. }
  262. Mutex& GetStaticMutex()
  263. {
  264. return staticMutex;
  265. }