Skip to content

Commit

Permalink
Merge #2040
Browse files Browse the repository at this point in the history
2040: timerfd: Add TFD_TIMER_CANCEL_ON_SET flag r=asomers a=apohrebniak

Hi.
This PR adds an TFD_TIMER_CANCEL_ON_SET flag to use with `timerfd` on Linux and Android

Co-authored-by: Andrii Pohrebniak <[email protected]>
  • Loading branch information
bors[bot] and apohrebniak authored May 21, 2023
2 parents 622e53e + a4dd9cd commit dfc689a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Added `mq_timedreceive` to `::nix::mqueue`.
([#1966])(https://github.com/nix-rust/nix/pull/1966)
- Added `LocalPeerPid` to `nix::sys::socket::sockopt` for macOS. ([#1967](https://github.com/nix-rust/nix/pull/1967))
- Added `TFD_TIMER_CANCEL_ON_SET` to `::nix::sys::time::TimerSetTimeFlags` on Linux and Android.
([#2040](https://github.com/nix-rust/nix/pull/2040))

### Changed

Expand Down
1 change: 1 addition & 0 deletions src/sys/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub(crate) mod timer {
/// Flags that are used for arming the timer.
pub struct TimerSetTimeFlags: libc::c_int {
const TFD_TIMER_ABSTIME = libc::TFD_TIMER_ABSTIME;
const TFD_TIMER_CANCEL_ON_SET = libc::TFD_TIMER_CANCEL_ON_SET;
}
}
#[cfg(any(
Expand Down
10 changes: 10 additions & 0 deletions src/sys/timerfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ impl TimerFd {
/// Then the one shot TimeSpec and the delay TimeSpec of the delayed
/// interval are going to be interpreted as absolute.
///
/// # Cancel on a clock change
///
/// If you set a `TFD_TIMER_CANCEL_ON_SET` alongside `TFD_TIMER_ABSTIME`
/// and the clock for this timer is `CLOCK_REALTIME` or `CLOCK_REALTIME_ALARM`,
/// then this timer is marked as cancelable if the real-time clock undergoes
/// a discontinuous change.
///
/// # Disabling alarms
///
/// Note: Only one alarm can be set for any given timer. Setting a new alarm
Expand Down Expand Up @@ -202,6 +209,9 @@ impl TimerFd {
/// Note: If the alarm is unset, then you will wait forever.
pub fn wait(&self) -> Result<()> {
while let Err(e) = read(self.fd.as_fd().as_raw_fd(), &mut [0u8; 8]) {
if e == Errno::ECANCELED {
break;
}
if e != Errno::EINTR {
return Err(e);
}
Expand Down

0 comments on commit dfc689a

Please sign in to comment.