diff --git a/src/libsync/raw.rs b/src/libsync/raw.rs index d056566bb9a97..617943b2f93ad 100644 --- a/src/libsync/raw.rs +++ b/src/libsync/raw.rs @@ -109,6 +109,8 @@ struct SemGuard<'a, Q> { impl Sem { fn new(count: int, q: Q) -> Sem { + assert!(count >= 0, + "semaphores cannot be initialized with negative values"); Sem { lock: mutex::Mutex::new(), inner: Unsafe::new(SemInner { @@ -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, ()) } } @@ -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();