Mercurial > flash_v2
annotate packages/kernel/current/src/sched/bitmap.cxx @ 46:797268ecc331 ecos-sw-1999-10-19
Merge from eCos master repository on 1999-10-19-18:55:31-BST
| author | jlarmour |
|---|---|
| date | Tue, 19 Oct 1999 19:19:52 +0000 |
| parents | 443894e2e912 |
| children | 755351606154 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // sched/bitmap.cxx |
| 0 | 4 // |
| 2 | 5 // Bitmap scheduler class implementation |
| 0 | 6 // |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
| 9 // | |
| 10 // ------------------------------------------- | |
| 11 // The contents of this file are subject to the Cygnus eCos Public License | |
| 12 // Version 1.0 (the "License"); you may not use this file except in | |
| 13 // compliance with the License. You may obtain a copy of the License at | |
| 14 // http://sourceware.cygnus.com/ecos | |
| 15 // | |
| 16 // Software distributed under the License is distributed on an "AS IS" | |
| 17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 18 // License for the specific language governing rights and limitations under | |
| 19 // the License. | |
| 20 // | |
| 21 // The Original Code is eCos - Embedded Cygnus Operating System, released | |
| 22 // September 30, 1998. | |
| 23 // | |
| 24 // The Initial Developer of the Original Code is Cygnus. Portions created | |
| 2 | 25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
| 0 | 26 // ------------------------------------------- |
| 27 // | |
| 28 //####COPYRIGHTEND#### | |
| 29 //========================================================================== | |
| 30 //#####DESCRIPTIONBEGIN#### | |
| 31 // | |
| 2 | 32 // Author(s): nickg |
| 33 // Contributors: nickg | |
| 34 // Date: 1997-09-16 | |
| 35 // Purpose: Bitmap scheduler class implementation | |
| 36 // Description: This file contains the implementations of | |
| 0 | 37 // Cyg_Scheduler_Implementation and Cyg_SchedThread_Implementation. |
| 38 // | |
| 39 // | |
| 40 //####DESCRIPTIONEND#### | |
| 41 // | |
| 42 //========================================================================== | |
| 43 | |
| 44 #include <pkgconf/kernel.h> | |
| 45 | |
| 46 #include <cyg/kernel/ktypes.h> // base kernel types | |
| 47 #include <cyg/infra/cyg_trac.h> // tracing macros | |
| 48 #include <cyg/infra/cyg_ass.h> // assertion macros | |
| 49 | |
| 50 #include <cyg/kernel/sched.hxx> // our header | |
| 51 | |
| 52 #include <cyg/hal/hal_arch.h> // Architecture specific definitions | |
| 53 | |
| 54 #include <cyg/kernel/thread.inl> // thread inlines | |
| 55 #include <cyg/kernel/sched.inl> // scheduler inlines | |
| 56 | |
| 57 #ifdef CYGSEM_KERNEL_SCHED_BITMAP | |
| 58 | |
| 59 //========================================================================== | |
| 60 // Cyg_Scheduler_Implementation class members | |
| 61 | |
| 62 // ------------------------------------------------------------------------- | |
| 63 // Constructor. | |
| 64 | |
| 65 Cyg_Scheduler_Implementation::Cyg_Scheduler_Implementation() | |
| 66 { | |
| 67 CYG_REPORT_FUNCTION(); | |
| 68 | |
| 69 // At present we cannot init run_queue here because the absence of | |
| 70 // ordering of static constructors means that we could do this | |
| 71 // after the static idle thread has been created. (Guess how I | |
| 72 // found this out!) | |
| 73 // run_queue = 0; | |
| 74 | |
| 75 } | |
| 76 | |
| 77 // ------------------------------------------------------------------------- | |
| 78 // Choose the best thread to run next | |
| 79 | |
| 80 Cyg_Thread *Cyg_Scheduler_Implementation::schedule() | |
| 81 { | |
| 82 CYG_REPORT_FUNCTION(); | |
| 83 | |
| 84 // The run queue may _never_ be empty, there is always | |
| 85 // an idle thread at the lowest priority. | |
| 86 | |
| 87 CYG_ASSERT(run_queue != 0, "Run queue empty"); | |
| 88 | |
| 89 cyg_uint32 index; | |
| 90 | |
| 91 HAL_LSBIT_INDEX(index, run_queue); | |
| 92 | |
| 93 return thread_table[index]; | |
| 94 } | |
| 95 | |
| 96 // ------------------------------------------------------------------------- | |
| 97 | |
| 98 void Cyg_Scheduler_Implementation::add_thread(Cyg_Thread *thread) | |
| 99 { | |
| 100 CYG_REPORT_FUNCTION(); | |
| 101 | |
| 102 CYG_ASSERT( thread_table[thread->priority] == NULL || | |
| 103 thread_table[thread->priority] == thread, | |
| 104 "Duplicate thread priorities" ); | |
| 105 | |
|
46
797268ecc331
Merge from eCos master repository on 1999-10-19-18:55:31-BST
jlarmour
parents:
2
diff
changeset
|
106 CYG_ASSERT((CYG_THREAD_MIN_PRIORITY >= pri) |
|
797268ecc331
Merge from eCos master repository on 1999-10-19-18:55:31-BST
jlarmour
parents:
2
diff
changeset
|
107 && (CYG_THREAD_MAX_PRIORITY <= pri), |
|
797268ecc331
Merge from eCos master repository on 1999-10-19-18:55:31-BST
jlarmour
parents:
2
diff
changeset
|
108 "Priority out of range!"); |
|
797268ecc331
Merge from eCos master repository on 1999-10-19-18:55:31-BST
jlarmour
parents:
2
diff
changeset
|
109 |
| 0 | 110 CYG_ASSERT( (run_queue & (1<<thread->priority)) == 0, |
| 111 "Run queue bit already set" ); | |
| 112 | |
| 113 // If the thread is on some other queue, remove it | |
| 114 // here. | |
| 115 if( thread->queue != NULL ) | |
| 116 { | |
| 117 thread->queue->remove(thread); | |
| 118 thread->queue = NULL; | |
| 119 } | |
| 120 | |
| 121 run_queue |= 1<<thread->priority; | |
| 122 | |
| 123 // If the new thread is higher priority than the | |
| 124 // current thread, request a reschedule. | |
| 125 | |
| 126 if( thread->priority < Cyg_Scheduler::get_current_thread()->priority ) | |
| 127 need_reschedule = true; | |
| 128 } | |
| 129 | |
| 130 // ------------------------------------------------------------------------- | |
| 131 | |
| 132 void Cyg_Scheduler_Implementation::rem_thread(Cyg_Thread *thread) | |
| 133 { | |
| 134 CYG_REPORT_FUNCTION(); | |
| 135 | |
| 136 CYG_ASSERT( thread_table[thread->priority] == thread, | |
| 137 "Invalid thread priority" ); | |
| 138 | |
| 139 CYG_ASSERT( (run_queue & (1<<thread->priority)) != 0, | |
| 140 "Run queue bit not set" ); | |
| 141 | |
| 142 run_queue &= ~(1<<thread->priority); | |
| 143 | |
| 144 if( thread == Cyg_Scheduler::get_current_thread() ) | |
| 145 need_reschedule = true; | |
| 146 } | |
| 147 | |
| 148 // ------------------------------------------------------------------------- | |
| 149 // register thread with scheduler | |
| 150 | |
| 151 void Cyg_Scheduler_Implementation::register_thread(Cyg_Thread *thread) | |
| 152 { | |
| 153 CYG_REPORT_FUNCTION(); | |
| 154 | |
| 155 thread_table[thread->priority] = thread; | |
| 156 } | |
| 157 | |
| 158 // ------------------------------------------------------------------------- | |
| 159 | |
| 160 // deregister thread | |
| 161 void Cyg_Scheduler_Implementation::deregister_thread(Cyg_Thread *thread) | |
| 162 { | |
| 163 CYG_REPORT_FUNCTION(); | |
| 164 | |
| 165 thread_table[thread->priority] = NULL; | |
| 166 } | |
| 167 | |
| 168 // ------------------------------------------------------------------------- | |
| 169 // Test the given priority for uniqueness | |
| 170 | |
| 171 cyg_bool Cyg_Scheduler_Implementation::unique( cyg_priority priority) | |
| 172 { | |
| 173 CYG_REPORT_FUNCTION(); | |
| 174 | |
| 175 return thread_table[priority] == NULL; | |
| 176 } | |
| 177 | |
| 178 | |
| 179 //========================================================================== | |
| 180 // Cyg_Cyg_SchedThread_Implementation class members | |
| 181 | |
| 182 Cyg_SchedThread_Implementation::Cyg_SchedThread_Implementation | |
| 183 ( | |
| 184 CYG_ADDRWORD sched_info | |
| 185 ) | |
| 186 { | |
| 187 CYG_REPORT_FUNCTION(); | |
| 188 | |
| 189 #if 1 | |
| 190 // Assign this thread's priority to the supplied sched_info | |
| 191 // or the next highest priority available. | |
| 192 | |
| 193 priority = cyg_priority(sched_info); | |
| 194 | |
| 195 while( !Cyg_Scheduler::scheduler.unique(priority) ) | |
| 196 priority++; | |
| 197 | |
| 198 #else | |
| 199 // Assign initial priorities to threads in descending order of | |
| 200 // creation. | |
| 201 | |
| 202 static cyg_priority init_priority = 0; | |
| 203 | |
| 204 priority = init_priority++; | |
| 205 #endif | |
| 206 | |
| 207 } | |
| 208 | |
| 209 // ------------------------------------------------------------------------- | |
| 210 | |
| 211 void Cyg_SchedThread_Implementation::yield() | |
| 212 { | |
| 213 CYG_REPORT_FUNCTION(); | |
| 214 | |
| 215 // We cannot yield in this scheduler | |
| 216 } | |
| 217 | |
| 218 //========================================================================== | |
| 219 // Cyg_ThreadQueue_Implementation class members | |
| 220 | |
| 221 Cyg_ThreadQueue_Implementation::Cyg_ThreadQueue_Implementation() | |
| 222 { | |
| 223 CYG_REPORT_FUNCTION(); | |
| 224 | |
| 225 wait_queue = 0; // empty queue | |
| 2 | 226 |
| 227 CYG_REPORT_RETURN(); | |
| 0 | 228 } |
| 229 | |
| 230 | |
| 231 void Cyg_ThreadQueue_Implementation::enqueue(Cyg_Thread *thread) | |
| 232 { | |
| 233 CYG_REPORT_FUNCTION(); | |
| 234 | |
| 235 wait_queue |= 1<<thread->priority; | |
| 236 thread->queue = CYG_CLASSFROMBASE(Cyg_ThreadQueue, | |
| 237 Cyg_ThreadQueue_Implementation, | |
| 238 this); | |
| 239 } | |
| 240 | |
| 241 // ------------------------------------------------------------------------- | |
| 242 | |
| 243 Cyg_Thread *Cyg_ThreadQueue_Implementation::dequeue() | |
| 244 { | |
| 245 CYG_REPORT_FUNCTION(); | |
| 246 | |
| 247 // Isolate ls bit in run_queue. | |
| 248 cyg_sched_bitmap next_thread = wait_queue & -wait_queue; | |
| 249 | |
| 250 if( next_thread == 0 ) return NULL; | |
| 251 | |
| 252 wait_queue &= ~next_thread; | |
| 253 | |
| 254 cyg_uint32 index; | |
| 255 | |
| 256 HAL_LSBIT_INDEX(index, next_thread); | |
| 257 | |
| 258 Cyg_Thread *thread = Cyg_Scheduler::scheduler.thread_table[index]; | |
| 259 | |
| 260 thread->queue = NULL; | |
| 261 | |
| 262 return thread; | |
| 263 } | |
| 264 | |
| 265 // ------------------------------------------------------------------------- | |
| 266 | |
| 267 Cyg_Thread *Cyg_ThreadQueue_Implementation::highpri() | |
| 268 { | |
| 269 CYG_REPORT_FUNCTION(); | |
| 270 | |
| 271 // Isolate ls bit in run_queue. | |
| 272 cyg_sched_bitmap next_thread = wait_queue & -wait_queue; | |
| 273 | |
| 274 if( next_thread == 0 ) return NULL; | |
| 275 | |
| 276 cyg_uint32 index; | |
| 277 | |
| 278 HAL_LSBIT_INDEX(index, next_thread); | |
| 279 | |
| 280 return Cyg_Scheduler::scheduler.thread_table[index]; | |
| 281 } | |
| 282 | |
| 283 // ------------------------------------------------------------------------- | |
| 284 | |
| 285 void Cyg_ThreadQueue_Implementation::remove(Cyg_Thread *thread) | |
| 286 { | |
| 287 CYG_REPORT_FUNCTION(); | |
| 288 | |
| 289 wait_queue &= ~(1<<thread->priority); | |
| 290 thread->queue = NULL; | |
| 291 } | |
| 292 | |
| 293 #endif | |
| 294 | |
| 295 // ------------------------------------------------------------------------- | |
| 296 // EOF sched/bitmap.cxx |
