Skip to content

Support alarm and SIGALRM#693

Open
CvvT wants to merge 3 commits intoweiteng/ctrl_cfrom
weiteng/support_alarm
Open

Support alarm and SIGALRM#693
CvvT wants to merge 3 commits intoweiteng/ctrl_cfrom
weiteng/support_alarm

Conversation

@CvvT
Copy link
Contributor

@CvvT CvvT commented Feb 28, 2026

Add a new provider TimerProvider to create timer for alarm support.

For platforms that do not support timer, SIGALRM would only be sent when a syscall returns or a thread is about to be blocked (i.e., in check_for_interrupt).

@CvvT CvvT marked this pull request as ready for review February 28, 2026 23:48
Copy link
Member

@wdcui wdcui left a comment

Choose a reason for hiding this comment

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

LGTM. I left a minor comment below.

// `TimerHandle`. The handle's `Drop` impl waits for all in-flight
// callbacks before dropping the context, so this reference is valid.
let ctx = unsafe { &*context.cast::<TimerCallbackContext>() };
let thread = ACTIVE_THREADS.lock().unwrap().first().cloned();
Copy link
Member

Choose a reason for hiding this comment

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

I got the following comment from GH Copilot. Is this a valid concern? If so, we may want to add a note about it the comments.

"The one thing to watch is that ACTIVE_THREADS.lock().unwrap().first() always picks the same thread. If you ever support multiple guest threads that each need independent timer delivery, you may want to associate the timer with a specific thread rather than broadcasting to the first one in the list. But for single-process/single-main-thread use, the current approach is fine."

Comment on lines +115 to +128
/// Timer support for proactive signal delivery.
///
/// Platforms that support this should set [`SUPPORTS_TIMER`](Self::SUPPORTS_TIMER)
/// to `true`.
pub trait TimerProvider {
/// The platform-specific timer handle type.
type TimerHandle: TimerHandle;

/// Whether this platform supports [`TimerProvider`] for proactive timer delivery.
const SUPPORTS_TIMER: bool = false;

/// Create a new one-shot timer that delivers `signal` when it fires.
fn create_timer(&self, signal: crate::shim::Signal) -> Self::TimerHandle;
}
Copy link
Member

Choose a reason for hiding this comment

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

Minor:

I'm not sure SUPPORTS_TIMER is the ideal move, a possible alternative is to do

pub trait TimerProvider {
  type TimerHandle: TimerHandle; // Remind users to use `trivial_providers::UnsupportedTimerHandle` if not supported
  fn create_timer(&self, signal: Signal) -> Option<Self::TimerHandle> { None }
}

UnsupportedTimerHandle should be defined as pub enum UnsupportedTimerHandle {} which is equivalent to Rust !, which means that it can never actually be created; that way you are guaranteeing it via the type system.

Comment on lines +132 to +133
/// Dropping the handle **must** cancel any pending timer and ensure that the
/// associated callback will not fire after the drop returns.
Copy link
Member

Choose a reason for hiding this comment

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

"associated callback"? It is unclear what this refers to.

Also who is in charge of cancelling? As I understand it, it is saying that the platform cancels pending timers, yes? If so, the writing style should just be Dropping the handle cancels any pending timer or similar, and it is the platform's job to uphold this contract.

Copy link
Member

Choose a reason for hiding this comment

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

Re "associated callback", I think (if I understand correctly) this is making a long-range assumption that connects to the signals provider stuff. That should be documented if that's the design you want to go for. Ideally however, we do not use a long-range assumption like that and just literally have a callback registration right here?

Comment on lines +14 to +16
/// A [`TimerHandle`] stub for platforms that have not yet implemented
/// [`super::TimerProvider`]. All methods panic with `todo!()`.
pub struct StubTimerHandle(());
Copy link
Member

Choose a reason for hiding this comment

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

Mentioned elsewhere, define this as a pub enum UnsupportedTimerHandle {} instead of supporting its creation.

@CvvT CvvT force-pushed the weiteng/ctrl_c branch from 1681e12 to d8335d8 Compare March 4, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants