-
Notifications
You must be signed in to change notification settings - Fork 76
/
platform_WINDOWS.c
96 lines (77 loc) · 2.25 KB
/
platform_WINDOWS.c
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
/*
platform_WINDOWS.c - Windows specific functions with generic cross-platform interface
Part of Grbl Simulator
Copyright (c) 2014 Adam Shelly
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include "platform.h"
#define NS_PER_SEC 1000000000
#define MICRO_PER_MILLI 1000
double ns_per_perfcount;
//any platform-specific setup that must be done before sim starts here
void platform_init()
{
__int64 counts_per_sec;
QueryPerformanceFrequency((LARGE_INTEGER*)&counts_per_sec);
ns_per_perfcount = (float)NS_PER_SEC / counts_per_sec;
}
//cleanup int here;
void platform_terminate()
{
}
//returns a free-running 32 bit nanosecond counter which rolls over
uint32_t platform_ns()
{
static uint32_t gTimeBase = 0;
__int64 counts;
uint32_t ns;
QueryPerformanceCounter((LARGE_INTEGER*)&counts);
ns = (counts*ns_per_perfcount);
if (gTimeBase== 0){gTimeBase=ns;}
return ns-gTimeBase;
}
//sleep in microseconds
void platform_sleep(long microsec)
{
Sleep(microsec/MICRO_PER_MILLI);
}
//create a thread
plat_thread_t* platform_start_thread(plat_threadfunc_t threadfunc) {
plat_thread_t* th = malloc(sizeof(plat_thread_t));
th->tid = CreateThread(NULL,0,threadfunc,&th->exit,0,NULL);
if (!th->tid){
free(th);
return NULL;
}
return th;
}
//ask thread to exit nicely, wait
void platform_stop_thread(plat_thread_t* th){
th->exit = 1;
WaitForSingleObject(th->tid,INFINITE);
}
//force-kill thread
void platform_kill_thread(plat_thread_t* th){
th->exit = 1;
TerminateThread(th->tid, 0);
}
//return char if one available.
uint8_t platform_poll_stdin() {
if (_kbhit()) {
return getch();
}
return 0;
}