Mercurial > ecos-v3_0-branch
annotate packages/kernel/current/src/common/thread.cxx @ 132:0ae0bc38e387 ecos-sw-2000-10-31
Merge from eCos master repository on 2000-10-31-00:30:36-GMT
| author | jlarmour |
|---|---|
| date | Tue, 31 Oct 2000 20:53:09 +0000 |
| parents | 0ec04793409a |
| children | d397fc472bcf |
| 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 | |
|
78
59d97b6ba612
Merge from eCos master repository on 2000-03-28-19:50:47-BST
jlarmour
parents:
66
diff
changeset
|
285 cyg_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 | |
|
124
0ec04793409a
Merge from eCos master repository on 2000-09-11-03:00:13-BST
jlarmour
parents:
115
diff
changeset
|
461 Cyg_Scheduler::reschedule(); |
| 0 | 462 |
| 463 // clear the timer; if it actually fired, no worries. | |
| 464 clear_timer(); | |
| 465 } | |
| 466 } | |
| 467 else | |
| 468 // there is a queued wakeup, do not sleep | |
| 469 current->wakeup_count--; | |
| 470 | |
| 471 // Unlock the scheduler and switch threads | |
| 472 Cyg_Scheduler::unlock(); | |
| 473 | |
| 474 // and deal with anything we must do when we return | |
| 475 switch( current->wake_reason ) { | |
| 476 case DESTRUCT: | |
| 477 case EXIT: | |
| 478 current->exit(); | |
| 479 break; | |
| 480 | |
| 481 default: | |
| 482 break; | |
| 483 } | |
| 484 | |
| 485 CYG_REPORT_RETURN(); | |
| 486 } | |
| 487 #endif | |
| 488 | |
| 489 // ------------------------------------------------------------------------- | |
| 490 // Awaken the thread from sleep. | |
| 491 | |
| 492 void | |
| 493 Cyg_Thread::counted_wake() | |
| 494 { | |
| 495 CYG_REPORT_FUNCTION(); | |
| 496 | |
| 497 CYG_INSTRUMENT_THREAD(WAKE,this,Cyg_Scheduler::current_thread); | |
| 498 | |
| 499 // Prevent preemption | |
| 500 Cyg_Scheduler::lock(); | |
| 501 | |
| 502 if ( 0 == (state & COUNTSLEEP) ) // already awake, or waiting: | |
| 503 wakeup_count++; // not in a counted sleep anyway. | |
| 504 else { | |
| 505 sleep_reason = NONE; | |
| 506 wake_reason = DONE; | |
| 507 wake(); // and awaken the thread | |
| 508 } | |
| 509 | |
| 510 #ifdef CYGNUM_KERNEL_MAX_COUNTED_WAKE_COUNT_ASSERT | |
| 511 CYG_ASSERT( CYGNUM_KERNEL_MAX_COUNTED_WAKE_COUNT_ASSERT > wakeup_count, | |
| 512 "wakeup_count overflow" ); | |
| 513 #endif | |
| 514 | |
| 515 // Unlock the scheduler and maybe switch threads | |
| 516 Cyg_Scheduler::unlock(); | |
| 517 | |
| 518 CYG_REPORT_RETURN(); | |
| 519 } | |
| 520 | |
| 521 // ------------------------------------------------------------------------- | |
| 522 // Cancel wakeups for this thread and return how many were pending | |
| 523 cyg_uint32 | |
| 524 Cyg_Thread::cancel_counted_wake() | |
| 525 { | |
| 526 CYG_REPORT_FUNCTION(); | |
| 527 | |
| 528 CYG_INSTRUMENT_THREAD(WAKE,this,Cyg_Scheduler::current_thread); | |
| 529 | |
| 530 // Prevent preemption | |
| 531 Cyg_Scheduler::lock(); | |
| 532 | |
| 533 cyg_uint32 result = wakeup_count; | |
| 534 wakeup_count = 0; | |
| 535 | |
| 536 // Unlock the scheduler | |
| 537 Cyg_Scheduler::unlock(); | |
| 538 | |
| 539 CYG_REPORT_RETVAL( result ); | |
| 540 return result; | |
| 541 } | |
| 542 | |
| 543 // ------------------------------------------------------------------------- | |
| 544 // Suspend thread. Increment suspend count and deschedule thread | |
| 545 // if still running. | |
| 546 | |
| 547 void | |
| 548 Cyg_Thread::suspend() | |
| 549 { | |
| 550 CYG_REPORT_FUNCTION(); | |
| 551 | |
| 552 CYG_INSTRUMENT_THREAD(SUSPEND,this,Cyg_Scheduler::current_thread); | |
| 553 | |
| 554 // Prevent preemption | |
| 555 Cyg_Scheduler::lock(); | |
| 556 | |
| 557 suspend_count++; | |
| 558 | |
| 559 #ifdef CYGNUM_KERNEL_MAX_SUSPEND_COUNT_ASSERT | |
| 560 CYG_ASSERT( CYGNUM_KERNEL_MAX_SUSPEND_COUNT_ASSERT > suspend_count, | |
| 561 "suspend_count overflow" ); | |
| 562 #endif | |
| 563 | |
| 564 // If running, remove from run qs | |
| 565 if( state == RUNNING ) | |
| 566 Cyg_Scheduler::scheduler.rem_thread(this); | |
| 567 | |
| 568 // Set the state | |
| 569 state |= SUSPENDED; | |
| 570 | |
| 571 // Unlock the scheduler and maybe switch threads | |
| 572 Cyg_Scheduler::unlock(); | |
| 573 | |
| 574 CYG_REPORT_RETURN(); | |
| 575 } | |
| 576 | |
| 577 // ------------------------------------------------------------------------- | |
| 578 // Resume thread. Decrement suspend count and reschedule if it | |
| 579 // is zero. | |
| 580 | |
| 581 void | |
| 582 Cyg_Thread::resume() | |
| 583 { | |
| 584 CYG_REPORT_FUNCTION(); | |
| 585 | |
| 586 CYG_INSTRUMENT_THREAD(RESUME,this,Cyg_Scheduler::current_thread); | |
| 587 | |
| 588 // Prevent preemption | |
| 589 Cyg_Scheduler::lock(); | |
| 590 | |
| 591 // If we are about to zero the count, clear the state bit and | |
| 592 // reschedule the thread if possible. | |
| 593 | |
| 594 if( suspend_count == 1 ) | |
| 595 { | |
| 596 suspend_count = 0; | |
| 597 | |
| 598 CYG_ASSERT( (state & SUSPENDED) != 0, "SUSPENDED bit not set" ); | |
| 599 | |
| 600 // Set the state | |
| 601 state &= ~SUSPENDED; | |
| 602 | |
| 603 // Return thread to scheduler if runnable | |
| 604 if( state == RUNNING ) | |
| 605 Cyg_Scheduler::scheduler.add_thread(this); | |
| 606 } | |
| 607 else | |
| 608 if( suspend_count > 0 ) | |
| 609 suspend_count--; | |
| 610 // else ignore attempt to resume | |
| 611 | |
| 612 // Unlock the scheduler and maybe switch threads | |
| 613 Cyg_Scheduler::unlock(); | |
| 614 CYG_REPORT_RETURN(); | |
| 615 } | |
| 616 | |
| 617 // ------------------------------------------------------------------------- | |
| 618 // Forced Resume thread. Zero suspend count and reschedule... | |
| 619 | |
| 620 void | |
| 621 Cyg_Thread::force_resume() | |
| 622 { | |
| 623 CYG_REPORT_FUNCTION(); | |
| 624 | |
| 625 CYG_INSTRUMENT_THREAD(RESUME,this,Cyg_Scheduler::current_thread); | |
| 626 | |
| 627 // Prevent preemption | |
| 628 Cyg_Scheduler::lock(); | |
| 629 | |
| 630 // If we are about to zero the count, clear the state bit and | |
| 631 // reschedule the thread if possible. | |
| 632 | |
| 633 if ( 0 < suspend_count ) { | |
| 634 suspend_count = 0; | |
| 635 | |
| 636 CYG_ASSERT( (state & SUSPENDED) != 0, "SUSPENDED bit not set" ); | |
| 637 | |
| 638 // Set the state | |
| 639 state &= ~SUSPENDED; | |
| 640 | |
| 641 // Return thread to scheduler if runnable | |
| 642 if( state == RUNNING ) | |
| 643 Cyg_Scheduler::scheduler.add_thread(this); | |
| 644 } | |
| 645 | |
| 646 // Unlock the scheduler and maybe switch threads | |
| 647 Cyg_Scheduler::unlock(); | |
| 648 CYG_REPORT_RETURN(); | |
| 649 } | |
| 650 | |
| 651 // ------------------------------------------------------------------------- | |
| 652 // Force thread to wake up from a sleep with a wake_reason of | |
| 653 // BREAK. It is the responsibility of the woken thread to detect | |
| 654 // the release() and do the right thing. | |
| 655 | |
| 656 void | |
| 657 Cyg_Thread::release() | |
| 658 { | |
| 659 CYG_REPORT_FUNCTION(); | |
| 660 // Prevent preemption | |
| 661 Cyg_Scheduler::lock(); | |
| 662 | |
| 663 // If the thread is in any of the sleep states, set the | |
| 664 // wake reason and wake it up. | |
| 665 | |
| 666 switch( sleep_reason ) | |
| 667 { | |
| 668 | |
| 669 case NONE: | |
| 670 // The thread is not sleeping for any reason, do nothing. | |
| 671 // drop through... | |
| 672 | |
| 673 case DESTRUCT: | |
| 674 case BREAK: | |
| 675 case EXIT: | |
| 676 case DONE: | |
| 677 // Do nothing in any of these cases. They are here to | |
| 678 // keep the compiler happy. | |
| 679 | |
| 680 Cyg_Scheduler::unlock(); | |
| 681 CYG_REPORT_RETURN(); | |
| 682 return; | |
| 683 | |
| 684 case WAIT: | |
| 685 // The thread was waiting for some sync object to do | |
| 686 // something. | |
| 687 // drop through... | |
| 688 | |
| 689 case TIMEOUT: | |
| 690 // The thread was waiting on a sync object with a timeout. | |
| 691 // drop through... | |
| 692 | |
| 693 case DELAY: | |
| 694 // The thread was simply delaying, unless it has been | |
| 695 // woken up for some other reason, wake it now. | |
| 696 sleep_reason = NONE; | |
| 697 wake_reason = BREAK; | |
| 698 break; | |
| 699 } | |
| 700 | |
| 701 wake(); | |
| 702 | |
| 703 // Allow preemption | |
| 704 Cyg_Scheduler::unlock(); | |
| 705 | |
| 706 CYG_REPORT_RETURN(); | |
| 707 } | |
| 708 | |
| 709 // ------------------------------------------------------------------------- | |
| 710 // Exit thread. This puts the thread into EXITED state. | |
| 711 | |
| 712 void | |
| 713 Cyg_Thread::exit() | |
| 714 { | |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
78
diff
changeset
|
715 CYG_REPORT_FUNCTION(); |
|
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
78
diff
changeset
|
716 |
| 0 | 717 // The thread should never return from this function. |
| 718 | |
| 719 Cyg_Thread *self = Cyg_Thread::self(); | |
| 720 | |
| 721 Cyg_Scheduler::lock(); | |
| 722 | |
| 723 // clear the timer; if there was none, no worries. | |
| 724 clear_timer(); | |
| 725 | |
| 726 self->state = EXITED; | |
| 727 | |
| 728 Cyg_Scheduler::scheduler.rem_thread(self); | |
| 729 | |
| 730 // Un-nest any scheduler locks we have until we | |
| 731 // suspend. | |
| 732 for( ;; ) Cyg_Scheduler::unlock(); | |
| 733 } | |
| 734 | |
| 735 // ------------------------------------------------------------------------- | |
| 736 // Kill thread. Force the thread into EXITED state externally, or | |
| 737 // make it wake up and call exit(). | |
| 738 | |
| 739 void | |
| 740 Cyg_Thread::kill() | |
| 741 { | |
| 742 CYG_REPORT_FUNCTION(); | |
| 743 // If this is called by the current thread on itself, | |
| 744 // just call exit(), which is what he should have done | |
| 745 // in the first place. | |
| 746 if( this == Cyg_Scheduler::get_current_thread() ) | |
| 747 exit(); | |
| 748 | |
| 749 // Prevent preemption | |
| 750 Cyg_Scheduler::lock(); | |
| 751 | |
| 752 // We are killing someone else. Find out what state he is | |
| 753 // in and force him to wakeup and call exit(). | |
| 754 | |
| 755 force_resume(); // this is necessary for when | |
| 756 // he is asleep AND suspended. | |
| 757 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 758 timer.disable(); // and make sure the timer | |
| 759 // does not persist. | |
| 760 #endif | |
| 761 | |
|
8
ece80412419a
Merge from eCos master repository on 1999-05-21-22:05:54-BST
jlarmour
parents:
6
diff
changeset
|
762 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
|
763 // Only do any of this if the thread is not in pending death already: |
| 0 | 764 |
| 765 case NONE: | |
| 766 // The thread is not sleeping for any reason, it must be | |
| 767 // on a run queue. | |
| 768 // We can safely deschedule and set its state. | |
| 769 if( state == RUNNING ) Cyg_Scheduler::scheduler.rem_thread(this); | |
| 770 state = EXITED; | |
| 771 break; | |
| 772 | |
| 773 case DESTRUCT: | |
| 774 case BREAK: | |
| 775 case EXIT: | |
| 776 case DONE: | |
| 777 // Do nothing in any of these cases. They are here to | |
| 778 // keep the compiler happy. | |
| 779 | |
| 780 Cyg_Scheduler::unlock(); | |
| 781 CYG_REPORT_RETURN(); | |
| 782 return; | |
| 783 | |
| 784 case WAIT: | |
| 785 // The thread was waiting for some sync object to do | |
| 786 // something. | |
| 787 // drop through... | |
| 788 | |
| 789 case TIMEOUT: | |
| 790 // The thread was waiting on a sync object with a timeout. | |
| 791 // drop through... | |
| 792 | |
| 793 case DELAY: | |
| 794 // The thread was simply delaying, unless it has been | |
| 795 // woken up for some other reason, wake it now. | |
| 796 sleep_reason = NONE; | |
| 797 wake_reason = EXIT; | |
| 798 break; | |
| 799 } | |
| 800 | |
| 801 wake(); | |
| 802 | |
| 803 // Allow preemption | |
| 804 Cyg_Scheduler::unlock(); | |
| 805 CYG_REPORT_RETURN(); | |
| 806 } | |
| 807 | |
| 808 // ------------------------------------------------------------------------- | |
| 809 // Set thread priority | |
| 810 | |
| 811 #ifdef CYGIMP_THREAD_PRIORITY | |
| 812 | |
| 813 void | |
| 814 Cyg_Thread::set_priority( cyg_priority new_priority ) | |
| 815 { | |
| 816 CYG_REPORT_FUNCTION(); | |
| 817 | |
| 818 // CYG_ASSERT( new_priority >= CYG_THREAD_MAX_PRIORITY, "Priority out of range"); | |
| 819 // CYG_ASSERT( new_priority <= CYG_THREAD_MIN_PRIORITY, "Priority out of range"); | |
| 820 | |
| 821 CYG_INSTRUMENT_THREAD(PRIORITY,this,priority); | |
| 822 | |
| 823 // Prevent preemption | |
| 824 Cyg_Scheduler::lock(); | |
| 825 | |
| 826 Cyg_ThreadQueue *queue = NULL; | |
| 827 | |
| 828 // If running, remove from run qs | |
| 829 if( state == RUNNING ) | |
| 830 Cyg_Scheduler::scheduler.rem_thread(this); | |
| 831 else if( state & SLEEPING ) | |
| 832 { | |
| 833 // Remove thread from current queue. | |
| 834 queue = get_current_queue(); | |
| 835 // if indeed we are on a queue | |
| 836 if ( NULL != queue ) { | |
| 837 CYG_CHECK_DATA_PTR(queue, "Bad queue pointer"); | |
| 838 remove(); | |
| 839 } | |
| 840 } | |
| 841 | |
| 842 Cyg_Scheduler::scheduler.deregister_thread(this); | |
| 843 | |
|
132
0ae0bc38e387
Merge from eCos master repository on 2000-10-31-00:30:36-GMT
jlarmour
parents:
124
diff
changeset
|
844 #if CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES |
| 0 | 845 |
| 846 // Check that there are no other threads at this priority. | |
| 847 // If so, leave is as it is. | |
| 848 | |
| 849 CYG_ASSERT( Cyg_Scheduler::scheduler.unique(new_priority), "Priority not unique"); | |
| 850 | |
| 851 if( Cyg_Scheduler::scheduler.unique(new_priority) ) | |
| 852 priority = new_priority; | |
| 853 | |
|
132
0ae0bc38e387
Merge from eCos master repository on 2000-10-31-00:30:36-GMT
jlarmour
parents:
124
diff
changeset
|
854 #else // !CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES |
| 0 | 855 |
|
115
6ed91473a1cd
Merge from eCos master repository on 2000-08-21-22:40:54-BST
jlarmour
parents:
78
diff
changeset
|
856 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE |
| 0 | 857 |
| 858 // When we have priority inheritance, we must update the original | |
| 859 // priority and not the inherited one. If the new priority is | |
| 860 // better than the current inherited one, then use that | |
| 861 // immediately. We remain in inherited state to avoid problems | |
| 862 // with multiple mutex inheritances. | |
| 863 | |
| 864 if( priority_inherited ) | |
| 865 { | |
| 866 original_priority = new_priority; | |
| 867 if( priority > new_priority ) priority = new_priority; | |
| 868 } | |
| 869 else priority = new_priority; | |
| 870 | |
| 871 #else | |
| 872 | |
| 873 priority = new_priority; | |
| 874 | |
| 875 #endif | |
| 876 | |
|
132
0ae0bc38e387
Merge from eCos master repository on 2000-10-31-00:30:36-GMT
jlarmour
parents:
124
diff
changeset
|
877 #endif // CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES |
| 0 | 878 |
| 879 Cyg_Scheduler::scheduler.register_thread(this); | |
| 880 | |
| 881 // Return thread to scheduler if runnable | |
| 882 if( state == RUNNING ) | |
| 883 Cyg_Scheduler::scheduler.add_thread(this); | |
| 884 else if ( state & SLEEPING ) | |
| 885 { | |
| 886 // return to current queue | |
| 887 // if indeed we are on a queue | |
| 888 if ( NULL != queue ) { | |
| 889 CYG_CHECK_DATA_PTR(queue, "Bad queue pointer"); | |
| 890 queue->enqueue(this); | |
| 891 } | |
| 892 } | |
| 893 | |
| 894 // If the current thread is being reprioritized, set the | |
| 895 // reschedule flag to ensure that it gets rescheduled if | |
| 896 // necessary. (Strictly we only need to do this if the new | |
| 897 // priority is less than that of some other runnable thread, in | |
| 898 // practice checking that is as expensive as what the scheduler | |
| 899 // will do anyway). | |
| 900 | |
| 901 if( this == Cyg_Scheduler::get_current_thread() ) | |
| 902 Cyg_Scheduler::need_reschedule = true; | |
| 903 | |
| 904 // Unlock the scheduler and maybe switch threads | |
| 905 Cyg_Scheduler::unlock(); | |
| 906 CYG_REPORT_RETURN(); | |
| 907 } | |
| 908 | |
| 909 #endif | |
| 910 | |
| 911 | |
| 912 // ------------------------------------------------------------------------- | |
| 913 // Thread delay function | |
| 914 | |
| 915 void | |
| 916 Cyg_Thread::delay( cyg_tick_count delay) | |
| 917 { | |
| 918 CYG_REPORT_FUNCTION(); | |
| 919 | |
| 2 | 920 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
| 0 | 921 |
| 922 CYG_INSTRUMENT_THREAD(DELAY,this,delay); | |
| 923 | |
| 924 // Prevent preemption | |
| 925 Cyg_Scheduler::lock(); | |
| 926 | |
| 927 sleep(); | |
| 928 | |
| 929 set_timer( Cyg_Clock::real_time_clock->current_value()+delay, DELAY ); | |
| 930 | |
| 931 // Unlock the scheduler and maybe switch threads | |
| 932 Cyg_Scheduler::unlock(); | |
| 933 | |
| 934 // Clear the timeout. It is irrelevant whether the alarm has | |
| 935 // actually gone off or not. | |
| 936 clear_timer(); | |
| 937 | |
| 938 // and deal with anything else we must do when we return | |
| 939 switch( wake_reason ) { | |
| 940 case DESTRUCT: | |
| 941 case EXIT: | |
| 942 exit(); | |
| 943 break; | |
| 944 | |
| 945 default: | |
| 946 break; | |
| 947 } | |
| 948 #endif | |
| 949 CYG_REPORT_RETURN(); | |
| 950 } | |
| 951 | |
| 952 // ------------------------------------------------------------------------- | |
| 953 // | |
| 954 | |
| 955 #ifdef CYGPKG_KERNEL_EXCEPTIONS | |
| 956 | |
| 957 void | |
| 958 Cyg_Thread::deliver_exception( | |
| 959 cyg_code exception_number, // exception being raised | |
| 960 CYG_ADDRWORD exception_info // exception specific info | |
| 961 ) | |
| 962 { | |
| 963 if( this == Cyg_Scheduler::get_current_thread() ) | |
| 964 { | |
| 965 // Delivering to current thread, probably as a result | |
| 966 // of a real hardware exception. Simply invoke the appropriate | |
| 967 // handler. | |
| 968 | |
| 969 exception_control.deliver_exception( exception_number, exception_info ); | |
| 970 } | |
| 971 #ifdef CYGIMP_EXCEPTION_ASYNC | |
| 972 else | |
| 973 { | |
| 974 // Delivering to another thread, probably as a result of one thread | |
| 975 // invoking this function on another thread. Adjust the other thread's | |
| 976 // state to make it execute the exception routine when it next runs. | |
| 977 | |
| 978 // At present there is an unresolved problem here. We do not know what | |
| 979 // state the destination thread is in. It may not be a suitable point at | |
| 980 // which to invoke an exception routine. In most cases the exception | |
| 981 // routine will be run in the scheduler thread switch code, where the world is | |
| 982 // in an inconsistent state. We really need to run the routine at the | |
| 983 // end of unlock_inner(). However this would add extra code to the scheduler, | |
| 984 // and require a way of storing pending exceptions. So for now this option is | |
| 985 // disabled and not yet implemented, it may never be. | |
| 986 | |
| 987 } | |
| 988 #endif | |
| 989 } | |
| 990 | |
| 991 #endif | |
| 992 | |
| 993 // ------------------------------------------------------------------------- | |
| 994 // Per-thread data support | |
| 995 | |
| 996 #ifdef CYGVAR_KERNEL_THREADS_DATA | |
| 997 | |
| 998 // Set the data map bits for each free slot in the data array. | |
| 999 cyg_ucount32 Cyg_Thread::thread_data_map = (~CYGNUM_KERNEL_THREADS_DATA_ALL) & | |
| 1000 ((1<<CYGNUM_KERNEL_THREADS_DATA_MAX)-1); | |
| 1001 | |
| 1002 cyg_ucount32 | |
| 1003 Cyg_Thread::new_data_index() | |
| 1004 { | |
| 1005 Cyg_Scheduler::lock(); | |
| 1006 | |
| 1007 cyg_ucount32 index; | |
| 1008 | |
| 1009 CYG_ASSERT( thread_data_map != 0 , "No more thread data indexes"); | |
| 1010 | |
| 1011 // find ls set bit | |
| 1012 HAL_LSBIT_INDEX( index, thread_data_map ); | |
| 1013 | |
| 1014 // clear the bit | |
| 1015 thread_data_map &= ~(1<<index); | |
| 1016 | |
| 1017 Cyg_Scheduler::unlock(); | |
| 1018 | |
| 1019 return index; | |
| 1020 } | |
| 1021 | |
| 1022 void Cyg_Thread::free_data_index( cyg_ucount32 index ) | |
| 1023 { | |
| 1024 Cyg_Scheduler::lock(); | |
| 1025 | |
| 1026 thread_data_map |= (1<<index); | |
| 1027 | |
| 1028 Cyg_Scheduler::unlock(); | |
| 1029 } | |
| 1030 | |
| 1031 | |
| 1032 #endif | |
| 1033 | |
| 1034 // ========================================================================= | |
| 1035 // Cyg_ThreadTimer member functions | |
| 1036 | |
| 1037 // ------------------------------------------------------------------------- | |
| 1038 // Timer alarm function. Inspect the sleep_reason and if necessary wake | |
| 1039 // up the thread with an appropriate wake_reason. | |
| 1040 | |
| 1041 #ifdef CYGFUN_KERNEL_THREADS_TIMER | |
| 1042 | |
| 1043 void | |
| 1044 Cyg_ThreadTimer::alarm( | |
| 1045 Cyg_Alarm *alarm, | |
| 1046 CYG_ADDRWORD data | |
| 1047 ) | |
| 1048 { | |
| 1049 CYG_REPORT_FUNCTION(); | |
| 1050 | |
| 1051 Cyg_ThreadTimer *self = (Cyg_ThreadTimer *)data; | |
| 1052 Cyg_Thread *thread = self->thread; | |
| 1053 | |
| 1054 CYG_INSTRUMENT_THREAD(ALARM, 0, 0); | |
| 1055 | |
| 1056 Cyg_Scheduler::lock(); | |
| 1057 | |
| 1058 Cyg_Thread::cyg_reason sleep_reason = thread->get_sleep_reason(); | |
| 1059 | |
| 1060 switch( sleep_reason ) { | |
| 1061 | |
| 1062 case Cyg_Thread::DESTRUCT: | |
| 1063 case Cyg_Thread::BREAK: | |
| 1064 case Cyg_Thread::EXIT: | |
| 1065 case Cyg_Thread::NONE: | |
| 1066 case Cyg_Thread::WAIT: | |
| 1067 case Cyg_Thread::DONE: | |
| 1068 // Do nothing in any of these cases. Most are here to | |
| 1069 // keep the compiler happy. | |
| 1070 Cyg_Scheduler::unlock(); | |
| 1071 CYG_REPORT_RETURN(); | |
| 1072 return; | |
| 1073 | |
| 1074 case Cyg_Thread::DELAY: | |
| 1075 // The thread was simply delaying, unless it has been | |
| 1076 // woken up for some other reason, wake it now. | |
| 1077 thread->set_wake_reason(Cyg_Thread::DONE); | |
| 1078 break; | |
| 1079 | |
| 1080 case Cyg_Thread::TIMEOUT: | |
| 1081 // The thread has timed out, set the wake reason to | |
| 1082 // TIMEOUT and restart. | |
| 1083 thread->set_wake_reason(Cyg_Thread::TIMEOUT); | |
| 1084 break; | |
| 1085 } | |
| 1086 | |
| 1087 thread->wake(); | |
| 1088 | |
| 1089 Cyg_Scheduler::unlock(); | |
| 1090 CYG_REPORT_RETURN(); | |
| 1091 } | |
| 1092 | |
| 1093 #endif | |
| 1094 | |
| 1095 // ========================================================================= | |
| 1096 // The Idle thread | |
| 1097 // The idle thread is implemented as a single instance of the | |
| 1098 // Cyg_IdleThread class. This is so that it can be initialized before | |
| 1099 // main in a static constructor. | |
| 1100 | |
| 1101 // ------------------------------------------------------------------------- | |
| 1102 // Data definitions | |
| 1103 | |
| 1104 // stack | |
| 2 | 1105 #ifdef CYGNUM_HAL_STACK_SIZE_MINIMUM |
| 1106 # ifdef CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE | |
| 1107 # if CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE < CYGNUM_HAL_STACK_SIZE_MINIMUM | |
| 0 | 1108 |
| 2 | 1109 // then override the configured stack size |
| 1110 # undef CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE | |
| 1111 # define CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE CYGNUM_HAL_STACK_SIZE_MINIMUM | |
| 1112 | |
| 1113 # endif // CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE < CYGNUM_HAL_STACK_SIZE_MINIMUM | |
| 1114 # endif // CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE | |
| 1115 #endif // CYGNUM_HAL_STACK_SIZE_MINIMUM | |
| 1116 | |
| 1117 static char idle_thread_stack[CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE]; | |
| 0 | 1118 |
| 1119 // Loop counter for debugging/housekeeping | |
| 1120 cyg_uint32 idle_thread_loops = 1; | |
| 1121 | |
| 1122 // ------------------------------------------------------------------------- | |
| 1123 // Idle thread code. | |
| 1124 | |
| 1125 void | |
| 1126 idle_thread_main( CYG_ADDRESS data ) | |
| 1127 { | |
| 1128 CYG_REPORT_FUNCTION(); | |
| 1129 | |
| 1130 for(;;) | |
| 1131 { | |
| 1132 idle_thread_loops++; | |
| 1133 | |
| 1134 HAL_IDLE_THREAD_ACTION(idle_thread_loops); | |
| 1135 | |
| 1136 #if 0 | |
| 1137 // For testing, it is useful to be able to fake | |
| 1138 // clock interrupts in the idle thread. | |
| 1139 | |
| 1140 Cyg_Clock::real_time_clock->tick(); | |
| 1141 #endif | |
| 1142 #ifdef CYGIMP_IDLE_THREAD_YIELD | |
| 1143 // In single priority and non-preemptive systems, | |
| 1144 // the idle thread should yield repeatedly to | |
| 1145 // other threads. | |
| 1146 Cyg_Thread::yield(); | |
| 1147 #endif | |
| 1148 } | |
| 1149 } | |
| 1150 | |
| 1151 // ------------------------------------------------------------------------- | |
| 1152 // Idle thread class | |
| 1153 | |
| 1154 class Cyg_IdleThread : public Cyg_Thread | |
| 1155 { | |
| 1156 public: | |
| 1157 Cyg_IdleThread( | |
| 1158 cyg_thread_entry *entry, // entry point function | |
| 1159 CYG_ADDRWORD entry_data, // entry data | |
| 1160 cyg_ucount32 stack_size = 0, // stack size, 0 = use default | |
| 1161 CYG_ADDRESS stack_base = 0 // stack base, NULL = allocate | |
| 1162 ); | |
| 1163 | |
| 1164 }; | |
| 1165 | |
| 1166 // ------------------------------------------------------------------------- | |
| 1167 // Idle threads constructor | |
| 1168 | |
| 1169 Cyg_IdleThread::Cyg_IdleThread( | |
| 1170 cyg_thread_entry *entry, // entry point function | |
| 1171 CYG_ADDRWORD entry_data, // entry data | |
| 1172 cyg_ucount32 stack_size, // stack size, 0 = use default | |
| 1173 CYG_ADDRESS stack_base // stack base, NULL = allocate | |
| 1174 ) | |
| 1175 : Cyg_Thread( CYG_THREAD_MIN_PRIORITY, | |
| 1176 entry, | |
| 1177 entry_data, | |
| 1178 "Idle Thread", | |
| 1179 stack_base, | |
| 1180 stack_size) | |
| 1181 { | |
| 1182 CYG_REPORT_FUNCTION(); | |
| 1183 | |
| 1184 resume(); | |
| 1185 CYG_REPORT_RETURN(); | |
| 1186 } | |
| 1187 | |
| 1188 // ------------------------------------------------------------------------- | |
| 1189 // Instantiate the idle thread | |
| 1190 | |
| 1191 Cyg_IdleThread idle_thread CYG_INIT_PRIORITY( IDLE_THREAD ) = | |
| 1192 Cyg_IdleThread( idle_thread_main, | |
| 1193 0, | |
| 1194 CYGNUM_KERNEL_THREADS_IDLE_STACK_SIZE, | |
| 1195 CYG_ADDRESS(idle_thread_stack) | |
| 1196 ); | |
| 1197 | |
| 1198 | |
| 1199 // ------------------------------------------------------------------------- | |
| 1200 // EOF common/thread.cxx |
