-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibDriverPlatform.cpp
More file actions
279 lines (227 loc) · 6.69 KB
/
LibDriverPlatform.cpp
File metadata and controls
279 lines (227 loc) · 6.69 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
This file is part of the DSP-Crowd project
https://www.dsp-crowd.com
Author(s):
- Johannes Natter, office@dsp-crowd.com
File created on 16.04.2024
Copyright (C) 2024, Johannes Natter
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Platform includes
/*
* Literature
* - https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
*/
#if defined(__linux__) || defined(__APPLE__)
#include <pthread.h>
#elif defined(_WIN32)
#include <windows.h>
#else
#error "No specific drivers implemented for this platform"
#endif
// Common
#include "LibDriverPlatform.h"
using namespace std;
// Platform types
struct DriverPlatform
{
#if defined(__linux__) || defined(__APPLE__)
pthread_t pthread;
#elif defined(_WIN32)
HANDLE hThread;
DWORD idThread;
#endif
ConfigDriver config;
FuncInternalDrive pFctDrive;
void *pProc;
};
// class ConfigDriver
size_t ConfigDriver::sizeStackDefault = 16384;
void ConfigDriver::sizeStackDefaultSet(size_t sizeStack)
{
sizeStackDefault = sizeStack;
}
// Platform code
/*
* Literature
* - https://man7.org/linux/man-pages/man3/pthread_create.3.html
* - https://man7.org/linux/man-pages/man3/pthread_join.3.html
* - https://man7.org/linux/man-pages/man3/pthread_attr_init.3.html
* - https://man7.org/linux/man-pages/man3/pthread_attr_setstacksize.3.html
*/
#if defined(__linux__) || defined(__APPLE__)
void *threadExecute(void *pData)
{
DriverPlatform *pDrv = (DriverPlatform *)pData;
pDrv->pFctDrive(pDrv->pProc);
return NULL;
}
void *driverPlatformCreate(FuncInternalDrive pFctDrive, void *pProc, void *pConfigDriver)
{
//wrnLog("REMOVE_ME: creating custom driver");
pthread_attr_t attrThread;
ConfigDriver configDefault;
ConfigDriver *pConfig = &configDefault;
DriverPlatform *pDrv = NULL;
int res;
res = pthread_attr_init(&attrThread);
if (res)
{
errLog(-1, "could not initialize pthread attributes: %s (%d)", strerror(res), res);
return NULL;
}
if (pConfigDriver)
pConfig = (ConfigDriver *)pConfigDriver;
res = pthread_attr_setstacksize(&attrThread, pConfig->mSizeStack);
if (res)
{
errLog(-1, "could not set stack size: %s (%d)", strerror(res), res);
goto drvExit;
}
pDrv = new dNoThrow DriverPlatform;
if (!pDrv)
{
errLog(-1, "could not allocate custom driver");
goto drvExit;
}
pDrv->config = *pConfig;
pDrv->pFctDrive = pFctDrive;
pDrv->pProc = pProc;
res = pthread_create(&pDrv->pthread, &attrThread, threadExecute, pDrv);
if (res)
{
errLog(-1, "could not create pthread: %s (%d)", strerror(res), res);
delete pDrv;
pDrv = NULL;
}
drvExit:
res = pthread_attr_destroy(&attrThread);
if (res)
errLog(-1, "could not destroy pthread attributes: %s (%d)", strerror(res), res);
return pDrv;
}
void driverPlatformCleanUp(void *pDriver)
{
//wrnLog("REMOVE_ME: cleaning up custom driver");
DriverPlatform *pDrv = (DriverPlatform *)pDriver;
int res = pthread_join(pDrv->pthread, NULL);
if (res)
errLog(-1, "could not join pthread: %s (%d)", strerror(res), res);
delete pDrv;
pDrv = NULL;
}
/*
* Literature
* - https://man7.org/linux/man-pages/man3/pthread_getattr_np.3.html
*/
size_t sizeStackGet()
{
pthread_attr_t attrThread;
size_t sizeStack;
int res;
#if defined(__linux__)
res = pthread_getattr_np(pthread_self(), &attrThread); // non-standard
#else
res = pthread_attr_init(&attrThread);
#endif
if (res)
{
errLog(-1, "could not get pthread attributes: %s (%d)", strerror(res), res);
return 0;
}
res = pthread_attr_getstacksize(&attrThread, &sizeStack);
if (res)
{
errLog(-1, "could not get stack size: %s (%d)", strerror(res), res);
sizeStack = 0;
}
res = pthread_attr_destroy(&attrThread);
if (res)
errLog(-1, "could not destroy pthread attributes: %s (%d)", strerror(res), res);
return sizeStack;
}
#endif
/* Literature
* - https://learn.microsoft.com/en-us/windows/win32/procthread/creating-threads
* - https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread
* - https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror
*/
#if defined(_WIN32)
DWORD WINAPI threadExecute(LPVOID lpParam)
{
DriverPlatform *pDrv = (DriverPlatform *)lpParam;
pDrv->pFctDrive(pDrv->pProc);
return 0;
}
void *driverPlatformCreate(FuncInternalDrive pFctDrive, void *pProc, void *pConfigDriver)
{
ConfigDriver configDefault;
ConfigDriver *pConfig = &configDefault;
DriverPlatform *pDrv = NULL;
if (pConfigDriver)
pConfig = (ConfigDriver *)pConfigDriver;
pDrv = new dNoThrow DriverPlatform;
if (!pDrv)
{
errLog(-1, "could not allocate custom driver");
goto drvExit;
}
pDrv->config = *pConfig;
pDrv->pFctDrive = pFctDrive;
pDrv->pProc = pProc;
pDrv->hThread = CreateThread(
NULL, // default security attributes
pConfig->mSizeStack, // custom stack size
threadExecute, // thread function name
pDrv, // argument to thread function
0, // use default creation flags
&pDrv->idThread); // returns the thread identifier
if (pDrv->hThread == NULL)
{
errLog(-1, "could not create thread");
delete pDrv;
pDrv = NULL;
}
drvExit:
return pDrv;
}
/*
* Literature
* - https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject
* - https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
* - https://learn.microsoft.com/en-us/windows/win32/procthread/thread-stack-size?redirectedfrom=MSDN
*/
void driverPlatformCleanUp(void *pDriver)
{
DriverPlatform *pDrv = (DriverPlatform *)pDriver;
DWORD res = WaitForSingleObject(pDrv->hThread, INFINITE);
if (res != WAIT_OBJECT_0)
errLog(-1, "could not wait for thread");
BOOL ok = CloseHandle(pDrv->hThread);
if (!ok)
errLog(-1, "could not close thread");
pDrv->hThread = NULL;
delete pDrv;
pDrv = NULL;
}
/*
* Literature
* - https://learn.microsoft.com/en-us/windows/win32/procthread/thread-stack-size
* - https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentthreadstacklimits
*/
size_t sizeStackGet()
{
ULONG_PTR limitLow, limitHigh;
GetCurrentThreadStackLimits(&limitLow, &limitHigh);
return limitHigh - limitLow;
}
#endif