forked from Rtoax/fastq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastq.h
465 lines (366 loc) · 15.9 KB
/
fastq.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
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
/**********************************************************************************************************************\
* 文件: fastq.h
* 介绍: 低时延队列
* 作者: 荣涛
* 日期:
* 2021年1月 - 2021年5月
*
* API接口概述
*
* VOS_FastQCreateModule 注册消息队列
* VOS_FastQDeleteModule 删除消息队列
* VOS_FastQDump 显示信息
* VOS_FastQDumpAllModule 显示信息(所有模块)
* VOS_FastQMsgStatInfo 查询队列内存入队出队信息
* VOS_FastQSend 发送消息(轮询直至成功发送)
* VOS_FastQSendByName 模块名索引版本
* VOS_FastQTrySend 发送消息(尝试向队列中插入,当队列满是直接返回false)
* VOS_FastQTrySendByName 模块名索引版本
* VOS_FastQRecv 接收消息
* VOS_FastQMsgNum 获取消息数(需要开启统计功能 _FASTQ_STATS )
* VOS_FastQAddSet 动态添加 发送接收 set
*
*
\**********************************************************************************************************************/
#ifndef __fAStMQ_H
#define __fAStMQ_H 1
#include <stdio.h>
#include <stdbool.h>
#ifdef MODULE_ID_MAX // moduleID 最大模块索引值
#define FASTQ_ID_MAX MODULE_ID_MAX
#else
#define FASTQ_ID_MAX 256
#endif
/**
* Crypto
*/
#define __MOD_SETSIZE FASTQ_ID_MAX
#define __NMOD (8 * (int) sizeof (__mod_mask))
#define __MOD_ELT(d) ((d) / __NMOD)
#define __MOD_MASK(d) ((__mod_mask)(1UL << ((d) % __NMOD)))
typedef long int __mod_mask;
typedef struct {
__mod_mask __mod[__MOD_SETSIZE/__NMOD];
#define __MOD(set) ((set)->__mod)
#define __MOD_SET_INITIALIZER {0}
}__attribute__((aligned(64))) __mod_set;
#define __MOD_ZERO(s) \
do { \
unsigned int __i; \
__mod_set *__arr = (s); \
for (__i = 0; __i < sizeof (__mod_set) / sizeof (__mod_mask); ++__i) \
__MOD (__arr)[__i] = 0; \
} while (0)
#define __MOD_SET(d, s) \
((void) (__MOD (s)[__MOD_ELT(d)] |= __MOD_MASK(d)))
#define __MOD_CLR(d, s) \
((void) (__MOD (s)[__MOD_ELT(d)] &= ~ __MOD_MASK(d)))
#define __MOD_ISSET(d, s) \
((__MOD (s)[__MOD_ELT (d)] & __MOD_MASK (d)) != 0)
#define mod_set __mod_set
#define MOD_SET_INITIALIZER __MOD_SET_INITIALIZER
#define MOD_SETSIZE __MOD_SETSIZE
#define MOD_SET(bit, p_mod_set) __MOD_SET(bit, p_mod_set)
#define MOD_CLR(bit, p_mod_set) __MOD_CLR(bit, p_mod_set)
#define MOD_ISSET(bit, p_mod_set) __MOD_ISSET(bit, p_mod_set)
#define MOD_ZERO(p_mod_set) __MOD_ZERO(p_mod_set)
#ifndef always_inline
#define always_inline /*__attribute__ ((__always_inline__))*/
#endif
/**
* 源模块未初始化时可临时使用的模块ID,只允许使用一次
*
* 目前 0 号 不允许使用
*/
#define VOS_FastQTmpModuleID 0
/**
* FastQModuleMsgStatInfo - 统计信息
*
* src_module 源模块ID
* dst_module 目的模块ID
* enqueue 从 src_module 发往 dst_module 的统计, src_module 已发出的消息数
* dequeue 从 src_module 发往 dst_module 的统计, dst_module 已接收的消息数
*/
struct FastQModuleMsgStatInfo {
unsigned long src_module;
unsigned long dst_module;
unsigned long enqueue;
unsigned long dequeue;
};
/**
* fq_msg_handler_t - FastQRecvMain 接收函数
*
* param[in] src 源模块ID, 范围 1 - FASTQ_ID_MAX
* param[in] dst 目的模块ID, 范围 1 - FASTQ_ID_MAX
* param[in] type 消息类型
* param[in] code 消息码
* param[in] subcode 次消息码
* param[in] msg 接收消息地址
* param[in] sz 接收消息大小,与 FastQCreate (..., msg_size) 保持一致
*/
typedef void (*fq_msg_handler_t)(unsigned long src, unsigned long dst,\
unsigned long type, unsigned long code, unsigned long subcode, \
void*msg, size_t sz);
/**
* fq_module_filter_t - 根据目的和源模块ID进行过滤
*
* param[in] srcID 源模块ID
* param[in] dstID 目的模块ID
*
* 当 fq_module_filter_t 返回 true 时,该 源 到 目的 的消息队列将计入统计
*/
typedef bool (*fq_module_filter_t)(unsigned long srcID, unsigned long dstID);
/**
* VOS_FastQCreateModule - 注册消息队列
*
* param[in] moduleID 模块ID, 范围 1 - FASTQ_ID_MAX
* param[in] rxset 可能接收对应模块发来的消息 bitmap,见 select() fd_set
* param[in] txset 可能向对应模块发送消息 bitmap,见 select() fd_set
* param[in] msgMax 该模块 的 消息队列 的大小
* param[in] msgSize 最大传递的消息大小
*/
always_inline void inline
VOS_FastQCreateModule(const unsigned long moduleID,
const mod_set *rxset, const mod_set *txset,
const unsigned int msgMax, const unsigned int msgSize);
/**
* VOS_FastQAttachName - 绑定 Name 到 ModuleID 消息队列, 以使用 Name发送消息
*
* param[in] name 模块名(长度 <= 64)
* param[in] moduleID 模块ID, 范围 1 - FASTQ_ID_MAX
*/
always_inline bool inline
VOS_FastQAttachName(const unsigned long moduleID, const char *name);
/**
* VOS_FastQDeleteModule - 销毁消息队列
*
* param[in] moduleID 模块ID, 范围 1 - FASTQ_ID_MAX
*
* return 成功true 失败false
*
* 需要注意的是,name 和 moduleID 二选一,但是,如果与注册时的对应关系不一致
* 将销毁失败
*/
always_inline bool inline
VOS_FastQDeleteModule(const unsigned long moduleID);
/**
* VOS_FastQAddSet - 注册消息队列
*
* param[in] moduleID 模块ID, 范围 1 - FASTQ_ID_MAX, 通过 `VOS_FastQCreateModule` 注册的函数
* param[in] rxset 可能接收对应模块发来的消息 bitmap,见 select() fd_set
* param[in] txset 可能向对应模块发送消息 bitmap,见 select() fd_set
*
* 注意:这里的`rxset`和`txset`将是`VOS_FastQCreateModule`参数的并集
*/
always_inline bool inline
VOS_FastQAddSet(const unsigned long moduleID,
const mod_set *rxset, const mod_set *txset);
/**
* VOS_FastQDump - 显示信息
*
* param[in] fp 文件指针
* param[in] module_id 需要显示的模块ID, 等于 0 时显示全部
*/
always_inline void inline
VOS_FastQDump(FILE*fp, unsigned long moduleID);
/**
* VOS_FastQDump - 显示全部模块信息
*
* param[in] fp 文件指针
*/
always_inline void inline
VOS_FastQDumpAllModule(FILE*fp);
/**
* VOS_FastQMsgStatInfo - 获取统计信息
*
* param[in] buf FastQModuleMsgStatInfo 信息结构体
* param[in] buf_mod_size buf 信息结构体个数
* param[in] num 函数返回时填回 的 FastQModuleMsgStatInfo 结构个数
* param[in] filter 根据目的和源模块ID进行过滤 详见 fq_module_filter_t
*/
always_inline bool inline
VOS_FastQMsgStatInfo(struct FastQModuleMsgStatInfo *buf, unsigned int buf_mod_size, unsigned int *num,
fq_module_filter_t filter);
/**
* VOS_FastQSend - 发送消息(轮询直至成功发送)
*
* param[in] from 源模块ID, 范围 1 - FASTQ_ID_MAX
* param[in] to 目的模块ID, 范围 1 - FASTQ_ID_MAX
* param[in] msgType 消息类型
* param[in] msgCode 消息码
* param[in] msgSubCode 次消息码
* param[in] msg 传递的消息体
* param[in] size 传递的消息大小
*
* return 成功true (轮询直至发送成功,只可能返回 true )
*
* 注意:from 和 to 需要使用 FastQCreateModule 注册后使用
*/
always_inline bool inline
VOS_FastQSend(unsigned int from, unsigned int to, unsigned long msgType, unsigned long msgCode, unsigned long msgSubCode,
const void *msg, size_t size);
/**
* VOS_FastQSendByName - 发送消息(轮询直至成功发送)
*
* param[in] from 源模块名
* param[in] to 目的模块名
* param[in] msgType 消息类型
* param[in] msgCode 消息码
* param[in] msgSubCode 次消息码
* param[in] msg 传递的消息体
* param[in] size 传递的消息大小
*
* return 成功true (轮询直至发送成功,只可能返回 true )
*
* 注意:from 和 to 需要使用 FastQCreateModule 注册后使用
*/
always_inline bool inline
VOS_FastQSendByName(const char *from, const char *to, unsigned long msgType, unsigned long msgCode, unsigned long msgSubCode,
const void *msg, size_t size);
/**
* VOS_FastQTrySend - 发送消息(尝试向队列中插入,当队列满是直接返回false)
*
* param[in] from 源模块ID, 范围 1 - FASTQ_ID_MAX
* param[in] to 目的模块ID, 范围 1 - FASTQ_ID_MAX
* param[in] msgType 消息类型
* param[in] msgCode 消息码
* param[in] msgSubCode 次消息码
* param[in] msg 传递的消息体
* param[in] size 传递的消息大小
*
* return 成功true 失败false
*
* 注意:from 和 to 需要使用 FastQCreateModule 注册后使用
*/
always_inline bool inline
VOS_FastQTrySend(unsigned int from, unsigned int to, unsigned long msgType, unsigned long msgCode, unsigned long msgSubCode,
const void *msg, size_t size);
/**
* VOS_FastQTrySendByName - 发送消息(尝试发送)
*
* param[in] from 源模块名
* param[in] to 目的模块名
* param[in] msgType 消息类型
* param[in] msgCode 消息码
* param[in] msgSubCode 次消息码
* param[in] msg 传递的消息体
* param[in] size 传递的消息大小
*
* return 成功true 失败false
*
* 注意:from 和 to 需要使用 FastQCreateModule 注册后使用
*/
always_inline bool inline
VOS_FastQTrySendByName(const char *from, const char *to, unsigned long msgType, unsigned long msgCode, unsigned long msgSubCode,
const void *msg, size_t size);
/**
* VOS_FastQRecv - 接收消息
*
* param[in] from 从模块ID from 中读取消息, 范围 1 - FASTQ_ID_MAX
* param[in] handler 消息处理函数,参照 fq_msg_handler_t 说明
*
* return 成功true 失败false
*
* 注意:from 需要使用 FastQCreateModule 注册后使用
*/
always_inline bool inline
VOS_FastQRecv(unsigned int from, fq_msg_handler_t handler);
/**
* VOS_FastQRecvByName - 接收消息
*
* param[in] from 源模块名
* param[in] handler 消息处理函数,参照 fq_msg_handler_t 说明
*
* return 成功true 失败false
*
* 注意:from 需要使用 FastQCreateModule 注册后使用
*/
always_inline bool inline
VOS_FastQRecvByName(const char *from, fq_msg_handler_t handler);
/**
* VOS_FastQMsgNum - 获取消息数
*
* param[in] ID 从模块ID from 中读取消息, 范围 1 - FASTQ_ID_MAX
* param[in] nr_enqueues 总入队数
* param[in] nr_dequeues 总出队数
* param[in] nr_currents 当前消息数
*
* return 成功true 失败false(不支持, 编译宏控制 _FASTQ_STATS 开启统计功能)
*
* 注意:ID 需要使用 FastQCreateModule 注册后使用
*/
always_inline bool inline
VOS_FastQMsgNum(unsigned int ID, unsigned long *nr_enqueues, unsigned long *nr_dequeues, unsigned long *nr_currents);
/**********************************************************************************************************************\
**
** 从此至该头文件末尾,所有接口禁止应用层使用
**
\**********************************************************************************************************************/
#define FastQTmpModuleID VOS_FastQTmpModuleID
# define VOS_FastQCreateModule(moduleID, rxset, txset, msgMax, msgSize) \
FastQCreateModule(moduleID, rxset, txset, msgMax, msgSize, __FILE__, __func__, __LINE__)
# define VOS_FastQDeleteModule(moduleID) \
FastQDeleteModule(moduleID)
# define VOS_FastQAttachName(moduleID, Name) \
FastQAttachName(moduleID, Name)
# define VOS_FastQAddSet(moduleID, rxset, txset) \
FastQAddSet(moduleID, rxset, txset)
# define VOS_FastQDump(fp, moduleID) \
FastQDump(fp, moduleID)
# define VOS_FastQDumpAllModule(fp) \
FastQDump(fp, 0)
# define VOS_FastQMsgStatInfo(buf, bufSize, pnum, filter) \
FastQMsgStatInfo(buf, bufSize, pnum, filter)
# define VOS_FastQSend(moduleSrc, moduleDst, msgType, msgCode, msgSubCode, pmsg, msgSize) \
FastQSend(moduleSrc, moduleDst, msgType, msgCode, msgSubCode, pmsg, msgSize)
# define VOS_FastQSendByName(moduleSrc, moduleDst, msgType, msgCode, msgSubCode, pmsg, msgSize) \
FastQSendByName(moduleSrc, moduleDst, msgType, msgCode, msgSubCode, pmsg, msgSize)
# define VOS_FastQTrySend(moduleSrc, moduleDst, msgType, msgCode, msgSubCode, pmsg, msgSize) \
FastQTrySend(moduleSrc, moduleDst, msgType, msgCode, msgSubCode, pmsg, msgSize)
# define VOS_FastQTrySendByName(moduleSrc, moduleDst, msgType, msgCode, msgSubCode, pmsg, msgSize) \
FastQTrySendByName(moduleSrc, moduleDst, msgType, msgCode, msgSubCode, pmsg, msgSize)
# define VOS_FastQRecv(fromModule, msgHandlerFn) \
FastQRecv(fromModule, msgHandlerFn)
# define VOS_FastQRecvByName(fromModule, msgHandlerFn) \
FastQRecvByName(fromModule, msgHandlerFn)
# define VOS_FastQMsgNum(moduleID, nr_en, nr_de, nt_curr) \
FastQMsgNum(moduleID, nr_en, nr_de, nt_curr)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
always_inline void inline
FastQCreateModule(const unsigned long moduleID,
const mod_set *rxset, const mod_set *txset,
const unsigned int msgMax, const unsigned int msgSize,
const char *_file, const char *_func, const int _line);
always_inline bool inline
FastQDeleteModule(const unsigned long moduleID);
always_inline bool inline
FastQAttachName(const unsigned long moduleID, const char *name);
always_inline bool inline
FastQAddSet(const unsigned long moduleID,
const mod_set *rxset, const mod_set *txset);
always_inline void inline
FastQDump(FILE*fp, unsigned long module_id);
always_inline bool inline
FastQMsgStatInfo(struct FastQModuleMsgStatInfo *buf, unsigned int buf_mod_size, unsigned int *num,
fq_module_filter_t filter);
always_inline bool inline
FastQSend(unsigned int from, unsigned int to, unsigned long msgType, unsigned long msgCode, unsigned long msgSubCode,
const void *msg, size_t size);
always_inline bool inline
FastQSendByName(const char* from, const char* to, unsigned long msgType, unsigned long msgCode, unsigned long msgSubCode,
const void *msg, size_t size);
always_inline bool inline
FastQTrySend(unsigned int from, unsigned int to, unsigned long msgType, unsigned long msgCode, unsigned long msgSubCode,
const void *msg, size_t size);
always_inline bool inline
FastQTrySendByName(const char* from, const char* to, unsigned long msgType, unsigned long msgCode, unsigned long msgSubCode,
const void *msg, size_t size);
always_inline bool inline
FastQRecv(unsigned int from, fq_msg_handler_t handler);
always_inline bool inline
FastQRecvByName(const char *from, fq_msg_handler_t handler);
always_inline bool inline
FastQMsgNum(unsigned int ID, unsigned long *nr_enqueues, unsigned long *nr_dequeues, unsigned long *nr_currents);
#pragma GCC diagnostic pop
#endif /*<__fAStMQ_H>*/