Skip to content

Commit

Permalink
Added code documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dam7633 committed Mar 25, 2013
1 parent 43cde99 commit 9b68ff8
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 75 deletions.
4 changes: 4 additions & 0 deletions EDFScheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* EDFScheduler.cpp
*
* Concrete subclass of a Scheduler implementing the
* Earliest Deadline First scheduling algorithm.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/
Expand All @@ -14,6 +17,7 @@ EDFScheduler::~EDFScheduler() {
}

void EDFScheduler::scheduleTasks() {
// Initialize variables for determining the earliest deadline task.
int earliestIdx = -1;
long earliestDeadline = LONG_MAX;
long currentEarliestDeadline;
Expand Down
8 changes: 6 additions & 2 deletions EDFScheduler.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* EDFScheduler.h
*
* Concrete subclass of a Scheduler implementing the
* Earliest Deadline First scheduling algorithm.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/
Expand All @@ -9,14 +12,15 @@
#define EDFSCHEDULER_H_

#include "Scheduler.h"
#include "Task.h"
#include <climits>

class EDFScheduler: public Scheduler {
public:
EDFScheduler();
virtual ~EDFScheduler();

/**
* Template method implementing the EDF algorithm.
*/
void scheduleTasks();
};

Expand Down
17 changes: 17 additions & 0 deletions RMAScheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* RMAScheduler.cpp
*
* Concrete subclass of a Scheduler implementing the
* Rate-Monotonic scheduling algorithm.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/
Expand All @@ -10,6 +13,14 @@
#include <cassert>
#include "Task.h"

/**
* Comparator function for sorting tasks based on their
* period.
*
* @param t1 - first task
* @param t2 - second task
* @return period of t1 < period of t2
*/
bool comparePeriod(Task *t1, Task *t2) {
return t1->getPeriod() < t2->getPeriod();
}
Expand All @@ -21,16 +32,22 @@ RMAScheduler::~RMAScheduler() {
}

void RMAScheduler::scheduleTasks() {
// Since RMA is a fixed-priority scheduling algorithm, we
// only need to perform scheduling at the beginning.
if (!taskPrioritiesSet) {
// Assert that there are enough priorities for the task set.
assert(taskSet.size() < TP_SCHEDULER);

// Sort the tasks by the smallest period.
std::sort(taskSet.begin(), taskSet.end(), comparePeriod);

// Assign priorities to tasks in a descending order from TP_SCHEDULER.
for (std::size_t i = 0; i < taskSet.size(); i++) {
Task *task = taskSet[i];
task->setPriority(TP_SCHEDULER - (i + 1));
}

// Set this flag so that scheduling is only done once.
taskPrioritiesSet = true;
}
}
18 changes: 18 additions & 0 deletions RMAScheduler.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* RMAScheduler.h
*
* Concrete subclass of a Scheduler implementing the
* Rate-Monotonic scheduling algorithm.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/
Expand All @@ -15,9 +18,24 @@ class RMAScheduler: public Scheduler {
RMAScheduler();
virtual ~RMAScheduler();

/**
* Template method implementing the RMA algorithm.
*
* This scheduler fully utilizes the QNX fixed-priority
* scheduler by assigning descending priorities starting
* from TP_SCHEDULER.
*
* Because of this, RMA can only be run with a task set
* smaller in size than TP_SCHEDULER, and will cause an
* assertion failure otherwise.
*/
void scheduleTasks();

private:
/**
* Flag used to only schedule tasks on the first run,
* since RMA is a fixed-priority scheduler.
*/
bool taskPrioritiesSet;
};

Expand Down
5 changes: 5 additions & 0 deletions SCTScheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/*
* SCTScheduler.cpp
*
* Concrete subclass of a Scheduler implementing the
* Shortest Completion Time scheduling algorithm.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/

#include "SCTScheduler.h"
#include "Task.h"

SCTScheduler::SCTScheduler() {
}

SCTScheduler::~SCTScheduler() {
}

void SCTScheduler::scheduleTasks() {
int shortestIdx = -1;
int shortestTimeLeft = INT_MAX;
Expand Down
7 changes: 6 additions & 1 deletion SCTScheduler.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* SCTScheduler.h
*
* Concrete subclass of a Scheduler implementing the
* Shortest Completion Time scheduling algorithm.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/
Expand All @@ -9,13 +12,15 @@
#define SCTSCHEDULER_H_

#include "Scheduler.h"
#include "Task.h"

class SCTScheduler: public Scheduler {
public:
SCTScheduler();
virtual ~SCTScheduler();

/**
* Template method implementing the SCT algorithm.
*/
void scheduleTasks();
};

