Skip to content

Commit 79adf8b

Browse files
committed
Docs and style in cpupool
1 parent f85f0f5 commit 79adf8b

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

futures-cpupool/src/lib.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ impl CpuPool {
116116
};
117117

118118
for _ in 0..size {
119-
let pool = CpuPool { inner: pool.inner.clone() };
120-
thread::spawn(|| pool.work());
119+
let inner = pool.inner.clone();
120+
thread::spawn(move || work(&inner));
121121
}
122122

123123
return pool
@@ -129,13 +129,26 @@ impl CpuPool {
129129
CpuPool::new(num_cpus::get())
130130
}
131131

132-
/// Execute some work on this thread pool, returning a future to the work
133-
/// that's running on the thread pool.
132+
/// Spawns a future to run on this thread pool, returning a future
133+
/// representing the produced value.
134134
///
135-
/// This function will execute the closure `f` on the associated thread
135+
/// This function will execute the future `f` on the associated thread
136136
/// pool, and return a future representing the finished computation. The
137-
/// future will either resolve to `R` if the computation finishes
138-
/// successfully or to `Box<Any+Send>` if it panics.
137+
/// returned future serves as a proxy to the computation that `F` is
138+
/// running.
139+
///
140+
/// To simply run an arbitrary closure on a thread pool and extract the
141+
/// result, you can use the `futures::lazy` combinator to defer work to
142+
/// executing on the thread pool itself.
143+
///
144+
/// Note that if the future `f` panics it will be caught by default and the
145+
/// returned future will propagate the panic. That is, panics will not tear
146+
/// down the thread pool and will be propagated to the returned future's
147+
/// `poll` method if queried.
148+
///
149+
/// If the returned future is dropped then this `CpuPool` will attempt to
150+
/// cancel the computation, if possible. That is, if the computation is in
151+
/// the middle of working, it will be interrupted when possible.
139152
pub fn spawn<F>(&self, f: F) -> CpuFuture<F::Item, F::Error>
140153
where F: Future + Send + 'static,
141154
F::Item: Send + 'static,
@@ -152,14 +165,13 @@ impl CpuPool {
152165
Task::new(self.inner.clone(), sender.boxed()).unpark();
153166
CpuFuture { inner: rx }
154167
}
168+
}
155169

156-
fn work(self) {
157-
let mut done = false;
158-
while !done {
159-
match self.inner.queue.pop() {
160-
Message::Close => done = true,
161-
Message::Run(r) => r.run(),
162-
}
170+
fn work(inner: &Inner) {
171+
loop {
172+
match inner.queue.pop() {
173+
Message::Run(r) => r.run(),
174+
Message::Close => break,
163175
}
164176
}
165177
}
@@ -173,11 +185,10 @@ impl Clone for CpuPool {
173185

174186
impl Drop for CpuPool {
175187
fn drop(&mut self) {
176-
if self.inner.cnt.fetch_sub(1, Ordering::Relaxed) > 1 {
177-
return
178-
}
179-
for _ in 0..self.inner.size {
180-
self.inner.queue.push(Message::Close);
188+
if self.inner.cnt.fetch_sub(1, Ordering::Relaxed) == 1 {
189+
for _ in 0..self.inner.size {
190+
self.inner.queue.push(Message::Close);
191+
}
181192
}
182193
}
183194
}

0 commit comments

Comments
 (0)