2012-12-26 08:29:42 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2012-12-26 08:29:42 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-12-26 08:29:42 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-09-30 Bernard first version.
|
2016-10-31 08:39:49 +08:00
|
|
|
* 2016-10-31 armink fix some resume push and pop thread bugs
|
2012-12-26 08:29:42 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
2023-07-29 06:26:51 +08:00
|
|
|
#include <rtdevice.h>
|
2012-12-26 08:29:42 +08:00
|
|
|
|
2020-04-08 11:05:37 +08:00
|
|
|
#define DATAQUEUE_MAGIC 0xbead0e0e
|
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
struct rt_data_item
|
|
|
|
{
|
|
|
|
const void *data_ptr;
|
|
|
|
rt_size_t data_size;
|
|
|
|
};
|
|
|
|
|
2021-11-23 16:05:17 +08:00
|
|
|
/**
|
2021-11-24 21:59:37 +08:00
|
|
|
* @brief This function will initialize the data queue. Calling this function will
|
2021-11-24 14:14:45 +08:00
|
|
|
* initialize the data queue control block and set the notification callback function.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 15:21:30 +08:00
|
|
|
* @param queue is a pointer to the data queue object.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 14:14:45 +08:00
|
|
|
* @param size is the maximum number of data in the data queue.
|
|
|
|
*
|
|
|
|
* @param lwm is low water mark.
|
2021-11-24 15:21:30 +08:00
|
|
|
* When the number of data in the data queue is less than this value, this function will
|
2021-11-24 14:14:45 +08:00
|
|
|
* wake up the thread waiting for write data.
|
|
|
|
*
|
|
|
|
* @param evt_notify is the notification callback function.
|
|
|
|
*
|
|
|
|
* @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
|
2023-03-23 12:56:38 +08:00
|
|
|
* When the return value is -RT_ENOMEM, it means insufficient memory allocation failed.
|
2021-11-23 16:05:17 +08:00
|
|
|
*/
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_err_t
|
|
|
|
rt_data_queue_init(struct rt_data_queue *queue,
|
|
|
|
rt_uint16_t size,
|
|
|
|
rt_uint16_t lwm,
|
|
|
|
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event))
|
|
|
|
{
|
|
|
|
RT_ASSERT(queue != RT_NULL);
|
2020-09-18 09:33:34 +08:00
|
|
|
RT_ASSERT(size > 0);
|
2012-12-26 08:29:42 +08:00
|
|
|
|
|
|
|
queue->evt_notify = evt_notify;
|
|
|
|
|
2021-11-23 16:40:55 +08:00
|
|
|
queue->magic = DATAQUEUE_MAGIC;
|
2012-12-26 08:29:42 +08:00
|
|
|
queue->size = size;
|
|
|
|
queue->lwm = lwm;
|
|
|
|
|
|
|
|
queue->get_index = 0;
|
|
|
|
queue->put_index = 0;
|
2020-09-18 09:33:34 +08:00
|
|
|
queue->is_empty = 1;
|
|
|
|
queue->is_full = 0;
|
2012-12-26 08:29:42 +08:00
|
|
|
|
|
|
|
rt_list_init(&(queue->suspended_push_list));
|
|
|
|
rt_list_init(&(queue->suspended_pop_list));
|
2020-09-18 09:45:47 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
queue->queue = (struct rt_data_item *)rt_malloc(sizeof(struct rt_data_item) * size);
|
|
|
|
if (queue->queue == RT_NULL)
|
|
|
|
{
|
|
|
|
return -RT_ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_data_queue_init);
|
|
|
|
|
2021-11-23 16:05:17 +08:00
|
|
|
/**
|
2021-11-24 14:14:45 +08:00
|
|
|
* @brief This function will write data to the data queue. If the data queue is full,
|
|
|
|
* the thread will suspend for the specified amount of time.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 15:21:30 +08:00
|
|
|
* @param queue is a pointer to the data queue object.
|
2021-11-24 14:14:45 +08:00
|
|
|
* .
|
|
|
|
* @param data_ptr is the buffer pointer of the data to be written.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 14:14:45 +08:00
|
|
|
* @param size is the size in bytes of the data to be written.
|
|
|
|
*
|
|
|
|
* @param timeout is the waiting time.
|
|
|
|
*
|
|
|
|
* @return Return the operation status. When the return value is RT_EOK, the operation is successful.
|
2023-03-22 03:38:02 +08:00
|
|
|
* When the return value is -RT_ETIMEOUT, it means the specified time out.
|
2021-11-23 16:05:17 +08:00
|
|
|
*/
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
|
|
|
|
const void *data_ptr,
|
|
|
|
rt_size_t data_size,
|
|
|
|
rt_int32_t timeout)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_thread_t thread;
|
|
|
|
rt_err_t result;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
2020-09-18 09:33:34 +08:00
|
|
|
RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
|
2012-12-26 08:29:42 +08:00
|
|
|
|
2022-01-26 16:02:24 +08:00
|
|
|
/* current context checking */
|
2022-01-26 21:39:06 +08:00
|
|
|
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
|
2022-01-26 16:02:24 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
result = RT_EOK;
|
|
|
|
thread = rt_thread_self();
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
2020-09-18 09:33:34 +08:00
|
|
|
while (queue->is_full)
|
2012-12-26 08:29:42 +08:00
|
|
|
{
|
|
|
|
/* queue is full */
|
|
|
|
if (timeout == 0)
|
|
|
|
{
|
|
|
|
result = -RT_ETIMEOUT;
|
|
|
|
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset thread error number */
|
|
|
|
thread->error = RT_EOK;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
/* suspend thread on the push list */
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_list_insert_before(&(queue->suspended_push_list), &(thread->tlist));
|
|
|
|
/* start timer */
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
|
|
|
/* reset the timeout of thread timer and start it */
|
|
|
|
rt_timer_control(&(thread->thread_timer),
|
|
|
|
RT_TIMER_CTRL_SET_TIME,
|
|
|
|
&timeout);
|
|
|
|
rt_timer_start(&(thread->thread_timer));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
/* do schedule */
|
|
|
|
rt_schedule();
|
|
|
|
|
|
|
|
/* thread is waked up */
|
|
|
|
result = thread->error;
|
|
|
|
level = rt_hw_interrupt_disable();
|
2016-10-29 14:35:07 +08:00
|
|
|
if (result != RT_EOK) goto __exit;
|
2012-12-26 08:29:42 +08:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
queue->queue[queue->put_index].data_ptr = data_ptr;
|
|
|
|
queue->queue[queue->put_index].data_size = data_size;
|
2012-12-26 08:29:42 +08:00
|
|
|
queue->put_index += 1;
|
2020-09-18 09:33:34 +08:00
|
|
|
if (queue->put_index == queue->size)
|
|
|
|
{
|
|
|
|
queue->put_index = 0;
|
|
|
|
}
|
|
|
|
queue->is_empty = 0;
|
|
|
|
if (queue->put_index == queue->get_index)
|
|
|
|
{
|
|
|
|
queue->is_full = 1;
|
|
|
|
}
|
2012-12-26 08:29:42 +08:00
|
|
|
|
2016-11-02 08:17:24 +08:00
|
|
|
/* there is at least one thread in suspended list */
|
2012-12-26 08:29:42 +08:00
|
|
|
if (!rt_list_isempty(&(queue->suspended_pop_list)))
|
|
|
|
{
|
|
|
|
/* get thread entry */
|
|
|
|
thread = rt_list_entry(queue->suspended_pop_list.next,
|
|
|
|
struct rt_thread,
|
|
|
|
tlist);
|
|
|
|
|
|
|
|
/* resume it */
|
|
|
|
rt_thread_resume(thread);
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
/* perform a schedule */
|
|
|
|
rt_schedule();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
__exit:
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
if ((result == RT_EOK) && queue->evt_notify != RT_NULL)
|
|
|
|
{
|
|
|
|
queue->evt_notify(queue, RT_DATAQUEUE_EVENT_PUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_data_queue_push);
|
|
|
|
|
2021-11-23 16:05:17 +08:00
|
|
|
/**
|
2021-11-24 14:14:45 +08:00
|
|
|
* @brief This function will pop data from the data queue. If the data queue is empty,the thread
|
|
|
|
* will suspend for the specified amount of time.
|
|
|
|
*
|
2021-11-24 15:21:30 +08:00
|
|
|
* @note When the number of data in the data queue is less than lwm(low water mark), will
|
2021-11-24 14:14:45 +08:00
|
|
|
* wake up the thread waiting for write data.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 15:21:30 +08:00
|
|
|
* @param queue is a pointer to the data queue object.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 14:14:45 +08:00
|
|
|
* @param data_ptr is the buffer pointer of the data to be fetched.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 14:14:45 +08:00
|
|
|
* @param size is the size in bytes of the data to be fetched.
|
|
|
|
*
|
|
|
|
* @param timeout is the waiting time.
|
|
|
|
*
|
|
|
|
* @return Return the operation status. When the return value is RT_EOK, the operation is successful.
|
2023-03-22 03:38:02 +08:00
|
|
|
* When the return value is -RT_ETIMEOUT, it means the specified time out.
|
2021-11-23 16:05:17 +08:00
|
|
|
*/
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
|
2022-07-25 10:21:18 +08:00
|
|
|
const void **data_ptr,
|
2021-03-08 18:19:04 +08:00
|
|
|
rt_size_t *size,
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_int32_t timeout)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_thread_t thread;
|
|
|
|
rt_err_t result;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
2020-09-18 09:33:34 +08:00
|
|
|
RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
|
2012-12-26 08:29:42 +08:00
|
|
|
RT_ASSERT(data_ptr != RT_NULL);
|
|
|
|
RT_ASSERT(size != RT_NULL);
|
|
|
|
|
2022-01-26 16:02:24 +08:00
|
|
|
/* current context checking */
|
2022-01-26 21:39:06 +08:00
|
|
|
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
|
2022-01-26 16:02:24 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
result = RT_EOK;
|
|
|
|
thread = rt_thread_self();
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
2020-09-18 09:33:34 +08:00
|
|
|
while (queue->is_empty)
|
2012-12-26 08:29:42 +08:00
|
|
|
{
|
|
|
|
/* queue is empty */
|
|
|
|
if (timeout == 0)
|
|
|
|
{
|
|
|
|
result = -RT_ETIMEOUT;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset thread error number */
|
|
|
|
thread->error = RT_EOK;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
/* suspend thread on the pop list */
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_list_insert_before(&(queue->suspended_pop_list), &(thread->tlist));
|
|
|
|
/* start timer */
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
|
|
|
/* reset the timeout of thread timer and start it */
|
|
|
|
rt_timer_control(&(thread->thread_timer),
|
|
|
|
RT_TIMER_CTRL_SET_TIME,
|
|
|
|
&timeout);
|
|
|
|
rt_timer_start(&(thread->thread_timer));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
/* do schedule */
|
|
|
|
rt_schedule();
|
|
|
|
|
|
|
|
/* thread is waked up */
|
|
|
|
result = thread->error;
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
if (result != RT_EOK)
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
*data_ptr = queue->queue[queue->get_index].data_ptr;
|
|
|
|
*size = queue->queue[queue->get_index].data_size;
|
2012-12-26 08:29:42 +08:00
|
|
|
queue->get_index += 1;
|
2020-09-18 09:33:34 +08:00
|
|
|
if (queue->get_index == queue->size)
|
|
|
|
{
|
|
|
|
queue->get_index = 0;
|
|
|
|
}
|
|
|
|
queue->is_full = 0;
|
|
|
|
if (queue->put_index == queue->get_index)
|
|
|
|
{
|
|
|
|
queue->is_empty = 1;
|
|
|
|
}
|
2012-12-26 08:29:42 +08:00
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
if (rt_data_queue_len(queue) <= queue->lwm)
|
2012-12-26 08:29:42 +08:00
|
|
|
{
|
2016-11-02 08:17:24 +08:00
|
|
|
/* there is at least one thread in suspended list */
|
2012-12-26 08:29:42 +08:00
|
|
|
if (!rt_list_isempty(&(queue->suspended_push_list)))
|
|
|
|
{
|
|
|
|
/* get thread entry */
|
|
|
|
thread = rt_list_entry(queue->suspended_push_list.next,
|
|
|
|
struct rt_thread,
|
|
|
|
tlist);
|
|
|
|
|
|
|
|
/* resume it */
|
|
|
|
rt_thread_resume(thread);
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
/* perform a schedule */
|
|
|
|
rt_schedule();
|
|
|
|
}
|
2016-10-29 14:35:07 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
2012-12-26 08:29:42 +08:00
|
|
|
|
|
|
|
if (queue->evt_notify != RT_NULL)
|
|
|
|
queue->evt_notify(queue, RT_DATAQUEUE_EVENT_LWM);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
__exit:
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
if ((result == RT_EOK) && (queue->evt_notify != RT_NULL))
|
|
|
|
{
|
|
|
|
queue->evt_notify(queue, RT_DATAQUEUE_EVENT_POP);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_data_queue_pop);
|
|
|
|
|
2021-11-23 16:05:17 +08:00
|
|
|
/**
|
2021-11-24 15:21:30 +08:00
|
|
|
* @brief This function will fetch but retaining data in the data queue.
|
2021-11-24 14:14:45 +08:00
|
|
|
*
|
2021-11-24 15:21:30 +08:00
|
|
|
* @param queue is a pointer to the data queue object.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 14:14:45 +08:00
|
|
|
* @param data_ptr is the buffer pointer of the data to be fetched.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 14:14:45 +08:00
|
|
|
* @param size is the size in bytes of the data to be fetched.
|
|
|
|
*
|
|
|
|
* @return Return the operation status. When the return value is RT_EOK, the operation is successful.
|
2021-12-01 14:43:53 +08:00
|
|
|
* When the return value is -RT_EEMPTY, it means the data queue is empty.
|
2021-11-23 16:05:17 +08:00
|
|
|
*/
|
2020-12-31 16:02:42 +08:00
|
|
|
rt_err_t rt_data_queue_peek(struct rt_data_queue *queue,
|
2022-07-25 10:21:18 +08:00
|
|
|
const void **data_ptr,
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_size_t *size)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
2020-09-18 09:33:34 +08:00
|
|
|
RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
|
2012-12-26 08:29:42 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
if (queue->is_empty)
|
2012-12-26 08:29:42 +08:00
|
|
|
{
|
|
|
|
return -RT_EEMPTY;
|
|
|
|
}
|
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
*data_ptr = queue->queue[queue->get_index].data_ptr;
|
|
|
|
*size = queue->queue[queue->get_index].data_size;
|
2012-12-26 08:29:42 +08:00
|
|
|
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2020-12-31 16:02:42 +08:00
|
|
|
RTM_EXPORT(rt_data_queue_peek);
|
2012-12-26 08:29:42 +08:00
|
|
|
|
2021-11-23 16:05:17 +08:00
|
|
|
/**
|
2021-11-24 15:21:30 +08:00
|
|
|
* @brief This function will reset the data queue.
|
2021-11-24 14:14:45 +08:00
|
|
|
*
|
|
|
|
* @note Calling this function will wake up all threads on the data queue
|
|
|
|
* that are hanging and waiting.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 15:21:30 +08:00
|
|
|
* @param queue is a pointer to the data queue object.
|
2021-11-23 16:05:17 +08:00
|
|
|
*/
|
2012-12-26 08:29:42 +08:00
|
|
|
void rt_data_queue_reset(struct rt_data_queue *queue)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2012-12-26 08:29:42 +08:00
|
|
|
struct rt_thread *thread;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
2020-04-08 11:05:37 +08:00
|
|
|
RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
|
2020-09-18 09:33:34 +08:00
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
queue->get_index = 0;
|
|
|
|
queue->put_index = 0;
|
|
|
|
queue->is_empty = 1;
|
|
|
|
queue->is_full = 0;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_enter_critical();
|
|
|
|
/* wakeup all suspend threads */
|
|
|
|
|
|
|
|
/* resume on pop list */
|
|
|
|
while (!rt_list_isempty(&(queue->suspended_pop_list)))
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
2020-09-18 09:33:34 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2012-12-26 08:29:42 +08:00
|
|
|
|
|
|
|
/* get next suspend thread */
|
|
|
|
thread = rt_list_entry(queue->suspended_pop_list.next,
|
|
|
|
struct rt_thread,
|
|
|
|
tlist);
|
2023-03-17 12:12:16 +08:00
|
|
|
/* set error code to -RT_ERROR */
|
2012-12-26 08:29:42 +08:00
|
|
|
thread->error = -RT_ERROR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* resume thread
|
|
|
|
* In rt_thread_resume function, it will remove current thread from
|
|
|
|
* suspend list
|
|
|
|
*/
|
|
|
|
rt_thread_resume(thread);
|
|
|
|
|
|
|
|
/* enable interrupt */
|
2020-09-18 09:33:34 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2012-12-26 08:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* resume on push list */
|
|
|
|
while (!rt_list_isempty(&(queue->suspended_push_list)))
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
2020-09-18 09:33:34 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2012-12-26 08:29:42 +08:00
|
|
|
|
|
|
|
/* get next suspend thread */
|
|
|
|
thread = rt_list_entry(queue->suspended_push_list.next,
|
|
|
|
struct rt_thread,
|
|
|
|
tlist);
|
2023-03-17 12:12:16 +08:00
|
|
|
/* set error code to -RT_ERROR */
|
2012-12-26 08:29:42 +08:00
|
|
|
thread->error = -RT_ERROR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* resume thread
|
|
|
|
* In rt_thread_resume function, it will remove current thread from
|
|
|
|
* suspend list
|
|
|
|
*/
|
|
|
|
rt_thread_resume(thread);
|
|
|
|
|
|
|
|
/* enable interrupt */
|
2020-09-18 09:33:34 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2012-12-26 08:29:42 +08:00
|
|
|
}
|
|
|
|
rt_exit_critical();
|
|
|
|
|
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_data_queue_reset);
|
2020-04-07 13:35:09 +08:00
|
|
|
|
2021-11-23 16:05:17 +08:00
|
|
|
/**
|
2021-11-24 15:21:30 +08:00
|
|
|
* @brief This function will deinit the data queue.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 15:21:30 +08:00
|
|
|
* @param queue is a pointer to the data queue object.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 14:14:45 +08:00
|
|
|
* @return Return the operation status. When the return value is RT_EOK, the operation is successful.
|
2021-11-23 16:05:17 +08:00
|
|
|
*/
|
2020-04-07 13:35:09 +08:00
|
|
|
rt_err_t rt_data_queue_deinit(struct rt_data_queue *queue)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2020-04-07 13:35:09 +08:00
|
|
|
|
|
|
|
RT_ASSERT(queue != RT_NULL);
|
2020-09-18 09:33:34 +08:00
|
|
|
RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
|
2020-04-07 13:35:09 +08:00
|
|
|
|
|
|
|
/* wakeup all suspend threads */
|
|
|
|
rt_data_queue_reset(queue);
|
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
2020-04-08 11:05:37 +08:00
|
|
|
queue->magic = 0;
|
2020-04-07 13:35:09 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
rt_free(queue->queue);
|
2020-04-07 13:35:09 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_data_queue_deinit);
|
2020-09-18 09:33:34 +08:00
|
|
|
|
2021-11-23 16:05:17 +08:00
|
|
|
/**
|
2021-11-24 14:14:45 +08:00
|
|
|
* @brief This function will get the number of data in the data queue.
|
|
|
|
*
|
2021-11-24 15:21:30 +08:00
|
|
|
* @param queue is a pointer to the data queue object.
|
2021-11-23 16:40:55 +08:00
|
|
|
*
|
2021-11-24 14:14:45 +08:00
|
|
|
* @return Return the number of data in the data queue.
|
2021-11-23 16:05:17 +08:00
|
|
|
*/
|
2020-09-18 09:33:34 +08:00
|
|
|
rt_uint16_t rt_data_queue_len(struct rt_data_queue *queue)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2020-09-18 09:33:34 +08:00
|
|
|
rt_int16_t len;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
RT_ASSERT(queue != RT_NULL);
|
|
|
|
RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
|
|
|
|
|
|
|
|
if (queue->is_empty)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
if (queue->put_index > queue->get_index)
|
|
|
|
{
|
|
|
|
len = queue->put_index - queue->get_index;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = queue->size + queue->put_index - queue->get_index;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2020-09-18 09:33:34 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_data_queue_len);
|
|
|
|
|