/**************************************************************************//**
 * @item     CosyOS-III Kernel
 * @file     sv_task.h
 * @brief    任务服务
 * @details  仅在任务中调用并执行。
 * @author   迟凯峰
 * @version  v3.0.0
 * @date     2026.07.21
 ******************************************************************************/

#ifndef __SV_TASK_H
#define __SV_TASK_H

#include "os_var.h"

#if defined ( __C51__ ) || defined ( __C251__ )
void       su_schedule_locks    (void);
void       su_kernel_lock0      (void);
void       su_kernel_unlock0    (void);
s_ecode_t  su_kernel_unlocks_rec(s_ecode_t e);
void      *su_kernel_unlocks_rvp(void *p);
#endif

#if (SYSCFG_KERNELLOCKNESTING == __DISABLED__)
#define    su_kernel_locks      su_kernel_lock0
#elif defined ( __C51__ ) || defined ( __C251__ )
void       su_kernel_locks      (void);
#endif

void       su_schedule_unlocks  (void);
void       su_kernel_unlocks    (void);
void       su_kernel_unlock0_psv(void);

s_ecode_t  su_startup_task_0    (s_taskload_tsp hload, s_u8_t status);
s_ecode_t  su_startup_task_1    (s_taskload_tsp hload, s_u8_t status, s_voidvoid_tfp entry);
s_ecode_t  su_resume_suspend    (s_taskctrl_tsp htask);
void       su_yield_tasking     (void);
void       su_delay             (s_delay_t tick);
bool       su_take_mutex        (s_mutex_tsp hmut, s_delay_t tick);
void       su_back_mutex        (s_mutex_tsp hmut);
bool       su_wait_binary       (s_binary_tsp hbin, s_delay_t tick);
bool       su_take_binary       (s_binary_tsp hbin, s_delay_t tick);
bool       su_take_semaph       (s_semaph_tsp hsem, s_delay_t tick);
m_fetion_t su_recv_fetion       (s_tionbox_tsp htbox, s_delay_t tick);
void      *su_recv_mail         (s_mailbox_tsp hmbox, s_delay_t tick);
void      *su_recv_msg          (s_msgque_tsp hque, s_delay_t tick);
bool       su_recv_taskmsg      (s_u8_t  volatile _STATIC_MEM_ *tmrf, s_delay_t tick);
bool       su_wait_group_1      (s_u8_t  volatile _STATIC_MEM_ *hgrp, s_delay_t tick);
bool       su_wait_group_2      (s_u16_t volatile _STATIC_MEM_ *hgrp, s_delay_t tick);
bool       su_wait_group_4      (s_u32_t volatile _STATIC_MEM_ *hgrp, s_delay_t tick);
bool       su_query_group       (void    volatile _STATIC_MEM_ *hgrp, s_u8_t size);
bool       su_init_mempool      (s_thrmem_tsp hmem, size_t size);
void _MALLOC_MEM_ *su_talloc    (s_thrmem_tsp hmem, size_t size);
void _MALLOC_MEM_ *su_xalloc    (void _MALLOC_MEM_ *p);


/**
 @addtogroup CosyOS_内核服务
 @{
 *//**
 \defgroup   任务服务
 \brief      仅在任务中调用并执行的服务。
 @{
 */

#if !defined ( __C51__ ) && !defined ( __C251__ )
/** 调度上锁（进入嵌套任务临界区）*/
__STATIC_FORCEINLINE void su_schedule_locks(void)
{
	s_sign_schedule.every[0] = true;
	__mx_MMB();
	s_schedulelock_counter++;
}

/** 内核上锁（进入非嵌套服务层临界区）*/
__STATIC_FORCEINLINE bool su_kernel_lock0(void)
{
	mSysIRQ_Disable();
    return true;
}

/** 内核解锁（退出非嵌套服务层临界区）*//*
__STATIC_FORCEINLINE bool su_kernel_unlock0(void)
{
	mSysIRQ_Enable();
	return true;
}*/

/** 内核上锁（进入嵌套服务层临界区）*/
#if (SYSCFG_KERNELLOCKNESTING == __ENABLED__)
__STATIC_FORCEINLINE bool su_kernel_locks(void)
{
	mSysIRQ_Disable();
	s_kernellock_counter++;
	return true;
}
#endif

