2012-09-30 15:33:13 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2012-09-30 15:33:13 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-09-30 15:33:13 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-09-30 Bernard first version.
|
2021-08-18 14:10:50 +08:00
|
|
|
* 2021-08-18 chenyingchun add comments
|
2012-09-30 15:33:13 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
2012-11-02 12:05:38 +08:00
|
|
|
#define RT_COMPLETED 1
|
|
|
|
#define RT_UNCOMPLETED 0
|
2012-09-30 15:33:13 +08:00
|
|
|
|
2021-08-15 21:27:14 +08:00
|
|
|
/**
|
2021-08-18 14:10:50 +08:00
|
|
|
* @brief This function will initialize a completion object.
|
2021-08-16 13:10:26 +08:00
|
|
|
*
|
2021-08-18 14:10:50 +08:00
|
|
|
* @param completion is a pointer to a completion object.
|
2021-08-15 21:27:14 +08:00
|
|
|
*/
|
2012-11-02 12:05:38 +08:00
|
|
|
void rt_completion_init(struct rt_completion *completion)
|
2012-09-30 15:33:13 +08:00
|
|
|
{
|
2012-11-02 12:05:38 +08:00
|
|
|
rt_base_t level;
|
|
|
|
RT_ASSERT(completion != RT_NULL);
|
2012-09-30 15:33:13 +08:00
|
|
|
|
2012-11-02 12:05:38 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
completion->flag = RT_UNCOMPLETED;
|
|
|
|
rt_list_init(&completion->suspended_list);
|
|
|
|
rt_hw_interrupt_enable(level);
|
2012-09-30 15:33:13 +08:00
|
|
|
}
|
2015-05-11 12:33:15 +08:00
|
|
|
RTM_EXPORT(rt_completion_init);
|
2012-09-30 15:33:13 +08:00
|
|
|
|
2021-08-15 21:27:14 +08:00
|
|
|
/**
|
2021-08-18 14:10:50 +08:00
|
|
|
* @brief This function will wait for a completion, if the completion is unavailable, the thread shall wait for
|
|
|
|
* the completion up to a specified time.
|
2021-08-16 13:10:26 +08:00
|
|
|
*
|
2021-08-18 14:10:50 +08:00
|
|
|
* @param completion is a pointer to a completion object.
|
|
|
|
*
|
2021-08-23 18:52:31 +08:00
|
|
|
* @param timeout is a timeout period (unit: OS ticks). If the completion is unavailable, the thread will wait for
|
2021-08-18 14:10:50 +08:00
|
|
|
* the completion done up to the amount of time specified by the argument.
|
|
|
|
* NOTE: Generally, we use the macro RT_WAITING_FOREVER to set this parameter, which means that when the
|
|
|
|
* completion is unavailable, the thread will be waitting forever.
|
2021-08-16 13:10:26 +08:00
|
|
|
*
|
2021-08-23 18:52:31 +08:00
|
|
|
* @return Return the operation status. ONLY when the return value is RT_EOK, the operation is successful.
|
2021-08-15 21:27:14 +08:00
|
|
|
* If the return value is any other values, it means that the completion wait failed.
|
2021-08-18 14:10:50 +08:00
|
|
|
*
|
2021-08-23 18:52:31 +08:00
|
|
|
* @warning This function can ONLY be called in the thread context. It MUST NOT be called in interrupt context.
|
2021-08-15 21:27:14 +08:00
|
|
|
*/
|
2012-11-02 12:05:38 +08:00
|
|
|
rt_err_t rt_completion_wait(struct rt_completion *completion,
|
|
|
|
rt_int32_t timeout)
|
2012-09-30 15:33:13 +08:00
|
|
|
{
|
2012-11-02 12:05:38 +08:00
|
|
|
rt_err_t result;
|
|
|
|
rt_base_t level;
|
|
|
|
rt_thread_t thread;
|
|
|
|
RT_ASSERT(completion != 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-11-02 12:05:38 +08:00
|
|
|
result = RT_EOK;
|
|
|
|
thread = rt_thread_self();
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
if (completion->flag != RT_COMPLETED)
|
|
|
|
{
|
|
|
|
/* only one thread can suspend on complete */
|
|
|
|
RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));
|
|
|
|
|
|
|
|
if (timeout == 0)
|
|
|
|
{
|
|
|
|
result = -RT_ETIMEOUT;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* reset thread error number */
|
|
|
|
thread->error = RT_EOK;
|
|
|
|
|
|
|
|
/* suspend thread */
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
|
2012-11-02 12:05:38 +08:00
|
|
|
/* add to suspended list */
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_list_insert_before(&(completion->suspended_list),
|
|
|
|
&(thread->tlist));
|
2012-11-02 12:05:38 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* current context checking */
|
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
|
|
2012-11-02 12:05:38 +08:00
|
|
|
/* start timer */
|
|
|
|
if (timeout > 0)
|
|
|
|
{
|
|
|
|
/* reset the timeout of thread timer and start it */
|
2012-12-26 08:29:42 +08:00
|
|
|
rt_timer_control(&(thread->thread_timer),
|
|
|
|
RT_TIMER_CTRL_SET_TIME,
|
|
|
|
&timeout);
|
2012-11-02 12:05:38 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 15:54:08 +08:00
|
|
|
/* clean completed flag */
|
|
|
|
completion->flag = RT_UNCOMPLETED;
|
2012-09-30 15:33:13 +08:00
|
|
|
|
|
|
|
__exit:
|
2012-11-02 12:05:38 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2012-11-02 12:05:38 +08:00
|
|
|
return result;
|
2012-09-30 15:33:13 +08:00
|
|
|
}
|
2015-05-11 12:33:15 +08:00
|
|
|
RTM_EXPORT(rt_completion_wait);
|
2012-09-30 15:33:13 +08:00
|
|
|
|
2021-08-15 21:27:14 +08:00
|
|
|
/**
|
2021-08-23 18:52:31 +08:00
|
|
|
* @brief This function indicates a completion has done.
|
2021-08-16 13:10:26 +08:00
|
|
|
*
|
2021-08-18 14:10:50 +08:00
|
|
|
* @param completion is a pointer to a completion object.
|
2021-08-15 21:27:14 +08:00
|
|
|
*/
|
2012-11-02 12:05:38 +08:00
|
|
|
void rt_completion_done(struct rt_completion *completion)
|
2012-09-30 15:33:13 +08:00
|
|
|
{
|
2012-11-02 12:05:38 +08:00
|
|
|
rt_base_t level;
|
|
|
|
RT_ASSERT(completion != RT_NULL);
|
|
|
|
|
|
|
|
if (completion->flag == RT_COMPLETED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
completion->flag = RT_COMPLETED;
|
|
|
|
|
|
|
|
if (!rt_list_isempty(&(completion->suspended_list)))
|
|
|
|
{
|
|
|
|
/* there is one thread in suspended list */
|
|
|
|
struct rt_thread *thread;
|
|
|
|
|
|
|
|
/* get thread entry */
|
2012-12-26 08:29:42 +08:00
|
|
|
thread = rt_list_entry(completion->suspended_list.next,
|
|
|
|
struct rt_thread,
|
|
|
|
tlist);
|
|
|
|
|
2012-11-02 12:05:38 +08:00
|
|
|
/* resume it */
|
|
|
|
rt_thread_resume(thread);
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
/* perform a schedule */
|
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
2012-09-30 15:33:13 +08:00
|
|
|
}
|
2015-05-11 12:33:15 +08:00
|
|
|
RTM_EXPORT(rt_completion_done);
|
|
|
|
|