|
0
|
1 #ifndef CYGONCE_KERNEL_SCHED_HXX |
|
|
2 #define CYGONCE_KERNEL_SCHED_HXX |
|
|
3 |
|
|
4 //========================================================================== |
|
|
5 // |
|
|
6 // sched.hxx |
|
|
7 // |
|
|
8 // Scheduler class declaration(s) |
|
|
9 // |
|
|
10 //========================================================================== |
|
|
11 //####COPYRIGHTBEGIN#### |
|
|
12 // |
|
|
13 // ------------------------------------------- |
|
|
14 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
15 // Version 1.0 (the "License"); you may not use this file except in |
|
|
16 // compliance with the License. You may obtain a copy of the License at |
|
|
17 // http://sourceware.cygnus.com/ecos |
|
|
18 // |
|
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
21 // License for the specific language governing rights and limitations under |
|
|
22 // the License. |
|
|
23 // |
|
|
24 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
25 // September 30, 1998. |
|
|
26 // |
|
|
27 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
28 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
29 // ------------------------------------------- |
|
|
30 // |
|
|
31 //####COPYRIGHTEND#### |
|
|
32 //========================================================================== |
|
|
33 //#####DESCRIPTIONBEGIN#### |
|
|
34 // |
|
|
35 // Author(s): nickg |
|
|
36 // Contributors: nickg |
|
|
37 // Date: 1997-09-09 |
|
|
38 // Purpose: Define Scheduler class interfaces |
|
|
39 // Description: These class definitions supply the internal API |
|
|
40 // used to scheduler threads. |
|
|
41 // Usage: #include <cyg/kernel/sched.hxx> |
|
|
42 // |
|
|
43 //####DESCRIPTIONEND#### |
|
|
44 // |
|
|
45 //========================================================================== |
|
|
46 |
|
|
47 #include <cyg/kernel/ktypes.h> |
|
|
48 #include <cyg/infra/cyg_ass.h> // assertion macros |
|
|
49 |
|
|
50 // ------------------------------------------------------------------------- |
|
|
51 // Scheduler base class. This defines stuff that is needed by the |
|
|
52 // specific scheduler implementation. Each scheduler comprises three |
|
|
53 // classes: Cyg_Scheduler_Base, Cyg_Scheduler_Implementation which |
|
|
54 // inherits from it and Cyg_Scheduler which inherits from _it_ in turn. |
|
|
55 |
|
|
56 class Cyg_Scheduler_Base |
|
|
57 { |
|
|
58 friend class Cyg_HardwareThread; |
|
|
59 friend class Cyg_SchedThread; |
|
|
60 |
|
|
61 protected: |
|
|
62 // The following variables are implicit in the API, but are |
|
|
63 // not publically visible. |
|
|
64 |
|
|
65 static volatile cyg_ucount32 sched_lock; // lock counter |
|
|
66 |
|
|
67 static Cyg_Thread *current_thread; // current running thread |
|
|
68 |
|
|
69 static cyg_bool need_reschedule; // set when schedule needed |
|
|
70 |
|
|
71 static cyg_ucount32 thread_switches; // count of number of thread switches |
|
|
72 }; |
|
|
73 |
|
|
74 // ------------------------------------------------------------------------- |
|
|
75 // Include the scheduler implementation header |
|
|
76 |
|
|
77 #include CYGPRI_KERNEL_SCHED_IMPL_HXX |
|
|
78 |
|
|
79 // Do some checking that we have a consistent universe. |
|
|
80 |
|
|
81 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE |
|
|
82 # ifndef CYGIMP_THREAD_PRIORITY |
|
|
83 # error Priority inheritance will not work without priorities!!! |
|
|
84 # endif |
|
|
85 # if CYG_SCHED_UNIQUE_PRIORITIES |
|
|
86 # error Priority inheritance needs non-unique priorities!!! |
|
|
87 # endif |
|
|
88 #endif |
|
|
89 |
|
|
90 // ------------------------------------------------------------------------- |
|
|
91 // Scheduler class. This is the public scheduler interface seen by the |
|
|
92 // rest of the kernel. |
|
|
93 |
|
|
94 class Cyg_Scheduler |
|
|
95 : public Cyg_Scheduler_Implementation |
|
|
96 { |
|
|
97 friend class Cyg_Thread; |
|
|
98 |
|
|
99 // This function is the actual implementation of the unlock |
|
|
100 // function. The unlock() later is an inline shell that deals |
|
|
101 // with the common case. |
|
|
102 |
|
|
103 static void unlock_inner(); |
|
|
104 |
|
|
105 public: |
|
|
106 |
|
|
107 CYGDBG_DEFINE_CHECK_THIS |
|
|
108 |
|
|
109 // The following API functions are common to all scheduler |
|
|
110 // implementations. |
|
|
111 |
|
|
112 // claim the preemption lock |
|
|
113 static void lock(); |
|
|
114 |
|
|
115 // release the preemption lock |
|
|
116 static void unlock(); |
|
|
117 |
|
|
118 // return a pointer to the current thread |
|
|
119 static Cyg_Thread *get_current_thread(); |
|
|
120 |
|
|
121 // Return current value of lock |
|
|
122 static cyg_ucount32 get_sched_lock(); |
|
|
123 |
|
|
124 // Return current number of thread switches |
|
|
125 static cyg_ucount32 get_thread_switches(); |
|
|
126 |
|
|
127 // Start execution of the scheduler |
|
|
128 static void start() __attribute__ ((noreturn)); |
|
|
129 |
|
|
130 // The only scheduler instance should be this one... |
|
|
131 static Cyg_Scheduler scheduler; |
|
|
132 |
|
|
133 }; |
|
|
134 |
|
|
135 // ------------------------------------------------------------------------- |
|
|
136 // This class encapsulates the scheduling abstractions in a thread. |
|
|
137 // Cyg_SchedThread is included as a base class of Cyg_Thread. The actual |
|
|
138 // implementation of the abstractions is in Cyg_SchedThread_Implementation |
|
|
139 // so this class has little to do. |
|
|
140 |
|
|
141 class Cyg_SchedThread |
|
|
142 : public Cyg_SchedThread_Implementation |
|
|
143 { |
|
|
144 friend class Cyg_ThreadQueue_Implementation; |
|
|
145 friend class Cyg_Scheduler_Implementation; |
|
|
146 |
|
|
147 Cyg_ThreadQueue *queue; |
|
|
148 |
|
|
149 |
|
|
150 public: |
|
|
151 |
|
|
152 Cyg_SchedThread(Cyg_Thread *thread, CYG_ADDRWORD sched_info); |
|
|
153 |
|
|
154 // Return current queue pointer |
|
|
155 |
|
|
156 Cyg_ThreadQueue *get_current_queue(); |
|
|
157 |
|
|
158 // Remove this thread from current queue |
|
|
159 void remove(); |
|
|
160 |
|
|
161 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE |
|
|
162 |
|
|
163 private: |
|
|
164 |
|
|
165 // For all priority inheritance mechanisms we need to |
|
|
166 // keep track of how many mutexes we have locked. |
|
|
167 cyg_count32 mutex_count; |
|
|
168 |
|
|
169 public: |
|
|
170 // Count and uncount the number of mutexes held by |
|
|
171 // this thread. |
|
|
172 void count_mutex() { mutex_count++; }; |
|
|
173 void uncount_mutex() { mutex_count--; }; |
|
|
174 |
|
|
175 protected: |
|
|
176 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE_SIMPLE |
|
|
177 |
|
|
178 // The simple priority inheritance mechanism simply needs |
|
|
179 // somewhere to store the base priority of the current thread. |
|
|
180 |
|
|
181 cyg_priority original_priority; // our original priority |
|
|
182 |
|
|
183 cyg_bool priority_inherited; // have we inherited? |
|
|
184 |
|
|
185 #endif |
|
|
186 |
|
|
187 public: |
|
|
188 |
|
|
189 // Inherit the priority of the provided thread if it |
|
|
190 // has higher priority than this. |
|
|
191 void inherit_priority( Cyg_Thread *thread); |
|
|
192 |
|
|
193 // Lose priority inheritance |
|
|
194 void disinherit_priority(); |
|
|
195 |
|
|
196 #endif |
|
|
197 |
|
|
198 }; |
|
|
199 |
|
|
200 // ------------------------------------------------------------------------- |
|
|
201 // Simple inline accessor functions |
|
|
202 |
|
|
203 inline Cyg_Thread *Cyg_Scheduler::get_current_thread() |
|
|
204 { |
|
|
205 return current_thread; |
|
|
206 } |
|
|
207 |
|
|
208 // Return current value of lock |
|
|
209 inline cyg_ucount32 Cyg_Scheduler::get_sched_lock() |
|
|
210 { |
|
|
211 return sched_lock; |
|
|
212 } |
|
|
213 |
|
|
214 // Return current number of thread switches |
|
|
215 inline cyg_ucount32 Cyg_Scheduler::get_thread_switches() |
|
|
216 { |
|
|
217 return thread_switches; |
|
|
218 } |
|
|
219 |
|
|
220 // Return current queue pointer |
|
|
221 inline Cyg_ThreadQueue *Cyg_SchedThread::get_current_queue() |
|
|
222 { |
|
|
223 return queue; |
|
|
224 } |
|
|
225 |
|
|
226 // ------------------------------------------------------------------------- |
|
|
227 #endif // ifndef __SCHED_HXX__ |
|
|
228 // EOF sched.hxx |