µGFX library fork

gos_win32.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.org/license.html
  6. */
  7. // We need to include stdio.h below. Turn off GFILE_NEED_STDIO just for this file to prevent conflicts
  8. #define GFILE_NEED_STDIO_MUST_BE_OFF
  9. #include "../../gfx.h"
  10. #if GFX_USE_OS_WIN32
  11. #include <stdio.h>
  12. static HANDLE SystemMutex;
  13. void _gosInit(void)
  14. {
  15. /* No initialization of the operating system itself is needed */
  16. }
  17. void _gosPostInit(void)
  18. {
  19. }
  20. void _gosDeinit(void)
  21. {
  22. }
  23. void gfxHalt(const char *msg) {
  24. if (msg)
  25. fprintf(stderr, "%s\n", msg);
  26. ExitProcess(1);
  27. }
  28. void gfxSleepMicroseconds(delaytime_t ms) {
  29. static LARGE_INTEGER pcfreq;
  30. static int initflag;
  31. LARGE_INTEGER t1, t2, tdiff;
  32. switch(ms) {
  33. case TIME_IMMEDIATE:
  34. return;
  35. case TIME_INFINITE:
  36. while(1)
  37. Sleep(1000);
  38. return;
  39. }
  40. if (!initflag) {
  41. QueryPerformanceFrequency(&pcfreq);
  42. initflag++;
  43. }
  44. tdiff.QuadPart = pcfreq.QuadPart * ms / 1000000;
  45. QueryPerformanceCounter(&t1);
  46. do {
  47. QueryPerformanceCounter(&t2);
  48. } while (t2.QuadPart - t1.QuadPart < tdiff.QuadPart);
  49. }
  50. void gfxSystemLock(void) {
  51. if (!SystemMutex)
  52. SystemMutex = CreateMutex(0, FALSE, 0);
  53. WaitForSingleObject(SystemMutex, INFINITE);
  54. }
  55. void gfxSystemUnlock(void) {
  56. ReleaseMutex(SystemMutex);
  57. }
  58. bool_t gfxSemWait(gfxSem *psem, delaytime_t ms) {
  59. return WaitForSingleObject(*psem, ms) == WAIT_OBJECT_0;
  60. }
  61. typedef LONG (__stdcall *_NtQuerySemaphore)(
  62. HANDLE SemaphoreHandle,
  63. DWORD SemaphoreInformationClass, /* Would be SEMAPHORE_INFORMATION_CLASS */
  64. PVOID SemaphoreInformation, /* but this is to much to dump here */
  65. ULONG SemaphoreInformationLength,
  66. PULONG ReturnLength OPTIONAL
  67. );
  68. semcount_t gfxSemCounter(gfxSem *pSem) {
  69. static _NtQuerySemaphore NtQuerySemaphore;
  70. struct _SEMAPHORE_BASIC_INFORMATION {
  71. ULONG CurrentCount;
  72. ULONG MaximumCount;
  73. } BasicInfo;
  74. if (!NtQuerySemaphore)
  75. NtQuerySemaphore = (_NtQuerySemaphore)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQuerySemaphore");
  76. NtQuerySemaphore(*pSem, 0, &BasicInfo, sizeof(BasicInfo), 0);
  77. return BasicInfo.CurrentCount;
  78. }
  79. gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION(*fn,p), void *param) {
  80. (void) stackarea;
  81. HANDLE thd;
  82. if (!(thd = CreateThread(0, stacksz, fn, param, CREATE_SUSPENDED, 0)))
  83. return 0;
  84. SetThreadPriority(thd, prio);
  85. ResumeThread(thd);
  86. return thd;
  87. }
  88. threadreturn_t gfxThreadWait(gfxThreadHandle thread) {
  89. DWORD ret;
  90. WaitForSingleObject(thread, INFINITE);
  91. GetExitCodeThread(thread, &ret);
  92. CloseHandle(thread);
  93. return ret;
  94. }
  95. #endif /* GFX_USE_OS_WIN32 */
  96. /** @} */