/*******filename:xiaohw*****
author:xiaohw
Datetime:20090324 21:58
***********/
#include<windows.h>
#include "MMSystem.h"
#include "setjmptest.h"
#include<stdio.h>
#define TIMER_INTERVAL 10
#define TIMER_ACCURACY 1
task_manage GloablMyTaskManage;
MMRESULT g_timer_wid;
void main();
void CALLBACK mtimer_task(UINT wTimerID,UINT nMsg,DWORD dwUser,DWORD dw1,DWORD dw2)
{
unsigned char i;
for(i=0;i<=MAXTASK-1;i++)
{
if(DELAY==GloablMyTaskManage.taskTcp[i].status)
{
GloablMyTaskManage.taskTcp[i].sleepTimeCount= (GloablMyTaskManage.taskTcp[i].sleepTimeCount-10)>0 ?(GloablMyTaskManage.taskTcp[i].sleepTimeCount-10):0;
}
}
}
void mtimer_sys_init()
{
g_timer_wid= timeSetEvent(TIMER_INTERVAL, TIMER_ACCURACY, mtimer_task, NULL, TIME_PERIODIC );
}
TASKID FindNextReadTask()/****查找下个应该运行的任务******/
{
unsigned char i;
for(i=0;i<=MAXTASK-1;i++)/**向后查找(优先级低)**/
{
if(READY==GloablMyTaskManage.taskTcp[i].status||AWAKENING==GloablMyTaskManage.taskTcp[i].status)
{
return i;
}
if((DELAY==GloablMyTaskManage.taskTcp[i].status)&&(0==GloablMyTaskManage.taskTcp[i].sleepTimeCount))
{
return i;
}
}
return MAXTASK-1;
}
void InitTaskManage()/***初始化任务管理器,将main函数作为最低优先级的任务****/
{
int i=0;
GloablMyTaskManage.taskTcp[MAXTASK-1].status=READY;
GloablMyTaskManage.taskTcp[MAXTASK-1].sleepTimeCount=0;
GloablMyTaskManage.taskTcp[MAXTASK-1].TaskFunction=main;
for(;i<=MAXTASK-2;i++)
{
GloablMyTaskManage.taskTcp[i].status=DELETED;
GloablMyTaskManage.taskTcp[i].sleepTimeCount=0;
GloablMyTaskManage.taskTcp[i].TaskFunction=NULL;
}
GloablMyTaskManage.curRunTaskID=MAXTASK-1;
}
TASKID CreatMyTask(FUNCTASK fun,unsigned char priority)/****创建任务***/
{
if(priority>=(MAXTASK-1)||priority<0)
{
return -1;
}
if(GloablMyTaskManage.taskTcp[priority].status!=DELETED)
{
return -1;
}
GloablMyTaskManage.taskTcp[priority].sleepTimeCount=0;
GloablMyTaskManage.taskTcp[priority].status=READY;
GloablMyTaskManage.taskTcp[priority].TaskFunction=fun;
return priority;
}
int DestoryMyTask(TASKID taskid)/****销毁任务******/
{
if(MAXTASK-1<taskid||taskid<0)
{
return -1;
}
GloablMyTaskManage.taskTcp[taskid].status=DELETED;
GloablMyTaskManage.taskTcp[taskid].sleepTimeCount=0;
GloablMyTaskManage.taskTcp[taskid].TaskFunction=NULL;
return 1;
}
void MyTaskSleep(unsigned int mstime)
{
unsigned char temp;
GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].status=DELAY;
GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].sleepTimeCount=mstime;
temp=FindNextReadTask();
GloablMyTaskManage.curRunTaskID=temp;
if(DELAY==GloablMyTaskManage.taskTcp[temp].status||AWAKENING==GloablMyTaskManage.taskTcp[temp].status)/***唤起和延时的任务,使用跳转***/
{
longjmp(GloablMyTaskManage.taskTcp[temp].Task_Env,0);
}
else if(READY==GloablMyTaskManage.taskTcp[temp].status)/**就绪的任务是第一次调用,直接执行他的功能函数***/
{
(GloablMyTaskManage.taskTcp[temp].TaskFunction)();
}
else
{
printf("temp=%d\n",temp);
printf("error\n");
}
}
void MyTaskSuspend()/****任务自身挂起****/
{
unsigned char temp;
GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].status=SUSPEND;
temp=FindNextReadTask();
GloablMyTaskManage.curRunTaskID=temp;
if(DELAY==GloablMyTaskManage.taskTcp[temp].status)
{
longjmp(GloablMyTaskManage.taskTcp[temp].Task_Env,0);
}
else if(READY==GloablMyTaskManage.taskTcp[temp].status)
{
printf("exec\n");
(GloablMyTaskManage.taskTcp[temp].TaskFunction)();
}
else
{
printf("temp=%d\n",temp);
printf("error\n");
}
}
int MyTaskResume(TASKID taskid)/*****唤醒任务*****/
{
if(SUSPEND==GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].status)
{
GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].status=AWAKENING;
return 1;
}
return -1;
}
void task1()
{
while (1) {
if(setjmp(GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].Task_Env)==0)
{
MyTaskSleep(100);
}
else
{
printf("task1\n");
continue;
}
}
}
void task2()
{
while (1) {
if(setjmp(GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].Task_Env)==0)
{
MyTaskSleep(100);
}
else
{
printf("task2\n");
continue;
}
}
}
void task3()
{
while (1) {
if(setjmp(GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].Task_Env)==0)
{
MyTaskSleep(100);
}
else
{
printf("task3\n");
continue;
}
}
}
void main()
{
TASKID task1ID,task2ID,task3ID;
InitTaskManage();
mtimer_sys_init();
task1ID=CreatMyTask(task1,3);
task2ID=CreatMyTask(task2,2);
task3ID=CreatMyTask(task3,5);
while (1) {
if(setjmp(GloablMyTaskManage.taskTcp[GloablMyTaskManage.curRunTaskID].Task_Env)==0)
{
MyTaskSleep(100);
}
else
{
continue;
}
}
}