1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include "cpp_queue.h"
6 
7 using namespace AOS;
8 
9 /**
10  * This function will create a buf-queue
11  * @param[in]  name     name of the queue
12  * @param[in]  buf      pointer to the buf
13  * @param[in]  size     size of the buf
14  * @param[in]  max_msg  max size of one msg
15  * @return  the operation status, RHINO_SUCCESS is OK, others is error
16  */
create(const name_t * name,void * buf,size_t size,size_t max_msg)17 kstat_t Queue::create(const name_t *name, void *buf, size_t size, size_t max_msg)
18 {
19     return krhino_buf_queue_create(&_buf_queue_def, name, buf, size, max_msg);
20 }
21 
22 /**
23  * This function will delete a queue
24  * @param[in]  NULL
25  * @return  the operation status, RHINO_SUCCESS is OK, others is error
26  */
destory(void)27 kstat_t Queue::destory(void)
28 {
29     return krhino_buf_queue_del(&_buf_queue_def);
30 }
31 
32 /**
33  * This function will send a msg at the end of queue
34  * @param[in]  msg    pointer to msg to be send
35  * @param[in]  size   size of the msg
36  * @return  the operation status, RHINO_SUCCESS is OK, others is error
37  */
send(void * msg,size_t size)38 kstat_t Queue::send(void *msg, size_t size)
39 {
40     return krhino_buf_queue_send(&_buf_queue_def, msg, size);
41 }
42 
43 /**
44  * This function will receive msg form aqueue
45  * @param[in]   ticks  ticks to wait before receiving msg
46  * @param[out]  msg    pointer to the buf to save msg
47  * @param[out]  size   size of received msg
48  * @return  the operation status, RHINO_SUCCESS is OK, others is error
49  */
receive(void * msg,size_t * size,uint32_t millisec)50 kstat_t Queue::receive(void *msg, size_t *size, uint32_t millisec)
51 {
52     tick_t ticks = 0;
53 
54     if (millisec == 0) {
55         ticks = RHINO_NO_WAIT;
56     } else if (millisec == Queue_WAIT_FOREVER){
57         ticks = RHINO_WAIT_FOREVER;
58     } else {
59         ticks = krhino_ms_to_ticks(millisec);
60     }
61 
62     return krhino_buf_queue_recv(&_buf_queue_def, ticks, msg, size);
63 }
64 
65 /**
66  * This function will reset queue
67  * @param[in]  NULL
68  * @return  the operation status, RHINO_SUCCESS is OK, others is error
69  */
flush(void)70 kstat_t Queue::flush(void)
71 {
72     return krhino_buf_queue_flush(&_buf_queue_def);
73 }
74 
75 /**
76  * This function will get information of a queue
77  * @param[out]  free   free size of the queue buf
78  * @param[out]  total  total size of the queue buf
79  * @return  the operation status, RHINO_SUCCESS is OK, others is error
80  */
info_get(kbuf_queue_info_t * info)81 kstat_t Queue::info_get(kbuf_queue_info_t *info)
82 {
83     return krhino_buf_queue_info_get(&_buf_queue_def, info);
84 }
85 
86 /**
87  * This function will get a Queue struct pointer
88  * @param[in]  none
89  * @return  Queue struct pointer
90  */
self(void)91 kbuf_queue_t *Queue::self(void)
92 {
93     return &_buf_queue_def;
94 }
95