StuBS
|
The is an object used by the scheduler. More...
#include <thread/thread.h>
Public Member Functions | |
Thread () | |
Constructor Initializes the context using prepareContext with the highest aligned address of the reserved_stack_space array as stack pointer (top of stack). Furthermore, assign each thread a unique id using the global variable idCounter. More... | |
void | go () |
Activates the first thread on this CPU. More... | |
void | resume (Thread *next) |
Switches from the currently running thread to the next one. More... | |
virtual void | action ()=0 |
Method that contains the thread's program code. More... | |
Waitingroom * | getWaitingroom () const |
Returns the Waitingroom in which the thread is currently waiting. More... | |
void | setWaitingroom (Waitingroom *w) |
Marks the thread as waiting for an event in Waitingroom w . More... | |
Public Attributes | |
const size_t | id |
Unique ID of thread. | |
volatile bool | kill_flag |
Marker for a dying thread. | |
Static Public Attributes | |
static const size_t | STACK_SIZE = 4096 |
Stack size for each thread. | |
Static Protected Member Functions | |
static void | kickoff (Thread *object) |
Function to start a thread. More... | |
Protected Attributes | |
StackPointer | stackpointer |
Current stack pointer of thread for context switch. | |
The is an object used by the scheduler.
Thread::Thread | ( | ) |
Constructor Initializes the context using prepareContext with the highest aligned address of the reserved_stack_space
array as stack pointer (top of stack). Furthermore, assign each thread a unique id using the global variable idCounter.
|
staticprotected |
Function to start a thread.
For the first activation of a thread, we need a "return address" pointing to a function that will take care of calling C++ virtual methods. For this purpose, we use this kickoff()
function.
Activating kickoff
The thread initialization via prepareContext() not only initializes the Stack for the first thread change, but also pushes the address of kickoff()
as return address to the stack. Consequently, the first execution of context_switch() will start execution by returning to the beginning of kickoff()
.
This kickoff()
function simply calls the action() method on the thread passed as parameter and, thus, resolves the virtual C++ method.
object | Thread to be started |
void Thread::go | ( | ) |
Activates the first thread on this CPU.
Calling the method starts the first thread on the calling CPU. From then on, Thread::resume() must be used all subsequent context switches.
void Thread::resume | ( | Thread * | next | ) |
Switches from the currently running thread to the next
one.
The values currently present in the non-scratch (callee-saved) registers will be stored on this thread's stack; the corresponding values belonging to next
thread will be loaded (from next
's stack).
next | Pointer to the next thread. |
|
pure virtual |
Method that contains the thread's program code.
Derived classes are meant to override this method to provide meaningful code to be run in this thread.
Implemented in IdleThread, Application, and KeyboardApplication.
|
inline |
Returns the Waitingroom in which the thread is currently waiting.
Returns 0 in case the thread does not waiting in any Waitingroom, that is, the thread is either running, in the ready list, already terminated or not yet registered at the scheduler.
nullptr
.
|
inline |
Marks the thread as waiting for an event in Waitingroom w
.
w | The Waitingroom this thread is waiting in. |