OOStuBS - Technische Informatik II (TI-II)  2.4
scheduler.cc
gehe zur Dokumentation dieser Datei
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2  * Technische Informatik II *
3  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4  * *
5  * S C H E D U L E R *
6  * *
7 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
8 
9 /* * * * * * * * * * * * * * * * * * * * * * * * *\
10 # INCLUDES #
11 \* * * * * * * * * * * * * * * * * * * * * * * * */
12 
13 #include "thread/scheduler.h"
14 #include "locking/scopedLock.h"
15 #include "common/null.h"
16 #include "object/lock.h"
17 #include "object/log.h"
18 #include "object/cpu.h"
19 
20 
21 /* * * * * * * * * * * * * * * * * * * * * * * * *\
22 # METHODS #
23 \* * * * * * * * * * * * * * * * * * * * * * * * */
24 
25 Scheduler::Scheduler() : started(false){}
26 
28  started=false;
29 }
30 
32  ScopedLock scopedLock(lock);
33  started=true;
34  Thread* next = static_cast<Thread*>(threads.pop_front());
35  if(next){
36  log << "System starts first Thread " << next << endl;
37  Dispatcher::start(*next);
38  }else{
39  log << "System starts idle Thread " << endl;
41  }
42 }
43 
45  ScopedLock scopedLock(lock);
46  threads.push_back(that);
47  log << "Thread " << &that << " is ready" << endl;
48 }
49 
51 {
52  Thread* next = static_cast<Thread*>(threads.pop_front());
53  if(!next)
54  log << "Thread " << &idle << " starting (idle Thread)" << endl;
55  next=&idle;
56  dispatch(*next);
57 }
58 
60  ScopedLock scopedLock(lock);
61  log << "Thread " << active() << " ended" << endl;
62  next();
63 }
64 
65 bool Scheduler::kill(Thread& that){
66  if(&that==active()){
67  next();
68  return true;
69  }
70  return threads.remove(that);
71 }
72 
74  ScopedLock scopedLock(lock);
75  preempt();
76 }
77 
79  if(!started)
80  return;
81  if(active())
83  next();
84 }
85 
86 void Scheduler::reactivate(Thread& unblocked){
87  threads.push_back(unblocked);
88 }
89 
91  if(Dispatcher::active()==&idle)
92  return NULL;
93  else
94  return Dispatcher::active();
95 }
96 
98  while(true){
99  cpu.halt();
100  yield();
101  }
102 }