This problem occurs because some header files must contain the windows. h header files. For example, when mmsystem. h is used.
# Ifndef setjmp_test_h
# Define setjmp_test_h
# Include <setjmp. h>
# Ifdef _ cplusplus
Extern "C "{
# Endif
# Define maxtask 20/* Maximum number of tasks */
# Define taskid unsigned char
Typedef void (* functask) (void );
Enum taskstatus {
Ready = 0,/*** create a task waiting for CPU **/
Runing,/******* the task is running *******/
Suspend,/******* the task is suspended *******/
Delay,/******* task latency *******/
Awakening,/***** the task is aroused ******/
Deleted/****** the task is deleted *******/
};
Typedef struct mytask_tcbtest
{
Jmp_buf task_env;/** task environment **/
Taskstatus status;/** task status **/
Functask taskfunction;/** task function **/
Int sleeptimecount;/** task latency (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);/***** create a task ***/
Extern int destorymytask (taskid);/***** destroy task ******/
Extern void mytasksleep (unsigned int mstime);/***** the task itself gets the CPU *****/
Extern void mytasksuspend ();/***** the task suspends itself ****/
Extern int mytaskresume (taskid);/****** wake up a task *****/
Taskid findnextreadtask ();
# Ifdef _ cplusplus
}
# Endif
# Endif
/Xxx/
# 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 ()/***** find the next task that should be run ******/
{
Unsigned char I;
For (I = 0; I <= MAXTASK-1; I ++)/** backward Lookup (low priority )**/
{
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 ()/*** initialize the task manager and use the main function as the task with the lowest priority ****/
{
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)/***** create a task ***/
{
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)/***** destroy a task ******/
{
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)/*** call and delay task, use Jump ***/
{
Longjmp (gloablmytaskmanage. tasktcp [temp]. task_env, 0 );
}
Else if (ready = gloablmytaskmanage. tasktcp [temp]. Status)/** the ready task is called for the first time, and its function ***/
{
(Gloablmytaskmanage. tasktcp [temp]. taskfunction )();
}
Else
{
Printf ("temp = % d/N", temp );
Printf ("error/N ");
}
}
Void mytasksuspend ()/***** the task is suspended ****/
{
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)/***** wake up task *****/
{
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;
}
}
}