Skip to content
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

Add alarm and cancel_alarm #828

Closed
wants to merge 1 commit into from
Closed

Add alarm and cancel_alarm #828

wants to merge 1 commit into from

Conversation

Thomasdezeeuw
Copy link
Contributor

No description provided.

src/unistd.rs Outdated
/// a second will be ignored (not rounded up). E.g. 1 second and 800 miliseconds
/// will become 1 second.
///
/// Also, Strictly conforming the POSIX spec, duriations longer then `65535`
Copy link
Member

Choose a reason for hiding this comment

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

IMHO the warning in the POSIX specification is dumb. It just warns you against using constants greater than UINT_MAX, which is something your compiler can already warn you about. However, this Rust wrapper introduces a new problem: secs is a u64, which can overflow at runtime. You should probably assert that secs <= libc::c_uint::max_value()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You want just an assert, or a silent convertion into the max value with a warning in the doc?

Copy link
Member

Choose a reason for hiding this comment

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

I think an assert would be better than a saturating conversion. After all, why would somebody ever set an alarm for more than 4 billion seconds into the future? Such a thing is probably a bug.

src/unistd.rs Outdated
/// if any is canceld.
///
/// [`alarm`]: fn.alarm.html
pub fn cancel_alarm() -> Option<Duration> {
Copy link
Member

Choose a reason for hiding this comment

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

Is this function really necessary? Can't we accomplish the same thing just by making alarm's argument an Option<Duration>?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change it.

assert_eq!(alarm(Duration::from_secs(1)), Some(Duration::from_secs(60)));
let start = Instant::now();

let handler = SigHandler::Handler(alarm_signal_handler);
Copy link
Member

Choose a reason for hiding this comment

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

You should set up the hander before calling alarm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know, however this test takes at least a second so I though I add the handler after so it removes some of the overhead, but I'll before it before the call.

@Thomasdezeeuw
Copy link
Contributor Author

I addressed all feedback and squashed the commits.


#[test]
fn test_canceling_alarm() {
#[allow(unused_variables)]
Copy link
Member

Choose a reason for hiding this comment

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

I believe you can eliminate this directive if you change the variable's name from m to _m. That didn't work with older Rust releases, but it should be possible now that the minimum Rust is 1.20.0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shall I do it for all uses while I'm at it?

Copy link
Member

Choose a reason for hiding this comment

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

If you do, make it a separate commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then I'll make a seperate PR for that, including this line.

static mut ALARM_CALLED: bool = false;

// Used in `test_alarm`.
#[no_mangle]
Copy link
Member

Choose a reason for hiding this comment

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

What's the purpose of no_mangle? Using extern should be sufficient to pass the function pointer to a C function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just copied a handle from my code, which (if I can remeber correctly) needed this. Will see if I can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right it wasn't needed.

@Susurrus
Copy link
Contributor

Susurrus commented Jan 7, 2018

I'm not a big fan of using Duration with these functions given that there are limitations with it's range that cannot be checked at compile time. What's wrong with making it take a c_uint directly? Anyone using a Duration can convert it easily.

@Thomasdezeeuw
Copy link
Contributor Author

@Susurrus I don't really mind either way, I just though to make the API little more Rust-like.

@Thomasdezeeuw
Copy link
Contributor Author

Chages the parameter and return value to libc::c_uint.

@asomers
Copy link
Member

asomers commented Jan 7, 2018

The tests are failing because Buildbot can't figure out how to clone your repo. Probably something to do with your most recent force push. But it looks like you need to rebase anyway, to resolve the conflicts. Could you please do that?

@Thomasdezeeuw
Copy link
Contributor Author

This should be good now, trying to resolve the conflict on the GitHub website messed up the branch for me. But I reset based on the new master and it should be good.

@Susurrus
Copy link
Contributor

Susurrus commented Jan 7, 2018

Looking at this diff on GitHub shows it as adding all of test_unistd.rs and unistd.rs. I think something's still wrong with this branch.

@Thomasdezeeuw
Copy link
Contributor Author

I'll make another pr tomorrow from a fresh repo, although I'm not sure what when wrong.

@Thomasdezeeuw Thomasdezeeuw mentioned this pull request Jan 7, 2018
@Thomasdezeeuw
Copy link
Contributor Author

New pr in #830.

bors bot added a commit that referenced this pull request Jan 8, 2018
830: Add alarm r=asomers a=Thomasdezeeuw

Fixed #828.
bors bot added a commit that referenced this pull request Jan 8, 2018
830: Add alarm r=asomers a=Thomasdezeeuw

Fixed #828.
bors bot added a commit that referenced this pull request Feb 3, 2018
830: Add alarm r=asomers a=Thomasdezeeuw

Fixed #828.
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.

None yet

3 participants