📄 stacktaskschedule.c
字号:
#include "includes.h"
//-------------------------------------------------------------------------------------------------------
// Scheduler data
STACK_TASK_INFO pStackTasks[STACK_TASK_COUNT];
STACK_TASK_QUEUE pStackTaskQueues[STACK_TASK_PRIORITY_COUNT];
volatile UINT8 totalTaskNum=0;
//-------------------------------------------------------------------------------------------------------
// void stackSchInit(void)
//
// DESCRIPTION:
// Initializes the task pool and the task queues.
//-------------------------------------------------------------------------------------------------------
void stackSchInit(void) {
UINT8 n = 0;
// Initialize the task pool
for (n = 0; n < STACK_TASK_COUNT; n++) {
pStackTasks[n].occupied = FALSE;
pStackTasks[n].taskNumber = n;
}
// Initialize the task queues
for (n = 0; n < STACK_TASK_PRIORITY_COUNT; n++) {
pStackTaskQueues[n].firstTask = NO_TASK;
pStackTaskQueues[n].lastTask = NO_TASK;
pStackTaskQueues[n].taskInProgress = FALSE;
}
} // stackSchInit
//-------------------------------------------------------------------------------------------------------
// UINT8 stackSchReserveTask(void)
//
// DESCRIPTION:
// Reserves a task from the task pool (a task must be reserved before it can be used.
//
// RETURN VALUE:
// UINT8
// The index of the reserved task (to be used with mschAddTask(...))
//-------------------------------------------------------------------------------------------------------
UINT8 stackSchReserveTask(void) {
UINT8 n = 0;
// Find an unused task table entry and reserve it
for (n = 0; n < STACK_TASK_COUNT; n++) {
// DISABLE_GLOBAL_INT();
if (!pStackTasks[n].occupied) {
pStackTasks[n].occupied = TRUE;
// ENABLE_GLOBAL_INT();
return n;
}
// ENABLE_GLOBAL_INT();
}
return NO_TASK;
} // stackSchReserveTask
//-------------------------------------------------------------------------------------------------------
// void stackSchReleaseTask(UINT8 taskNumber)
//
// DESCRIPTION:
// Clears the reservation for an unused task.
//
// ARGUMENTS:
// UINT8 taskNumber
// The number of the task (returned by mschReserveTask())
//-------------------------------------------------------------------------------------------------------
//void stackSchReleaseTask(UINT8 taskNumber) {
//
// if (taskNumber != NO_TASK)
// {
// pStackTasks[taskNumber].occupied = FALSE;
// //_endthreadex();
// }
// totalTaskNum--;
//} // stackSchReleaseTask
void stackSchReleaseTask(UINT8 taskNumber) {
UINT8 i;
if (taskNumber != NO_TASK)
{
for(i=0;i<STACK_TASK_COUNT;i++)
{
if(pStackTasks[i].taskNumber==taskNumber)
break;
else
continue;
}
pStackTasks[taskNumber].occupied = FALSE;
//_endthreadex(pStackTasks[i].threadFuncId);
printf("end some thread function!\n");
//_endthreadex();
}
totalTaskNum--;
} // stackSchReleaseTask
//-------------------------------------------------------------------------------------------------------
// ZBOOL stackSchAddTask(UINT8 taskNumber, UINT8 priority, TFPTR pTaskFunc, WORD taskData)
//
// DESCRIPTION:
// Configures and adds a reserved to task to the desired task queue.
//
// ARGUMENTS:
// UINT8 taskNumber
// The number of the task (returned by mschReserveTask(...))
// UINT8 priority
// MAC_TASK_PRI_HIGHEST, MAC_TASK_PRI_HIGH, MAC_TASK_PRI_MEDIUM or MAC_TASK_PRI_LOW
// TFPTR pTaskFunc
// A pointer to the task function (void TaskFunction(MAC_TASK_INFO *pTask))
// WORD taskData
// Two bytes of task data (usually a pointer to more data)
//
// RETURN VALUE:
// ZBOOL
// The task was added (the task number was OK)
//-------------------------------------------------------------------------------------------------------
//ZBOOL stackSchAddTask(UINT8 taskNumber, UINT8 priority, TFPTR pTaskFunc, WORD taskData) {
// STACK_TASK_INFO *pTask;
// STACK_TASK_QUEUE *pQueue;
//
// // The task index must be valid
// if (taskNumber == NO_TASK) return FALSE;
//
// // DISABLE_GLOBAL_INT();
//
// // Initialize the task
// pTask = &pStackTasks[taskNumber];
// pTask->pTaskFunc = pTaskFunc;
// pTask->taskData = taskData;
// pTask->state = 0;
// pTask->priority = priority;
// pTask->nextTask = NO_TASK;
// pTask->threadFuncId=0; //should be removed if not use thread function!
//
// // Update the queue pointers at the insertion point (last task in the queue)
// pQueue = &pStackTaskQueues[priority];
// if (pQueue->firstTask == NO_TASK) {
// pQueue->firstTask = taskNumber;
// pQueue->lastTask = taskNumber;
// } else {
// pStackTasks[pQueue->lastTask].nextTask = taskNumber;
// pQueue->lastTask = taskNumber;
// }
// // ENABLE_GLOBAL_INT();
// totalTaskNum++;
// return TRUE;
//
//} //stackSchAddTask
ZBOOL stackSchAddTask(UINT8 taskNumber, UINT8 priority, THREADFPTR pTaskFunc, WORD taskData) {
STACK_TASK_INFO *pTask;
STACK_TASK_QUEUE *pQueue;
// The task index must be valid
if (taskNumber == NO_TASK) return FALSE;
// DISABLE_GLOBAL_INT();
// Initialize the task
pTask = &pStackTasks[taskNumber];
pTask->pTaskFunc = pTaskFunc;
pTask->taskData = taskData;
pTask->state = 0;
pTask->priority = priority;
pTask->nextTask = NO_TASK;
pTask->threadFuncId=0; //should be removed if not use thread function!
// Update the queue pointers at the insertion point (last task in the queue)
pQueue = &pStackTaskQueues[priority];
if (pQueue->firstTask == NO_TASK) {
pQueue->firstTask = taskNumber;
pQueue->lastTask = taskNumber;
} else {
pStackTasks[pQueue->lastTask].nextTask = taskNumber;
pQueue->lastTask = taskNumber;
}
// ENABLE_GLOBAL_INT();
totalTaskNum++;
return TRUE;
} //stackSchAddTask
//-------------------------------------------------------------------------------------------------------
// void stackSchRemoveTask(UINT8 priority, BYTE flags)
//
// DESCRIPTION:
// Removes the active task from the specified task queue. This function is usually run from the task
// function when it's about to finished, or before it makes a call to the higher layer.
//
// ARGUMENTS:
// UINT8 priority
// The priority of the task to be removed
// BYTE flags
// MSCH_KEEP_TASK_RESERVED_BM: The task is not released back to the task pool
// MSCH_KEEP_TASK_IN_PROGRESS_BM: Does not allow other tasks of the same priority to run before
// the current task has finished (even if it is removed from the queue).
//-------------------------------------------------------------------------------------------------------
void stackSchRemoveTask(UINT8 priority, ZBOOL flags) {
UINT8 taskNumber;
STACK_TASK_INFO *pTask;
STACK_TASK_QUEUE *pQueue = &pStackTaskQueues[priority];
// DISABLE_GLOBAL_INT();
// Get the task table index number, and make sure that it is valid
taskNumber = pQueue->firstTask;
// Flag-dependent operations
pTask = &pStackTasks[taskNumber];
printf("pTask->nextTask; is %d!\n",pTask->nextTask);
if (!(flags & STACK_SCH_KEEP_TASK_RESERVED_BM))
pTask->occupied = FALSE;
if (!(flags & STACK_SCH_KEEP_TASK_IN_PROGRESS_BM))
pQueue->taskInProgress = FALSE;
// Move the "first task" pointer one step forward
pQueue->firstTask = pTask->nextTask;
if (pQueue->firstTask == NO_TASK) {
pQueue->lastTask = NO_TASK;
} else if (pQueue->lastTask == taskNumber) {
pQueue->lastTask = NO_TASK;
}
// ENABLE_GLOBAL_INT();
} // stackSchRemoveTask
//-------------------------------------------------------------------------------------------------------
// void stackSchRescheduleTask(MAC_TASK_INFO *pTask, UINT8 state)
//
// DESCRIPTION:
// Removes the currently running task from a queue, and adds it to the back of the same queue,
// possibly behind a number of waiting tasks which now will be run first.
//
// ARGUMENTS:
// MAC_TASK_INFO *pTask
// The task to be rescheduled
// UINT8 state
// The task state when run after the rescheduling.
//-------------------------------------------------------------------------------------------------------
void stackSchRescheduleTask(STACK_TASK_INFO *pTask, UINT8 state) {
stackSchRemoveTask(pTask->priority, STACK_SCH_KEEP_TASK_RESERVED_BM | STACK_SCH_KEEP_TASK_IN_PROGRESS_BM);
stackSchAddTask(pTask->taskNumber, pTask->priority, pTask->pTaskFunc, pTask->taskData);
pTask->state = state;
} // stackSchRescheduleTask
//-------------------------------------------------------------------------------------------------------
// void stackSchDoTask(void)
//
// DESCRIPTION:
// Used by the timer module to start or resume a task at every backoff slot boundary
//
// ARGUMENTS:
// None
//
// RETURN VALUE:
// None
//-------------------------------------------------------------------------------------------------------
//void stackSchDoTask(void) {
// UINT8 taskNumber=0;
// STACK_TASK_QUEUE *pQueue=NULL;
// STACK_TASK_INFO *pTask=NULL;
// TFPTR taskFunc;
//
// // Execute tasks by priority
// UINT8 n = STACK_TASK_PRIORITY_COUNT - 1;
// //printf("In the stackSchDoTask() function!\n");
// do {
//
// // Stop if there's a task in progress
// pQueue = &pStackTaskQueues[n];
// if (pQueue->taskInProgress)
// return;
//
// // If there's a valid task in the queue, then start it
//// DISABLE_GLOBAL_INT();
// taskNumber = pQueue->firstTask;
// if (taskNumber != NO_TASK) {
// pQueue->taskInProgress = TRUE;
// pTask = &pStackTasks[taskNumber];
//// ENABLE_GLOBAL_INT(); --It's important!
// ///((TFPTR) pTask->pTaskFunc)(pTask);
// // ((TFPTR)(pTask->pTaskFunc))(pTask);
// taskFunc=(TFPTR)(pTask->pTaskFunc);
// (*taskFunc)(pTask);
// pQueue->taskInProgress = FALSE;
// return;
// }
// // ENABLE_GLOBAL_INT();
// } while (n--);
//
//} // stackSchDoTask
//typedef void (*TFPTR)(STACK_TASK_INFO *pTask);
void stackSchDoTask(void) {
UINT8 taskNumber=0;
UINT8 n;
//TFPTR taskFunc;
THREADFPTR taskFunc;
STACK_TASK_QUEUE *pQueue=NULL;
STACK_TASK_INFO *pTask=NULL;
// Execute tasks by priority
n= STACK_TASK_PRIORITY_COUNT - 1;
//printf("In the stackSchDoTask() function!\n");
do {
// Stop if there's a task in progress
pQueue = &pStackTaskQueues[n];
if (pQueue->taskInProgress)
//return;
continue;
// If there's a valid task in the queue, then start it
// DISABLE_GLOBAL_INT();
taskNumber = pQueue->firstTask;
if (taskNumber != NO_TASK) {
pQueue->taskInProgress = TRUE;
pTask = &pStackTasks[taskNumber];
// ENABLE_GLOBAL_INT(); --It's important!
///((TFPTR) pTask->pTaskFunc)(pTask);
// ((TFPTR)(pTask->pTaskFunc))(pTask);
// taskFunc=(THREADFPTR)(pTask->pTaskFunc);
pTask->threadFuncId=_beginthreadex(NULL,0,taskFunc,&(pTask->taskNumber),1,NULL);
printf("begin one thread!\n");
//because the task function need parameter,and the parameter of task function is STACK_TASK_INFO *;
//(*taskFunc)(pTask);
// pQueue->taskInProgress = FALSE;
return;
}
// ENABLE_GLOBAL_INT();
} while (n--);
} // stackSchDoTask
//the following is the definition style of the thread function
//unsigned _stdcall taskScan(void *p);
//
////线程函
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -