os.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2010-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef BX_OS_H_HEADER_GUARD
  6. #define BX_OS_H_HEADER_GUARD
  7. #include "bx.h"
  8. #include "debug.h"
  9. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT
  10. # include <windows.h>
  11. #elif BX_PLATFORM_ANDROID \
  12. || BX_PLATFORM_EMSCRIPTEN \
  13. || BX_PLATFORM_FREEBSD \
  14. || BX_PLATFORM_IOS \
  15. || BX_PLATFORM_LINUX \
  16. || BX_PLATFORM_NACL \
  17. || BX_PLATFORM_OSX \
  18. || BX_PLATFORM_RPI
  19. # include <sched.h> // sched_yield
  20. # if BX_PLATFORM_FREEBSD || BX_PLATFORM_IOS || BX_PLATFORM_NACL || BX_PLATFORM_OSX
  21. # include <pthread.h> // mach_port_t
  22. # endif // BX_PLATFORM_IOS || BX_PLATFORM_OSX || BX_PLATFORM_NACL
  23. # if BX_PLATFORM_NACL
  24. # include <sys/nacl_syscalls.h> // nanosleep
  25. # else
  26. # include <time.h> // nanosleep
  27. # include <dlfcn.h> // dlopen, dlclose, dlsym
  28. # endif // BX_PLATFORM_NACL
  29. # if BX_PLATFORM_LINUX || BX_PLATFORM_RPI
  30. # include <unistd.h> // syscall
  31. # include <sys/syscall.h>
  32. # endif // BX_PLATFORM_LINUX || BX_PLATFORM_RPI
  33. # if BX_PLATFORM_ANDROID
  34. # include "debug.h" // getTid is not implemented...
  35. # endif // BX_PLATFORM_ANDROID
  36. #endif // BX_PLATFORM_
  37. #if BX_COMPILER_MSVC_COMPATIBLE
  38. # include <direct.h> // _getcwd
  39. #else
  40. # include <unistd.h> // getcwd
  41. #endif // BX_COMPILER_MSVC
  42. namespace bx
  43. {
  44. inline void sleep(uint32_t _ms)
  45. {
  46. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
  47. ::Sleep(_ms);
  48. #elif BX_PLATFORM_WINRT
  49. BX_UNUSED(_ms);
  50. debugOutput("sleep is not implemented"); debugBreak();
  51. #else
  52. timespec req = {(time_t)_ms/1000, (long)((_ms%1000)*1000000)};
  53. timespec rem = {0, 0};
  54. ::nanosleep(&req, &rem);
  55. #endif // BX_PLATFORM_
  56. }
  57. inline void yield()
  58. {
  59. #if BX_PLATFORM_WINDOWS
  60. ::SwitchToThread();
  61. #elif BX_PLATFORM_XBOX360
  62. ::Sleep(0);
  63. #elif BX_PLATFORM_WINRT
  64. debugOutput("yield is not implemented"); debugBreak();
  65. #else
  66. ::sched_yield();
  67. #endif // BX_PLATFORM_
  68. }
  69. inline uint32_t getTid()
  70. {
  71. #if BX_PLATFORM_WINDOWS
  72. return ::GetCurrentThreadId();
  73. #elif BX_PLATFORM_LINUX || BX_PLATFORM_RPI
  74. return (pid_t)::syscall(SYS_gettid);
  75. #elif BX_PLATFORM_IOS || BX_PLATFORM_OSX
  76. return (mach_port_t)::pthread_mach_thread_np(pthread_self() );
  77. #elif BX_PLATFORM_FREEBSD || BX_PLATFORM_NACL
  78. // Casting __nc_basic_thread_data*... need better way to do this.
  79. return *(uint32_t*)::pthread_self();
  80. #else
  81. //# pragma message "not implemented."
  82. debugOutput("getTid is not implemented"); debugBreak();
  83. return 0;
  84. #endif //
  85. }
  86. inline void* dlopen(const char* _filePath)
  87. {
  88. #if BX_PLATFORM_WINDOWS
  89. return (void*)::LoadLibraryA(_filePath);
  90. #elif BX_PLATFORM_NACL || BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_WINRT
  91. BX_UNUSED(_filePath);
  92. return NULL;
  93. #else
  94. return ::dlopen(_filePath, RTLD_LOCAL|RTLD_LAZY);
  95. #endif // BX_PLATFORM_
  96. }
  97. inline void dlclose(void* _handle)
  98. {
  99. #if BX_PLATFORM_WINDOWS
  100. ::FreeLibrary( (HMODULE)_handle);
  101. #elif BX_PLATFORM_NACL || BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_WINRT
  102. BX_UNUSED(_handle);
  103. #else
  104. ::dlclose(_handle);
  105. #endif // BX_PLATFORM_
  106. }
  107. inline void* dlsym(void* _handle, const char* _symbol)
  108. {
  109. #if BX_PLATFORM_WINDOWS
  110. return (void*)::GetProcAddress( (HMODULE)_handle, _symbol);
  111. #elif BX_PLATFORM_NACL || BX_PLATFORM_EMSCRIPTEN || BX_PLATFORM_WINRT
  112. BX_UNUSED(_handle, _symbol);
  113. return NULL;
  114. #else
  115. return ::dlsym(_handle, _symbol);
  116. #endif // BX_PLATFORM_
  117. }
  118. inline void setenv(const char* _name, const char* _value)
  119. {
  120. #if BX_PLATFORM_WINDOWS
  121. ::SetEnvironmentVariableA(_name, _value);
  122. #elif BX_PLATFORM_WINRT
  123. BX_UNUSED(_name, _value);
  124. #else
  125. ::setenv(_name, _value, 1);
  126. #endif // BX_PLATFORM_
  127. }
  128. inline void unsetenv(const char* _name)
  129. {
  130. #if BX_PLATFORM_WINDOWS
  131. ::SetEnvironmentVariableA(_name, NULL);
  132. #elif BX_PLATFORM_WINRT
  133. BX_UNUSED(_name);
  134. #else
  135. ::unsetenv(_name);
  136. #endif // BX_PLATFORM_
  137. }
  138. inline int chdir(const char* _path)
  139. {
  140. #if BX_PLATFORM_WINRT
  141. BX_UNUSED(_path);
  142. #elif BX_COMPILER_MSVC_COMPATIBLE
  143. return ::_chdir(_path);
  144. #else
  145. return ::chdir(_path);
  146. #endif // BX_COMPILER_
  147. }
  148. inline char* pwd(char* _buffer, uint32_t _size)
  149. {
  150. #if BX_PLATFORM_WINRT
  151. BX_UNUSED(_buffer, _size);
  152. #elif BX_COMPILER_MSVC_COMPATIBLE
  153. return ::_getcwd(_buffer, (int)_size);
  154. #else
  155. return ::getcwd(_buffer, _size);
  156. #endif // BX_COMPILER_
  157. }
  158. } // namespace bx
  159. #endif // BX_OS_H_HEADER_GUARD