platform-windows.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <windows.h>
  17. #include <shellapi.h>
  18. #include <shlobj.h>
  19. #include "base.h"
  20. #include "platform.h"
  21. #include "dstr.h"
  22. #include "../../deps/w32-pthreads/pthread.h"
  23. static bool have_clockfreq = false;
  24. static LARGE_INTEGER clock_freq;
  25. static uint32_t winver = 0;
  26. static inline uint64_t get_clockfreq(void)
  27. {
  28. if (!have_clockfreq)
  29. QueryPerformanceFrequency(&clock_freq);
  30. return clock_freq.QuadPart;
  31. }
  32. static inline uint32_t get_winver(void)
  33. {
  34. if (!winver) {
  35. OSVERSIONINFO osvi;
  36. memset(&osvi, 0, sizeof(osvi));
  37. winver = (osvi.dwMajorVersion << 16) | (osvi.dwMinorVersion);
  38. }
  39. return winver;
  40. }
  41. void *os_dlopen(const char *path)
  42. {
  43. struct dstr dll_name;
  44. wchar_t *wpath;
  45. HMODULE h_library = NULL;
  46. dstr_init_copy(&dll_name, path);
  47. dstr_cat(&dll_name, ".dll");
  48. os_utf8_to_wcs(dll_name.array, 0, &wpath);
  49. h_library = LoadLibraryW(wpath);
  50. bfree(wpath);
  51. dstr_free(&dll_name);
  52. if (!h_library)
  53. blog(LOG_INFO, "LoadLibrary failed for '%s', error: %u",
  54. path, GetLastError());
  55. return h_library;
  56. }
  57. void *os_dlsym(void *module, const char *func)
  58. {
  59. void *handle;
  60. handle = (void*)GetProcAddress(module, func);
  61. return handle;
  62. }
  63. void os_dlclose(void *module)
  64. {
  65. FreeLibrary(module);
  66. }
  67. void os_sleepto_ns(uint64_t time_target)
  68. {
  69. uint64_t t = os_gettime_ns();
  70. uint32_t milliseconds;
  71. if (t >= time_target)
  72. return;
  73. milliseconds = (uint32_t)((time_target - t)/1000000);
  74. if (milliseconds > 1)
  75. os_sleep_ms(milliseconds);
  76. for (;;) {
  77. t = os_gettime_ns();
  78. if (t >= time_target)
  79. return;
  80. #if 1
  81. Sleep(1);
  82. #else
  83. Sleep(0);
  84. #endif
  85. }
  86. }
  87. void os_sleep_ms(uint32_t duration)
  88. {
  89. /* windows 8+ appears to have decreased sleep precision */
  90. if (get_winver() >= 0x0602 && duration > 0)
  91. duration--;
  92. Sleep(duration);
  93. }
  94. uint64_t os_gettime_ns(void)
  95. {
  96. LARGE_INTEGER current_time;
  97. double time_val;
  98. QueryPerformanceCounter(&current_time);
  99. time_val = (double)current_time.QuadPart;
  100. time_val *= 1000000000.0;
  101. time_val /= (double)get_clockfreq();
  102. return (uint64_t)time_val;
  103. }
  104. /* returns %appdata%\[name] on windows */
  105. char *os_get_config_path(const char *name)
  106. {
  107. char *ptr;
  108. wchar_t path_utf16[MAX_PATH];
  109. struct dstr path;
  110. SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
  111. path_utf16);
  112. os_wcs_to_utf8(path_utf16, 0, &ptr);
  113. dstr_init_move_array(&path, ptr);
  114. dstr_cat(&path, "\\");
  115. dstr_cat(&path, name);
  116. return path.array;
  117. }
  118. bool os_file_exists(const char *path)
  119. {
  120. WIN32_FIND_DATA wfd;
  121. HANDLE hFind;
  122. wchar_t *path_utf16;
  123. if (!os_utf8_to_wcs(path, 0, &path_utf16))
  124. return false;
  125. hFind = FindFirstFileW(path_utf16, &wfd);
  126. if (hFind != INVALID_HANDLE_VALUE)
  127. FindClose(hFind);
  128. bfree(path_utf16);
  129. return hFind != INVALID_HANDLE_VALUE;
  130. }
  131. int os_mkdir(const char *path)
  132. {
  133. wchar_t *path_utf16;
  134. BOOL success;
  135. if (!os_utf8_to_wcs(path, 0, &path_utf16))
  136. return MKDIR_ERROR;
  137. success = CreateDirectory(path_utf16, NULL);
  138. bfree(path_utf16);
  139. if (!success)
  140. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  141. MKDIR_EXISTS : MKDIR_ERROR;
  142. return MKDIR_SUCCESS;
  143. }
  144. #ifdef PTW32_STATIC_LIB
  145. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  146. {
  147. switch (reason) {
  148. case DLL_PROCESS_ATTACH:
  149. pthread_win32_process_attach_np();
  150. break;
  151. case DLL_PROCESS_DETACH:
  152. pthread_win32_process_detach_np();
  153. break;
  154. case DLL_THREAD_ATTACH:
  155. pthread_win32_thread_attach_np();
  156. break;
  157. case DLL_THREAD_DETACH:
  158. pthread_win32_thread_detach_np();
  159. break;
  160. }
  161. return true;
  162. }
  163. #endif