platform-windows.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <mmsystem.h>
  18. #include <shellapi.h>
  19. #include <shlobj.h>
  20. #include "base.h"
  21. #include "platform.h"
  22. #include "dstr.h"
  23. #include "../../deps/w32-pthreads/pthread.h"
  24. static bool have_clockfreq = false;
  25. static LARGE_INTEGER clock_freq;
  26. static uint32_t winver = 0;
  27. static inline uint64_t get_clockfreq(void)
  28. {
  29. if (!have_clockfreq)
  30. QueryPerformanceFrequency(&clock_freq);
  31. return clock_freq.QuadPart;
  32. }
  33. static inline uint32_t get_winver(void)
  34. {
  35. if (!winver) {
  36. OSVERSIONINFO osvi;
  37. memset(&osvi, 0, sizeof(osvi));
  38. winver = (osvi.dwMajorVersion << 16) | (osvi.dwMinorVersion);
  39. }
  40. return winver;
  41. }
  42. void *os_dlopen(const char *path)
  43. {
  44. struct dstr dll_name;
  45. wchar_t *wpath;
  46. HMODULE h_library = NULL;
  47. if (!path)
  48. return NULL;
  49. dstr_init_copy(&dll_name, path);
  50. if (!dstr_find(&dll_name, ".dll"))
  51. dstr_cat(&dll_name, ".dll");
  52. os_utf8_to_wcs_ptr(dll_name.array, 0, &wpath);
  53. h_library = LoadLibraryW(wpath);
  54. bfree(wpath);
  55. dstr_free(&dll_name);
  56. if (!h_library)
  57. blog(LOG_INFO, "LoadLibrary failed for '%s', error: %u",
  58. path, GetLastError());
  59. return h_library;
  60. }
  61. void *os_dlsym(void *module, const char *func)
  62. {
  63. void *handle;
  64. handle = (void*)GetProcAddress(module, func);
  65. return handle;
  66. }
  67. void os_dlclose(void *module)
  68. {
  69. FreeLibrary(module);
  70. }
  71. union time_data {
  72. FILETIME ft;
  73. unsigned long long val;
  74. };
  75. struct os_cpu_usage_info {
  76. union time_data last_time, last_sys_time, last_user_time;
  77. DWORD core_count;
  78. };
  79. os_cpu_usage_info_t os_cpu_usage_info_start(void)
  80. {
  81. struct os_cpu_usage_info *info = bzalloc(sizeof(*info));
  82. SYSTEM_INFO si;
  83. FILETIME dummy;
  84. GetSystemInfo(&si);
  85. GetSystemTimeAsFileTime(&info->last_time.ft);
  86. GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
  87. &info->last_sys_time.ft, &info->last_user_time.ft);
  88. info->core_count = si.dwNumberOfProcessors;
  89. return info;
  90. }
  91. double os_cpu_usage_info_query(os_cpu_usage_info_t info)
  92. {
  93. union time_data cur_time, cur_sys_time, cur_user_time;
  94. FILETIME dummy;
  95. double percent;
  96. if (!info)
  97. return 0.0;
  98. GetSystemTimeAsFileTime(&cur_time.ft);
  99. GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
  100. &cur_sys_time.ft, &cur_user_time.ft);
  101. percent = (double)(cur_sys_time.val - info->last_sys_time.val +
  102. (cur_user_time.val - info->last_user_time.val));
  103. percent /= (double)(cur_time.val - info->last_time.val);
  104. percent /= (double)info->core_count;
  105. info->last_time.val = cur_time.val;
  106. info->last_sys_time.val = cur_sys_time.val;
  107. info->last_user_time.val = cur_user_time.val;
  108. return percent * 100.0;
  109. }
  110. void os_cpu_usage_info_destroy(os_cpu_usage_info_t info)
  111. {
  112. if (info)
  113. bfree(info);
  114. }
  115. bool os_sleepto_ns(uint64_t time_target)
  116. {
  117. uint64_t t = os_gettime_ns();
  118. uint32_t milliseconds;
  119. if (t >= time_target)
  120. return false;
  121. milliseconds = (uint32_t)((time_target - t)/1000000);
  122. if (milliseconds > 1)
  123. Sleep(milliseconds-1);
  124. for (;;) {
  125. t = os_gettime_ns();
  126. if (t >= time_target)
  127. return true;
  128. #if 1
  129. Sleep(1);
  130. #else
  131. Sleep(0);
  132. #endif
  133. }
  134. }
  135. void os_sleep_ms(uint32_t duration)
  136. {
  137. /* windows 8+ appears to have decreased sleep precision */
  138. if (get_winver() >= 0x0602 && duration > 0)
  139. duration--;
  140. Sleep(duration);
  141. }
  142. uint64_t os_gettime_ns(void)
  143. {
  144. LARGE_INTEGER current_time;
  145. double time_val;
  146. QueryPerformanceCounter(&current_time);
  147. time_val = (double)current_time.QuadPart;
  148. time_val *= 1000000000.0;
  149. time_val /= (double)get_clockfreq();
  150. return (uint64_t)time_val;
  151. }
  152. /* returns %appdata%\[name] on windows */
  153. char *os_get_config_path(const char *name)
  154. {
  155. char *ptr;
  156. wchar_t path_utf16[MAX_PATH];
  157. struct dstr path;
  158. SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
  159. path_utf16);
  160. os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
  161. dstr_init_move_array(&path, ptr);
  162. dstr_cat(&path, "\\");
  163. dstr_cat(&path, name);
  164. return path.array;
  165. }
  166. bool os_file_exists(const char *path)
  167. {
  168. WIN32_FIND_DATAW wfd;
  169. HANDLE hFind;
  170. wchar_t *path_utf16;
  171. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  172. return false;
  173. hFind = FindFirstFileW(path_utf16, &wfd);
  174. if (hFind != INVALID_HANDLE_VALUE)
  175. FindClose(hFind);
  176. bfree(path_utf16);
  177. return hFind != INVALID_HANDLE_VALUE;
  178. }
  179. struct os_dir {
  180. HANDLE handle;
  181. WIN32_FIND_DATA wfd;
  182. bool first;
  183. struct os_dirent out;
  184. };
  185. os_dir_t os_opendir(const char *path)
  186. {
  187. struct dstr path_str = {0};
  188. struct os_dir *dir = NULL;
  189. WIN32_FIND_DATA wfd;
  190. HANDLE handle;
  191. wchar_t *w_path;
  192. dstr_copy(&path_str, path);
  193. dstr_cat(&path_str, "/*.*");
  194. if (os_utf8_to_wcs_ptr(path_str.array, path_str.len, &w_path) > 0) {
  195. handle = FindFirstFileW(w_path, &wfd);
  196. if (handle != INVALID_HANDLE_VALUE) {
  197. dir = bzalloc(sizeof(struct os_dir));
  198. dir->handle = handle;
  199. dir->first = true;
  200. dir->wfd = wfd;
  201. }
  202. bfree(w_path);
  203. }
  204. dstr_free(&path_str);
  205. return dir;
  206. }
  207. struct os_dirent *os_readdir(os_dir_t dir)
  208. {
  209. if (!dir)
  210. return NULL;
  211. if (dir->first) {
  212. dir->first = false;
  213. } else {
  214. if (!FindNextFileW(dir->handle, &dir->wfd))
  215. return NULL;
  216. }
  217. os_wcs_to_utf8(dir->wfd.cFileName, 0, dir->out.d_name,
  218. sizeof(dir->out.d_name));
  219. dir->out.directory =
  220. !!(dir->wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  221. return &dir->out;
  222. }
  223. void os_closedir(os_dir_t dir)
  224. {
  225. if (dir) {
  226. FindClose(dir->handle);
  227. bfree(dir);
  228. }
  229. }
  230. int os_unlink(const char *path)
  231. {
  232. wchar_t *w_path;
  233. bool success;
  234. os_utf8_to_wcs_ptr(path, 0, &w_path);
  235. if (!w_path)
  236. return -1;
  237. success = !!DeleteFileW(w_path);
  238. bfree(w_path);
  239. return success ? 0 : -1;
  240. }
  241. int os_mkdir(const char *path)
  242. {
  243. wchar_t *path_utf16;
  244. BOOL success;
  245. if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
  246. return MKDIR_ERROR;
  247. success = CreateDirectory(path_utf16, NULL);
  248. bfree(path_utf16);
  249. if (!success)
  250. return (GetLastError() == ERROR_ALREADY_EXISTS) ?
  251. MKDIR_EXISTS : MKDIR_ERROR;
  252. return MKDIR_SUCCESS;
  253. }
  254. BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
  255. {
  256. switch (reason) {
  257. case DLL_PROCESS_ATTACH:
  258. timeBeginPeriod(1);
  259. #ifdef PTW32_STATIC_LIB
  260. pthread_win32_process_attach_np();
  261. #endif
  262. break;
  263. case DLL_PROCESS_DETACH:
  264. timeEndPeriod(1);
  265. #ifdef PTW32_STATIC_LIB
  266. pthread_win32_process_detach_np();
  267. #endif
  268. break;
  269. case DLL_THREAD_ATTACH:
  270. #ifdef PTW32_STATIC_LIB
  271. pthread_win32_thread_attach_np();
  272. #endif
  273. break;
  274. case DLL_THREAD_DETACH:
  275. #ifdef PTW32_STATIC_LIB
  276. pthread_win32_thread_detach_np();
  277. #endif
  278. break;
  279. }
  280. UNUSED_PARAMETER(hinst_dll);
  281. UNUSED_PARAMETER(reserved);
  282. return true;
  283. }