-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSysHelperLin.h
More file actions
134 lines (114 loc) · 2.44 KB
/
Copy pathSysHelperLin.h
File metadata and controls
134 lines (114 loc) · 2.44 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#pragma once
// TODO remove std=gnu99 ?
// #define __USE_POSIX199309
#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
static struct timespec initSpec;
// static long clock2()
// {
// return clock()/(CLOCKS_PER_SEC/1000);
// }
__attribute__((constructor))
void SysHelperInit()
{
clock_gettime(CLOCK_REALTIME, &initSpec);
}
void SysHelperGetConsolePosition(int* x, int* y)
{
// #error SysHelperGetConsolePosition not implemented
*x = 0;
*y = 0;
}
int SysHelperGetPid()
{
return getpid();
}
int SysHelperGetCpuUsage()
{
// #error GetCpuUsage not implemented
return 100;
}
long SysHelperGetTime()
{
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
unsigned long ms1 = initSpec.tv_sec * 1000 + (double)(int)(initSpec.tv_nsec / 1.0e6);
unsigned long ms2 = spec.tv_sec * 1000 + (double)(int)( spec.tv_nsec / 1.0e6);
long ms = ms2 - ms1;
return ms;
}
void SysHelperWait(long milliseconds)
{
long end = SysHelperGetTime() + milliseconds;
while (SysHelperGetTime() < end) {}
}
void SysHelperHalt(long milliseconds)
{
// TODO review
struct timespec ts;
int res;
if (milliseconds < 0)
errno = EINVAL;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
do
{
res = nanosleep(&ts, &ts);
}
while (res && errno == EINTR);
// usleep(milliseconds * 1000);
}
void SysHelperWaitLoop(long timestep)
{
static long start = 0;
// if wait time 0
long now = SysHelperGetTime();
long calcTime = now - start;
long waitTime = timestep - calcTime;
if (waitTime <= 0)
{
start = now;
return;
}
long end = start + timestep;
while (SysHelperGetTime() < end) {}
start = end;
}
void SysHelperHaltLoop(long timestep)
{
static long start = 0;
// if wait time 0
long now = SysHelperGetTime();
long calcTime = now - start;
long waitTime = timestep - calcTime;
if (waitTime <= 0)
{
start = now;
return;
}
long end = start + timestep;
SysHelperHalt(waitTime);
start = end;
}
void SysHelperCreateEscThread()
{
}
int SysHelperGetCurentThreadPriority()
{
return 0;
}
void SysHelperSetCurentThreadPriorityNormal()
{
}
void SysHelperSetCurentThreadPriorityHigh()
{
}
void SysHelperSetCurentThreadPriorityCritical()
{
}