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