Skip to content

Commit a98860d

Browse files
authored
Merge pull request #4942 from Jackistang/master
[add] add commets for ringbuffer and workqueue.
2 parents d71e2ac + 4c6b402 commit a98860d

File tree

4 files changed

+179
-13
lines changed

4 files changed

+179
-13
lines changed

components/drivers/include/ipc/ringbuffer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* Change Logs:
77
* Date Author Notes
8+
* 2021-08-14 Jackistang add comments for function interface.
89
*/
910
#ifndef RINGBUFFER_H__
1011
#define RINGBUFFER_H__
@@ -80,6 +81,13 @@ struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);
8081
void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
8182
#endif
8283

84+
/**
85+
* @brief Get the buffer size of the ring buffer object.
86+
*
87+
* @param rb A pointer to the ring buffer object.
88+
*
89+
* @return Buffer size.
90+
*/
8391
rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
8492
{
8593
RT_ASSERT(rb != RT_NULL);

components/drivers/include/ipc/workqueue.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2021-08-01 Meco Man remove rt_delayed_work_init() and rt_delayed_work structure
9+
* 2021-08-14 Jackistang add comments for rt_work_init()
910
*/
1011
#ifndef WORKQUEUE_H__
1112
#define WORKQUEUE_H__
@@ -67,6 +68,13 @@ rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time);
6768
rt_err_t rt_work_cancel(struct rt_work *work);
6869
#endif /* RT_USING_SYSTEM_WORKQUEUE */
6970

