ProcessUtils.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #else
  38. // From http://stereopsis.com/FPU.html
  39. #define FPU_CW_PREC_MASK 0x0300
  40. #define FPU_CW_PREC_SINGLE 0x0000
  41. #define FPU_CW_PREC_DOUBLE 0x0200
  42. #define FPU_CW_PREC_EXTENDED 0x0300
  43. #define FPU_CW_ROUND_MASK 0x0c00
  44. #define FPU_CW_ROUND_NEAR 0x0000
  45. #define FPU_CW_ROUND_DOWN 0x0400
  46. #define FPU_CW_ROUND_UP 0x0800
  47. #define FPU_CW_ROUND_CHOP 0x0c00
  48. inline unsigned GetFPUState()
  49. {
  50. unsigned control = 0;
  51. __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
  52. return control;
  53. }
  54. inline void SetFPUState(unsigned control)
  55. {
  56. __asm__ __volatile__ ("fldcw %0" : : "m" (control));
  57. }
  58. #endif
  59. #include "DebugNew.h"
  60. #ifdef WIN32
  61. static bool consoleOpened = false;
  62. #endif
  63. static String currentLine;
  64. static Vector<String> arguments;
  65. static Mutex staticMutex;
  66. void InitFPU()
  67. {
  68. // Make sure FPU is in round-to-nearest, single precision mode
  69. // This is needed for ODE to behave predictably in float mode
  70. #ifdef _MSC_VER
  71. _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
  72. #else
  73. unsigned control = GetFPUState();
  74. control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
  75. control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
  76. SetFPUState(control);
  77. #endif
  78. }
  79. void ErrorDialog(const char* title, const char* message)
  80. {
  81. #ifdef WIN32
  82. MessageBox(0, message, title, 0);
  83. #else
  84. printf("%s\n", message);
  85. #endif
  86. }
  87. void ErrorExit(const String& message, int exitCode)
  88. {
  89. printf("%s", message.CString());
  90. exit(exitCode);
  91. }
  92. void OpenConsoleWindow()
  93. {
  94. #ifdef WIN32
  95. if (consoleOpened)
  96. return;
  97. AllocConsole();
  98. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  99. int hCrt = _open_osfhandle((long)hOut, _O_TEXT);
  100. FILE* outFile = _fdopen(hCrt, "w");
  101. setvbuf(outFile, NULL, _IONBF, 1);
  102. *stdout = *outFile;
  103. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  104. hCrt = _open_osfhandle((long)hIn, _O_TEXT);
  105. FILE* inFile = _fdopen(hCrt, "r");
  106. setvbuf(inFile, NULL, _IONBF, 128);
  107. *stdin = *inFile;
  108. consoleOpened = true;
  109. #endif
  110. }
  111. void PrintLine(const String& str)
  112. {
  113. printf("%s\n", str.CString());
  114. }
  115. void PrintLine(const char* str)
  116. {
  117. printf("%s\n", str);
  118. }
  119. const Vector<String>& ParseArguments(const char* cmdLine)
  120. {
  121. arguments.Clear();
  122. if (!cmdLine)
  123. return arguments;
  124. String cmdStr(cmdLine);
  125. unsigned cmdStart = 0, cmdEnd = 0;
  126. bool inCmd = false;
  127. bool inQuote = false;
  128. for (unsigned i = 0; i < cmdStr.Length(); ++i)
  129. {
  130. if (cmdStr[i] == '\"')
  131. inQuote = !inQuote;
  132. if (cmdStr[i] == ' ' && !inQuote)
  133. {
  134. if (inCmd)
  135. {
  136. inCmd = false;
  137. cmdEnd = i;
  138. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  139. }
  140. }
  141. else
  142. {
  143. if (!inCmd)
  144. {
  145. inCmd = true;
  146. cmdStart = i;
  147. }
  148. }
  149. }
  150. if (inCmd)
  151. {
  152. cmdEnd = cmdStr.Length();
  153. arguments.Push(cmdStr.Substring(cmdStart, cmdEnd - cmdStart));
  154. }
  155. return arguments;
  156. }
  157. const Vector<String>& GetArguments()
  158. {
  159. return arguments;
  160. }
  161. String GetConsoleInput()
  162. {
  163. String ret;
  164. #ifdef WIN32
  165. HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
  166. if (input == INVALID_HANDLE_VALUE)
  167. return ret;
  168. // Use char-based input
  169. SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
  170. INPUT_RECORD record;
  171. DWORD events = 0;
  172. DWORD readEvents = 0;
  173. if (!GetNumberOfConsoleInputEvents(input, &events))
  174. return ret;
  175. while (events--)
  176. {
  177. ReadConsoleInput(input, &record, 1, &readEvents);
  178. if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
  179. {
  180. char c = record.Event.KeyEvent.uChar.AsciiChar;
  181. if (c)
  182. {
  183. if (c == '\b')
  184. {
  185. printf("\b \b");
  186. int length = currentLine.Length();
  187. if (length)
  188. currentLine.Resize(length - 1);
  189. }
  190. else if (c == '\r')
  191. {
  192. printf("\n");
  193. ret = currentLine;
  194. currentLine.Clear();
  195. return ret;
  196. }
  197. else
  198. {
  199. // We have disabled echo, so echo manually
  200. printf("%c", c);
  201. currentLine += c;
  202. }
  203. }
  204. }
  205. }
  206. #else
  207. int flags = fcntl(STDIN_FILENO, F_GETFL);
  208. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  209. for (;;)
  210. {
  211. int ch = fgetc(stdin);
  212. if (ch >= 0 && ch != '\n')
  213. ret += (char)ch;
  214. else
  215. break;
  216. }
  217. #endif
  218. return ret;
  219. }
  220. unsigned GetNumCPUCores()
  221. {
  222. #ifdef WIN32
  223. SYSTEM_INFO info;
  224. GetSystemInfo(&info);
  225. unsigned numCores = info.dwNumberOfProcessors;
  226. #else
  227. unsigned numCores = sysconf(_SC_NPROCESSORS_ONLN);
  228. #endif
  229. // If CPU uses hyperthreading, report only half of the cores, as using the "extra" cores for worker threads
  230. // seems to result in extra time spent synchronizing
  231. unsigned func = 1;
  232. unsigned a, b, c, d;
  233. // CPUID inline assembly from http://softpixel.com/~cwright/programming/simd/cpuid.php
  234. #ifdef _MSC_VER
  235. __asm
  236. {
  237. mov eax, func
  238. cpuid
  239. mov a, eax
  240. mov b, ebx
  241. mov c, ecx
  242. mov d, edx
  243. }
  244. #else
  245. __asm__ __volatile__ ("cpuid":
  246. "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (func));
  247. #endif
  248. if (d & 0x10000000)
  249. numCores >>= 1;
  250. return numCores;
  251. }
  252. Mutex& GetStaticMutex()
  253. {
  254. return staticMutex;
  255. }