The official µGFX library repository.

gos_win32.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 _gosDeinit(void)
  18. {
  19. }
  20. void gfxHalt(const char *msg) {
  21. if (msg)
  22. fprintf(stderr, "%s\n", msg);
  23. ExitProcess(1);
  24. }
  25. void gfxSleepMicroseconds(delaytime_t ms) {
  26. static LARGE_INTEGER pcfreq;
  27. static int initflag;
  28. LARGE_INTEGER t1, t2, tdiff;
  29. switch(ms) {
  30. case TIME_IMMEDIATE:
  31. return;
  32. case TIME_INFINITE:
  33. while(1)
  34. Sleep(1000);
  35. return;
  36. }
  37. if (!initflag) {
  38. QueryPerformanceFrequency(&pcfreq);
  39. initflag++;
  40. }
  41. tdiff.QuadPart = pcfreq.QuadPart * ms / 1000000;
  42. QueryPerformanceCounter(&t1);
  43. do {
  44. QueryPerformanceCounter(&t2);
  45. } while (t2.QuadPart - t1.QuadPart < tdiff.QuadPart);
  46. }
  47. void gfxSystemLock(void) {
  48. if (!SystemMutex)
  49. SystemMutex = CreateMutex(0, FALSE, 0);
  50. WaitForSingleObject(SystemMutex, INFINITE);
  51. }
  52. void gfxSystemUnlock(void) {
  53. ReleaseMutex(SystemMutex);
  54. }
  55. bool_t gfxSemWait(gfxSem *psem, delaytime_t ms) {
  56. return WaitForSingleObject(*psem, ms) == WAIT_OBJECT_0;
  57. }
  58. typedef LONG __stdcall (*_NtQuerySemaphore)(
  59. HANDLE SemaphoreHandle,
  60. DWORD SemaphoreInformationClass, /* Would be SEMAPHORE_INFORMATION_CLASS */
  61. PVOID SemaphoreInformation, /* but this is to much to dump here */
  62. ULONG SemaphoreInformationLength,
  63. PULONG ReturnLength OPTIONAL
  64. );
  65. semcount_t gfxSemCounter(gfxSem *pSem) {
  66. static _NtQuerySemaphore NtQuerySemaphore;
  67. struct _SEMAPHORE_BASIC_INFORMATION {
  68. ULONG CurrentCount;
  69. ULONG MaximumCount;
  70. } BasicInfo;
  71. if (!NtQuerySemaphore)
  72. NtQuerySemaphore = (_NtQuerySemaphore)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQuerySemaphore");
  73. NtQuerySemaphore(*pSem, 0, &BasicInfo, sizeof(BasicInfo), 0);
  74. return BasicInfo.CurrentCount;
  75. }
  76. gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) {
  77. (void) stackarea;
  78. HANDLE thd;
  79. if (!(thd = CreateThread(0, stacksz, fn, param, CREATE_SUSPENDED, 0)))
  80. return FALSE;
  81. SetThreadPriority(thd, prio);
  82. ResumeThread(thd);
  83. return thd;
  84. }
  85. threadreturn_t gfxThreadWait(gfxThreadHandle thread) {
  86. DWORD ret;
  87. WaitForSingleObject(thread, INFINITE);
  88. GetExitCodeThread(thread, &ret);
  89. CloseHandle(thread);
  90. return ret;
  91. }
  92. #endif /* GFX_USE_OS_WIN32 */
  93. /** @} */