71+
/**
72+
* @brief Initialize a work item, binding with a callback function.
73+
*
74+
* @param work A pointer to the work item object.
75+
* @param work_func A callback function that will be called when this work item is executed.
76+
* @param work_data A user data passed to the callback function as the second parameter.
77+
*/
7078
rt_inline void rt_work_init(struct rt_work *work, void (*work_func)(struct rt_work *work, void *work_data),
7179
void *work_data)
7280
{

components/drivers/src/ringbuffer.c

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 2013-05-08 Grissiom reimplement
1010
* 2016-08-18 heyuanjie add interface
1111
* 2021-07-20 arminker fix write_index bug in function rt_ringbuffer_put_force
12+
* 2021-08-14 Jackistang add comments for function interface.
1213
*/
1314

1415
#include <rtthread.h>
@@ -27,6 +28,13 @@ rt_inline enum rt_ringbuffer_state rt_ringbuffer_status(struct rt_ringbuffer *rb
2728
return RT_RINGBUFFER_HALFFULL;
2829
}
2930

31+
/**
32+
* @brief Initialize the ring buffer object.
33+
*
34+
* @param rb A pointer to the ring buffer object.
35+
* @param pool A pointer to the buffer.
36+
* @param size The size of the buffer in bytes.
37+
*/
3038
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
3139
rt_uint8_t *pool,
3240
rt_int16_t size)
@@ -45,7 +53,13 @@ void rt_ringbuffer_init(struct rt_ringbuffer *rb,
4553
RTM_EXPORT(rt_ringbuffer_init);
4654

4755
/**
48-
* put a block of data into ring buffer
56+
* @brief Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will discard out-of-range data.
57+
*
58+
* @param rb A pointer to the ring buffer object.
59+
* @param ptr A pointer to the data buffer.
60+
* @param length The size of data in bytes.
61+
*
62+
* @return Return the data size we put into the ring buffer.
4963
*/
5064
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
5165
const rt_uint8_t *ptr,
@@ -92,9 +106,13 @@ rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
92106
RTM_EXPORT(rt_ringbuffer_put);
93107

94108
/**
95-
* put a block of data into ring buffer
109+
* @brief Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will overwrite the existing data in the ring buffer.
110+
*
111+
* @param rb A pointer to the ring buffer object.
112+
* @param ptr A pointer to the data buffer.
113+
* @param length The size of data in bytes.
96114
*
97-
* When the buffer is full, it will discard the old data.
115+
* @return Return the data size we put into the ring buffer.
98116
*/
99117
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
100118
const rt_uint8_t *ptr,
@@ -149,7 +167,13 @@ rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
149167
RTM_EXPORT(rt_ringbuffer_put_force);
150168

151169
/**
152-
* get data from ring buffer
170+
* @brief Get data from the ring buffer.
171+
*
172+
* @param rb A pointer to the ring buffer.
173+
* @param ptr A pointer to the data buffer.
174+
* @param length The size of the data we want to read from the ring buffer.
175+
*
176+
* @return Return the data size we read from the ring buffer.
153177
*/
154178
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
155179
rt_uint8_t *ptr,
@@ -196,7 +220,14 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
196220
RTM_EXPORT(rt_ringbuffer_get);
197221

198222
/**
199-
* peak data from ring buffer
223+
* @brief Get the first readable byte of the ring buffer.
224+
*
225+
* @param rb A pointer to the ringbuffer.
226+
* @param ptr When this function return, *ptr is a pointer to the first readable byte of the ring buffer.
227+
*
228+
* @note It is recommended to read only one byte, otherwise it may cause buffer overflow.
229+
*
230+
* @return Return the size of the ring buffer.
200231
*/
201232
rt_size_t rt_ringbuffer_peak(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
202233
{
@@ -230,7 +261,12 @@ rt_size_t rt_ringbuffer_peak(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
230261
RTM_EXPORT(rt_ringbuffer_peak);
231262

232263
/**
233-
* put a character into ring buffer
264+
* @brief Put a byte into the ring buffer. If ring buffer is full, this operation will fail.
265+
*
266+
* @param rb A pointer to the ring buffer object.
267+
* @param ch A byte put into the ring buffer.
268+
*
269+
* @return Return the data size we put into the ring buffer. The ring buffer is full if returns 0. Otherwise, it will return 1.
234270
*/
235271
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
236272
{
@@ -258,9 +294,12 @@ rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
258294
RTM_EXPORT(rt_ringbuffer_putchar);
259295

260296
/**
261-
* put a character into ring buffer
297+
* @brief Put a byte into the ring buffer. If ring buffer is full, it will discard an old data and put into a new data.
262298
*
263-
* When the buffer is full, it will discard one old data.
299+
* @param rb A pointer to the ring buffer object.
300+
* @param ch A byte put into the ring buffer.
301+
*
302+
* @return Return the data size we put into the ring buffer. Always return 1.
264303
*/
265304
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch)
266305
{
@@ -295,7 +334,13 @@ rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t
295334
RTM_EXPORT(rt_ringbuffer_putchar_force);
296335

297336
/**
298-
* get a character from a ringbuffer
337+
* @brief Get a byte from the ring buffer.
338+
*
339+
* @param rb The pointer to the ring buffer object.
340+
* @param ch A pointer to the buffer, used to store one byte.
341+
*
342+
* @return 0 The ring buffer is empty.
343+
* @return 1 Success
299344
*/
300345
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
301346
{
@@ -305,7 +350,7 @@ rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
305350
if (!rt_ringbuffer_data_len(rb))
306351
return 0;
307352

308-
/* put character */
353+
/* put byte */
309354
*ch = rb->buffer_ptr[rb->read_index];
310355

311356
if (rb->read_index == rb->buffer_size-1)
@@ -323,7 +368,11 @@ rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
323368
RTM_EXPORT(rt_ringbuffer_getchar);
324369

325370
/**
326-
* get the size of data in rb
371+
* @brief Get the size of data in the ring buffer in bytes.
372+
*
373+
* @param rb The pointer to the ring buffer object.
374+
*
375+
* @return Return the size of data in the ring buffer in bytes.
327376
*/
328377
rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
329378
{
@@ -348,7 +397,9 @@ rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
348397
RTM_EXPORT(rt_ringbuffer_data_len);
349398

350399
/**
351-
* empty the rb
400+
* @brief Reset the ring buffer object, and clear all contents in the buffer.
401+
*
402+
* @param rb A pointer to the ring buffer object.
352403
*/
353404
void rt_ringbuffer_reset(struct rt_ringbuffer *rb)
354405
{
@@ -363,7 +414,14 @@ RTM_EXPORT(rt_ringbuffer_reset);
363414

364415
#ifdef RT_USING_HEAP
365416

366-
struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t size)
417+
/**
418+
* @brief Create a ring buffer object with a given size.
419+
*
420+
* @param size The size of the buffer in bytes.
421+
*
422+
* @return Return a pointer to ring buffer object. When the return value is RT_NULL, it means this creation failed.
423+
*/
424+
struct rt_ringbuffer *rt_ringbuffer_create(rt_uint16_t size)
367425
{
368426
struct rt_ringbuffer *rb;
369427
rt_uint8_t *pool;
@@ -390,6 +448,11 @@ struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t size)
390448
}
391449
RTM_EXPORT(rt_ringbuffer_create);
392450

451+
/**
452+
* @brief Destroy the ring buffer object, which is created by rt_ringbuffer_create() .
453+
*
454+
* @param rb A pointer to the ring buffer object.
455+
*/
393456
void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
394457
{
395458
RT_ASSERT(rb != RT_NULL);

components/drivers/src/workqueue.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Date Author Notes
88
* 2017-02-27 Bernard fix the re-work issue.
99
* 2021-08-01 Meco Man remove rt_delayed_work_init()
10+
* 2021-08-14 Jackistang add comments for function interface.
1011
*/
1112

1213
#include <rthw.h>
@@ -209,6 +210,15 @@ static void _delayed_work_timeout_handler(void *parameter)
209210
}
210211
}
211212

213+
/**
214+
* @brief Create a work queue with a thread inside.
215+
*
216+
* @param name The name of the work queue thread.
217+
* @param stack_size The stack size of the work queue thread.
218+
* @param priority The priority of the work queue thread.
219+
*
220+
* @return Return A pointer to the workqueue object. It will return RT_NULL if failed.
221+
*/
212222
struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_size, rt_uint8_t priority)
213223
{
214224
struct rt_workqueue *queue = RT_NULL;
@@ -236,6 +246,13 @@ struct rt_workqueue *rt_workqueue_create(const char *name, rt_uint16_t stack_siz
236246
return queue;
237247
}
238248

249+
/**
250+
* @brief Destroy a work queue.
251+
*
252+
* @param queue A pointer to the workqueue object.
253+
*
254+
* @return RT_EOK Success.
255+
*/
239256
rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue)
240257
{
241258
RT_ASSERT(queue != RT_NULL);
@@ -248,6 +265,15 @@ rt_err_t rt_workqueue_destroy(struct rt_workqueue *queue)
248265
return RT_EOK;
249266
}
250267

268+
/**
269+
* @brief Submit a work item to the work queue without delay.
270+
*
271+
* @param queue A pointer to the workqueue object.
272+
* @param work A pointer to the work item object.
273+
*
274+
* @return RT_EOK Success.
275+
* @return -RT_EBUSY This work item is executing.
276+
*/
251277
rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work)
252278
{
253279
RT_ASSERT(queue != RT_NULL);
@@ -256,6 +282,17 @@ rt_err_t rt_workqueue_dowork(struct rt_workqueue *queue, struct rt_work *work)
256282
return _workqueue_submit_work(queue, work, 0);
257283
}
258284

285+
/**
286+
* @brief Submit a work item to the work queue with a delay.
287+
*
288+
* @param queue A pointer to the workqueue object.
289+
* @param work A pointer to the work item object.
290+
* @param time The delay time (unit: OS ticks) for the work item to be submitted to the work queue.
291+
*
292+
* @return RT_EOK Success.
293+
* @return -RT_EBUSY This work item is executing.
294+
* @return -RT_ERROR The time parameter is invalid.
295+
*/
259296
rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t time)
260297
{
261298
RT_ASSERT(queue != RT_NULL);
@@ -264,6 +301,14 @@ rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *wo
264301
return _workqueue_submit_work(queue, work, time);
265302
}
266303

304+
/**
305+
* @brief Submit a work item to the work queue without delay. This work item will be executed after the current work item.
306+
*
307+
* @param queue A pointer to the workqueue object.
308+
* @param work A pointer to the work item object.
309+
*
310+
* @return RT_EOK Success.
311+
*/
267312
rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work *work)
268313
{
269314
rt_base_t level;
@@ -291,13 +336,30 @@ rt_err_t rt_workqueue_critical_work(struct rt_workqueue *queue, struct rt_work *
291336
return RT_EOK;
292337
}
293338

339+
/**
340+
* @brief Cancel a work item in the work queue.
341+
*
342+
* @param queue A pointer to the workqueue object.
343+
* @param work A pointer to the work item object.
344+
*
345+
* @return RT_EOK Success.
346+
* @return -RT_EBUSY This work item is executing.
347+
*/
294348
rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work)
295349
{
296350
RT_ASSERT(work != RT_NULL);
297351
RT_ASSERT(queue != RT_NULL);
298352
return _workqueue_cancel_work(queue, work);
299353
}
300354

355+
/**
356+
* @brief Cancel a work item in the work queue. If the work item is executing, this function will block until it is done.
357+
*
358+
* @param queue A pointer to the workqueue object.
359+
* @param work A pointer to the work item object.
360+
*
361+
* @return RT_EOK Success.
362+
*/
301363
rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work)
302364
{
303365
RT_ASSERT(queue != RT_NULL);
@@ -316,6 +378,13 @@ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_wor
316378
return RT_EOK;
317379
}
318380

