Skip to content

kernel: Timeslicing and yield to share code #85538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

peter-mitsis
Copy link
Collaborator

When timeslicing moves the current thread to the end of the priority queue, it is functionally equivalent to yielding. It makes sense for the two to share common code.

When timeslicing moves the current thread to the end of the
priority queue, it is functionally equivalent to yielding.

Signed-off-by: Peter Mitsis <[email protected]>
@peter-mitsis
Copy link
Collaborator Author

Updated as per @cfriedt 's comments.

@peter-mitsis peter-mitsis requested a review from cfriedt February 11, 2025 19:42
@peter-mitsis peter-mitsis assigned andyross and unassigned wearyzen Feb 26, 2025
Copy link
Collaborator

@andyross andyross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real complaints here, but just to be That Guy: if this is really removing duplicated code, why is it increasing the line count by 5?

Sometimes refactoring just isn't worth it, IMHO. In this case the API changes needed to effect the unification are larger than the code being duplicated. Not worth it, IMHO.

@peter-mitsis
Copy link
Collaborator Author

@andyross - There is one other thing to consider. We recently did some optimization on k_yield(). By having move_thread_to_end_of_prio_q() / z_yield() use the same logic as k_yield(), we also gain the benefits of those optimizations at ISR level.

Also, FWIW, I think this also cleans up the SMP behavior as well. Without this proposed change, the SMP version of move_thread_to_end_of_prio_q() / z_yield() logically becomes (after realizing that thread is always _current) ...

	thread->base.thread_state |= _THREAD_QUEUED;
	_current_cpu->swap_ok = true;
  1. However, the compiler is not clever enough to reach this level of simplification. It will generate a lot of extra overhead that we don't need.
  2. Also note the unconditional setting of the _THREAD_QUEUED bit. Please correct me if I am wrong, but I don't think that we want that here. Isn't all that we truly need for SMP is just _current_cpu->swap_ok = true;?

@andyross
Copy link
Collaborator

Yes, QUEUED should only be needed when SMP=y because live threads always remain in the queue on UP as an optimization (they have to come out on SMP so that other CPUs don't try to select them, but obviously that isn't a problem when there are no other CPUs). It would be good to clean that logic up for sure, to e.g. unify the list operations with the bit set such that one can be elided in an obvious way. There are gotchas though with the way things interact with other optimizations though, IIRC.

@nashif nashif requested a review from Copilot April 23, 2025 17:21
Copilot

This comment was marked as outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ARM ARM (32-bit) Architecture area: Kernel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants