Skip to content

Commit b0a72d8

Browse files
renato-zannonseanmonstar
authored andcommitted
feat(server): make AcceptorPool::accept() block and allow non'-static data
Change AcceptorPool to not spawn detached threads anymore. This, together with the recent `Send` changes, allows the `work` closure to close over non-`'static` data. This doesn't change the high-level `Server` interface, because that would make it's `listen` a blocking call (it's currently non-blocking) - which would be a breaking change.
1 parent b47f936 commit b0a72d8

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/server/acceptor.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::thread::{self, JoinGuard};
2-
use std::sync::Arc;
32
use std::sync::mpsc;
3+
use std::collections::VecMap;
44
use net::NetworkAcceptor;
55

66
pub struct AcceptorPool<A: NetworkAcceptor> {
77
acceptor: A
88
}
99

10-
impl<A: NetworkAcceptor + 'static> AcceptorPool<A> {
10+
impl<'a, A: NetworkAcceptor + 'a> AcceptorPool<A> {
1111
/// Create a thread pool to manage the acceptor.
1212
pub fn new(acceptor: A) -> AcceptorPool<A> {
1313
AcceptorPool { acceptor: acceptor }
@@ -18,33 +18,39 @@ impl<A: NetworkAcceptor + 'static> AcceptorPool<A> {
1818
/// ## Panics
1919
///
2020
/// Panics if threads == 0.
21-
pub fn accept<F>(self, work: F, threads: usize) -> JoinGuard<'static, ()>
22-
where F: Fn(A::Stream) + Send + Sync + 'static {
21+
pub fn accept<F>(self, work: F, threads: usize)
22+
where F: Fn(A::Stream) + Send + Sync + 'a {
2323
assert!(threads != 0, "Can't accept on 0 threads.");
2424

25-
// Replace with &F when Send changes land.
26-
let work = Arc::new(work);
27-
2825
let (super_tx, supervisor_rx) = mpsc::channel();
2926

30-
let spawn =
31-
move || spawn_with(super_tx.clone(), work.clone(), self.acceptor.clone());
27+
let counter = &mut 0;
28+
let work = &work;
29+
let mut spawn = move || {
30+
let id = *counter;
31+
let guard = spawn_with(super_tx.clone(), work, self.acceptor.clone(), id);
32+
*counter += 1;
33+
(id, guard)
34+
};
3235

3336
// Go
34-
for _ in 0..threads { spawn() }
37+
let mut guards: VecMap<_> = (0..threads).map(|_| spawn()).collect();
3538

36-
// Spawn the supervisor
37-
thread::scoped(move || for () in supervisor_rx.iter() { spawn() })
39+
for id in supervisor_rx.iter() {
40+
guards.remove(&id);
41+
let (id, guard) = spawn();
42+
guards.insert(id, guard);
43+
}
3844
}
3945
}
4046

41-
fn spawn_with<A, F>(supervisor: mpsc::Sender<()>, work: Arc<F>, mut acceptor: A)
42-
where A: NetworkAcceptor + 'static,
43-
F: Fn(<A as NetworkAcceptor>::Stream) + Send + Sync + 'static {
47+
fn spawn_with<'a, A, F>(supervisor: mpsc::Sender<usize>, work: &'a F, mut acceptor: A, id: usize) -> JoinGuard<'a, ()>
48+
where A: NetworkAcceptor + 'a,
49+
F: Fn(<A as NetworkAcceptor>::Stream) + Send + Sync + 'a {
4450
use std::old_io::EndOfFile;
4551

46-
thread::spawn(move || {
47-
let sentinel = Sentinel::new(supervisor, ());
52+
thread::scoped(move || {
53+
let sentinel = Sentinel::new(supervisor, id);
4854

4955
loop {
5056
match acceptor.accept() {
@@ -60,7 +66,7 @@ where A: NetworkAcceptor + 'static,
6066
}
6167
}
6268
}
63-
});
69+
})
6470
}
6571

6672
struct Sentinel<T: Send> {

src/server/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::old_io::{Listener, BufferedReader, BufferedWriter};
33
use std::old_io::net::ip::{IpAddr, Port, SocketAddr};
44
use std::os;
5-
use std::thread::JoinGuard;
5+
use std::thread::{self, JoinGuard};
66

77
pub use self::request::Request;
88
pub use self::response::Response;
@@ -77,8 +77,10 @@ S: NetworkStream + Clone + Send> Server<L> {
7777
let pool = AcceptorPool::new(acceptor.clone());
7878
let work = move |stream| handle_connection(stream, &handler);
7979

80+
let guard = thread::scoped(move || pool.accept(work, threads));
81+
8082
Ok(Listening {
81-
_guard: pool.accept(work, threads),
83+
_guard: guard,
8284
socket: socket,
8385
acceptor: acceptor
8486
})

0 commit comments

Comments
 (0)