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 = "the interaction between semaphores and the acquisition/release \
12 of resources is currently unclear"]
15 use sync::{Mutex, Condvar};
17 /// A counting, blocking, semaphore.
19 /// Semaphores are a form of atomic counter where access is only granted if the
20 /// counter is a positive value. Each acquisition will block the calling thread
21 /// until the counter is positive, and each release will increment the counter
22 /// and unblock any threads if necessary.
27 /// use std::sync::Semaphore;
29 /// // Create a semaphore that represents 5 resources
30 /// let sem = Semaphore::new(5);
32 /// // Acquire one of the resources
35 /// // Acquire one of the resources for a limited period of time
37 /// let _guard = sem.access();
39 /// } // resources is released here
41 /// // Release our initially acquired resource
44 pub struct Semaphore {
49 /// An RAII guard which will release a resource acquired from a semaphore when
51 pub struct SemaphoreGuard<'a> {
56 /// Creates a new semaphore with the initial count specified.
58 /// The count specified can be thought of as a number of resources, and a
59 /// call to `acquire` or `access` will block until at least one resource is
60 /// available. It is valid to initialize a semaphore with a negative count.
61 pub fn new(count: int) -> Semaphore {
63 lock: Mutex::new(count),
68 /// Acquires a resource of this semaphore, blocking the current thread until
71 /// This method will block until the internal count of the semaphore is at
73 pub fn acquire(&self) {
74 let mut count = self.lock.lock().unwrap();
76 count = self.cvar.wait(count).unwrap();
81 /// Release a resource from this semaphore.
83 /// This will increment the number of resources in this semaphore by 1 and
84 /// will notify any pending waiters in `acquire` or `access` if necessary.
85 pub fn release(&self) {
86 *self.lock.lock().unwrap() += 1;
87 self.cvar.notify_one();
90 /// Acquires a resource of this semaphore, returning an RAII guard to
91 /// release the semaphore when dropped.
93 /// This function is semantically equivalent to an `acquire` followed by a
94 /// `release` when the guard returned is dropped.
95 pub fn access(&self) -> SemaphoreGuard {
97 SemaphoreGuard { sem: self }
103 impl<'a> Drop for SemaphoreGuard<'a> {
114 use super::Semaphore;
115 use sync::mpsc::channel;
119 fn test_sem_acquire_release() {
120 let s = Semaphore::new(1);
127 fn test_sem_basic() {
128 let s = Semaphore::new(1);
133 fn test_sem_as_mutex() {
134 let s = Arc::new(Semaphore::new(1));
136 let _t = Thread::spawn(move|| {
137 let _g = s2.access();
143 fn test_sem_as_cvar() {
144 /* Child waits and parent signals */
145 let (tx, rx) = channel();
146 let s = Arc::new(Semaphore::new(0));
148 let _t = Thread::spawn(move|| {
150 tx.send(()).unwrap();
155 /* Parent waits and child signals */
156 let (tx, rx) = channel();
157 let s = Arc::new(Semaphore::new(0));
159 let _t = Thread::spawn(move|| {
164 tx.send(()).unwrap();
168 fn test_sem_multi_resource() {
169 // Parent and child both get in the critical section at the same
170 // time, and shake hands.
171 let s = Arc::new(Semaphore::new(2));
173 let (tx1, rx1) = channel();
174 let (tx2, rx2) = channel();
175 let _t = Thread::spawn(move|| {
176 let _g = s2.access();
178 tx1.send(()).unwrap();
181 tx2.send(()).unwrap();
186 fn test_sem_runtime_friendly_blocking() {
187 let s = Arc::new(Semaphore::new(1));
189 let (tx, rx) = channel();
192 Thread::spawn(move|| {
193 tx.send(()).unwrap();
195 tx.send(()).unwrap();
197 rx.recv().unwrap(); // wait for child to come alive
199 rx.recv().unwrap(); // wait for child to be done