1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![unstable(feature = "semaphore",
12 reason = "the interaction between semaphores and the acquisition/release \
13 of resources is currently unclear")]
16 use sync::{Mutex, Condvar};
18 /// A counting, blocking, semaphore.
20 /// Semaphores are a form of atomic counter where access is only granted if the
21 /// counter is a positive value. Each acquisition will block the calling thread
22 /// until the counter is positive, and each release will increment the counter
23 /// and unblock any threads if necessary.
28 /// #![feature(semaphore)]
30 /// use std::sync::Semaphore;
32 /// // Create a semaphore that represents 5 resources
33 /// let sem = Semaphore::new(5);
35 /// // Acquire one of the resources
38 /// // Acquire one of the resources for a limited period of time
40 /// let _guard = sem.access();
42 /// } // resources is released here
44 /// // Release our initially acquired resource
47 pub struct Semaphore {
52 /// An RAII guard which will release a resource acquired from a semaphore when
54 pub struct SemaphoreGuard<'a> {
59 /// Creates a new semaphore with the initial count specified.
61 /// The count specified can be thought of as a number of resources, and a
62 /// call to `acquire` or `access` will block until at least one resource is
63 /// available. It is valid to initialize a semaphore with a negative count.
64 pub fn new(count: isize) -> Semaphore {
66 lock: Mutex::new(count),
71 /// Acquires a resource of this semaphore, blocking the current thread until
74 /// This method will block until the internal count of the semaphore is at
76 pub fn acquire(&self) {
77 let mut count = self.lock.lock().unwrap();
79 count = self.cvar.wait(count).unwrap();
84 /// Release a resource from this semaphore.
86 /// This will increment the number of resources in this semaphore by 1 and
87 /// will notify any pending waiters in `acquire` or `access` if necessary.
88 pub fn release(&self) {
89 *self.lock.lock().unwrap() += 1;
90 self.cvar.notify_one();
93 /// Acquires a resource of this semaphore, returning an RAII guard to
94 /// release the semaphore when dropped.
96 /// This function is semantically equivalent to an `acquire` followed by a
97 /// `release` when the guard returned is dropped.
98 pub fn access(&self) -> SemaphoreGuard {
100 SemaphoreGuard { sem: self }
104 #[stable(feature = "rust1", since = "1.0.0")]
105 impl<'a> Drop for SemaphoreGuard<'a> {
116 use super::Semaphore;
117 use sync::mpsc::channel;
121 fn test_sem_acquire_release() {
122 let s = Semaphore::new(1);
129 fn test_sem_basic() {
130 let s = Semaphore::new(1);
135 fn test_sem_as_mutex() {
136 let s = Arc::new(Semaphore::new(1));
138 let _t = thread::spawn(move|| {
139 let _g = s2.access();
145 fn test_sem_as_cvar() {
146 /* Child waits and parent signals */
147 let (tx, rx) = channel();
148 let s = Arc::new(Semaphore::new(0));
150 let _t = thread::spawn(move|| {
152 tx.send(()).unwrap();
157 /* Parent waits and child signals */
158 let (tx, rx) = channel();
159 let s = Arc::new(Semaphore::new(0));
161 let _t = thread::spawn(move|| {
166 tx.send(()).unwrap();
170 fn test_sem_multi_resource() {
171 // Parent and child both get in the critical section at the same
172 // time, and shake hands.
173 let s = Arc::new(Semaphore::new(2));
175 let (tx1, rx1) = channel();
176 let (tx2, rx2) = channel();
177 let _t = thread::spawn(move|| {
178 let _g = s2.access();
180 tx1.send(()).unwrap();
183 tx2.send(()).unwrap();
188 fn test_sem_runtime_friendly_blocking() {
189 let s = Arc::new(Semaphore::new(1));
191 let (tx, rx) = channel();
194 thread::spawn(move|| {
195 tx.send(()).unwrap();
197 tx.send(()).unwrap();
199 rx.recv().unwrap(); // wait for child to come alive
201 rx.recv().unwrap(); // wait for child to be done