Skip to content

sync: Fail with init semaphore count < 0 #15781

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

Merged
merged 1 commit into from
Jul 24, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/libsync/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ struct SemGuard<'a, Q> {

impl<Q: Send> Sem<Q> {
fn new(count: int, q: Q) -> Sem<Q> {
assert!(count >= 0,
"semaphores cannot be initialized with negative values");
Sem {
lock: mutex::Mutex::new(),
inner: Unsafe::new(SemInner {
Expand Down Expand Up @@ -364,6 +366,10 @@ pub struct SemaphoreGuard<'a> {

impl Semaphore {
/// Create a new semaphore with the specified count.
///
/// # Failure
///
/// This function will fail if `count` is negative.
pub fn new(count: int) -> Semaphore {
Semaphore { sem: Sem::new(count, ()) }
}
Expand Down Expand Up @@ -637,6 +643,11 @@ mod tests {
let _g = s.access();
}
#[test]
#[should_fail]
fn test_sem_basic2() {
Semaphore::new(-1);
}
#[test]
fn test_sem_as_mutex() {
let s = Arc::new(Semaphore::new(1));
let s2 = s.clone();
Expand Down