OOStuBS - Technische Informatik II (TI-II)
2.4
Hauptseite
Zusätzliche Informationen
Klassen
Dateien
Auflistung der Dateien
Datei-Elemente
src
thread
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
27
Scheduler::~Scheduler
(){
28
started
=
false
;
29
}
30
31
void
Scheduler::start
(){
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
;
40
Dispatcher::start
(
idle
);
41
}
42
}
43
44
void
Scheduler::insert
(
Thread
& that){
45
ScopedLock
scopedLock(
lock
);
46
threads
.
push_back
(that);
47
log
<<
"Thread "
<< &that <<
" is ready"
<<
endl
;
48
}
49
50
void
Scheduler::next
()
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
59
void
Scheduler::exit
(){
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
73
void
Scheduler::yield
(){
74
ScopedLock
scopedLock(
lock
);
75
preempt
();
76
}
77
78
void
Scheduler::preempt
(){
79
if
(!
started
)
80
return
;
81
if
(
active
())
82
threads
.
push_back
(*
active
());
83
next
();
84
}
85
86
void
Scheduler::reactivate
(
Thread
& unblocked){
87
threads
.
push_back
(unblocked);
88
}
89
90
Thread
*
Scheduler::active
()
const
{
91
if
(
Dispatcher::active
()==&
idle
)
92
return
NULL
;
93
else
94
return
Dispatcher::active
();
95
}
96
97
void
Scheduler::Idle::action
(){
98
while
(
true
){
99
cpu
.
halt
();
100
yield
();
101
}
102
}
Erzeugt am Mon Jun 30 2014 07:13:28 für OOStuBS - Technische Informatik II (TI-II) von
1.8.1.2