📄 hs_os.c
字号:
/******************************************************************************
*
* Copyright (c), State Key Lab, Hisense Group, Qingdao, China.
* All rights reserved.
*
* Original filename: hs_os.c
* Author: Shang Xiuyuan
* Date: 2008.07.14
* Update: 2009.02.12
* Description: The common OS portable module for st linux.
*
* Update:
* $Log: hs_os.c,v $
*******************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#if 0
#include <ostime.h>
#include <mutex.h>
#include <message.h>
#include <task.h>
#include <semaphor.h>
#endif /* 0 */
#include <stdlib.h>
#include <string.h>
#include "stcommon.h"
#include "sttbx.h"
//#include "stos.h"
#ifdef ST_OSLINUX
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#define _XOPEN_SOURCE 600
#include <time.h>
#include <mqueue.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/sysinfo.h>
#endif
#include "hs_pub.h"
#include "hs_os.h"
#include "hs_utils.h"
#define HS_OS_VERSIONSTR "HS_OS(STLINUX)_V1.09.00"
#ifndef _DEBUG_
#define _DEBUG_ 1
#endif
#ifndef _DEBUG_MEMORY_
#define _DEBUG_MEMORY_ 0
#endif
#include "hs_debug.h"
#ifndef HS_BYTEORDER
#define HS_BYTEORDER HS_LIL_ENDIAN
#endif
#define INFO_LEVEL 1
#define WARN_LEVEL 2
#define ERROR_LEVEL 3
#define NO_INFO 4
/*========================================================================*/
static int g_nDebugLevel = INFO_LEVEL;
/*========================================================================*/
typedef struct
{
S8 szName[TASK_NAME_MAX_LEN];
U16 usUsed;
U8 ucStatus; /* task status */
U8 ucPriority; /* 0-99*/
U32 ulStackSize; /* stack size*/
HS_OS_TaskStyle_e eFlag; /* recycle mode*/
PFTaskEntry pfEntry;
void *pArg;
void *pRet;
pthread_t hTaskId; /* task id from system assigned */
}tcb_t;
/*
typedef struct
{
HANDLE hSem;
BOOL bUsed;
U32 ulTotalNum;
U32 ulCurNum;
}sm_t;
*/
typedef struct
{
mqd_t mqd;
BOOL bUsed;
U8 mqname[33];
HANDLE hSndSem;
HANDLE hRcvSem;
}mqm_t;
static tcb_t s_atTask[MAX_TCB_NUM + 1];
//static sm_t s_asSem[MAX_SEM_NUM + 1];
static mqm_t s_amMQ[64 + 1];
static BOOL s_bTaskInit = FALSE;
//static BOOL s_bSemInit = FALSE;
static BOOL s_bMQInit = FALSE;
static HANDLE s_hTaskMutex;
//static HANDLE s_hSemMutex;
static HANDLE s_hMQMutex;
#if 0
void hs_debug_print(int iLevel, char *format, ...)
{
char g_strOutBuff[1024];
if (iLevel >= g_nDebugLevel)
{
va_list args;
va_start(args, format);
vsprintf(g_strOutBuff, format, args);
va_end(args);
printf(g_strOutBuff);
//STTBX_Print((g_strOutBuff));
}
}
#endif
#define HS_ERR printf
#define HS_INFO printf
U32 HS_OS_GetDefaultStackSize(void)
{
#ifndef ST_OSLINUX
hs_debug_info(("OS21_DEF_MIN_STACK_SIZE is:%dB,%dKB\n", OS21_DEF_MIN_STACK_SIZE, OS21_DEF_MIN_STACK_SIZE / K_BYTE));
return OS21_DEF_MIN_STACK_SIZE;
#else
hs_debug_print(INFO_LEVEL, "STLINUX_DEF_STACK_SIZE is: %dB, %dKB\n", TASK_DEFAULT_STACK_SIZE, TASK_DEFAULT_STACK_SIZE / K_BYTE);
return TASK_DEFAULT_STACK_SIZE;
#endif
}
/*******************************************************************
* 进程相关函数接口
*******************************************************************/
/***************************************************************************
* Function:
* HS_OS_ProcessCreate
*
* Description:
* Create a Process
*
* Input:
* eFlag: 回收类型
* szAppName: 带路径的应用程序名,默认路径/bin和/usr/bin
* apcArg: 应用程序输入参数字符串
* envs: 应用程序需设置的环境变量
*
* Output:
* phProcessHandle: 进程句柄
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_ProcessCreate( HS_OS_TaskStyle_e eFlag,
U8 szAppName[],
S8 *apcArg[],
S8 * envs[],
U32 *phProcessHandle)
{
U32 hAppId;
S32 lRet;
if (NULL == phProcessHandle)
{
return HS_ERR_INVALID_PARA;
}
hAppId = vfork();
if (hAppId < 0)
{
HS_ERR("create app false\r\n");
return HS_ERR_SYS_ERR;
}
if (hAppId == 0)
{
lRet = execve(szAppName, apcArg, envs);
if (lRet < 0)
{
HS_ERR("execve() error");
_exit(-1);
}
}
else
{
if (phProcessHandle != NULL)
{
*phProcessHandle = hAppId;
}
}
if (eFlag == HS_OS_TASKSTYLE_DETACH)
{
/*not process*/
}
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_ProcessWait
*
* Description:
* 当创建某一进程时, 如果usFlag 参数取值为HS_ATTACH 时,
* 则必须调用此函数回收它, 而调用此函数的任务会挂
* 起直到应用程序运行结束
*
* Input:
* hProcessHandle: 进程句柄
*
* Output:
* plRet: 程序退出返回值
*
* Return:
* 其他 - 函数执行失败错误类型;
* HS_NO_ERROR - 执行成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_ProcessWait(HANDLE hProcessHandle, S32 *plRet)
{
S32 plRst;
if (hProcessHandle != waitpid(hProcessHandle, &plRst, 0))
{
hs_debug_print(ERROR_LEVEL, "wait app exit failure.\r\n");
return HS_FAIL;
}
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_ProcessExit
*
* Description:
* 退出应用程序( 自身)
*
* Input:
* lStatus: 退出返回值
*
* Output:
* 无
*
* Return:
* 其他 - 函数执行失败错误类型;
* HS_NO_ERROR - 执行成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_ProcessExit(S32 lStatus)
{
/*exit app*/
exit(lStatus);
}
/***************************************************************************
* Function:
* HS_OS_ProcessKill
*
* Description:
* 强制删除应用程序
*
* Input:
* hProcessHandle: 进程句柄
*
* Output:
* 无
*
* Return:
* 其他 - 函数执行失败错误类型;
* HS_NO_ERROR - 执行成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_ProcessKill(HANDLE hProcessHandle)
{
kill(hProcessHandle, SIGTERM);
kill(hProcessHandle, SIGKILL);
return HS_NO_ERROR;
}
/*******************************************************************
* 任务( 线程) 相关函数接口
*******************************************************************/
static void * CommonTask(void * pArg)
{
S32 lTaskId;
void * ret;
lTaskId = (S32)pArg;
s_atTask[lTaskId].ucStatus = TASK_RUN;
ret = (void *)s_atTask[lTaskId].pfEntry(s_atTask[lTaskId].pArg);
s_atTask[lTaskId].ucStatus = TASK_END;
if (s_atTask[lTaskId].eFlag == HS_OS_TASKSTYLE_DETACH)
{
HS_OS_MutexLock(s_hTaskMutex, HS_TIMEOUT_INFINITY, NULL);
s_atTask[lTaskId].usUsed = FALSE;
HS_OS_MutexUnlock(s_hTaskMutex);
}
return ret;
}
static S32 FindUnusedTask(void)
{
U32 i;
for (i = 1; i < MAX_TCB_NUM + 1; i++)
{
if (s_atTask[i].usUsed == FALSE)
{
return i;
}
}
return (-1);
}
static HS_ErrorCode_e HS_OS_TaskInit(void)
{
if ( s_bTaskInit )
{
return HS_ERROR_ALREADY_INITIALIZED;
}
memset(s_atTask, '\0', sizeof(s_atTask));
s_hTaskMutex = hs_mutex_create();
if (NULL == s_hTaskMutex)
{
hs_debug_print(ERROR_LEVEL, "Unable to create mutex while initialising task\n");
return HS_ERROR_FAILURE;
}
s_bTaskInit = TRUE;
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_TaskCreate
*
* Description:
* 创建任务
*
* Input:
* pTaskName: 任务名称,不允许重复
* pfEntry: 任务入口函数指针
* pParam: 传递给任务入口函数的参数
* stackSize: 任务堆栈大小(以字节为单位)
* priority: 任务优先级(0~255 ),0 最小,255 最大
*
* Output:
* phTaskHandle: 任务句柄
*
* Return value:
* 其他 - 创建任务失败
* HS_NO_ERROR - 创建任务成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_TaskCreate( IN U8* pTaskName,
IN PFTaskEntry pfEntry,
IN void* pParam,
IN U32 stackSize,
IN U32 priority,
OUT HANDLE *phTaskHandle )
{
#if 1
return HS_OS_TaskCreateEx(pTaskName,
HS_OS_TASKSTYLE_DETACH,
pfEntry,
pParam,
stackSize,
priority,
phTaskHandle);
#else
S32 lRet;
S32 lTaskId;
struct sched_param tSchedParam;
HS_ErrorCode_e eRet = HS_NO_ERROR;
pthread_attr_t tTaskAttr;
if (FALSE == s_bTaskInit)
{
lRet = HS_OS_TaskInit();
if (HS_NO_ERROR != lRet)
{
hs_debug_print(ERROR_LEVEL, "task init fail\n");
return HS_ERROR_FAILURE;
}
}
if (pfEntry == NULL || priority > MAX_PRIORITY || priority < MIN_PRIORITY)
{
hs_debug_print(ERROR_LEVEL, "input para err\n");
return HS_ERROR_PARA;
}
if (0 == stackSize)
{
stackSize = TASK_DEFAULT_STACK_SIZE;
}
else if ( TASK_MIN_STACK_SIZE > stackSize)
{
/* the stackSize seem can't be less than 16k*/
stackSize = TASK_MIN_STACK_SIZE;
}
hs_mutex_wait(s_hTaskMutex, HS_TIMEOUT_INFINITY, 0);
lTaskId = FindUnusedTask();
if (lTaskId < 0)
{
hs_debug_print(ERROR_LEVEL, "Reached the maximum number of tasks\n");
eRet = HS_ERROR_FAILURE;
goto err_return;
}
strncpy(s_atTask[lTaskId].szName, pTaskName, TASK_NAME_MAX_LEN - 1);
s_atTask[lTaskId].szName[TASK_NAME_MAX_LEN - 1] = '\0';
s_atTask[lTaskId].ucPriority = (U8)priority;
s_atTask[lTaskId].ulStackSize = stackSize;
//s_atTask[lTaskId].eFlag = eFlag;
s_atTask[lTaskId].pfEntry = pfEntry;
s_atTask[lTaskId].pArg = pParam;
s_atTask[lTaskId].usUsed = TRUE;
lRet = pthread_attr_init(&tTaskAttr);
if (lRet != 0)
{
hs_debug_print(ERROR_LEVEL, "Set pthread attr failure.\n");
eRet = HS_ERROR_FAILURE;
goto err_return;
}
lRet = pthread_attr_setstacksize(&tTaskAttr, (int)stackSize);
if (lRet != 0)
{
hs_debug_print(ERROR_LEVEL, "set stack size fail\n");
eRet = HS_ERROR_FAILURE;
goto err_return;
}
lRet = pthread_attr_setdetachstate(&tTaskAttr, PTHREAD_CREATE_DETACHED);
if (lRet != 0)
{
hs_debug_print(ERROR_LEVEL, "set detach state fail\n");
eRet = HS_ERROR_FAILURE;
goto err_return;
}
tSchedParam.sched_priority = priority / 3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -