]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/kern/sched_context-fixed_prio.cpp
update
[l4.git] / kernel / fiasco / src / kern / sched_context-fixed_prio.cpp
1
2 /*
3  * Timeslice infrastructure
4  */
5
6 INTERFACE [sched_fixed_prio]:
7
8 #include <dlist>
9 #include "member_offs.h"
10 #include "types.h"
11 #include "globals.h"
12 #include "ready_queue_fp.h"
13
14
15 class Sched_context : public cxx::D_list_item
16 {
17   MEMBER_OFFSET();
18   friend class Jdb_list_timeouts;
19   friend class Jdb_thread_list;
20
21   template<typename T>
22   friend struct Jdb_thread_list_policy;
23
24   union Sp
25   {
26     L4_sched_param p;
27     L4_sched_param_legacy legacy_fixed_prio;
28     L4_sched_param_fixed_prio fixed_prio;
29   };
30
31 public:
32   typedef cxx::Sd_list<Sched_context> Fp_list;
33
34   class Ready_queue_base : public Ready_queue_fp<Sched_context>
35   {
36   public:
37     void activate(Sched_context *s)
38     { _current_sched = s; }
39     Sched_context *current_sched() const { return _current_sched; }
40     void ready_enqueue(Sched_context *sc)
41     {
42       assert_kdb(cpu_lock.test());
43
44       // Don't enqueue threads which are already enqueued
45       if (EXPECT_FALSE (sc->in_ready_list()))
46         return;
47
48       enqueue(sc, sc == current_sched());
49     }
50
51   private:
52     Sched_context *_current_sched;
53   };
54
55   Context *context() const { return context_of(this); }
56
57 private:
58   unsigned short _prio;
59   Unsigned64 _quantum;
60   Unsigned64 _left;
61
62   friend class Ready_queue_fp<Sched_context>;
63 };
64
65
66 IMPLEMENTATION [sched_fixed_prio]:
67
68 #include <cassert>
69 #include "cpu_lock.h"
70 #include "kdb_ke.h"
71 #include "std_macros.h"
72 #include "config.h"
73
74
75 /**
76  * Constructor
77  */
78 PUBLIC
79 Sched_context::Sched_context()
80 : _prio(Config::Default_prio),
81   _quantum(Config::Default_time_slice),
82   _left(Config::Default_time_slice)
83 {}
84
85
86 /**
87  * Return priority of Sched_context
88  */
89 PUBLIC inline
90 unsigned short
91 Sched_context::prio() const
92 {
93   return _prio;
94 }
95
96 PUBLIC
97 int
98 Sched_context::set(L4_sched_param const *_p)
99 {
100   Sp const *p = reinterpret_cast<Sp const *>(_p);
101   if (p->p.sched_class >= 0)
102     {
103       // legacy fixed prio
104       _prio = p->legacy_fixed_prio.prio;
105       if (p->legacy_fixed_prio.prio > 255)
106         _prio = 255;
107
108       _quantum = p->legacy_fixed_prio.quantum;
109       if (p->legacy_fixed_prio.quantum == 0)
110         _quantum = Config::Default_time_slice;
111       return 0;
112     }
113
114   switch (p->p.sched_class)
115     {
116     case L4_sched_param_fixed_prio::Class:
117       _prio = p->fixed_prio.prio;
118       if (p->fixed_prio.prio > 255)
119         _prio = 255;
120
121       _quantum = p->fixed_prio.quantum;
122       if (p->fixed_prio.quantum == 0)
123         _quantum = Config::Default_time_slice;
124       break;
125
126     default:
127       return L4_err::ERange;
128     };
129   return 0;
130 }
131
132 /**
133  * Return remaining time quantum of Sched_context
134  */
135 PUBLIC inline
136 Unsigned64
137 Sched_context::left() const
138 {
139   return _left;
140 }
141
142 PUBLIC inline NEEDS[Sched_context::set_left]
143 void
144 Sched_context::replenish()
145 { set_left(_quantum); }
146
147 /**
148  * Set remaining time quantum of Sched_context
149  */
150 PUBLIC inline
151 void
152 Sched_context::set_left(Unsigned64 left)
153 {
154   _left = left;
155 }
156
157
158 /**
159  * Check if Context is in ready-list.
160  * @return 1 if thread is in ready-list, 0 otherwise
161  */
162 PUBLIC inline
163 Mword
164 Sched_context::in_ready_list() const
165 {
166   return Fp_list::in_list(this);
167 }
168
169 PUBLIC inline
170 bool
171 Sched_context::dominates(Sched_context *sc)
172 { return prio() > sc->prio(); }
173