-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added priority queue to task scheduler.
- Loading branch information
1 parent
ab82708
commit dab443b
Showing
4 changed files
with
153 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "PriorityQueue.h" | ||
|
||
#ifndef NULL | ||
#define NULL 0 | ||
#endif | ||
|
||
//forward declarations | ||
void Heapify(PriorityQueue *queue, int index); | ||
void SwapItems(PriorityQueue *queue, int indexA, int indexB); | ||
int ParentIndex(int index); | ||
|
||
void InitializeQueue(PriorityQueue *queue){ | ||
uint8_t i = 0; | ||
queue->nTasks = 0; | ||
for(i = 0; i < MAX_TASKS; i++){ | ||
queue->pendingTasks[i] = NULL; | ||
} | ||
} | ||
|
||
void AddTaskToQueue(PriorityQueue *queue, Task* task){ | ||
if(!IsQueueFull(queue)){ | ||
uint8_t i, parent; | ||
|
||
//Add task to queue | ||
queue->pendingTasks[queue->nTasks] = task; | ||
queue->nTasks++; | ||
|
||
//Fix min heap property of queue | ||
i = queue->nTasks - 1; | ||
parent = ParentIndex(i); | ||
while(i != 0 && queue->pendingTasks[parent]->priority > queue->pendingTasks[i]->priority){ | ||
SwapItems(queue, i, parent); | ||
i = parent; | ||
parent = ParentIndex(i); | ||
} | ||
} | ||
else{ | ||
//Do nothing, queue is full | ||
} | ||
} | ||
|
||
void RunNextTask(PriorityQueue *queue){ | ||
if(queue->nTasks > 0){ | ||
queue->pendingTasks[0]->pCallback(); | ||
|
||
//Remove the task | ||
SwapItems(queue, 0, queue->nTasks - 1); | ||
queue->pendingTasks[queue->nTasks - 1] = NULL; | ||
queue->nTasks--; | ||
|
||
//Reorder the queue | ||
Heapify(queue, 0); | ||
} | ||
else{ | ||
//No tasks to run | ||
} | ||
} | ||
|
||
void RunAllTasks(PriorityQueue *queue){ | ||
while(queue->nTasks > 0){ | ||
RunNextTask(queue); | ||
} | ||
} | ||
|
||
bool IsQueueFull(PriorityQueue *queue){ | ||
if(queue->nTasks >= MAX_TASKS){ | ||
return true; | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
|
||
//Fixes the min heap when extracting a node | ||
void Heapify(PriorityQueue *queue, int index){ | ||
uint8_t left = 2*index + 1; | ||
uint8_t right = 2*index + 2; | ||
uint8_t smallest = index; | ||
|
||
//If the left task has lower priority, set it as the lowest | ||
if(left < queue->nTasks && queue->pendingTasks[left]->priority < queue->pendingTasks[smallest]->priority){ | ||
smallest = left; | ||
} | ||
|
||
//If the right task has lower priority, set it as the lowest | ||
if(right < queue->nTasks && queue->pendingTasks[right]->priority < queue->pendingTasks[smallest]->priority){ | ||
smallest = right; | ||
} | ||
|
||
//If the passed index was not the lowest, swap the two and repeat | ||
if(smallest != index){ | ||
SwapItems(queue, smallest, index); | ||
Heapify(queue, smallest); | ||
} | ||
} | ||
|
||
void SwapItems(PriorityQueue *queue, int indexA, int indexB){ | ||
Task* tempTaskPointer = queue->pendingTasks[indexB]; | ||
queue->pendingTasks[indexB] = queue->pendingTasks[indexA]; | ||
queue->pendingTasks[indexA] = tempTaskPointer; | ||
} | ||
|
||
int ParentIndex(int index){ | ||
return (index - 1) / 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef TIVA_TASK_SCHEDULER_PRIORITY_QUEUE_H | ||
#define TIVA_TASK_SCHEDULER_PRIORITY_QUEUE_H | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
|
||
#include "TaskScheduler.h" | ||
|
||
#define MAX_TASKS 15 //Must be an order of 2 - 1 (i.e 1, 3, 7, 15, 31) | ||
|
||
|
||
struct PriorityQueue_tag; | ||
typedef struct PriorityQueue_tag PriorityQueue; | ||
|
||
struct PriorityQueue_tag{ | ||
int nTasks; | ||
Task* pendingTasks[MAX_TASKS]; | ||
}; | ||
|
||
void InitializeQueue(PriorityQueue *queue); | ||
void AddTaskToQueue(PriorityQueue *queue, Task* task); | ||
void RunNextTask(PriorityQueue *queue); | ||
void RunAllTasks(PriorityQueue *queue); | ||
bool IsQueueFull(PriorityQueue *queue); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters