-
Notifications
You must be signed in to change notification settings - Fork 6
feat: update state machine for process #5
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
TomGoh
wants to merge
7
commits into
Starry-OS:main
Choose a base branch
from
TomGoh:feat/state-machine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+255
−11
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dc2f85b
feat: update state machine for process
TomGoh a7411b9
fix: fix memory ordering issue and update comments
TomGoh 2f2bb17
feat: add wait event tracking to Process for POSIX-compliant waitpid
TomGoh 5586ee1
Revert "feat: add wait event tracking to Process for POSIX-compliant …
TomGoh e9c410d
fix: resolve bitflag comparison issue.
TomGoh c40e98f
test: Add test for process state management. Update group, process, a…
TomGoh ed92122
chore: update comments. remove test scratch files
TomGoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,9 +5,10 @@ use alloc::{ | |||||
| }; | ||||||
| use core::{ | ||||||
| fmt, | ||||||
| sync::atomic::{AtomicBool, Ordering}, | ||||||
| sync::atomic::{AtomicU8, Ordering}, | ||||||
| }; | ||||||
|
|
||||||
| use bitflags::bitflags; | ||||||
| use kspin::SpinNoIrq; | ||||||
| use lazyinit::LazyInit; | ||||||
| use weak_map::StrongMap; | ||||||
|
|
@@ -21,10 +22,31 @@ pub(crate) struct ThreadGroup { | |||||
| pub(crate) group_exited: bool, | ||||||
| } | ||||||
|
|
||||||
| bitflags! { | ||||||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | ||||||
| pub(crate) struct ProcessState: u8 { | ||||||
| const RUNNING = 1 << 0; | ||||||
| const STOPPED = 1 << 1; | ||||||
| const ZOMBIE = 1 << 2; | ||||||
| } | ||||||
| } | ||||||
TomGoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| impl PartialEq<u8> for ProcessState { | ||||||
| fn eq(&self, other: &u8) -> bool { | ||||||
| self.bits() == *other | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl PartialEq<ProcessState> for u8 { | ||||||
| fn eq(&self, other: &ProcessState) -> bool { | ||||||
| *self == other.bits() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| /// A process. | ||||||
| pub struct Process { | ||||||
| pid: Pid, | ||||||
| is_zombie: AtomicBool, | ||||||
| state: AtomicU8, | ||||||
| pub(crate) tg: SpinNoIrq<ThreadGroup>, | ||||||
|
|
||||||
| // TODO: child subreaper9 | ||||||
|
|
@@ -191,10 +213,97 @@ impl Process { | |||||
| impl Process { | ||||||
| /// Returns `true` if the [`Process`] is a zombie process. | ||||||
| pub fn is_zombie(&self) -> bool { | ||||||
| self.is_zombie.load(Ordering::Acquire) | ||||||
| self.state.load(Ordering::Acquire) == ProcessState::ZOMBIE | ||||||
| } | ||||||
|
|
||||||
| /// Returns `true` if the [`Process`] is running. | ||||||
| pub fn is_running(&self) -> bool { | ||||||
| self.state.load(Ordering::Acquire) == ProcessState::RUNNING | ||||||
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| /// Returns `true` if the [`Process`] is stopped. | ||||||
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| pub fn is_stopped(&self) -> bool { | ||||||
TomGoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| self.state.load(Ordering::Acquire) == ProcessState::STOPPED | ||||||
| } | ||||||
|
|
||||||
| /// Change the [`Process`] from Running to `Stopped`. | ||||||
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| /// | ||||||
| /// This method atomically transitions the process state to STOPPED using | ||||||
| /// CAS, ensuring the state is either successfully changed or already in | ||||||
| /// ZOMBIE state (in which case no change occurs). | ||||||
| /// | ||||||
| /// # Memory Ordering | ||||||
| /// | ||||||
| /// Uses `Release` ordering on success to synchronize with `Acquire` loads | ||||||
| /// in `is_stopped()`. This ensures that any writes before this | ||||||
| /// transition, such as setting `stop_signal` in the | ||||||
| /// `ProcessSignalManager`, are visible to threads that observe the | ||||||
| /// `STOPPED` state transition. | ||||||
| pub fn transition_to_stopped(&self) { | ||||||
| let _ = self.state.fetch_update( | ||||||
| Ordering::Release, // Success: synchronize with is_stopped() | ||||||
| Ordering::Relaxed, // Failure: no synchronization needed | ||||||
| |curr| { | ||||||
| if curr == ProcessState::ZOMBIE.bits() { | ||||||
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| None // Already zombie, don't transition | ||||||
| } else { | ||||||
| Some(ProcessState::STOPPED.bits()) | ||||||
| } | ||||||
| }, | ||||||
| ); | ||||||
| } | ||||||
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| /// Change the [`Process`] from `Stopped` to `Running`. | ||||||
|
||||||
| /// Change the [`Process`] from `Stopped` to `Running`. | |
| /// Transitions the [`Process`] to `Running` state. |
TomGoh marked this conversation as resolved.
Show resolved
Hide resolved
TomGoh marked this conversation as resolved.
Show resolved
Hide resolved
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
TomGoh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.