381+
/**
382+
* @brief This function will cancel all work items in work queue.
383+
*
384+
* @param queue A pointer to the workqueue object.
385+
*
386+
* @return RT_EOK Success.
387+
*/
319388
rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue)
320389
{
321390
struct rt_work *work;
@@ -343,11 +412,29 @@ rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue *queue)
343412
#ifdef RT_USING_SYSTEM_WORKQUEUE
344413
static struct rt_workqueue *sys_workq;
345414

415+
/**
416+
* @brief Submit a work item to the system work queue with a delay.
417+
*
418+
* @param work A pointer to the work item object.
419+
* @param time The delay time (unit: OS ticks) for the work item to be submitted to the work queue.
420+
*
421+
* @return RT_EOK Success.
422+
* @return -RT_EBUSY This work item is executing.
423+
* @return -RT_ERROR The time parameter is invalid.
424+
*/
346425
rt_err_t rt_work_submit(struct rt_work *work, rt_tick_t time)
347426
{
348427
return rt_workqueue_submit_work(sys_workq, work, time);
349428
}
350429

430+
/**
431+
* @brief Cancel a work item in the system work queue.
432+
*
433+
* @param work A pointer to the work item object.
434+
*
435+
* @return RT_EOK Success.
436+
* @return -RT_EBUSY This work item is executing.
437+
*/
351438
rt_err_t rt_work_cancel(struct rt_work *work)
352439
{
353440
return rt_workqueue_cancel_work(sys_workq, work);

0 commit comments

Comments
 (0)