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