Mercurial > nand-ecoscentric
annotate packages/kernel/current/src/common/thread.cxx @ 66:bf00f99aec69 ecos-sw-2000-02-02
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
| author | jlarmour |
|---|---|
| date | Wed, 02 Feb 2000 19:57:02 +0000 |
| parents | c38311975d4f |
| children | 59d97b6ba612 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 2 | 3 // common/thread.cxx |
| 0 | 4 // |
| 2 | 5 // Thread class implementations |
| 0 | 6 // |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
12 // Version 1.1 (the "License"); you may not use this file except in |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
14 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
15 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
8
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 2 | 34 // Author(s): nickg |
| 35 // Contributors: nickg | |
| 36 // Date: 1997-09-15 | |
| 37 // Purpose: Thread class implementation | |
| 38 // Description: This file contains the definitions of the thread class | |
| 0 | 39 // member functions that are common to all thread implementations. |
| 40 // | |
| 41 //####DESCRIPTIONEND#### | |
| 42 // | |
| 43 //========================================================================== | |
| 44 | |
| 45 #include <pkgconf/kernel.h> // kernel configuration file | |
| 46 | |
| 2 | 47 #include <cyg/hal/hal_arch.h> // HAL_REORDER_BARRIER & |
| 48 // CYGNUM_HAL_STACK_SIZE_TYPICAL | |
| 49 | |
| 0 | 50 #include <cyg/kernel/ktypes.h> // base kernel types |
| 51 #include <cyg/infra/cyg_trac.h> // tracing macros | |
| 52 #include <cyg/infra/cyg_ass.h> // assertion macros | |
| 53 #include <cyg/kernel/instrmnt.h> // instrumentation | |
| 54 | |
| 55 #include <cyg/kernel/thread.hxx> // our header | |
| 56 | |
| 57 #include <cyg/kernel/intr.hxx> // Interrupt support | |
| 58 | |
| 59 #include <cyg/kernel/thread.inl> // thread inlines | |
| 60 #include <cyg/kernel/sched.inl> // scheduler inlines | |
| 61 #include <cyg/kernel/clock.inl> // clock inlines | |
| 62 | |
| 63 // ========================================================================= | |
| 64 // Cyg_HardwareThread members | |
| 65 | |
| 66 // ------------------------------------------------------------------------- | |
| 67 // Thread entry point. | |
| 68 // This is inserted as the PC value in all initial thread contexts. | |
| 69 // It does some housekeeping and then calls the real entry point. | |
| 70 | |
| 71 void | |
| 72 Cyg_HardwareThread::thread_entry( Cyg_Thread *thread ) | |
| 73 { | |
| 74 CYG_REPORT_FUNCTION(); | |
| 75 | |
| 76 Cyg_Scheduler::scheduler.need_reschedule = false; // finished rescheduling | |
| 77 Cyg_Scheduler::scheduler.current_thread = thread; // restore current thread pointer | |
| 78 | |
|
6
d376b777e2ce
Merge from eCos master repository on 1999-05-14-19:27:43-BST
jlarmour
parents:
2
diff
changeset
|
79 CYG_INSTRUMENT_THREAD(ENTER,thread,0); |
|
d376b777e2ce
Merge from eCos master repository on 1999-05-14-19:27:43-BST
jlarmour
parents:
2
diff
changeset
|
80 |
| 0 | 81 #ifdef CYGSEM_KERNEL_SCHED_TIMESLICE |
| 82 // Reset the timeslice counter so that this thread gets a full | |
| 83 // quantum. | |
| 84 Cyg_Scheduler::reset_timeslice_count(); | |
| 85 #endif | |
| 86 | |
| 87 // Zero the lock | |
| 2 | 88 HAL_REORDER_BARRIER (); // Prevent the compiler from moving |
| 89 Cyg_Scheduler::sched_lock = 0; // the assignment into the code above. | |
| 90 HAL_REORDER_BARRIER(); | |
| 0 | 91 |
| 92 // Call entry point in a loop. | |
| 93 | |
| 94 for(;;) | |
| 95 { | |
| 96 thread->entry_point(thread->entry_data); | |
| 97 thread->exit(); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 // ========================================================================= | |
| 102 // Cyg_Thread members | |
| 103 | |
| 104 // ------------------------------------------------------------------------- | |
| 105 // Statics and thread list functions | |
| 106 | |
| 107 #ifdef CYGVAR_KERNEL_THREADS_LIST | |
| 108 | |
| 109 // List of all extant threads | |
| 110 Cyg_Thread *Cyg_Thread::thread_list = 0; | |
| 111 | |
| 112 inline void | |
| 113 Cyg_Thread::add_to_list( void ) | |
| 114 { | |
| 115 // Add thread to housekeeping list | |
| 116 Cyg_Scheduler::lock(); | |
| 117 | |
| 118 if( thread_list == 0 ) | |
| 119 list_next = this; | |
| 120 else { | |
| 121 Cyg_Thread *prev = thread_list; | |
| 122 do { | |
| 123 if ( this == prev ) | |
| 124 break; // found it already! | |
| 125 prev = prev->list_next; | |
| 126 } while ( prev != thread_list ); | |
| 127 if ( this != prev ) { | |
| 128 // insert it in the list: | |
| 129 list_next = thread_list->list_next; | |
| 130 thread_list->list_next = this; | |
| 131 } | |
| 132 } | |
| 133 thread_list = this; | |
| 134 | |
| 135 Cyg_Scheduler::unlock(); | |
| 136 } | |
| 137 | |
| 138 inline void | |
| 139 Cyg_Thread::remove_from_list( void ) | |
| 140 { | |
| 141 // remove thread from housekeeping list | |
| 142 Cyg_Scheduler::lock(); | |
| 143 | |
| 144 Cyg_Thread *prev = thread_list; | |
| 145 | |
| 146 do { | |
| 147 if( prev->list_next == this ) { | |
| 148 prev->list_next = list_next; | |
| 149 if( thread_list == this ) | |
| 150 thread_list = list_next; | |
| 151 break; | |
| 152 } | |
| 153 prev = prev->list_next; | |
| 154 } while ( prev != thread_list ); | |
| 155 | |
| 156 Cyg_Scheduler::unlock(); | |
| 157 } | |
| 158 | |
| 159 #endif | |
| 160 | |
| 161 static cyg_uint16 next_unique_id = 1; | |
| 162 | |
| 163 // ------------------------------------------------------------------------- | |
| 164 // Magic new operator to allow the thread constructor to be | |
| 165 // recalled. | |
| 166 | |
| 167 inline void * | |
| 168 operator new(size_t size, Cyg_Thread *ptr) | |
| 169 { return (void *)ptr; }; | |
| 170 | |
| 171 // Constructor | |
| 172 | |
| 173 Cyg_Thread::Cyg_Thread( | |
| 174 CYG_ADDRWORD sched_info, // Scheduling parameter(s) | |
| 175 cyg_thread_entry *entry, // entry point function | |
| 176 CYG_ADDRWORD entry_data, // entry data | |
| 177 char *name_arg, // thread name cookie | |
| 178 CYG_ADDRESS stack_base, // stack base, NULL = allocate | |
| 179 cyg_ucount32 stack_size // stack size, 0 = use default | |
| 180 ) | |
| 181 : Cyg_HardwareThread(entry, entry_data, stack_size, stack_base), | |
| 182 Cyg_SchedThread(this, sched_info) | |
| 183 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 184 ,timer(this) | |
| 185 #endif | |
| 186 { | |
| 187 CYG_REPORT_FUNCTION(); | |
| 188 | |
| 189 // Start the thread in suspended state. | |
| 190 state = SUSPENDED; | |
| 191 suspend_count = 1; | |
| 192 | |
| 193 // Initialize sleep_reason which is used by kill, release | |
| 194 sleep_reason = NONE; | |
| 195 wake_reason = NONE; | |
| 196 | |
| 197 // Assign a 16 bit id to the thread. | |
| 198 unique_id = next_unique_id++; | |
| 199 | |
| 200 #ifdef CYGVAR_KERNEL_THREADS_DATA | |
| 201 // Zero all per-thread data entries. | |
| 202 for( int i = 0; i < CYGNUM_KERNEL_THREADS_DATA_MAX; i++ ) | |
| 203 thread_data[i] = 0; | |
| 204 #endif | |
| 205 #ifdef CYGVAR_KERNEL_THREADS_NAME | |
| 206 name = name_arg; | |
| 207 #endif | |
| 208 #ifdef CYGVAR_KERNEL_THREADS_LIST | |
| 209 // Add thread to housekeeping list | |
| 210 add_to_list(); | |
| 211 #endif | |
| 212 | |
| 213 Cyg_Scheduler::scheduler.register_thread(this); | |
| 214 | |
| 215 init_context(this); | |
| 216 | |
| 217 CYG_REPORT_RETURN(); | |
| 218 } | |
| 219 | |
| 220 | |
| 221 // ------------------------------------------------------------------------- | |
| 222 // Re-initialize this thread. | |
| 223 // We do this by re-invoking the constructor with the original | |
| 224 // arguments, which are still available in the object. | |
| 225 | |
| 226 void | |
| 227 Cyg_Thread::reinitialize() | |
| 228 { | |
| 229 CYG_REPORT_FUNCTION(); | |
| 230 | |
| 231 CYG_ASSERTCLASS( this, "Bad thread"); | |
| 232 CYG_ASSERT( this != Cyg_Scheduler::get_current_thread(), | |
| 233 "Attempt to reinitialize current thread"); | |
| 234 CYG_ASSERT( get_current_queue() == NULL , "Thread is still on a queue"); | |
| 235 | |
| 236 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 237 // Clear the timeout. It is irrelevant whether there was | |
| 238 // actually a timeout pending. | |
| 239 timer.disable(); | |
| 240 #endif | |
| 241 | |
| 242 // Ensure the scheduler has let go of us. | |
| 243 Cyg_Scheduler::scheduler.deregister_thread(this); | |
| 244 | |
| 245 cyg_priority pri = get_priority(); | |
| 246 #ifdef CYGVAR_KERNEL_THREADS_NAME | |
| 247 char * name_arg = name; | |
| 248 #else | |
| 249 char * name_arg = NULL; | |
| 250 #endif | |
| 251 | |
| 252 new(this) Cyg_Thread( pri, | |
| 253 entry_point, entry_data, | |
| 254 name_arg, | |
| 255 stack_base, stack_size ); | |
| 256 // the constructor re-registers the thread with the scheduler. | |
| 257 | |
| 258 CYG_ASSERTCLASS( this, "Thread corrupted by reinitialize"); | |
| 259 | |
| 260 CYG_REPORT_RETURN(); | |
| 261 } | |
| 262 | |
| 263 // ------------------------------------------------------------------------- | |
| 264 // Destructor. | |
| 265 | |
| 266 Cyg_Thread::~Cyg_Thread() | |
| 267 { | |
| 268 CYG_REPORT_FUNCTION(); | |
| 269 | |
| 270 Cyg_Scheduler::scheduler.deregister_thread(this); | |
| 271 | |
| 272 #ifdef CYGVAR_KERNEL_THREADS_LIST | |
| 273 // Remove thread from housekeeping list. | |
| 274 remove_from_list(); | |
| 275 #endif | |
| 276 | |
| 277 CYG_REPORT_RETURN(); | |
| 278 } | |
| 279 | |
| 280 // ------------------------------------------------------------------------- | |
| 281 // Thread consistency checker. | |
| 282 | |
| 283 #ifdef CYGDBG_USE_ASSERTS | |
| 284 | |
| 285 bool | |
| 2 | 286 Cyg_Thread::check_this( cyg_assert_class_zeal zeal) const |
| 0 | 287 { |
| 288 // CYG_REPORT_FUNCTION(); | |
| 289 | |
| 290 // check that we have a non-NULL pointer first | |
| 291 if( this == NULL ) return false; | |
| 292 | |
| 293 switch( zeal ) | |
| 294 { | |
| 295 case cyg_system_test: | |
| 296 case cyg_extreme: | |
| 297 case cyg_thorough: | |
| 298 if( (state & SUSPENDED) && (suspend_count == 0) ) return false; | |
| 299 case cyg_quick: | |
| 2 | 300 // Check that the stackpointer is within its limits. |
| 301 // Note: This does not check the current stackpointer value | |
| 302 // of the executing thread. | |
| 303 if( (stack_ptr > (stack_base + stack_size)) || | |
| 304 (stack_ptr < stack_base) ) return false; | |
| 0 | 305 case cyg_trivial: |
| 306 case cyg_none: | |
| 307 default: | |
| 308 break; | |
| 309 }; | |
| 310 | |
| 311 return true; | |
| 312 } | |
| 313 | |
| 314 #endif | |
| 315 | |
| 316 // ------------------------------------------------------------------------- | |
| 317 // Put the thread to sleep. | |
| 318 // This can only be called by the current thread on itself, hence | |
| 319 // it is a static function. | |
| 320 | |
| 321 void | |
| 322 Cyg_Thread::sleep() | |
| 323 { | |
| 324 CYG_REPORT_FUNCTION(); | |
| 325 | |
| 326 Cyg_Thread *current = Cyg_Scheduler::get_current_thread(); | |
| 327 | |
| 328 CYG_ASSERTCLASS( current, "Bad current thread" ); | |
| 329 | |
| 330 CYG_INSTRUMENT_THREAD(SLEEP,current,0); | |
| 331 | |
| 332 // Prevent preemption | |
| 333 Cyg_Scheduler::lock(); | |
| 334 | |
| 335 // If running, remove from run qs | |
| 336 if ( current->state == RUNNING ) | |
| 337 Cyg_Scheduler::scheduler.rem_thread(current); | |
| 338 | |
| 339 // Set the state | |
| 340 current->state |= SLEEPING; | |
| 341 | |
| 342 // Unlock the scheduler and switch threads | |
| 343 Cyg_Scheduler::unlock(); | |
| 344 | |
| 345 CYG_REPORT_RETURN(); | |
| 346 } | |
| 347 | |
| 348 // ------------------------------------------------------------------------- | |
| 349 // Awaken the thread from sleep. | |
| 350 | |
| 351 void | |
| 352 Cyg_Thread::wake() | |
| 353 { | |
| 354 CYG_REPORT_FUNCTION(); | |
| 355 | |
| 356 CYG_INSTRUMENT_THREAD(WAKE,this,Cyg_Scheduler::current_thread); | |
| 357 | |
| 358 // Prevent preemption | |
| 359 Cyg_Scheduler::lock(); | |
| 360 | |
| 361 if( 0 != (state & SLEEPSET) ) | |
| 362 { | |
| 363 // Set the state | |
| 364 state &= ~SLEEPSET; | |
| 365 | |
| 366 // remove from any queue we were on | |
| 367 remove(); | |
| 368 | |
| 369 // If the thread is now runnable, return it to run queue | |
| 370 if( state == RUNNING ) | |
| 371 Cyg_Scheduler::scheduler.add_thread(this); | |
| 372 | |
| 373 } | |
| 374 | |
| 375 // Unlock the scheduler and maybe switch threads | |
| 376 Cyg_Scheduler::unlock(); | |
| 377 | |
| 378 CYG_REPORT_RETURN(); | |
| 379 } | |
| 380 | |
| 381 // ------------------------------------------------------------------------- | |
| 382 // Put the thread to sleep, with wakeup count. | |
| 383 // This can only be called by the current thread on itself, hence | |
| 384 // it is a static function. | |
| 385 | |
| 386 void | |
| 387 Cyg_Thread::counted_sleep() | |
| 388 { | |
| 389 CYG_REPORT_FUNCTION(); | |
| 390 | |
| 391 Cyg_Thread *current = Cyg_Scheduler::get_current_thread(); | |
| 392 | |
| 393 CYG_ASSERTCLASS( current, "Bad current thread" ); | |
| 394 | |
| 395 CYG_INSTRUMENT_THREAD(SLEEP,current,0); | |
| 396 | |
| 397 // Prevent preemption | |
| 398 Cyg_Scheduler::lock(); | |
| 399 | |
| 400 if ( 0 == current->wakeup_count ) { | |
| 401 set_sleep_reason( Cyg_Thread::WAIT ); | |
| 402 current->sleep(); // prepare to sleep | |
| 403 current->state |= COUNTSLEEP; // Set the state | |
| 404 } | |
| 405 else | |
| 406 // there is a queued wakeup, do not sleep | |
| 407 current->wakeup_count--; | |
| 408 | |
| 409 // Unlock the scheduler and switch threads | |
| 410 Cyg_Scheduler::unlock(); | |
| 411 | |
| 412 // and deal with anything we must do when we return | |
| 413 switch( current->wake_reason ) { | |
| 414 case DESTRUCT: | |
| 415 case EXIT: | |
| 416 current->exit(); | |
| 417 break; | |
| 418 | |
| 419 default: | |
| 420 break; | |
| 421 } | |
| 422 | |
| 423 CYG_REPORT_RETURN(); | |
| 424 } | |
| 425 | |
| 426 // ------------------------------------------------------------------------- | |
| 427 // Put the thread to sleep for a delay, with wakeup count. | |
| 428 // This can only be called by the current thread on itself, hence | |
| 429 // it is a static function. | |
| 430 | |
| 431 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 432 void | |
| 433 Cyg_Thread::counted_sleep( cyg_tick_count delay ) | |
| 434 { | |
| 435 CYG_REPORT_FUNCTION(); | |
| 436 | |
| 437 Cyg_Thread *current = Cyg_Scheduler::get_current_thread(); | |
| 438 | |
| 439 CYG_ASSERTCLASS( current, "Bad current thread" ); | |
| 440 | |
| 441 CYG_INSTRUMENT_THREAD(SLEEP,current,0); | |
| 442 | |
| 443 // Prevent preemption | |
| 444 Cyg_Scheduler::lock(); | |
| 445 | |
| 446 if ( 0 == current->wakeup_count ) { | |
| 447 | |
| 448 // Set the timer (once outside any waiting loop.) | |
| 449 set_timer( Cyg_Clock::real_time_clock->current_value()+delay, | |
| 450 Cyg_Thread::TIMEOUT ); | |
| 451 | |
| 452 // If the timeout is in the past, the wake reason will have been | |
| 453 // set to something other than NONE already. | |
| 454 | |
| 455 if( current->get_wake_reason() == Cyg_Thread::NONE ) | |
| 456 { | |
| 457 set_sleep_reason( Cyg_Thread::TIMEOUT ); | |
| 458 current->sleep(); // prepare to sleep | |
| 459 current->state |= COUNTSLEEP; // Set the state | |
| 460 | |
| 461 CYG_ASSERT( Cyg_Scheduler::get_sched_lock() == 1, | |
| 462 "Called with non-zero scheduler lock"); | |
| 463 | |
| 464 Cyg_Scheduler::unlock(); | |
| 465 Cyg_Scheduler::lock(); | |
| 466 | |
| 467 // clear the timer; if it actually fired, no worries. | |
| 468 clear_timer(); | |
| 469 } | |
| 470 } | |
| 471 else | |
| 472 // there is a queued wakeup, do not sleep | |
| 473 current->wakeup_count--; | |
| 474 | |
| 475 // Unlock the scheduler and switch threads | |
| 476 Cyg_Scheduler::unlock(); | |
| 477 | |
| 478 // and deal with anything we must do when we return | |
| 479 switch( current->wake_reason ) { | |
| 480 case DESTRUCT: | |
| 481 case EXIT: | |
| 482 current->exit(); | |
| 483 break; | |
| 484 | |
| 485 default: | |
| 486 break; | |
| 487 } | |
| 488 | |
| 489 CYG_REPORT_RETURN(); | |
| 490 } | |
| 491 #endif | |
| 492 | |
| 493 // ------------------------------------------------------------------------- | |
| 494 // Awaken the thread from sleep. | |
| 495 | |
| 496 void | |
| 497 Cyg_Thread::counted_wake() | |
| 498 { | |
| 499 CYG_REPORT_FUNCTION(); | |
| 500 | |
| 501 CYG_INSTRUMENT_THREAD(WAKE,this,Cyg_Scheduler::current_thread); | |
| 502 | |
| 503 // Prevent preemption | |
| 504 Cyg_Scheduler::lock(); | |
| 505 | |
| 506 if ( 0 == (state & COUNTSLEEP) ) // already awake, or waiting: | |
| 507 wakeup_count++; // not in a counted sleep anyway. | |
| 508 else { | |
| 509 sleep_reason = NONE; | |
| 510 wake_reason = DONE; | |
| 511 wake(); // and awaken the thread | |
| 512 } | |
| 513 | |
| 514 #ifdef CYGNUM_KERNEL_MAX_COUNTED_WAKE_COUNT_ASSERT | |
| 515 CYG_ASSERT( CYGNUM_KERNEL_MAX_COUNTED_WAKE_COUNT_ASSERT > wakeup_count, | |
| 516 "wakeup_count overflow" ); | |
| 517 #endif | |
| 518 | |
| 519 // Unlock the scheduler and maybe switch threads | |
| 520 Cyg_Scheduler::unlock(); | |
| 521 | |
| 522 CYG_REPORT_RETURN(); | |
| 523 } | |
| 524 | |
| 525 // ------------------------------------------------------------------------- | |
| 526 // Cancel wakeups for this thread and return how many were pending | |
| 527 cyg_uint32 | |
| 528 Cyg_Thread::cancel_counted_wake() | |
| 529 { | |
| 530 CYG_REPORT_FUNCTION(); | |
| 531 | |
| 532 CYG_INSTRUMENT_THREAD(WAKE,this,Cyg_Scheduler::current_thread); | |
| 533 | |
| 534 // Prevent preemption | |
| 535 Cyg_Scheduler::lock(); | |
| 536 | |
| 537 cyg_uint32 result = wakeup_count; | |
| 538 wakeup_count = 0; | |
| 539 | |
| 540 // Unlock the scheduler | |
| 541 Cyg_Scheduler::unlock(); | |
| 542 | |
| 543 CYG_REPORT_RETVAL( result ); | |
| 544 return result; | |
| 545 } | |
| 546 | |
| 547 // ------------------------------------------------------------------------- | |
| 548 // Suspend thread. Increment suspend count and deschedule thread | |
| 549 // if still running. | |
| 550 | |
| 551 void | |
| 552 Cyg_Thread::suspend() | |
| 553 { | |
| 554 CYG_REPORT_FUNCTION(); | |
| 555 | |
| 556 CYG_INSTRUMENT_THREAD(SUSPEND,this,Cyg_Scheduler::current_thread); | |
| 557 | |
| 558 // Prevent preemption | |
| 559 Cyg_Scheduler::lock(); | |
| 560 | |
| 561 suspend_count++; | |
| 562 | |
| 563 #ifdef CYGNUM_KERNEL_MAX_SUSPEND_COUNT_ASSERT | |
| 564 CYG_ASSERT( CYGNUM_KERNEL_MAX_SUSPEND_COUNT_ASSERT > suspend_count, | |
| 565 "suspend_count overflow" ); | |
| 566 #endif | |
| 567 | |
| 568 // If running, remove from run qs | |
| 569 if( state == RUNNING ) | |
| 570 Cyg_Scheduler::scheduler.rem_thread(this); | |
| 571 | |
| 572 // Set the state | |
| 573 state |= SUSPENDED; | |
| 574 | |
| 575 // Unlock the scheduler and maybe switch threads | |
| 576 Cyg_Scheduler::unlock(); | |
| 577 | |
| 578 CYG_REPORT_RETURN(); | |
| 579 } | |
| 580 | |
| 581 // ------------------------------------------------------------------------- | |
| 582 // Resume thread. Decrement suspend count and reschedule if it | |
| 583 // is zero. | |
| 584 | |
| 585 void | |
| 586 Cyg_Thread::resume() | |
| 587 { | |
| 588 CYG_REPORT_FUNCTION(); | |
| 589 | |
| 590 CYG_INSTRUMENT_THREAD(RESUME,this,Cyg_Scheduler::current_thread); | |
| 591 | |
| 592 // Prevent preemption | |
| 593 Cyg_Scheduler::lock(); | |
| 594 | |
| 595 // If we are about to zero the count, clear the state bit and | |
| 596 // reschedule the thread if possible. | |
| 597 | |
| 598 if( suspend_count == 1 ) | |
| 599 { | |
| 600 suspend_count = 0; | |
| 601 | |
| 602 CYG_ASSERT( (state & SUSPENDED) != 0, "SUSPENDED bit not set" ); | |
| 603 | |
| 604 // Set the state | |
| 605 state &= ~SUSPENDED; | |
| 606 | |
| 607 // Return thread to scheduler if runnable | |
| 608 if( state == RUNNING ) | |
| 609 Cyg_Scheduler::scheduler.add_thread(this); | |
| 610 } | |
| 611 else | |
| 612 if( suspend_count > 0 ) | |
| 613 suspend_count--; | |
| 614 // else ignore attempt to resume | |
| 615 | |
| 616 // Unlock the scheduler and maybe switch threads | |
| 617 Cyg_Scheduler::unlock(); | |
| 618 CYG_REPORT_RETURN(); | |
| 619 } | |
| 620 | |
| 621 // ------------------------------------------------------------------------- | |
| 622 // Forced Resume thread. Zero suspend count and reschedule... | |
| 623 | |
| 624 void | |
| 625 Cyg_Thread::force_resume() | |
| 626 { | |
| 627 CYG_REPORT_FUNCTION(); | |
| 628 | |
| 629 CYG_INSTRUMENT_THREAD(RESUME,this,Cyg_Scheduler::current_thread); | |
| 630 | |
| 631 // Prevent preemption | |
| 632 Cyg_Scheduler::lock(); | |
| 633 | |
| 634 // If we are about to zero the count, clear the state bit and | |
| 635 // reschedule the thread if possible. | |
| 636 | |
| 637 if ( 0 < suspend_count ) { | |
| 638 suspend_count = 0; | |
| 639 | |
| 640 CYG_ASSERT( (state & SUSPENDED) != 0, "SUSPENDED bit not set" ); | |
| 641 | |
| 642 // Set the state | |
| 643 state &= ~SUSPENDED; | |
| 644 | |
| 645 // Return thread to scheduler if runnable | |
| 646 if( state == RUNNING ) | |
| 647 Cyg_Scheduler::scheduler.add_thread(this); | |
| 648 } | |
| 649 | |
| 650 // Unlock the scheduler and maybe switch threads | |
| 651 Cyg_Scheduler::unlock(); | |
| 652 CYG_REPORT_RETURN(); | |
| 653 } | |
| 654 | |
| 655 // ------------------------------------------------------------------------- | |
| 656 // Force thread to wake up from a sleep with a wake_reason of | |
| 657 // BREAK. It is the responsibility of the woken thread to detect | |
| 658 // the release() and do the right thing. | |
| 659 | |
| 660 void | |
| 661 Cyg_Thread::release() | |
| 662 { | |
| 663 CYG_REPORT_FUNCTION(); | |
| 664 // Prevent preemption | |
| 665 Cyg_Scheduler::lock(); | |
| 666 | |
| 667 // If the thread is in any of the sleep states, set the | |
| 668 // wake reason and wake it up. | |
| 669 | |
| 670 switch( sleep_reason ) | |
| 671 { | |
| 672 | |
| 673 case NONE: | |
| 674 // The thread is not sleeping for any reason, do nothing. | |
| 675 // drop through... | |
| 676 | |
| 677 case DESTRUCT: | |
| 678 case BREAK: | |
| 679 case EXIT: | |
| 680 case DONE: | |
| 681 // Do nothing in any of these cases. They are here to | |
| 682 // keep the compiler happy. | |
| 683 | |
| 684 Cyg_Scheduler::unlock(); | |
| 685 CYG_REPORT_RETURN(); | |
| 686 return; | |
| 687 | |
| 688 case WAIT: | |
| 689 // The thread was waiting for some sync object to do | |
| 690 // something. | |
| 691 // drop through... | |
| 692 | |
| 693 case TIMEOUT: | |
| 694 // The thread was waiting on a sync object with a timeout. | |
| 695 // drop through... | |
| 696 | |
| 697 case DELAY: | |
| 698 // The thread was simply delaying, unless it has been | |
| 699 // woken up for some other reason, wake it now. | |
| 700 sleep_reason = NONE; | |
| 701 wake_reason = BREAK; | |
| 702 break; | |
| 703 } | |
| 704 | |
| 705 wake(); | |
| 706 | |
| 707 // Allow preemption | |
| 708 Cyg_Scheduler::unlock(); | |
| 709 | |
| 710 CYG_REPORT_RETURN(); | |
| 711 } | |
| 712 | |
| 713 // ------------------------------------------------------------------------- | |
| 714 // Exit thread. This puts the thread into EXITED state. | |
| 715 | |
| 716 void | |
| 717 Cyg_Thread::exit() | |
| 718 { | |
| 719 // The thread should never return from this function. | |
| 720 | |
| 721 Cyg_Thread *self = Cyg_Thread::self(); | |
| 722 | |
| 723 Cyg_Scheduler::lock(); | |
| 724 | |
| 725 // clear the timer; if there was none, no worries. | |
| 726 clear_timer(); | |
| 727 | |
| 728 self->state = EXITED; | |
| 729 | |
| 730 Cyg_Scheduler::scheduler.rem_thread(self); | |
| 731 | |
| 732 // Un-nest any scheduler locks we have until we | |
| 733 // suspend. | |
| 734 for( ;; ) Cyg_Scheduler::unlock(); | |
| 735 } | |
| 736 | |
| 737 // ------------------------------------------------------------------------- | |
| 738 // Kill thread. Force the thread into EXITED state externally, or | |
| 739 // make it wake up and call exit(). | |
| 740 | |
| 741 void | |
| 742 Cyg_Thread::kill() | |
| 743 { | |
| 744 CYG_REPORT_FUNCTION(); | |
| 745 // If this is called by the current thread on itself, | |
| 746 // just call exit(), which is what he should have done | |
| 747 // in the first place. | |
| 748 if( this == Cyg_Scheduler::get_current_thread() ) | |
| 749 exit(); | |
| 750 | |
| 751 // Prevent preemption | |
| 752 Cyg_Scheduler::lock(); | |
| 753 | |
| 754 // We are killing someone else. Find out what state he is | |
| 755 // in and force him to wakeup and call exit(). | |
| 756 | |
| 757 force_resume(); // this is necessary for when | |
| 758 // he is asleep AND suspended. | |
| 759 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 760 timer.disable(); // and make sure the timer | |
| 761 // does not persist. | |
| 762 #endif | |
| 763 | |
|
8
ece80412419a
Merge from eCos master repository on 1999-05-21-22:05:54-BST
jlarmour
parents:
6
diff
changeset
|
764 if ( EXIT != wake_reason ) switch( sleep_reason ) { |
|
ece80412419a
Merge from eCos master repository on 1999-05-21-22:05:54-BST
jlarmour
parents:
6
diff
changeset
|
765 // Only do any of this if the thread is not in pending death already: |
| 0 | 766 |
| 767 case NONE: | |
| 768 // The thread is not sleeping for any reason, it must be | |
| 769 // on a run queue. | |
| 770 // We can safely deschedule and set its state. | |
| 771 if( state == RUNNING ) Cyg_Scheduler::scheduler.rem_thread(this); | |
| 772 state = EXITED; | |
| 773 break; | |
| 774 | |
| 775 case DESTRUCT: | |
| 776 case BREAK: | |
| 777 case EXIT: | |
| 778 case DONE: | |
| 779 // Do nothing in any of these cases. They are here to | |
| 780 // keep the compiler happy. | |
| 781 | |
| 782 Cyg_Scheduler::unlock(); | |
| 783 CYG_REPORT_RETURN(); | |
| 784 return; | |
| 785 | |
| 786 case WAIT: | |
| 787 // The thread was waiting for some sync object to do | |
| 788 // something. | |
| 789 // drop through... | |
| 790 | |
| 791 case TIMEOUT: | |
| 792 // The thread was waiting on a sync object with a timeout. | |
| 793 // drop through... | |
| 794 | |
| 795 case DELAY: | |
| 796 // The thread was simply delaying, unless it has been | |
| 797 // woken up for some other reason, wake it now. | |
| 798 sleep_reason = NONE; | |
| 799 wake_reason = EXIT; | |
| 800 break; | |
| 801 } | |
| 802 | |
| 803 wake(); | |
| 804 | |
| 805 // Allow preemption | |
| 806 Cyg_Scheduler::unlock(); | |
| 807 CYG_REPORT_RETURN(); | |
| 808 } | |
| 809 | |
| 810 // ------------------------------------------------------------------------- | |
| 811 // Set thread priority | |
| 812 | |
| 813 #ifdef CYGIMP_THREAD_PRIORITY | |
| 814 | |
| 815 void | |
| 816 Cyg_Thread::set_priority( cyg_priority new_priority ) | |
| 817 { | |
| 818 CYG_REPORT_FUNCTION(); | |
| 819 | |
| 820 // CYG_ASSERT( new_priority >= CYG_THREAD_MAX_PRIORITY, "Priority out of range"); | |
| 821 // CYG_ASSERT( new_priority <= CYG_THREAD_MIN_PRIORITY, "Priority out of range"); | |
| 822 | |
| 823 CYG_INSTRUMENT_THREAD(PRIORITY,this,priority); | |
| 824 | |
| 825 // Prevent preemption | |
| 826 Cyg_Scheduler::lock(); | |
| 827 | |
| 828 Cyg_ThreadQueue *queue = NULL; | |
| 829 | |
| 830 // If running, remove from run qs | |
| 831 if( state == RUNNING ) | |
| 832 Cyg_Scheduler::scheduler.rem_thread(this); | |
| 833 else if( state & SLEEPING ) | |
| 834 { | |
| 835 // Remove thread from current queue. | |
| 836 queue = get_current_queue(); | |
| 837 // if indeed we are on a queue | |
| 838 if ( NULL != queue ) { | |
| 839 CYG_CHECK_DATA_PTR(queue, "Bad queue pointer"); | |
| 840 remove(); | |
| 841 } | |
| 842 } | |
| 843 | |
| 844 Cyg_Scheduler::scheduler.deregister_thread(this); | |
| 845 | |
| 846 #if CYG_SCHED_UNIQUE_PRIORITIES | |
| 847 | |
| 848 // Check that there are no other threads at this priority. | |
| 849 // If so, leave is as it is. | |
| 850 | |
| 851 CYG_ASSERT( Cyg_Scheduler::scheduler.unique(new_priority), "Priority not unique"); | |
| 852 | |
| 853 if( Cyg_Scheduler::scheduler.unique(new_priority) ) | |
| 854 priority = new_priority; | |
| 855 | |
| 856 #else // !CYG_SCHED_UNIQUE_PRIORITIES | |
| 857 | |
| 858 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INHERITANCE_SIMPLE | |
| 859 | |
| 860 // When we have priority inheritance, we must update the original | |
| 861 // priority and not the inherited one. If the new priority is | |
| 862 // better than the current inherited one, then use that | |
| 863 // immediately. We remain in inherited state to avoid problems | |
| 864 // with multiple mutex inheritances. | |
| 865 | |
| 866 if( priority_inherited ) | |
| 867 { | |
| 868 original_priority = new_priority; | |
| 869 if( priority > new_priority ) priority = new_priority; | |
| 870 } | |
| 871 else priority = new_priority; | |
| 872 | |
| 873 #else | |
| 874 | |
| 875 priority = new_priority; | |
| 876 | |
| 877 #endif | |
| 878 | |
| 879 #endif // CYG_SCHED_UNIQUE_PRIORITIES | |
| 880 | |
| 881 Cyg_Scheduler::scheduler.register_thread(this); | |
| 882 | |
| 883 // Return thread to scheduler if runnable | |
| 884 if( state == RUNNING ) | |
| 885 Cyg_Scheduler::scheduler.add_thread(this); | |
| 886 else if ( state & SLEEPING ) | |
| 887 { | |
| 888 // return to current queue | |
| 889 // if indeed we are on a queue | |
| 890 if ( NULL != queue ) { | |
| 891 CYG_CHECK_DATA_PTR(queue, "Bad queue pointer"); | |
| 892 queue->enqueue(this); | |
| 893 } | |
| 894 } | |
| 895 | |
| 896 // If the current thread is being reprioritized, set the | |
| 897 // reschedule flag to ensure that it gets rescheduled if | |
| 898 // necessary. (Strictly we only need to do this if the new | |
| 899 // priority is less than that of some other runnable thread, in | |
| 900 // practice checking that is as expensive as what the scheduler | |
| 901 // will do anyway). | |
| 902 | |
| 903 if( this == Cyg_Scheduler::get_current_thread() ) | |
| 904 Cyg_Scheduler::need_reschedule = true; | |
| 905 | |
| 906 // Unlock the scheduler and maybe switch threads | |
| 907 Cyg_Scheduler::unlock(); | |
| 908 CYG_REPORT_RETURN(); | |
| 909 } | |
| 910 | |
| 911 #endif | |
| 912 | |
| 913 | |
| 914 // ------------------------------------------------------------------------- | |
| 915 // Thread delay function | |
| 916 | |
| 917 void | |
| 918 Cyg_Thread::delay( cyg_tick_count delay) | |
| 919 { | |
| 920 CYG_REPORT_FUNCTION(); | |
| 921 | |
| 2 | 922 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
| 0 | 923 |
| 924 CYG_INSTRUMENT_THREAD(DELAY,this,delay); | |
| 925 | |
| 926 // Prevent preemption | |
| 927 Cyg_Scheduler::lock(); | |
| 928 | |
| 929 sleep(); | |
| 930 | |
| 931 set_timer( Cyg_Clock::real_time_clock->current_value()+delay, DELAY ); | |
| 932 | |
| 933 // Unlock the scheduler and maybe switch threads | |
| 934 Cyg_Scheduler::unlock(); | |
| 935 | |
| 936 // Clear the timeout. It is irrelevant whether the alarm has | |
| 937 // actually gone off or not. | |
| 938 clear_timer(); | |
| 939 | |
| 940 // and deal with anything else we must do when we return | |
| 941 switch( wake_reason ) { | |
| 942 case DESTRUCT: | |
| 943 case EXIT: | |
| 944 exit(); | |
| 945 break; | |
| 946 | |
| 947 default: | |
| 948 break; | |
| 949 } | |
| 950 #endif | |
| 951 CYG_REPORT_RETURN(); | |
| 952 } | |
| 953 | |
| 954 // ------------------------------------------------------------------------- | |
| 955 // | |
| 956 | |
| 957 #ifdef CYGPKG_KERNEL_EXCEPTIONS | |
| 958 | |
| 959 void | |
| 960 Cyg_Thread::deliver_exception( | |
| 961 cyg_code exception_number, // exception being raised | |
| 962 CYG_ADDRWORD exception_info // exception specific info | |
| 963 ) | |
| 964 { | |
| 965 if( this == Cyg_Scheduler::get_current_thread() ) | |
| 966 { | |
| 967 // Delivering to current thread, probably as a result | |
| 968 // of a real hardware exception. Simply invoke the appropriate | |
| 969 // handler. | |
| 970 | |
| 971 exception_control.deliver_exception( exception_number, exception_info ); | |
| 972 } | |
| 973 #ifdef CYGIMP_EXCEPTION_ASYNC | |
| 974 else | |
| 975 { | |
| 976 // Delivering to another thread, probably as a result of one thread | |
| 977 // invoking this function on another thread. Adjust the other thread's | |
| 978 // state to make it execute the exception routine when it next runs. | |
| 979 | |
| 980 // At present there is an unresolved problem here. We do not know what | |
| 981 // state the destination thread is in. It may not be a suitable point at | |
| 982 // which to invoke an exception routine. In most cases the exception | |
| 983 // routine will be run in the scheduler thread switch code, where the world is | |
| 984 // in an inconsistent state. We really need to run the routine at the | |
| 985 // end of unlock_inner(). However this would add extra code to the scheduler, | |
| 986 // and require a way of storing pending exceptions. So for now this option is | |
| 987 // disabled and not yet implemented, it may never be. | |
| 988 | |
| 989 } | |
| 990 #endif | |
| 991 } | |
| 992 | |
| 993 #endif | |
| 994 | |
| 995 // ------------------------------------------------------------------------- | |
| 996 // Per-thread data support | |
| 997 | |
| 998 #ifdef CYGVAR_KERNEL_THREADS_DATA | |
| 999 | |
| 1000 // Set the data map bits for each free slot in the data array. | |
| 1001 cyg_ucount32 Cyg_Thread::thread_data_map = (~CYGNUM_KERNEL_THREADS_DATA_ALL) & | |
| 1002 ((1<<CYGNUM_KERNEL_THREADS_DATA_MAX)-1); | |
| 1003 | |
| 1004 cyg_ucount32 | |
| 1005 Cyg_Thread::new_data_index() | |
| 1006 { | |
| 1007 Cyg_Scheduler::lock(); | |
| 1008 | |
| 1009 cyg_ucount32 index; | |
| 1010 | |
| 1011 CYG_ASSERT( thread_data_map != 0 , "No more thread data indexes"); | |
| 1012 | |
| 1013 // find ls set bit | |
| 1014 HAL_LSBIT_INDEX( index, thread_data_map ); | |
| 1015 | |
| 1016 // clear the bit | |
| 1017 thread_data_map &= ~(1<<index); | |
| 1018 | |
| 1019 Cyg_Scheduler::unlock(); | |
| 1020 | |
| 1021 return index; | |
| 1022 } | |
| 1023 | |
| 1024 void Cyg_Thread::free_data_index( cyg_ucount32 index ) | |
| 1025 { | |
| 1026 Cyg_Scheduler::lock(); | |
| 1027 | |
| 1028 thread_data_map |= (1<<index); | |
| 1029 | |
| 1030 Cyg_Scheduler::unlock(); | |
| 1031 } | |
| 1032 | |
| 1033 | |
| 1034 #endif | |
| 1035 | |
| 1036 // ========================================================================= | |
| 1037 // Cyg_ThreadTimer member functions | |
| 1038 | |
| 1039 // ------------------------------------------------------------------------- | |
| 1040 // Timer alarm function. Inspect the sleep_reason and if necessary wake | |
| 1041 // up the thread with an appropriate wake_reason. | |
| 1042 | |
| 1043 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 1044 | |
| 1045 void | |
| 1046 Cyg_ThreadTimer::alarm( | |
| 1047 Cyg_Alarm *alarm, | |
| 1048 CYG_ADDRWORD data | |
| 1049 ) | |
| 1050 { | |
| 1051 CYG_REPORT_FUNCTION(); | |
| 1052 | |
| 1053 Cyg_ThreadTimer *self = (Cyg_ThreadTimer *)data; | |
| 1054 Cyg_Thread *thread = self->thread; | |
| 1055 | |
| 1056 CYG_INSTRUMENT_THREAD(ALARM, 0, 0); | |
| 1057 | |
| 1058 Cyg_Scheduler::lock(); | |
| 1059 | |
| 1060 Cyg_Thread::cyg_reason sleep_reason = thread->get_sleep_reason(); | |
| 1061 | |
| 1062 switch( sleep_reason ) { | |
| 1063 | |
| 1064 case Cyg_Thread::DESTRUCT: | |
| 1065 case Cyg_Thread::BREAK: | |
| 1066 case Cyg_Thread::EXIT: | |
| 1067 case Cyg_Thread::NONE: | |
| 1068 case Cyg_Thread::WAIT: | |
| 1069 case Cyg_Thread::DONE: | |
| 1070 // Do nothing in any of these cases. Most are here to | |
| 1071 // keep the compiler happy. | |
| 1072 Cyg_Scheduler::unlock(); | |
| 1073 CYG_REPORT_RETURN(); | |
| 1074 return; | |
| 1075 | |
| 1076 case Cyg_Thread::DELAY: | |
| 1077 // The thread was simply delaying, unless it has been | |
| 1078 // woken up for some other reason, wake it now. | |
| 1079 thread->set_wake_reason(Cyg_Thread::DONE); | |
| 1080 break; | |
| 1081 | |
| 1082 case Cyg_Thread::TIMEOUT: | |
| 1083 // The thread has timed out, set the wake reason to | |
| 1084 // TIMEOUT and restart. | |
| 1085 thread->set_wake_reason(Cyg_Thread::TIMEOUT); | |
| 1086 break; | |
| 1087 } | |
| 1088 | |
| 1089 thread->wake(); | |
| 1090 | |
| 1091 Cyg_Scheduler::unlock(); | |
| 1092 CYG_REPORT_RETURN(); | |
| 1093 } | |
| 1094 | |
| 1095 #endif | |
| 1096 | |
| 1097 // ========================================================================= | |
| 1098 // The Idle thread | |
| 1099 // The idle thread is implemented as a single instance of the | |
| 1100 // Cyg_IdleThread class. This is so that it can be initialized before | |
| 1101 // main in a static constructor. | |
| 1102 | |
| 1103 // ------------------------------------------------------------------------- | |
| 1104 // Data definitions | |
| 1105 | |
| 1106 // stack | |
| 2 | 1107 #ifdef CYGNUM_HAL_STACK_SIZE_MINIMUM |
| 1108 # ifdef CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE | |
| 1109 # if CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE < CYGNUM_HAL_STACK_SIZE_MINIMUM | |
| 0 | 1110 |
| 2 | 1111 // then override the configured stack size |
| 1112 # undef CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE | |
| 1113 # define CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE CYGNUM_HAL_STACK_SIZE_MINIMUM | |
| 1114 | |
| 1115 # endif // CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE < CYGNUM_HAL_STACK_SIZE_MINIMUM | |
| 1116 # endif // CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE | |
| 1117 #endif // CYGNUM_HAL_STACK_SIZE_MINIMUM | |
| 1118 | |
| 1119 static char idle_thread_stack[CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE]; | |
| 0 | 1120 |
| 1121 // Loop counter for debugging/housekeeping | |
| 1122 cyg_uint32 idle_thread_loops = 1; | |
| 1123 | |
| 1124 // ------------------------------------------------------------------------- | |
| 1125 // Idle thread code. | |
| 1126 | |
| 1127 void | |
| 1128 idle_thread_main( CYG_ADDRESS data ) | |
| 1129 { | |
| 1130 CYG_REPORT_FUNCTION(); | |
| 1131 | |
| 1132 for(;;) | |
| 1133 { | |
| 1134 idle_thread_loops++; | |
| 1135 | |
| 1136 HAL_IDLE_THREAD_ACTION(idle_thread_loops); | |
| 1137 | |
| 1138 #if 0 | |
| 1139 // For testing, it is useful to be able to fake | |
| 1140 // clock interrupts in the idle thread. | |
| 1141 | |
| 1142 Cyg_Clock::real_time_clock->tick(); | |
| 1143 #endif | |
| 1144 #ifdef CYGIMP_IDLE_THREAD_YIELD | |
| 1145 // In single priority and non-preemptive systems, | |
| 1146 // the idle thread should yield repeatedly to | |
| 1147 // other threads. | |
| 1148 Cyg_Thread::yield(); | |
| 1149 #endif | |
| 1150 } | |
| 1151 } | |
| 1152 | |
| 1153 // ------------------------------------------------------------------------- | |
| 1154 // Idle thread class | |
| 1155 | |
| 1156 class Cyg_IdleThread : public Cyg_Thread | |
| 1157 { | |
| 1158 public: | |
| 1159 Cyg_IdleThread( | |
| 1160 cyg_thread_entry *entry, // entry point function | |
| 1161 CYG_ADDRWORD entry_data, // entry data | |
| 1162 cyg_ucount32 stack_size = 0, // stack size, 0 = use default | |
| 1163 CYG_ADDRESS stack_base = 0 // stack base, NULL = allocate | |
| 1164 ); | |
| 1165 | |
| 1166 }; | |
| 1167 | |
| 1168 // ------------------------------------------------------------------------- | |
| 1169 // Idle threads constructor | |
| 1170 | |
| 1171 Cyg_IdleThread::Cyg_IdleThread( | |
| 1172 cyg_thread_entry *entry, // entry point function | |
| 1173 CYG_ADDRWORD entry_data, // entry data | |
| 1174 cyg_ucount32 stack_size, // stack size, 0 = use default | |
| 1175 CYG_ADDRESS stack_base // stack base, NULL = allocate | |
| 1176 ) | |
| 1177 : Cyg_Thread( CYG_THREAD_MIN_PRIORITY, | |
| 1178 entry, | |
| 1179 entry_data, | |
| 1180 "Idle Thread", | |
| 1181 stack_base, | |
| 1182 stack_size) | |
| 1183 { | |
| 1184 CYG_REPORT_FUNCTION(); | |
| 1185 | |
| 1186 resume(); | |
| 1187 CYG_REPORT_RETURN(); | |
| 1188 } | |
| 1189 | |
| 1190 // ------------------------------------------------------------------------- | |
| 1191 // Instantiate the idle thread | |
| 1192 | |
| 1193 Cyg_IdleThread idle_thread CYG_INIT_PRIORITY( IDLE_THREAD ) = | |
| 1194 Cyg_IdleThread( idle_thread_main, | |
| 1195 0, | |
| 1196 CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE, | |
| 1197 CYG_ADDRESS(idle_thread_stack) | |
| 1198 ); | |
| 1199 | |
| 1200 | |
| 1201 // ------------------------------------------------------------------------- | |
| 1202 // EOF common/thread.cxx |
