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.
12 use sync::{Mutex, Condvar};
14 /// A counting, blocking, semaphore.
16 /// Semaphores are a form of atomic counter where access is only granted if the
17 /// counter is a positive value. Each acquisition will block the calling thread
18 /// until the counter is positive, and each release will increment the counter
19 /// and unblock any threads if necessary.
24 /// use std::sync::Semaphore;
26 /// // Create a semaphore that represents 5 resources
27 /// let sem = Semaphore::new(5);
29 /// // Acquire one of the resources
32 /// // Acquire one of the resources for a limited period of time
34 /// let _guard = sem.access();
36 /// } // resources is released here
38 /// // Release our initially acquired resource
41 pub struct Semaphore {
46 /// An RAII guard which will release a resource acquired from a semaphore when
48 pub struct SemaphoreGuard<'a> {
53 /// Creates a new semaphore with the initial count specified.
55 /// The count specified can be thought of as a number of resources, and a
56 /// call to `acquire` or `access` will block until at least one resource is
57 /// available. It is valid to initialize a semaphore with a negative count.
58 pub fn new(count: int) -> Semaphore {
60 lock: Mutex::new(count),
65 /// Acquires a resource of this semaphore, blocking the current thread until
68 /// This method will block until the internal count of the semaphore is at
70 pub fn acquire(&self) {
71 let mut count = self.lock.lock().unwrap();
73 count = self.cvar.wait(count).unwrap();
78 /// Release a resource from this semaphore.
80 /// This will increment the number of resources in this semaphore by 1 and
81 /// will notify any pending waiters in `acquire` or `access` if necessary.
82 pub fn release(&self) {
83 *self.lock.lock().unwrap() += 1;
84 self.cvar.notify_one();
87 /// Acquires a resource of this semaphore, returning an RAII guard to
88 /// release the semaphore when dropped.
90 /// This function is semantically equivalent to an `acquire` followed by a
91 /// `release` when the guard returned is dropped.
92 pub fn access(&self) -> SemaphoreGuard {
94 SemaphoreGuard { sem: self }
99 impl<'a> Drop for SemaphoreGuard<'a> {
110 use super::Semaphore;
111 use sync::mpsc::channel;
115 fn test_sem_acquire_release() {
116 let s = Semaphore::new(1);
123 fn test_sem_basic() {
124 let s = Semaphore::new(1);
129 fn test_sem_as_mutex() {
130 let s = Arc::new(Semaphore::new(1));
132 let _t = Thread::spawn(move|| {
133 let _g = s2.access();
139 fn test_sem_as_cvar() {
140 /* Child waits and parent signals */
141 let (tx, rx) = channel();
142 let s = Arc::new(Semaphore::new(0));
144 let _t = Thread::spawn(move|| {
146 tx.send(()).unwrap();
151 /* Parent waits and child signals */
152 let (tx, rx) = channel();
153 let s = Arc::new(Semaphore::new(0));
155 let _t = Thread::spawn(move|| {
160 tx.send(()).unwrap();
164 fn test_sem_multi_resource() {
165 // Parent and child both get in the critical section at the same
166 // time, and shake hands.
167 let s = Arc::new(Semaphore::new(2));
169 let (tx1, rx1) = channel();
170 let (tx2, rx2) = channel();
171 let _t = Thread::spawn(move|| {
172 let _g = s2.access();
174 tx1.send(()).unwrap();
177 tx2.send(()).unwrap();
182 fn test_sem_runtime_friendly_blocking() {
183 let s = Arc::new(Semaphore::new(1));
185 let (tx, rx) = channel();
188 Thread::spawn(move|| {
189 tx.send(()).unwrap();
191 tx.send(()).unwrap();
193 rx.recv().unwrap(); // wait for child to come alive
195 rx.recv().unwrap(); // wait for child to be done