update preemption demo
This commit is contained in:
36
c_os/user/demo/PreemptiveThreadDemo.cc
Normal file
36
c_os/user/demo/PreemptiveThreadDemo.cc
Normal file
@ -0,0 +1,36 @@
|
||||
#include "user/demo/PreemptiveThreadDemo.h"
|
||||
|
||||
void PreemptiveLoopThread::run() {
|
||||
int cnt = 0;
|
||||
while (true) {
|
||||
// Basic synchronization by semaphore
|
||||
sem->p();
|
||||
|
||||
kout.setpos(55, this->id);
|
||||
kout << this->id << ": " << dec << cnt++ << endl;
|
||||
|
||||
sem->v();
|
||||
}
|
||||
}
|
||||
|
||||
void PreemptiveThreadDemo::run() {
|
||||
kout << "PreemptiveThreadDemo ====================================" << endl;
|
||||
kout << "Initializing Semaphore" << endl;
|
||||
Semaphore* sem = new Semaphore(1); // Create this semaphore on the heap as this thread exits itself,
|
||||
// so stack allocated objects will be lost
|
||||
|
||||
kout << "Allocating LoopThread A" << endl;
|
||||
Thread* cntA = new PreemptiveLoopThread(0, sem);
|
||||
kout << "Allocating LoopThread B" << endl;
|
||||
Thread* cntB = new PreemptiveLoopThread(1, sem);
|
||||
kout << "Allocating LoopThread C" << endl;
|
||||
Thread* cntC = new PreemptiveLoopThread(2, sem);
|
||||
|
||||
kout << "Adding threads to ready queue" << endl;
|
||||
scheduler.ready(cntA);
|
||||
scheduler.ready(cntB);
|
||||
scheduler.ready(cntC);
|
||||
|
||||
kout << "Exiting main thread" << endl;
|
||||
scheduler.exit();
|
||||
}
|
||||
36
c_os/user/demo/PreemptiveThreadDemo.h
Normal file
36
c_os/user/demo/PreemptiveThreadDemo.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef __preemptive_thread_include__
|
||||
#define __preemptive_thread_include__
|
||||
|
||||
#include "kernel/Globals.h"
|
||||
#include "kernel/threads/Thread.h"
|
||||
#include "lib/Semaphore.h"
|
||||
|
||||
class PreemptiveLoopThread : public Thread {
|
||||
private:
|
||||
int id;
|
||||
Semaphore* sem;
|
||||
|
||||
PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren
|
||||
|
||||
public:
|
||||
// Gibt der Loop einen Stack und eine Id.
|
||||
PreemptiveLoopThread(int i, Semaphore* sem) : id(i), sem(sem) {}
|
||||
|
||||
// Zaehlt einen Zaehler hoch und gibt ihn auf dem Bildschirm aus.
|
||||
void run() override;
|
||||
};
|
||||
|
||||
class PreemptiveThreadDemo : public Thread {
|
||||
private:
|
||||
PreemptiveThreadDemo(const PreemptiveThreadDemo& copy) = delete; // Verhindere Kopieren
|
||||
|
||||
public:
|
||||
PreemptiveThreadDemo() {
|
||||
kout << "Initialized PreemptiveThreadDemo" << endl;
|
||||
}
|
||||
|
||||
// Thread-Startmethode
|
||||
void run() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user