aboutsummaryrefslogtreecommitdiffstats
path: root/src/gos/win32.c
blob: 02ba1b677a62576028b68d2231dd701ca3648cec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://chibios-gfx.com/license.html
 */

/**
 * @file    src/gos/chibios.c
 * @brief   GOS ChibiOS Operating System support.
 */
#include "gfx.h"

#if GFX_USE_OS_WIN32

#include <stdio.h>

static HANDLE	SystemMutex;

void _gosInit(void) {
}

void gfxHalt(const char *msg) {
	fprintf(stderr, "%s\n", msg);
	ExitProcess(1);
}

void gfxSleepMicroseconds(delaytime_t ms) {
    static LARGE_INTEGER pcfreq;
    static int initflag;
    LARGE_INTEGER t1, t2, tdiff;

    switch(ms) {
	case TIME_IMMEDIATE:	return;
	case TIME_INFINITE:		while(1) Sleep(1000);	return;
	}

    if (!initflag) {
        QueryPerformanceFrequency(&pcfreq);
        initflag++;
    }
    tdiff.QuadPart = pcfreq.QuadPart * ms / 1000000;

    QueryPerformanceCounter(&t1);
    do {
    	QueryPerformanceCounter(&t2);
    } while (t2.QuadPart - t1.QuadPart < tdiff.QuadPart);
}

void gfxSystemLock(void) {
	if (!SystemMutex)
		SystemMutex = CreateMutex(NULL, FALSE, NULL);
	WaitForSingleObject(SystemMutex, INFINITE);
}

void gfxSystemUnlock(void) {
	ReleaseMutex(SystemMutex);
}

bool_t gfxSemWait(gfxSem *psem, delaytime_t ms) {
	return WaitForSingleObject(*psem, ms) == WAIT_OBJECT_0;
}

typedef LONG __stdcall (*_NtQuerySemaphore)(
    HANDLE SemaphoreHandle,
    DWORD SemaphoreInformationClass, /* Would be SEMAPHORE_INFORMATION_CLASS */
    PVOID SemaphoreInformation,      /* but this is to much to dump here     */
    ULONG SemaphoreInformationLength,
    PULONG ReturnLength OPTIONAL
);

semcount_t gfxSemCounter(gfxSem *pSem) {
	static _NtQuerySemaphore NtQuerySemaphore;
	struct _SEMAPHORE_BASIC_INFORMATION {
	    ULONG CurrentCount;
	    ULONG MaximumCount;
	} BasicInfo;

    if (!NtQuerySemaphore)
    	NtQuerySemaphore = (_NtQuerySemaphore)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQuerySemaphore");

    NtQuerySemaphore(*pSem, 0, &BasicInfo, sizeof(BasicInfo), NULL);
    return BasicInfo.CurrentCount;
}

gfxThreadHandle gfxThreadCreate(void *stackarea, size_t stacksz, threadpriority_t prio, DECLARE_THREAD_FUNCTION((*fn),p), void *param) {
	(void)	stackarea;
	HANDLE	thd;

	if (!(thd = CreateThread(NULL, stacksz, fn, param, CREATE_SUSPENDED, NULL)))
		return FALSE;
	SetThreadPriority(thd, prio);
	ResumeThread(thd);
	return thd;
}

threadreturn_t gfxThreadWait(gfxThreadHandle thread) {
	DWORD	ret;

	WaitForSingleObject(thread, INFINITE);
	GetExitCodeThread(thread, &ret);
	CloseHandle(thread);
	return ret;
}

#endif /* GFX_USE_OS_WIN32 */
/** @} */