Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a basic shared task pool, and use it for image decoding.
- Loading branch information
Showing
4 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from pcwalton
at glennw@2663647
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging glennw/servo/taskpool = 2663647 into auto
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
glennw/servo/taskpool = 2663647 merged ok, testing candidate = 4d6a75f
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some tests failed:
failure: http://build.servo.org/builders/mac/builds/375
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bors: retry
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from pcwalton
at glennw@2663647
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging glennw/servo/taskpool = 2663647 into auto
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
glennw/servo/taskpool = 2663647 merged ok, testing candidate = 4795e9c
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all tests pass:
success: http://build.servo.org/builders/linux/builds/389
success: http://build.servo.org/builders/mac/builds/396
2663647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fast-forwarding master to auto = 4795e9c