mirror of
https://github.com/servo/servo.git
synced 2025-08-05 21:50:18 +01:00
Introduce a basic shared task pool, and use it for image decoding.
This commit is contained in:
parent
a983debaf1
commit
26636474a9
4 changed files with 84 additions and 23 deletions
|
@ -50,6 +50,7 @@ pub mod str;
|
|||
pub mod task;
|
||||
pub mod tid;
|
||||
pub mod time;
|
||||
pub mod taskpool;
|
||||
pub mod vec;
|
||||
pub mod workqueue;
|
||||
|
||||
|
|
53
components/util/taskpool.rs
Normal file
53
components/util/taskpool.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
//! A load-balancing task pool.
|
||||
//!
|
||||
//! This differs in implementation from std::sync::TaskPool in that each job is
|
||||
//! up for grabs by any of the child tasks in the pool.
|
||||
//!
|
||||
|
||||
//
|
||||
// This is based on the cargo task pool.
|
||||
// https://github.com/rust-lang/cargo/blob/master/src/cargo/util/pool.rs
|
||||
//
|
||||
// The only difference is that a normal channel is used instead of a sync_channel.
|
||||
//
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub struct TaskPool {
|
||||
tx: Sender<proc():Send>,
|
||||
}
|
||||
|
||||
impl TaskPool {
|
||||
pub fn new(tasks: uint) -> TaskPool {
|
||||
assert!(tasks > 0);
|
||||
let (tx, rx) = channel();
|
||||
|
||||
let state = Arc::new(Mutex::new(rx));
|
||||
|
||||
for _ in range(0, tasks) {
|
||||
let state = state.clone();
|
||||
spawn(proc() worker(&*state));
|
||||
}
|
||||
|
||||
return TaskPool { tx: tx };
|
||||
|
||||
fn worker(rx: &Mutex<Receiver<proc():Send>>) {
|
||||
loop {
|
||||
let job = rx.lock().recv_opt();
|
||||
match job {
|
||||
Ok(job) => job(),
|
||||
Err(..) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute(&self, job: proc():Send) {
|
||||
self.tx.send(job);
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue