µGFX  2.9
version 2.9
gos_win32.c
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.io/license.html
6  */
7 
8 // We need to include stdio.h below. Turn off GFILE_NEED_STDIO just for this file to prevent conflicts
9 #define GFILE_NEED_STDIO_MUST_BE_OFF
10 
11 #include "../../gfx.h"
12 
13 #if GFX_USE_OS_WIN32
14 
15 #include <stdio.h>
16 
17 static HANDLE SystemMutex;
18 
19 void _gosInit(void)
20 {
21  /* No initialization of the operating system itself is needed */
22 }
23 
24 void _gosPostInit(void)
25 {
26 }
27 
28 void _gosDeinit(void)
29 {
30 
31 }
32 
33 void gfxHalt(const char *msg) {
34  if (msg)
35  fprintf(stderr, "%s\n", msg);
36 
37  ExitProcess(1);
38 }
39 
40 void gfxSleepMicroseconds(gDelay ms) {
41  static LARGE_INTEGER pcfreq;
42  static int initflag;
43  LARGE_INTEGER t1, t2, tdiff;
44 
45  switch(ms) {
46  case gDelayNone:
47  return;
48 
49  case gDelayForever:
50  while(1)
51  Sleep(1000);
52  return;
53  }
54 
55  if (!initflag) {
56  QueryPerformanceFrequency(&pcfreq);
57  initflag++;
58  }
59  tdiff.QuadPart = pcfreq.QuadPart * ms / 1000000;
60 
61  QueryPerformanceCounter(&t1);
62  do {
63  QueryPerformanceCounter(&t2);
64  } while (t2.QuadPart - t1.QuadPart < tdiff.QuadPart);
65 }
66 
67 void gfxSystemLock(void) {
68  if (!SystemMutex)
69  SystemMutex = CreateMutex(0, FALSE, 0);
70  WaitForSingleObject(SystemMutex, INFINITE);
71 }
72 
73 void gfxSystemUnlock(void) {
74  ReleaseMutex(SystemMutex);
75 }
76 
77 gBool gfxSemWait(gSem *psem, gDelay ms) {
78  return WaitForSingleObject(*psem, ms) == WAIT_OBJECT_0;
79 }
80 
81 typedef LONG (__stdcall *_NtQuerySemaphore)(
82  HANDLE SemaphoreHandle,
83  DWORD SemaphoreInformationClass, /* Would be SEMAPHORE_INFORMATION_CLASS */
84  PVOID SemaphoreInformation, /* but this is to much to dump here */
85  ULONG SemaphoreInformationLength,
86  PULONG ReturnLength OPTIONAL
87 );
88 
89 /* - Left here simply because of its undocumented cleverness...
90 gSemcount gfxSemCounter(gSem *pSem) {
91  static _NtQuerySemaphore NtQuerySemaphore;
92  struct _SEMAPHORE_BASIC_INFORMATION {
93  ULONG CurrentCount;
94  ULONG MaximumCount;
95  } BasicInfo;
96 
97  if (!NtQuerySemaphore)
98  NtQuerySemaphore = (_NtQuerySemaphore)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQuerySemaphore");
99 
100  NtQuerySemaphore(*pSem, 0, &BasicInfo, sizeof(BasicInfo), 0);
101 
102  return BasicInfo.CurrentCount;
103 }
104 */
105 
106 gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION(*fn,p), void *param) {
107  (void) stackarea;
108  HANDLE thd;
109 
110  if (!(thd = CreateThread(0, stacksz, fn, param, CREATE_SUSPENDED, 0)))
111  return 0;
112 
113  SetThreadPriority(thd, prio);
114  ResumeThread(thd);
115 
116  return thd;
117 }
118 
119 gThreadreturn gfxThreadWait(gThread thread) {
120  DWORD ret;
121 
122  WaitForSingleObject(thread, INFINITE);
123  GetExitCodeThread(thread, &ret);
124  CloseHandle(thread);
125 
126  return ret;
127 }
128 
129 #endif /* GFX_USE_OS_WIN32 */
gThreadreturn gfxThreadWait(gThread thread)
Wait for a thread to finish.
void * gThread
A thread handle.
Definition: gos.h:116
#define GFX_THREAD_FUNCTION(fnName, param)
Declare a thread function.
Definition: gos.h:73
gBool gfxSemWait(gSem *psem, gDelay ms)
Wait on a semaphore.
void gfxSystemUnlock(void)
Unlock the operating system previous locked by gfxSystemLock()
void gfxSleepMicroseconds(gDelay us)
Put the current thread to sleep for the specified period in microseconds.
void gfxSystemLock(void)
Lock the operating system to protect a sequence of code.
gThread gfxThreadCreate(void *stackarea, gMemSize stacksz, gThreadpriority prio, GFX_THREAD_FUNCTION((*fn), p), void *param)
Start a new thread.
void gfxHalt(const char *msg)
Halt the GFX application due to an error.
A semaphore.
Definition: gos.h:104