FreeNOS
Scheduler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <Log.h>
19 #include "Kernel.h"
20 #include "Scheduler.h"
21 
23 {
24  DEBUG("");
25 }
26 
28 {
29  return m_queue.count();
30 }
31 
33 {
34  if (proc->getState() != Process::Ready && !ignoreState)
35  {
36  ERROR("process ID " << proc->getID() << " not in Ready state");
37  return InvalidArgument;
38  }
39 
40  m_queue.push(proc);
41  return Success;
42 }
43 
45 {
46  if (proc->getState() == Process::Ready && !ignoreState)
47  {
48  ERROR("process ID " << proc->getID() << " is in Ready state");
49  return InvalidArgument;
50  }
51 
52  Size count = m_queue.count();
53 
54  // Traverse the Queue to remove the Process
55  for (Size i = 0; i < count; i++)
56  {
57  Process *p = m_queue.pop();
58 
59  if (p == proc)
60  return Success;
61  else
62  m_queue.push(p);
63  }
64 
65  FATAL("process ID " << proc->getID() << " is not in the schedule");
66  return InvalidArgument;
67 }
68 
70 {
71  if (m_queue.count() > 0)
72  {
73  Process *p = m_queue.pop();
74  m_queue.push(p);
75 
76  return p;
77  }
78 
79  return (Process *) NULL;
80 }
Scheduler::count
Size count() const
Get number of processes on the schedule.
Definition: Scheduler.cpp:27
Kernel.h
Scheduler::Success
@ Success
Definition: Scheduler.h:45
Process::getState
State getState() const
Retrieves the current state.
Definition: Process.cpp:80
Process
Represents a process which may run on the host.
Definition: Process.h:44
Scheduler::InvalidArgument
@ InvalidArgument
Definition: Scheduler.h:46
Process::Ready
@ Ready
Definition: Process.h:68
Process::getID
ProcessID getID() const
Retrieve our ID number.
Definition: Process.cpp:60
Scheduler::Scheduler
Scheduler()
Constructor function.
Definition: Scheduler.cpp:22
Log.h
Scheduler::m_queue
Queue< Process *, MAX_PROCS > m_queue
Contains processes ready to run.
Definition: Scheduler.h:93
FATAL
#define FATAL(msg)
Output a critical message and terminate program immediatly.
Definition: Log.h:50
DEBUG
#define DEBUG(msg)
Output a debug message to standard output.
Definition: Log.h:89
NULL
#define NULL
NULL means zero.
Definition: Macros.h:39
Scheduler::select
Process * select()
Select the next process to run.
Definition: Scheduler.cpp:69
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Scheduler.h
Scheduler::enqueue
Result enqueue(Process *proc, bool ignoreState)
Add a Process to the run schedule.
Definition: Scheduler.cpp:32
Queue::count
virtual Size count() const
Returns the number of items in the Queue.
Definition: Queue.h:153
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
Queue::pop
T & pop()
Remove item from the tail of the Queue.
Definition: Queue.h:76
Queue::push
bool push(const T &item)
Add item to the head of the Queue.
Definition: Queue.h:55
Scheduler::dequeue
Result dequeue(Process *proc, bool ignoreState)
Remove a Process from the run schedule.
Definition: Scheduler.cpp:44
Scheduler::Result
Result
Result code.
Definition: Scheduler.h:43