Expand Down
36 changes: 28 additions & 8 deletions Scheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
/*
* Scheduler.cpp
*
* Threaded scheduler base class, which is responsible for
* accepting a task set, scheduling it, and running it.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/

#include <time.h>
#include <sys/siginfo.h>
#include <sys/trace.h>
#include "Scheduler.h"
#include "Task.h"
#include "SchedulingAlgorithms.h"

Scheduler::Scheduler() :
Thread("Scheduler"),
tickCounter(0) {

Thread("Scheduler") {
sem_init(&scheduleSem, 0, 0);
}

Expand All @@ -25,25 +29,37 @@ Scheduler::~Scheduler() {
sem_destroy(&scheduleSem);
}

void Scheduler::createTask(std::string name, int computeTime, int period,
int deadline) {
void Scheduler::createTask(std::string name, int computeTime, int period, int deadline) {
taskSet.push_back(new Task(name, computeTime, period, deadline, &scheduleSem));
}

void* Scheduler::run() {
// The scheduler must run at its priority - the
// highest in the test fixture.
this->setPriority(TP_SCHEDULER);

// calibrate nanospin_ns before the tasks do it.
// Calibrate nanospin_ns before the tasks do it.
nanospin_calibrate(0);

//When we first are told to start, start the tasks...
// When we first are told to start, start the tasks...
for(std::size_t i; i < taskSet.size(); i++) {

// Initialize task state so that all are ready to run.
taskSet[i]->setState(TP_READY);

// Create the pthread for the task.
taskSet[i]->start();

// taskSet[i] will post on scheduleSem.
// This ensures that all task threads are created before
// they are scheduled.
sem_wait(&scheduleSem);

// Lower the priority of the task to not running.
taskSet[i]->setPriority(TP_READY);
}

// Kick off the deadline timer for each task.
for(std::size_t i; i < taskSet.size(); i++) {
taskSet[i]->startDeadlineTimer();
}
Expand All @@ -52,18 +68,22 @@ void* Scheduler::run() {
TraceEvent(_NTO_TRACE_INSERTUSRSTREVENT, TRACE_EVENT_SCHEDULING_START, "Scheduling");
#endif

// Perform the first scheduling.
scheduleTasks();

#if TRACE_EVENT_LOG_DEBUG
TraceEvent(_NTO_TRACE_INSERTUSRSTREVENT, TRACE_EVENT_SCHEDULING_END, "Done Scheduling");
#endif

// Release all of the tasks.
for (std::size_t i = 0; i < taskSet.size(); i++) {
Task *task = taskSet[i];
task->postSem();
}

//then perform the scheduling
// Continue to perform scheduling when:
// 1. A Task completes its computation.
// 2. A Task's deadline timer puts it into a ready state.
while (!killThread) {
sem_wait(&scheduleSem);
#if TRACE_EVENT_LOG_DEBUG
Expand Down
46 changes: 28 additions & 18 deletions Scheduler.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* Scheduler.h
*
* Threaded scheduler base class, which is responsible for
* accepting a task set, scheduling it, and running it.
*
* Created on: Mar 13, 2013
* Author: dam7633
*/
Expand All @@ -11,38 +14,45 @@
#include <string>
#include <vector>
#include <semaphore.h>
#include "Thread.h"
#include <time.h>
#include <sys/siginfo.h>
#include <sys/trace.h>

// The frequency of the timer tick
#define TIMER_PERIOD_NANO (10000000)
#define TIMER_PERIOD_SEC (0)

class Task;

typedef std::vector<Task*> TaskSet;
#include "Task.h"

class Scheduler : public Thread {
public:
Scheduler();
virtual ~Scheduler();

/**
* Pure virtual function requiring subclasses to implement
* how to schedule task sets.
*/
virtual void scheduleTasks() = 0;

/**
* Creates a task in the scheduler's task set with the specified attributes.
*
* @param name - user-specified name of the task.
* @param computeTime - user-specified time which the task busy-waits for.
* @param period - user-specified interval of when the task must run.
* @param deadline - user-specified interval of when the task must finish.
*/
void createTask(std::string name, int computeTime, int period, int deadline);

/**
* Implementation of the Thread class's run method.
*/
void* run();

// static void tick(union sigval sig);

std::vector<Task*> *getTaskSet();

protected:
TaskSet taskSet;
/**
* Vector of the tasks for which this scheduler must schedule.
*/
std::vector<Task*> taskSet;

unsigned int tickCounter;
private:
/**
* Semaphore for blocking the execution of the scheduler while
* the scheduled task is running.
*/
sem_t scheduleSem;
};

Expand Down
Loading

0 comments on commit 9b68ff8

Please sign in to comment.