Changeset 6a0e568 in mainline
- Timestamp:
- 2024-01-19T16:56:48Z (11 months ago)
- Branches:
- master
- Children:
- 286da52
- Parents:
- c1eaec4
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2023-02-22 18:04:40)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-19 16:56:48)
- Location:
- kernel/generic
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/include/cpu.h
rc1eaec4 r6a0e568 77 77 context_t scheduler_context; 78 78 79 struct thread *prev_thread; 79 80 state_t exiting_state; 80 81 } cpu_local_t; -
kernel/generic/include/proc/scheduler.h
rc1eaec4 r6a0e568 64 64 extern void scheduler_enter(state_t); 65 65 66 extern void thread_main_func(void); 67 66 68 /* 67 69 * To be defined by architectures. -
kernel/generic/src/proc/scheduler.c
rc1eaec4 r6a0e568 425 425 halt(); 426 426 427 /* Check if we have a thread to switch to. */ 428 429 int rq_index; 430 thread_t *new_thread = try_find_thread(&rq_index); 431 432 if (new_thread == NULL && new_state == Running) { 433 /* No other thread to run, but we still have work to do here. */ 434 interrupts_restore(ipl); 435 return; 436 } 437 427 438 irq_spinlock_lock(&THREAD->lock, false); 428 439 THREAD->state = new_state; … … 446 457 CPU_LOCAL->exiting_state = new_state; 447 458 448 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack); 449 context_swap(&THREAD->saved_context, &CPU_LOCAL->scheduler_context); 459 if (new_thread) { 460 thread_t *old_thread = THREAD; 461 CPU_LOCAL->prev_thread = old_thread; 462 THREAD = new_thread; 463 /* No waiting necessary, we can switch to the new thread directly. */ 464 prepare_to_run_thread(rq_index); 465 466 current_copy(CURRENT, (current_t *) new_thread->kstack); 467 context_swap(&old_thread->saved_context, &new_thread->saved_context); 468 } else { 469 /* 470 * A new thread isn't immediately available, switch to a separate 471 * stack to sleep or do other idle stuff. 472 */ 473 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack); 474 context_swap(&THREAD->saved_context, &CPU_LOCAL->scheduler_context); 475 } 450 476 451 477 assert(CURRENT->mutex_locks == 0); 452 478 assert(interrupts_disabled()); 479 480 /* Check if we need to clean up after another thread. */ 481 if (CPU_LOCAL->prev_thread) { 482 cleanup_after_thread(CPU_LOCAL->prev_thread, CPU_LOCAL->exiting_state); 483 CPU_LOCAL->prev_thread = NULL; 484 } 453 485 454 486 interrupts_restore(ipl); … … 504 536 505 537 halt(); 538 } 539 540 /** Thread wrapper. 541 * 542 * This wrapper is provided to ensure that a starting thread properly handles 543 * everything it needs to do when first scheduled, and when it exits. 544 */ 545 void thread_main_func(void) 546 { 547 assert(interrupts_disabled()); 548 549 void (*f)(void *) = THREAD->thread_code; 550 void *arg = THREAD->thread_arg; 551 552 /* This is where each thread wakes up after its creation */ 553 554 /* Check if we need to clean up after another thread. */ 555 if (CPU_LOCAL->prev_thread) { 556 cleanup_after_thread(CPU_LOCAL->prev_thread, CPU_LOCAL->exiting_state); 557 CPU_LOCAL->prev_thread = NULL; 558 } 559 560 interrupts_enable(); 561 562 f(arg); 563 564 thread_exit(); 565 566 /* Not reached */ 506 567 } 507 568 -
kernel/generic/src/proc/thread.c
rc1eaec4 r6a0e568 108 108 static int threads_cmp(void *, void *); 109 109 110 /** Thread wrapper.111 *112 * This wrapper is provided to ensure that every thread makes a call to113 * thread_exit() when its implementing function returns.114 *115 * interrupts_disable() is assumed.116 *117 */118 static void cushion(void)119 {120 void (*f)(void *) = THREAD->thread_code;121 void *arg = THREAD->thread_arg;122 123 /* This is where each thread wakes up after its creation */124 interrupts_enable();125 126 f(arg);127 128 thread_exit();129 130 /* Not reached */131 }132 133 110 /** Initialization and allocation for thread_t structure 134 111 * … … 309 286 irq_spinlock_unlock(&tidlock, true); 310 287 311 context_create(&thread->saved_context, cushion, thread->kstack, STACK_SIZE); 288 context_create(&thread->saved_context, thread_main_func, 289 thread->kstack, STACK_SIZE); 312 290 313 291 current_initialize((current_t *) thread->kstack);
Note:
See TracChangeset
for help on using the changeset viewer.