-
Notifications
You must be signed in to change notification settings - Fork 5
/
system.h
213 lines (163 loc) · 4.47 KB
/
system.h
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Copyright (C) Tammo Hinrichs 2021. All rights reserved.
// Licensed under the MIT License. See LICENSE.md file for full license information
//
#pragma once
#include "types.h"
/*
struct ScreenMode
{
uint width;
uint height;
bool fullscreen;
};
*/
// time
// -------------------------------------------------------------------------------
int64 GetTicks(); // raw timer ticks
double GetTime(); // time since program start in seconds
struct SystemTime {
uint year;
uint month;
uint dayOfWeek;
uint day;
uint hour;
uint minute;
uint second;
uint milliseconds;
};
SystemTime GetSystemTime(); // wall-clock time
// streams / files
// -------------------------------------------------------------------------------
struct Stream
{
enum class From { Start, Current, End };
virtual ~Stream() {};
virtual bool CanRead() const { return false; }
virtual bool CanWrite() const { return false; }
virtual bool CanSeek() const { return false; }
virtual uint64 Read(void* ptr, uint64 len) = 0;
virtual uint64 Write(const void* ptr, uint64 len) = 0;
virtual uint64 Length() const { return 0; }
virtual uint64 Seek(int64, From) { return 0; }
virtual uint64 Pos() { return (uint64)Seek(0, From::Current); }
virtual RCPtr<Buffer> Map() { return RCPtr<Buffer>(); }
};
enum class OpenFileMode
{
Read,
Create,
Append,
RandomAccess,
};
bool FileExists(const char* path);
Stream* OpenFile(const char* path, OpenFileMode mode = OpenFileMode::Read);
RCPtr<Buffer> LoadFile(const char* path);
String ReadFileUTF8(const char* path);
void WriteFileUTF8(const String& str, const char* path);
RCPtr<Buffer> LoadResource(int name, int type);
// debug output
// -------------------------------------------------------------------------------
#if _DEBUG
void DPrintF(const char* format, ...);
#else
inline void DPrintF(const char*, ...) {}
#endif
[[ noreturn ]]
void Fatal(const char* format, ...);
void DbgOpenLog(const char* filename);
void DbgCloseLog();
// multithreading
// -------------------------------------------------------------------------------
class ThreadLock
{
public:
ThreadLock();
~ThreadLock();
void Lock();
void Unlock();
private:
void* P = nullptr;
};
class ScopeLock
{
public:
ScopeLock(ThreadLock& lock) : Lock(lock) { Lock.Lock(); }
~ScopeLock() { Lock.Unlock(); }
private:
ThreadLock &Lock;
};
// -------------------------------------------------------------------------------
class ThreadEvent
{
public:
ThreadEvent(bool autoReset = true);
~ThreadEvent();
void Fire();
void Reset();
void Wait();
bool Wait(int timeoutMs);
void* GetRawEvent() const;
private:
void* P = nullptr;
};
// -------------------------------------------------------------------------------
class Thread
{
public:
Thread(Func<void (Thread&)> threadFunc);
~Thread();
bool IsRunning() { return !ExitEv.Wait(0); }
void Wait() { ExitEv.Wait(); }
bool Wait(int timeoutMs) { return !ExitEv.Wait(timeoutMs); }
void Terminate() { ExitEv.Fire(); }
static void Sleep(int ms);
private:
struct Priv;
Priv* P = nullptr;
ThreadEvent ExitEv = ThreadEvent(false);
};
// -------------------------------------------------------------------------------
// concurrent queue
template <typename T, int SIZE> class Queue
{
public:
bool Enqueue(const T &value)
{
ScopeLock lock(Lock);
if (IsFull()) return false;
Buffer[(Write++) % SIZE] = value;
return true;
}
bool Dequeue(T &value)
{
ScopeLock lock(Lock);
if (IsEmpty()) return false;
value = Buffer[(Read++) % SIZE];
if (Write >= SIZE && Read >= SIZE)
{
Write -= SIZE;
Read -= SIZE;
}
return true;
}
bool Peek(T& value)
{
ScopeLock lock(Lock);
if (IsEmpty()) return false;
value = Buffer[Read % SIZE];
return true;
}
int Count() { ScopeLock lock(Lock); return (int)(Write - Read); }
bool IsEmpty() { ScopeLock lock(Lock); return Write == Read; }
bool IsFull() { ScopeLock lock(Lock); return Write - Read == SIZE; }
private:
ThreadLock Lock;
uint Read = 0;
uint Write = 0;
T Buffer[SIZE];
};
// -------------------------------------------------------------------------------
// -------------------------------------------------------------------------------
bool IsFullscreen();
void SetScrollLock(bool on);