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",
15 #![rustc_deprecated(since = "1.7.0",
16 reason
= "easily confused with system sempahores and not \
17 used enough to pull its weight")]
21 use sync
::{Mutex, Condvar}
;
23 /// A counting, blocking, semaphore.
25 /// Semaphores are a form of atomic counter where access is only granted if the
26 /// counter is a positive value. Each acquisition will block the calling thread
27 /// until the counter is positive, and each release will increment the counter
28 /// and unblock any threads if necessary.
33 /// #![feature(semaphore)]
35 /// use std::sync::Semaphore;
37 /// // Create a semaphore that represents 5 resources
38 /// let sem = Semaphore::new(5);
40 /// // Acquire one of the resources
43 /// // Acquire one of the resources for a limited period of time
45 /// let _guard = sem.access();
47 /// } // resources is released here
49 /// // Release our initially acquired resource
52 pub struct Semaphore
{
57 /// An RAII guard which will release a resource acquired from a semaphore when
59 pub struct SemaphoreGuard
<'a
> {
64 /// Creates a new semaphore with the initial count specified.
66 /// The count specified can be thought of as a number of resources, and a
67 /// call to `acquire` or `access` will block until at least one resource is
68 /// available. It is valid to initialize a semaphore with a negative count.
69 pub fn new(count
: isize) -> Semaphore
{
71 lock
: Mutex
::new(count
),
76 /// Acquires a resource of this semaphore, blocking the current thread until
79 /// This method will block until the internal count of the semaphore is at
81 pub fn acquire(&self) {
82 let mut count
= self.lock
.lock().unwrap();
84 count
= self.cvar
.wait(count
).unwrap();
89 /// Release a resource from this semaphore.
91 /// This will increment the number of resources in this semaphore by 1 and
92 /// will notify any pending waiters in `acquire` or `access` if necessary.
93 pub fn release(&self) {
94 *self.lock
.lock().unwrap() += 1;
95 self.cvar
.notify_one();
98 /// Acquires a resource of this semaphore, returning an RAII guard to
99 /// release the semaphore when dropped.
101 /// This function is semantically equivalent to an `acquire` followed by a
102 /// `release` when the guard returned is dropped.
103 pub fn access(&self) -> SemaphoreGuard
{
105 SemaphoreGuard { sem: self }
109 #[stable(feature = "rust1", since = "1.0.0")]
110 impl<'a
> Drop
for SemaphoreGuard
<'a
> {
121 use super::Semaphore
;
122 use sync
::mpsc
::channel
;
126 fn test_sem_acquire_release() {
127 let s
= Semaphore
::new(1);
134 fn test_sem_basic() {
135 let s
= Semaphore
::new(1);
140 fn test_sem_as_mutex() {
141 let s
= Arc
::new(Semaphore
::new(1));
143 let _t
= thread
::spawn(move|| {
144 let _g
= s2
.access();
150 fn test_sem_as_cvar() {
151 /* Child waits and parent signals */
152 let (tx
, rx
) = channel();
153 let s
= Arc
::new(Semaphore
::new(0));
155 let _t
= thread
::spawn(move|| {
157 tx
.send(()).unwrap();
162 /* Parent waits and child signals */
163 let (tx
, rx
) = channel();
164 let s
= Arc
::new(Semaphore
::new(0));
166 let _t
= thread
::spawn(move|| {
171 tx
.send(()).unwrap();
175 fn test_sem_multi_resource() {
176 // Parent and child both get in the critical section at the same
177 // time, and shake hands.
178 let s
= Arc
::new(Semaphore
::new(2));
180 let (tx1
, rx1
) = channel();
181 let (tx2
, rx2
) = channel();
182 let _t
= thread
::spawn(move|| {
183 let _g
= s2
.access();
185 tx1
.send(()).unwrap();
188 tx2
.send(()).unwrap();
193 fn test_sem_runtime_friendly_blocking() {
194 let s
= Arc
::new(Semaphore
::new(1));
196 let (tx
, rx
) = channel();
199 thread
::spawn(move|| {
200 tx
.send(()).unwrap();
202 tx
.send(()).unwrap();
204 rx
.recv().unwrap(); // wait for child to come alive
206 rx
.recv().unwrap(); // wait for child to be done