/**
* @file CM_MultiThread.c
* @brief 实现了线程操作的的封装
* @author GT-BU
* @date 2021.02.01
* @version V1.1
* @copyright TCT
* @par 修改日志
* <table>
* <tr><th>Date        <th>Version  <th>Author     <th>Description
* <tr><td>2017.12.20  <td>V1.0     <td>MSCP项目组 <td>创建初始版本
* <tr><td>2021.02.01  <td>V1.1     <td>GT-BU      <td>TSCP项目集成、更新
* </table>
*/
#include "CM_MultiThread.h"

#ifdef VXWORKS
/*
* @brief 创建线程
* @param thread 线程ID
* @param myfunc 线程执行函数
* @param threadName 线程名称
* @param stackSize 线程堆栈大小
* @param priority 线程优先级
* @param pargs 线程输入参数
* @return 执行结果
* - CM_TRUE 成功
* - CM_FALSE 失败
*/
CM_BOOL CM_Thread_Create(OUT CM_THREAD_T* thread, IN CM_THREAD_FUNC_T myfunc, IN const CM_CHAR* threadName, IN CM_INT32 stackSize, IN CM_INT32 priority, IN CM_Thread_Arg_Struct_T * pargs)
{/*传入的参数指针 不能赋值给局部变量*/
    CM_THREAD_T tmpThread = ERROR; /* 线程ID */
    CM_BOOL rt = CM_FALSE; /* 函数返回值 */

    /*- 检查输入参数是否非法 */
    if ((priority > CM_TASK_PRIORITY_99) || (priority < CM_TASK_PRIORITY_0) || (stackSize <= 0))
    {
        /*- 无操作 */
    }
    else
    {

        /*- 线程参数是否非空 */
        if (NULL != pargs)
        {
            /*- 带参数创建线程 */
            tmpThread = taskSpawn((char *)threadName, priority, VX_FP_TASK, stackSize, (FUNCPTR)myfunc,
                pargs->args0, pargs->args1, pargs->args2, pargs->args3, pargs->args4,
                pargs->args5, pargs->args6, pargs->args7, pargs->args8, pargs->args9);
        }
        else
        {
            /*- 按参数为0创建线程 */
            tmpThread = taskSpawn((char *)threadName, priority, VX_FP_TASK, stackSize, (FUNCPTR)myfunc,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        }

        /*- @alias 输出线程ID */
        if (thread != NULL)
        {/*给指针赋值，修改0223bug，*/
            *thread = tmpThread;
        }
    }

    /*- @alias 判断执行结果 */
    if (ERROR == tmpThread)
    {
        rt = CM_FALSE;
    }
    else
    {
        rt = CM_TRUE;
    }
    /*- 返回执行结果 */
    return rt;
}
/*
* @brief 获取当前线程ID
* @return 线程ID
*/
CM_THREAD_T CM_Thread_Self(void)
{
    /*- 获取当前线程ID */
    CM_THREAD_T td = taskIdSelf();
    /*- 返回线程ID */
    return td;
}
#endif


