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