/** 内核解锁（退出嵌套服务层临界区）并返回(s_ecode_t) */
__STATIC_FORCEINLINE s_ecode_t su_kernel_unlocks_rec(s_ecode_t e)
{
	su_kernel_unlocks();
	return e;
}

/** 内核解锁（退出嵌套服务层临界区）并返回(void *) */
__STATIC_FORCEINLINE void *su_kernel_unlocks_rvp(void *p)
{
	su_kernel_unlocks();
	return p;
}
#endif


/*
 * 操作任务
 */

/** 启动任务 */
#define sUSV_StartTask_0(hload, status) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_startup_task_0((s_taskload_tsp)hload, status) : OS_ECODE_NOERROR \
)

/** 启动任务 */
#define sUSV_StartTask_1(hload, status, entry) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_startup_task_1((s_taskload_tsp)hload, status, (s_voidvoid_tfp)entry) : OS_ECODE_NOERROR \
)

/** 恢复指定任务并挂起自身任务 */
#define sUSV_ResumeSuspend(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(su_resume_suspend(htask)) : OS_ECODE_NOERROR \
)

/** 恢复任务 */
#define sUSV_ResumeTask(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(sc_resume_task(htask)) : OS_ECODE_NOERROR \
)

/** 挂起任务 */
#define sUSV_SuspendTask(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(sc_suspend_task(htask)) : OS_ECODE_NOERROR \
)

/** 删除任务 */
#define sUSV_DeleteTask(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(sc_delete_task(htask)) : OS_ECODE_NOERROR \
)

/** 重置任务 */
#define sUSV_ResetTask(htask, status) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(sc_reset_task(htask, status)) : OS_ECODE_NOERROR \
)

/** 清除阻塞（状态）*/
#define sUSV_ClearBlock(htask) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(sc_clear_block(htask)) : OS_ECODE_NOERROR \
)

/** 设置阻塞（时间）*/
#define sUSV_SetBlock(htask, tick) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(sc_set_block(htask, tick)) : OS_ECODE_NOERROR \
)

/** 设置任务优先级 */
#define sUSV_SetTaskPri(htask, npri) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(sc_set_taskpri(htask, npri)) : OS_ECODE_NOERROR \
)

/** 自身任务延时 */
#define sUSV_Delay(tick) \
do{ \
	mSysIRQ_Disable(); \
	su_delay(tick); \
}while(false)


/*
 * 互斥信号量
 */

/** 获取互斥信号量 */
#define sUSV_TakeMut(mut, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_take_mutex(&mut, tick) : false \
)

/** 归还互斥信号量 */
#define sUSV_BackMut(mut) \
do{ \
	su_kernel_locks(); \
	su_back_mutex(&mut); \
}while(false)


/*
 * 二值信号量
 */

/** 等待二值信号量 */
#define sUSV_WaitBin(bin, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_wait_binary(&bin, tick) : false \
)

/** 获取二值信号量 */
#define sUSV_TakeBin(bin, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_take_binary(&bin, tick) : false \
)

/** 给予二值信号量 */
#define sUSV_GiveBin(bin) \
do{ \
	su_kernel_locks(); \
	sc_write_binary(&bin, true); \
	su_kernel_unlocks(); \
}while(false)


/*
 * 计数信号量
 */

/** 获取计数信号量 */
#define sUSV_TakeSem(sem, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_take_semaph(&sem, tick) : false \
)

/** 给予计数信号量 */
#define sUSV_GiveSem(sem) \
do{ \
	su_kernel_locks(); \
	sc_give_semaph(&sem); \
	su_kernel_unlocks(); \
}while(false)


/*
 * 私信
 */

/** 接收私信 */
#define sUSV_RecvTaskmsg(tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_recv_taskmsg(&u_tmrf, tick) : false \
)

/** 发送私信 */
#define sUSV_SendTaskmsg(task) \
	su_kernel_locks(); \
	s_taskmsg_sign = 2; \
	sa_block_to_ready(task); \
	u_task_##task


/*
 * 飞信
 */

/** 接收飞信 */
#define sUSV_RecvFetion(tbox, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_recv_fetion(&tbox, tick) : false \
)

/** 发送飞信 */
#define sUSV_SendFetion(tbox, tion) \
do{ \
	su_kernel_locks(); \
	sc_send_fetion(&tbox, tion); \
	su_kernel_unlocks(); \
}while(false)


/*
 * 邮箱
 */

/** 接收邮件 */
#define sUSV_RecvMail(mbox, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_recv_mail(&mbox, tick) : OS_NULL \
)

