在程序猿界混迹了多年,一直有个苦恼的问题,不管是什么样子的编程,最早的汇编,后来C语言,又接触了JAVA,始终有个念头就是整体程序架构问题,不管是你自己用不用操作系统,都要有一套很出色的编程模板。当然自己能搞出来的绝对是要花费大把时间,然而对于工作而言时间确实是非常奢侈的东西。那么,另一种方法就出现了,“抄袭”,但抄袭也是要自己有两把刷子的,而且要有广阔的眼界,有了眼界你才能找到多种多样的模式,模板。废话少说,先上个LINUX下编程的系统模板,这个是来自TI的ipnc中的对Linux下的系统编程。也当自己的记录笔记。
应用方法:
int AVSERVER_tskMain(struct OSA_TskHndl *pTsk, OSA_MsgHndl *pMsg, Uint32 curState )
{
int status;
Uint16 cmd = OSA_msgGetCmd(pMsg);
#ifdef AVSERVER_DEBUG_MAIN_THR
OSA_printf(" AVSERVER MAIN: Recevied CMD = 0x%04x, state = 0x%04x\n", cmd, curState);
#endif
switch(cmd) {
case AVSERVER_MAIN_CMD_START:
if(curState==AVSERVER_MAIN_STATE_IDLE) {
status = AVSERVER_tskStart(pTsk, pMsg);
if(status!=OSA_SOK)
AVSERVER_tskStop(pTsk, pMsg);
}
OSA_tskAckOrFreeMsg(pMsg, OSA_SOK);
break;
case AVSERVER_MAIN_CMD_STOP:
if(curState==AVSERVER_MAIN_STATE_RUNNING) {
AVSERVER_tskStop(pTsk, pMsg);
}
OSA_tskAckOrFreeMsg(pMsg, OSA_SOK);
break;
}
return OSA_SOK;
}
int AVSERVER_mainCreate()
{
int status;
status = OSA_mbxCreate(&gAVSERVER_ctrl.uiMbx);
if(status!=OSA_SOK) {
OSA_ERROR("OSA_mbxCreate()\n");
return status;
}
status = OSA_tskCreate( &gAVSERVER_ctrl.mainTsk, AVSERVER_tskMain, AVSERVER_MAIN_THR_PRI, AVSERVER_MAIN_STACK_SIZE, AVSERVER_MAIN_STATE_IDLE);
if(status!=OSA_SOK) {
OSA_ERROR("OSA_tskCreate()\n");
return status;
}
return status;
}
int AVSERVER_mainDelete()
{
int status;
status = OSA_tskDelete(&gAVSERVER_ctrl.mainTsk);
status |= OSA_mbxDelete(&gAVSERVER_ctrl.uiMbx);
if(status!=OSA_SOK)
OSA_ERROR("\n");
return status;
}
启动:
status |= OSA_tskSendMsg(&gVIDEO_ctrl.swosdTsk, pTsk, AVSERVER_CMD_DELETE, NULL, OSA_MBX_WAIT_ACK);
持续更新中。。。。
一个线程的生命周期,招式
/*
* gioInputMainThr.cpp
*
* Created on: Jun 30, 2019
* Author: CaRn
*/
extern "C"{
#include
#include
#include
#include
#include
#include
#include
}
#include "../main_app/gioInputMainThr.h"
#include "../main_app/appServer.h"
#include "interface/Def.h"
//#define ELEGIO_DEVPAT "/dev/gioCtl_device"
//
//int mEleGioFd;
int gioInputTskCreate(){//任务创建,操作流程,运气
int status = OSA_SOK;
// mEleGioFd = open(ELEGIO_DEVPAT, O_RDWR);
// if(mEleGioFd < 0){
// OSA_ERROR(">>>>FLOOR: Open ELEGIO_DEVPAT device: %s failed\n", ELEGIO_DEVPAT);
// return OSA_EFAIL;
// }
return status;
}
int gioInputTskStart(){//任务初始化,可以初始化首次运行参数 ,起手式
int status = OSA_SOK;
return status;
}
int gioInputTskRun(){//任务实际运行主体,数据处理主体, 出手式
int status = OSA_SOK;
return status;
}
int gioInputTskDelete(){//退出线程任务做的收尾工作, 收手式
int status = OSA_SOK;
// if(mEleGioFd > 0){
// close(mEleGioFd);
// }
return status;
}
int gioInputTskMain(struct OSA_TskHndl *pTsk, OSA_MsgHndl *pMsg, Uint32 curState){//主线程运行
int status;
Bool done= FALSE, ackMsg = FALSE;
Uint16 cmd = OSA_msgGetCmd(pMsg);
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
OSA_printf(" GIOINPUT: START Recevied CMD = 0x%04x\n", cmd);
#endif
if(cmd!=APPSERVER_CMD_CREATE) {
OSA_tskAckOrFreeMsg(pMsg, OSA_SOK);
return OSA_SOK;
}
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
OSA_printf(" GIOINPUT: Create...\n");
#endif
status = gioInputTskCreate();
OSA_tskAckOrFreeMsg(pMsg, status);
if(status !=OSA_SOK) {
OSA_ERROR(">>>>gioInputTskCreate()");
return OSA_SOK;
}
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
OSA_printf(" GIOINPUT: Create...DONE\n");
#endif
status = OSA_tskWaitMsg(pTsk, &pMsg);
if(status!=OSA_SOK)
return OSA_SOK;
cmd = OSA_msgGetCmd(pMsg);
if(cmd==APPSERVER_CMD_DELETE) {
done = TRUE;
ackMsg = TRUE;
} else {
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
OSA_printf(" GIOINPUT: Start...\n");
#endif
gioInputTskStart();
OSA_tskAckOrFreeMsg(pMsg, OSA_SOK);
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
OSA_printf(" GIOINPUT: Start...DONE\n");
#endif
}
while(!done) {
gioInputTskRun();
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
// OSA_printf(" GIOINPUT: gioInputTskRun\n");
#endif
OSA_waitMsecs(2000);
status = OSA_tskCheckMsg(pTsk, &pMsg);
if(status !=OSA_SOK)
continue;
cmd = OSA_msgGetCmd(pMsg);
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
OSA_printf(" GIOINPUT: Recevied CMD = 0x%04x\n", cmd);
#endif
switch(cmd) {
case APPSERVER_CMD_DELETE:
done = TRUE;
ackMsg = TRUE;
break;
default:
OSA_tskAckOrFreeMsg(pMsg, OSA_SOK);
break;
}
}
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
OSA_printf("GIOINPUT: Delete...\n");
#endif
gioInputTskDelete();
if(ackMsg)
OSA_tskAckOrFreeMsg(pMsg, OSA_SOK);
#ifdef APPSERVER_DEBUG_GIOINPUTMAIN_THR
OSA_printf("GIOINPUT: Delete...DONE\n");
#endif
return OSA_SOK;
}
int DEV_gioInputCreate(){//线程创建
int status;
status = OSA_tskCreate( &gCAPTURE_Ctl.gioInputTsk, gioInputTskMain, GIOINPUTMAIN_THR_PRI, GIOINPUTMAIN_STACK_SIZE, 0);
if(status!=OSA_SOK) {
OSA_ERROR(">>>>DEV_gioInputCreate()\n");
return status;
}
return status;
}
int DEV_gioInputDelete(){//线程删除退出
int status;
status = OSA_tskDelete( &gCAPTURE_Ctl.gioInputTsk );
if(status!=OSA_SOK) {
OSA_ERROR(">>>>DEV_gioInputDelete()\n");
}
return status;
}
关联4412_V4L2cam_RTMP_工程源码