该问题是因为使用有些头文件的时候,前面必须包含windows.h头文件。如使用MMSystem.h时。
#ifndef SETJMP_TEST_H
#define SETJMP_TEST_H
#include<setjmp.h>
#ifdef _cplusplus
extern "c"{
#endif
#define MAXTASK 20/*最大任务个数*/
#define TASKID unsigned char
typedef void(*FUNCTASK)(void);
enum TaskStatus {
READY=0,/***刚刚创建等待cpu的任务**/
RUNING,/******任务正在运行*******/
SUSPEND,/******任务被挂起*******/
DELAY,/******任务延时*******/
AWAKENING,/****任务被唤起******/
DELETED/******任务被删除*******/
};
typedef struct MyTask_TcbTest
{
jmp_buf Task_Env;/**任务环境**/
TaskStatus status;/**任务状态**/
FUNCTASK TaskFunction;/**任务函数**/
int sleepTimeCount;/**任务延时时间(ms)**/
}mytasktcb,*Pmytasktcp;
typedef struct Task_Manage
{
mytasktcb taskTcp[MAXTASK];
int curRunTaskID;
}task_manage,*Ptask_manage;
extern task_manage GloablMyTaskManage;
extern void InitTaskManage(task_manage mytask_manage);
extern TASKID CreatMyTask(FUNCTASK func,unsigned char priority);/****创建任务***/
extern int DestoryMyTask(TASKID taskid);/****销毁任务******/
extern void MyTaskSleep(unsigned int mstime);/*****任务自身让出CPU*****/
extern void MyTaskSuspend();/****任务自身挂起****/
extern int MyTaskResume(TASKID taskid);/*****唤醒任务*****/
TASKID FindNextReadTask();
#ifdef _cplusplus
}
#endif
#endif
/×××××××××××××××××××××××××××××××××××××/
#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;
}
}
}