/** 发送邮件 */
#define sUSV_SendMail(mbox, mail) \
do{ \
	su_kernel_locks(); \
	sc_send_mail(&mbox, mail); \
	su_kernel_unlocks(); \
}while(false)


/*
 * 消息队列
 */

/** 接收消息 */
#define sUSV_RecvMsg(que, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? su_recv_msg((s_msgque_tsp)&que, tick) : OS_NULL \
)

/** 发送消息 */
#define sUSV_SendMsg(que, msg) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rec(sc_send_msg((s_msgque_tsp)&que, msg)) : OS_ECODE_NOERROR \
)


/*
 * 事件标志组
 */

/** 查询标志组 */
#define sUSV_QueryFlagGroup(group) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_query_group((void *)&group, sizeof(group)) : false \
)

/** 等待标志组 */
#define sUSV_WaitFlagGroup(group, tick) \
( \
	(m_boolvoid_tf(su_kernel_lock0))() || true ? ( \
		  sizeof(group) == 1 ? su_wait_group_1((s_u8_t  *)&group, tick) \
		: sizeof(group) == 2 ? su_wait_group_2((s_u16_t *)&group, tick) \
		: sizeof(group) == 4 ? su_wait_group_4((s_u32_t *)&group, tick) \
		: false \
	) : false \
)

/** 清除标志组 */
#define sUSV_ClearFlagGroup(group) \
do{ \
	su_kernel_locks(); \
	sizeof(group) == 1 ? *(s_u8_t  *)&group = false : MCUCFG_TERNARY_MASK \
	sizeof(group) == 2 ? *(s_u16_t *)&group = false : MCUCFG_TERNARY_MASK \
	sizeof(group) == 4 ? *(s_u32_t *)&group = false : false; \
	su_kernel_unlocks(); \
}while(false)

/** 写多标志位 */
#define sUSV_WriteFlagBits(group, value, nbit) \
	suWriteFlagBits_##nbit(group, value, 


/*
 * 定时
 */

/** 定时中断 */
#define sUSV_TimInt(tmid, tick) \
do{ \
	su_kernel_locks(); \
	sc_timint(tmid, tick); \
	su_kernel_unlocks(); \
}while(false)

/** 定时查询 */
#define sUSV_TimQry(tmid, tick) \
do{ \
	su_kernel_locks(); \
	sc_timqry(tmid, tick); \
	su_kernel_unlocks(); \
}while(false)


/*
 * 软件RTC
 */

/** 获取时间 */
#define sUSV_GetTime(t) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_kernel_unlocks_rvp(s_memcpy(t, &s_rtc, sizeof(s_rtc))) : OS_NULL \
)

/** 设置时间 */
#define sUSV_SetTime(t) \
do{ \
	su_kernel_locks(); \
	s_memcpy(&s_rtc, t, sizeof(s_rtc)); \
	s_rtc_counter = 1000000UL / SYSCFG_SYSTICK_CYCLE / 2 + 1; \
	su_kernel_unlocks(); \
}while(false)


/*
 * 进程内存
 */

/** malloc */
#define sUSV_Malloc(size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_xalloc((void _MALLOC_MEM_ *)s_malloc(size)) : OS_NULL \
)

/** calloc */
#define sUSV_Calloc(nmemb, size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_xalloc((void _MALLOC_MEM_ *)s_calloc(nmemb, size)) : OS_NULL \
)

/** realloc */
#define sUSV_Realloc(p, size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_xalloc((void _MALLOC_MEM_ *)s_realloc(p, size)) : OS_NULL \
)

/** free */
#define sUSV_Free(p) \
do{ \
	su_kernel_locks(); \
	s_free(p); \
	su_kernel_unlocks(); \
}while(false)


/*
 * 线程内存
 */

/** 初始化线程内存池 */
#define sUSV_InitMempool(size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_init_mempool(&u_thrmem, size) : false \
)

/** 线程内存分配 */
#define sUSV_Talloc(size) \
( \
	(m_boolvoid_tf(su_kernel_locks))() || true ? su_xalloc(su_talloc(&u_thrmem, size)) : OS_NULL \
)

/** 释放线程内存池 */
#define sUSV_FreeMempool() \
do{ \
	sUSV_Free(u_thrmem.head); \
	u_thrmem.head = u_thrmem.move = OS_NULL; \
}while(false)

/** @} */
/** @} */

#endif
