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
|
2023-10-25 20:31:25 +08:00
|
|
|
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
|
2024-02-23 17:49:15 +08:00
|
|
|
* 2024-01-25 Shell reduce resource usage in completion for better synchronization
|
|
|
|
* and smaller footprint.
|
2012-09-30 15:33:13 +08:00
|
|
|
*/
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
#define DBG_TAG "drivers.ipc"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2012-09-30 15:33:13 +08:00
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
2024-05-08 09:25:57 +08:00
|
|
|
/**
|
|
|
|
* This is an implementation of completion core on UP system.
|
|
|
|
* Noted that spinlock is (preempt_lock + irq_mask) on UP scheduler.
|
|
|
|
*/
|
|
|
|
|
2012-11-02 12:05:38 +08:00
|
|
|
#define RT_COMPLETED 1
|
|
|
|
#define RT_UNCOMPLETED 0
|
2024-02-23 17:49:15 +08:00
|
|
|
#define RT_COMPLETION_FLAG(comp) ((comp)->susp_thread_n_flag & 1)
|
|
|
|
#define RT_COMPLETION_THREAD(comp) ((rt_thread_t)((comp)->susp_thread_n_flag & ~1))
|
|
|
|
#define RT_COMPLETION_NEW_STAT(thread, flag) (((flag) & 1) | (((rt_base_t)thread) & ~1))
|
|
|
|
|
|
|
|
static struct rt_spinlock _completion_lock = RT_SPINLOCK_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 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_ASSERT(completion != RT_NULL);
|
2012-09-30 15:33:13 +08:00
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_UNCOMPLETED);
|
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.
|
2024-05-08 09:25:57 +08:00
|
|
|
* @param suspend_flag suspend flags. See rt_thread_suspend_with_flag()
|
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
|
|
|
*/
|
2024-05-08 09:25:57 +08:00
|
|
|
rt_err_t rt_completion_wait_flags(struct rt_completion *completion,
|
|
|
|
rt_int32_t timeout, int suspend_flag)
|
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();
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
level = rt_spin_lock_irqsave(&_completion_lock);
|
2024-05-08 09:25:57 +08:00
|
|
|
|
|
|
|
__try_again:
|
2024-02-23 17:49:15 +08:00
|
|
|
if (RT_COMPLETION_FLAG(completion) != RT_COMPLETED)
|
2012-11-02 12:05:38 +08:00
|
|
|
{
|
|
|
|
/* only one thread can suspend on complete */
|
2024-02-23 17:49:15 +08:00
|
|
|
RT_ASSERT(RT_COMPLETION_THREAD(completion) == RT_NULL);
|
2012-11-02 12:05:38 +08:00
|
|
|
|
|
|
|
if (timeout == 0)
|
|
|
|
{
|
|
|
|
result = -RT_ETIMEOUT;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* reset thread error number */
|
|
|
|
thread->error = RT_EOK;
|
|
|
|
|
|
|
|
/* suspend thread */
|
2024-05-08 09:25:57 +08:00
|
|
|
result = rt_thread_suspend_with_flag(thread, suspend_flag);
|
2024-02-23 17:49:15 +08:00
|
|
|
if (result == RT_EOK)
|
2012-11-02 12:05:38 +08:00
|
|
|
{
|
2024-02-23 17:49:15 +08:00
|
|
|
/* add to suspended thread */
|
2024-05-08 09:25:57 +08:00
|
|
|
rt_base_t waiting_stat = RT_COMPLETION_NEW_STAT(thread, RT_UNCOMPLETED);
|
|
|
|
completion->susp_thread_n_flag = waiting_stat;
|
2024-02-23 17:49:15 +08:00
|
|
|
|
|
|
|
/* current context checking */
|
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
|
|
|
|
|
/* 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_spin_unlock_irqrestore(&_completion_lock, level);
|
|
|
|
|
|
|
|
/* do schedule */
|
|
|
|
rt_schedule();
|
|
|
|
|
|
|
|
level = rt_spin_lock_irqsave(&_completion_lock);
|
2024-05-08 09:25:57 +08:00
|
|
|
|
|
|
|
if (completion->susp_thread_n_flag != waiting_stat)
|
|
|
|
{
|
|
|
|
/* completion may be completed after we suspend */
|
|
|
|
timeout = 0;
|
|
|
|
goto __try_again;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no changes, waiting failed */
|
|
|
|
result = thread->error;
|
|
|
|
result = result > 0 ? -result : result;
|
|
|
|
RT_ASSERT(result != RT_EOK);
|
|
|
|
}
|
2012-11-02 12:05:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-23 17:49:15 +08:00
|
|
|
|
|
|
|
/* clean completed flag & remove susp_thread on the case of waking by timeout */
|
|
|
|
completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_UNCOMPLETED);
|
2012-09-30 15:33:13 +08:00
|
|
|
|
|
|
|
__exit:
|
2024-02-23 17:49:15 +08:00
|
|
|
rt_spin_unlock_irqrestore(&_completion_lock, 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
|
|
|
}
|
|
|
|
|
2024-08-15 21:30:58 +08:00
|
|
|
/**
|
|
|
|
* @brief This is same as rt_completion_wait_flags(), except that this API is NOT
|
|
|
|
* ISR-safe (you can NOT call completion_done() on isr routine).
|
|
|
|
*
|
|
|
|
* @param completion is a pointer to a completion object.
|
|
|
|
* @param timeout is a timeout period (unit: OS ticks). If the completion is unavailable, the thread will wait for
|
|
|
|
* 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.
|
|
|
|
* @param suspend_flag suspend flags. See rt_thread_suspend_with_flag()
|
|
|
|
*
|
|
|
|
* @return Return the operation status. ONLY when the return value is RT_EOK, the operation is successful.
|
|
|
|
* If the return value is any other values, it means that the completion wait failed.
|
|
|
|
*
|
|
|
|
* @warning This function can ONLY be called in the thread context. It MUST NOT be called in interrupt context.
|
|
|
|
*/
|
|
|
|
rt_err_t rt_completion_wait_flags_noisr(struct rt_completion *completion,
|
|
|
|
rt_int32_t timeout, int suspend_flag)
|
|
|
|
{
|
|
|
|
return rt_completion_wait_flags(completion, timeout, suspend_flag);
|
|
|
|
}
|
|
|
|
|
2021-08-15 21:27:14 +08:00
|
|
|
/**
|
2024-05-08 09:25:57 +08:00
|
|
|
* @brief This function indicates a completion has done and wakeup the thread
|
|
|
|
* and update its errno. No update is applied if it's a negative value.
|
2021-08-16 13:10:26 +08:00
|
|
|
*
|
2024-05-08 09:25:57 +08:00
|
|
|
* @param completion is a pointer to a completion object.
|
|
|
|
* @param thread_errno is the errno set to waking thread.
|
|
|
|
* @return RT_EOK if wakeup succeed.
|
|
|
|
* RT_EEMPTY if wakeup failure and the completion is set to completed.
|
|
|
|
* RT_EBUSY if the completion is still in completed state
|
2021-08-15 21:27:14 +08:00
|
|
|
*/
|
2024-05-08 09:25:57 +08:00
|
|
|
rt_err_t rt_completion_wakeup_by_errno(struct rt_completion *completion,
|
|
|
|
rt_err_t thread_errno)
|
2012-09-30 15:33:13 +08:00
|
|
|
{
|
2012-11-02 12:05:38 +08:00
|
|
|
rt_base_t level;
|
2024-02-23 17:49:15 +08:00
|
|
|
rt_err_t error;
|
|
|
|
rt_thread_t suspend_thread;
|
2012-11-02 12:05:38 +08:00
|
|
|
RT_ASSERT(completion != RT_NULL);
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
level = rt_spin_lock_irqsave(&_completion_lock);
|
|
|
|
if (RT_COMPLETION_FLAG(completion) == RT_COMPLETED)
|
|
|
|
{
|
|
|
|
rt_spin_unlock_irqrestore(&_completion_lock, level);
|
2024-03-28 23:42:56 +08:00
|
|
|
return -RT_EBUSY;
|
2024-02-23 17:49:15 +08:00
|
|
|
}
|
2012-11-02 12:05:38 +08:00
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
suspend_thread = RT_COMPLETION_THREAD(completion);
|
|
|
|
if (suspend_thread)
|
2012-11-02 12:05:38 +08:00
|
|
|
{
|
|
|
|
/* there is one thread in suspended list */
|
2012-12-26 08:29:42 +08:00
|
|
|
|
2024-05-08 09:25:57 +08:00
|
|
|
if (thread_errno >= 0)
|
|
|
|
{
|
|
|
|
suspend_thread->error = thread_errno;
|
|
|
|
}
|
|
|
|
|
2024-02-23 17:49:15 +08:00
|
|
|
error = rt_thread_resume(suspend_thread);
|
|
|
|
if (error)
|
|
|
|
{
|
2024-05-08 09:25:57 +08:00
|
|
|
LOG_D("%s: failed to resume thread with %d", __func__, error);
|
|
|
|
error = -RT_EEMPTY;
|
2024-02-23 17:49:15 +08:00
|
|
|
}
|
2012-11-02 12:05:38 +08:00
|
|
|
}
|
2024-03-28 23:42:56 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no thread waiting */
|
|
|
|
error = -RT_EEMPTY;
|
|
|
|
}
|
2024-02-23 17:49:15 +08:00
|
|
|
|
|
|
|
completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_COMPLETED);
|
|
|
|
|
|
|
|
rt_spin_unlock_irqrestore(&_completion_lock, level);
|
2024-03-28 23:42:56 +08:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|