-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProcessing.h
More file actions
475 lines (406 loc) · 11.9 KB
/
Processing.h
File metadata and controls
475 lines (406 loc) · 11.9 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
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 14.09.2018
Copyright (C) 2018, Johannes Natter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef PROCESSING_H
#define PROCESSING_H
#include "ProcConf.h"
#if CONFIG_PROC_HAVE_LIB_C_CUSTOM
#include "LibcCustom.h"
#endif
#if CONFIG_PROC_HAVE_LIB_STD_C
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#endif
#if CONFIG_PROC_HAVE_LIB_STD_CPP
#include <new>
#include <list>
#define dNoThrow (std::nothrow)
#endif
#ifndef dNoThrow
#define dNoThrow
#endif
#if CONFIG_PROC_LOG_HAVE_CHRONO
#include <chrono>
#endif
#if CONFIG_PROC_HAVE_DRIVERS
#include <thread>
#include <mutex>
typedef std::lock_guard<std::mutex> Guard;
#endif
#ifdef _MSC_VER
#include <BaseTsd.h>
#ifndef _SSIZE_T_DEFINED
typedef SSIZE_T ssize_t;
#define _SSIZE_T_DEFINED
#endif
#endif
enum DriverMode
{
DrivenByParent = 0,
DrivenByNewInternalDriver,
DrivenByExternalDriver
};
typedef int16_t Success;
enum SuccessState
{
Pending = 0,
Positive = 1
};
typedef void (*FuncGlobDestruct)();
typedef void (*FuncInternalDrive)(void *pProc);
typedef void * /* pDriver */ (*FuncDriverInternalCreate)(FuncInternalDrive pFctDrive, void *pProc, void *pConfigDriver);
typedef void (*FuncDriverInternalCleanUp)(void *pDriver);
class Processing
{
public:
// This area is used by the client
void treeTick();
bool progress() const;
Success success() const;
void unusedSet();
void procTreeDisplaySet(bool display);
bool initDone() const;
bool processDone() const;
bool shutdownDone() const;
size_t processTreeStr(char *pBuf, char *pBufEnd, bool detailed = true, bool colored = false);
#if CONFIG_PROC_HAVE_DRIVERS
void configDriverSet(void *pConfigDriver);
#endif
static void undrivenSet(Processing *pChild);
static void destroy(Processing *pChild);
static void applicationClose();
static void globalDestructorRegister(FuncGlobDestruct globDestr);
#if !CONFIG_PROC_HAVE_LIB_STD_C
static const char *strrchr(const char *x, char y);
static void *memcpy(void *to, const void *from, size_t cnt);
#endif
static void showAddressInIdSet(uint8_t val) { showAddressInId = val; }
static void disableTreeDefaultSet(uint8_t val) { disableTreeDefault = val; }
#if CONFIG_PROC_HAVE_DRIVERS
static void sleepUsInternalDriveSet(size_t delayUs);
static void sleepInternalDriveSet(std::chrono::microseconds delay);
static void sleepInternalDriveSet(std::chrono::milliseconds delay);
static void numBurstInternalDriveSet(size_t numBurst);
static void internalDriveSet(FuncInternalDrive pFctDrive);
static void driverInternalCreateAndCleanUpSet(
FuncDriverInternalCreate pFctCreate,
FuncDriverInternalCleanUp pFctCleanUp);
#endif
protected:
// This area is used by the concrete processes
Processing(const char *name);
virtual ~Processing();
const char *procName() const { return mName; }
Processing *start(Processing *pChild, DriverMode driver = DrivenByParent);
Processing *cancel(Processing *pChild);
Processing *repel(Processing *pChild);
Processing *whenFinishedRepel(Processing *pChild);
virtual Success initialize();
virtual Success process() = 0;
virtual Success shutdown();
virtual void processInfo(char *pBuf, char *pBufEnd);
virtual size_t processTrace(char *pBuf, char *pBufEnd);
Success childrenSuccess();
size_t mncpy(void *dest, size_t destSize, const void *src, size_t srcSize);
#if !CONFIG_PROC_HAVE_LIB_STD_CPP
void maxChildrenSet(uint16_t cnt);
#endif
DriverMode driver() const;
uint8_t levelDriver() const;
static size_t procId(char *pBuf, char *pBufEnd, const Processing *pProc);
static size_t progressStr(char *pBuf, char *pBufEnd, const int val, const int maxVal);
uint8_t mState;
uint8_t mStateOld;
private:
// This area is used by the abstract process
Processing()
: mState(0), mStateOld(0)
, mLevelTree(0), mLevelDriver(0)
, mName(NULL)
#if CONFIG_PROC_HAVE_LIB_STD_CPP
, mChildList()
#else
, mpChildList(NULL)
#endif
#if CONFIG_PROC_HAVE_DRIVERS
, mChildListMtx(), mpDriver(NULL)
, mpConfigDriver(NULL)
#endif
, mSuccess(Pending), mNumChildren(0)
, mStateAbstract(0), mStatParent(0)
, mDriver(DrivenByExternalDriver)
#if !CONFIG_PROC_HAVE_LIB_STD_CPP
, mNumChildrenMax(CONFIG_PROC_NUM_MAX_CHILDREN_DEFAULT)
#endif
, mStatDrv(0)
{}
Processing(const Processing &)
: mState(0), mStateOld(0)
, mLevelTree(0), mLevelDriver(0)
, mName(NULL)
#if CONFIG_PROC_HAVE_LIB_STD_CPP
, mChildList()
#else
, mpChildList(NULL)
#endif
#if CONFIG_PROC_HAVE_DRIVERS
, mChildListMtx(), mpDriver(NULL)
, mpConfigDriver(NULL)
#endif
, mSuccess(Pending), mNumChildren(0)
, mStateAbstract(0), mStatParent(0)
, mDriver(DrivenByExternalDriver)
#if !CONFIG_PROC_HAVE_LIB_STD_CPP
, mNumChildrenMax(CONFIG_PROC_NUM_MAX_CHILDREN_DEFAULT)
#endif
, mStatDrv(0)
{}
Processing &operator=(const Processing &)
{
mState = 0;
mStateOld = 0;
mLevelTree = 0;
mLevelDriver = 0;
mName = NULL;
#if CONFIG_PROC_HAVE_LIB_STD_CPP
mChildList.clear();
#else
mpChildList = NULL;
#endif
#if CONFIG_PROC_HAVE_DRIVERS
mpDriver = NULL;
mpConfigDriver = NULL;
#endif
mSuccess = Pending;
mNumChildren = 0;
mStateAbstract = 0;
mStatParent = 0;
mDriver = DrivenByExternalDriver;
#if !CONFIG_PROC_HAVE_LIB_STD_CPP
mNumChildrenMax = CONFIG_PROC_NUM_MAX_CHILDREN_DEFAULT;
#endif
mStatDrv = 0;
return *this;
}
/* member functions */
/* member variables */
uint8_t mLevelTree;
uint8_t mLevelDriver;
const char *mName;
#if CONFIG_PROC_HAVE_LIB_STD_CPP
std::list<Processing *> mChildList;
#else
Processing **childElemAdd(Processing *pChild);
Processing **childElemErase(Processing **pChildListElem);
Processing **mpChildList;
#endif
#if CONFIG_PROC_HAVE_DRIVERS
std::mutex mChildListMtx;
void *mpDriver;
void *mpConfigDriver;
#endif
Success mSuccess;
uint16_t mNumChildren;
uint8_t mStateAbstract;
uint8_t mStatParent;
DriverMode mDriver;
#if !CONFIG_PROC_HAVE_LIB_STD_CPP
uint16_t mNumChildrenMax;
#endif
uint8_t mStatDrv;
/* static functions */
static void parentalDrive(Processing *pChild);
#if CONFIG_PROC_HAVE_DRIVERS
static void internalDrive(void *pProc);
static void *driverInternalCreate(FuncInternalDrive pFctDrive, void *pProc, void *pConfigDriver);
static void driverInternalCleanUp(void *pDriver);
/* static variables */
static size_t sleepInternalDriveUs;
static size_t numBurstInternalDrive;
static FuncInternalDrive pFctInternalDrive;
static FuncDriverInternalCreate pFctDriverInternalCreate;
static FuncDriverInternalCleanUp pFctDriverInternalCleanUp;
#endif
static uint8_t showAddressInId;
static uint8_t disableTreeDefault;
#if CONFIG_PROC_HAVE_GLOBAL_DESTRUCTORS
#if CONFIG_PROC_HAVE_LIB_STD_CPP
static std::list<FuncGlobDestruct> globalDestructors;
#else
static FuncGlobDestruct *pGlobalDestructors;
#endif
#endif
};
#if CONFIG_PROC_HAVE_LIB_STD_C
#define procStrrChr(s, c) strrchr(s, c)
#else
#define procStrrChr(s, c) Processing::strrchr(s, c)
#endif
#define __PROC_FILENAME__ (procStrrChr(__FILE__, '/') ? procStrrChr(__FILE__, '/') + 1 : __FILE__)
typedef void (*FuncEntryLogCreate)(
const int severity,
#if CONFIG_PROC_LOG_HAVE_CHRONO
const char *pTimeAbs,
const std::chrono::system_clock::time_point &tLogged,
#endif
const char *pTimeCnt,
const char *pWhere,
const char *pSeverity,
const char *pWhatUser);
typedef uint32_t (*FuncCntTimeCreate)();
#if CONFIG_PROC_HAVE_LOG
void levelLogSet(int lvl);
void entryLogCreateSet(FuncEntryLogCreate pFct);
void cntTimeCreateSet(FuncCntTimeCreate pFct, int width = 8);
int16_t entryLogSimpleCreate(
const int isErr,
const int16_t code,
const char *msg, ...);
int16_t entryLogCreate(
const int severity,
const void *pProc,
const char *filename,
const char *function,
const int line,
const int16_t code,
const char *msg, ...);
#if CONFIG_PROC_LOG_HAVE_CHRONO
char *blockTimeRelAdd(
char *pBuf, char *pBufEnd,
const std::chrono::system_clock::time_point &tNow,
const std::chrono::system_clock::time_point &tOld);
#endif
#else
inline void levelLogSet(int lvl)
{
(void)lvl;
}
inline void entryLogCreateSet(FuncEntryLogCreate pFct)
{
(void)pFct;
}
inline void cntTimeCreateSet(FuncCntTimeCreate pFct, int width = 8)
{
(void)pFct;
(void)width;
}
inline int16_t entryLogSimpleCreate(
const int isErr,
const int16_t code,
const char *msg, ...)
{
(void)isErr;
(void)msg;
return code;
}
inline int16_t entryLogCreate(
const int severity,
const void *pProc,
const char *filename,
const char *function,
const int line,
const int16_t code,
const char *msg, ...)
{
(void)severity;
(void)pProc;
(void)filename;
(void)function;
(void)line;
(void)msg;
return code;
}
#endif
#define genericSimpleLog(e, c, m, ...) (entryLogSimpleCreate(e, c, m, ##__VA_ARGS__))
#define genericLog(l, p, c, m, ...) (entryLogCreate(l, p, __PROC_FILENAME__, __func__, __LINE__, c, m, ##__VA_ARGS__))
#define userErrLog(c, m, ...) (c < 0 ? genericSimpleLog(1, c, m, ##__VA_ARGS__) : c)
#define userInfLog(m, ...) (genericSimpleLog(3, 0, m, ##__VA_ARGS__))
#define errLog(c, m, ...) (c < 0 ? genericLog(1, NULL, c, m, ##__VA_ARGS__) : c)
#define wrnLog(m, ...) (genericLog(2, NULL, 0, m, ##__VA_ARGS__))
#define infLog(m, ...) (genericLog(3, NULL, 0, m, ##__VA_ARGS__))
#define dbgLog(m, ...) (genericLog(4, NULL, 0, m, ##__VA_ARGS__))
#define procErrLog(c, m, ...) (c < 0 ? genericLog(1, this, c, m, ##__VA_ARGS__) : c)
#define procWrnLog(m, ...) (genericLog(2, this, 0, m, ##__VA_ARGS__))
#define procInfLog(m, ...) (genericLog(3, this, 0, m, ##__VA_ARGS__))
#define procDbgLog(m, ...) (genericLog(4, this, 0, m, ##__VA_ARGS__))
inline void dInfoInternal(char * &pBuf, char *pBufEnd, const char *msg, ...)
{
if (!pBuf || !pBufEnd)
return;
if (pBuf >= pBufEnd)
return;
*pBuf = 0;
--pBufEnd;
*pBufEnd = 0;
++pBufEnd;
#if CONFIG_PROC_HAVE_LIB_STD_C
va_list args;
va_start(args, msg);
int lenDone = vsnprintf(pBuf, pBufEnd - pBuf, msg, args);
if (lenDone < 0)
{
va_end(args);
*pBuf = 0;
return;
}
va_end(args);
if (lenDone > pBufEnd - pBuf)
lenDone = pBufEnd - pBuf;
pBuf += lenDone;
#else
(void)msg;
#endif
}
#define dInfo(m, ...) dInfoInternal(pBuf, pBufEnd, m, ##__VA_ARGS__)
#define dProcessStateEnum(StateName) \
enum StateName \
{ \
dForEach_ ## StateName(dGen ## StateName ## Enum) \
}
#define dProcessStateStr(StateName) \
static const char *StateName ## String[] = \
{ \
dForEach_ ## StateName(dGen ## StateName ## String) \
}
// pst .. process state transition
#define dStateTrace \
if (mState != mStateOld) \
{ \
procDbgLog("pst: %s > %s", \
ProcStateString[mStateOld], \
ProcStateString[mState]); \
mStateOld = mState; \
}
template <typename T>
T PMIN(T a, T b)
{
return a < b ? a : b;
}
template <typename T>
T PMAX(T a, T b)
{
return a > b ? a : b;
}
#endif