//!
//! # Using a scheduler pool
//!
+//! This library adds a `GreenTaskBuilder` trait that extends the methods
+//! available on `std::task::TaskBuilder` to allow spawning a green task,
+//! possibly pinned to a particular scheduler thread:
+//!
//! ```rust
-//! use std::rt::task::TaskOpts;
-//! use green::{SchedPool, PoolConfig};
-//! use green::sched::{PinnedTask, TaskFromFriend};
+//! use std::task::TaskBuilder;
+//! use green::{SchedPool, PoolConfig, GreenTaskBuilder};
//!
//! let config = PoolConfig::new();
//! let mut pool = SchedPool::new(config);
//!
//! // Spawn tasks into the pool of schedulers
-//! pool.spawn(TaskOpts::new(), proc() {
+//! TaskBuilder::new().green(&mut pool).spawn(proc() {
//! // this code is running inside the pool of schedulers
//!
//! spawn(proc() {
//! let mut handle = pool.spawn_sched();
//!
//! // Pin a task to the spawned scheduler
-//! let task = pool.task(TaskOpts::new(), proc() { /* ... */ });
-//! handle.send(PinnedTask(task));
-//!
-//! // Schedule a task on this new scheduler
-//! let task = pool.task(TaskOpts::new(), proc() { /* ... */ });
-//! handle.send(TaskFromFriend(task));
+//! TaskBuilder::new().green_pinned(&mut pool, &mut handle).spawn(proc() {
+//! /* ... */
+//! });
//!
//! // Handles keep schedulers alive, so be sure to drop all handles before
//! // destroying the sched pool
//! ```
#![crate_id = "green#0.11.0-pre"]
+ #![experimental]
#![license = "MIT/ASL2"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
// NB this does *not* include globs, please keep it that way.
#![feature(macro_rules, phase)]
#![allow(visible_private_types)]
+#![allow(deprecated)]
+#![feature(default_type_params)]
#[cfg(test)] #[phase(plugin, link)] extern crate log;
#[cfg(test)] extern crate rustuv;
use std::rt;
use std::sync::atomics::{SeqCst, AtomicUint, INIT_ATOMIC_UINT};
use std::sync::deque;
+use std::task::{TaskBuilder, Spawner};
-use sched::{Shutdown, Scheduler, SchedHandle, TaskFromFriend, NewNeighbor};
+use sched::{Shutdown, Scheduler, SchedHandle, TaskFromFriend, PinnedTask, NewNeighbor};
use sleeper_list::SleeperList;
use stack::StackPool;
use task::GreenTask;
/// This is useful to create a task which can then be sent to a specific
/// scheduler created by `spawn_sched` (and possibly pin it to that
/// scheduler).
+ #[deprecated = "use the green and green_pinned methods of GreenTaskBuilder instead"]
pub fn task(&mut self, opts: TaskOpts, f: proc():Send) -> Box<GreenTask> {
GreenTask::configure(&mut self.stack_pool, opts, f)
}
/// New tasks are spawned in a round-robin fashion to the schedulers in this
/// pool, but tasks can certainly migrate among schedulers once they're in
/// the pool.
+ #[deprecated = "use the green and green_pinned methods of GreenTaskBuilder instead"]
pub fn spawn(&mut self, opts: TaskOpts, f: proc():Send) {
let task = self.task(opts, f);
}
}
}
+
+/// A spawner for green tasks
+pub struct GreenSpawner<'a>{
+ pool: &'a mut SchedPool,
+ handle: Option<&'a mut SchedHandle>
+}
+
+impl<'a> Spawner for GreenSpawner<'a> {
+ #[inline]
+ fn spawn(self, opts: TaskOpts, f: proc():Send) {
+ let GreenSpawner { pool, handle } = self;
+ match handle {
+ None => pool.spawn(opts, f),
+ Some(h) => h.send(PinnedTask(pool.task(opts, f)))
+ }
+ }
+}
+
+/// An extension trait adding `green` configuration methods to `TaskBuilder`.
+pub trait GreenTaskBuilder {
+ fn green<'a>(self, &'a mut SchedPool) -> TaskBuilder<GreenSpawner<'a>>;
+ fn green_pinned<'a>(self, &'a mut SchedPool, &'a mut SchedHandle)
+ -> TaskBuilder<GreenSpawner<'a>>;
+}
+
+impl<S: Spawner> GreenTaskBuilder for TaskBuilder<S> {
+ fn green<'a>(self, pool: &'a mut SchedPool) -> TaskBuilder<GreenSpawner<'a>> {
+ self.spawner(GreenSpawner {pool: pool, handle: None})
+ }
+
+ fn green_pinned<'a>(self, pool: &'a mut SchedPool, handle: &'a mut SchedHandle)
+ -> TaskBuilder<GreenSpawner<'a>> {
+ self.spawner(GreenSpawner {pool: pool, handle: Some(handle)})
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use std::task::TaskBuilder;
+ use super::{SchedPool, PoolConfig, GreenTaskBuilder};
+
+ #[test]
+ fn test_green_builder() {
+ let mut pool = SchedPool::new(PoolConfig::new());
+ let res = TaskBuilder::new().green(&mut pool).try(proc() {
+ "Success!".to_string()
+ });
+ assert_eq!(res.ok().unwrap(), "Success!".to_string());
+ pool.shutdown();
+ }
+}
*/
#![crate_id = "uuid#0.11.0-pre"]
+ #![experimental]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![license = "MIT/ASL2"]
/// * `d3` A 16-bit word
/// * `d4` Array of 8 octets
pub fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8]) -> Uuid {
- use std::mem::{to_be16, to_be32};
-
// First construct a temporary field-based struct
let mut fields = UuidFields {
data1: 0,
data4: [0, ..8]
};
- fields.data1 = to_be32(d1);
- fields.data2 = to_be16(d2);
- fields.data3 = to_be16(d3);
+ fields.data1 = d1.to_be();
+ fields.data2 = d2.to_be();
+ fields.data3 = d3.to_be();
slice::bytes::copy_memory(fields.data4, d4);
unsafe {
///
/// Example: `550e8400-e29b-41d4-a716-446655440000`
pub fn to_hyphenated_str(&self) -> String {
- use std::mem::{to_be16, to_be32};
// Convert to field-based struct as it matches groups in output.
// Ensure fields are in network byte order, as per RFC.
let mut uf: UuidFields;
unsafe {
uf = transmute_copy(&self.bytes);
}
- uf.data1 = to_be32(uf.data1);
- uf.data2 = to_be16(uf.data2);
- uf.data3 = to_be16(uf.data3);
+ uf.data1 = uf.data1.to_be();
+ uf.data2 = uf.data2.to_be();
+ uf.data3 = uf.data3.to_be();
let s = format!("{:08x}-{:04x}-{:04x}-{:02x}{:02x}-\
{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
uf.data1,