Skip to content

Commit b19261a

Browse files
committed
green: Remove the dependence on the crate map
This is the final nail in the coffin for the crate map. The `start` function for libgreen now has a new added parameter which is the event loop factory instead of inferring it from the crate map. The two current valid values for this parameter are `green::basic::event_loop` and `rustuv::event_loop`.
1 parent f8f60d8 commit b19261a

File tree

15 files changed

+84
-59
lines changed

15 files changed

+84
-59
lines changed

src/compiletest/compiletest.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ pub mod common;
4444
pub mod errors;
4545

4646
#[start]
47-
fn start(argc: int, argv: **u8) -> int { green::start(argc, argv, main) }
47+
fn start(argc: int, argv: **u8) -> int {
48+
green::start(argc, argv, rustuv::event_loop, main)
49+
}
4850

4951
pub fn main() {
5052
let args = os::args();

src/libgreen/basic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ mod test {
237237
fn pool() -> SchedPool {
238238
SchedPool::new(PoolConfig {
239239
threads: 1,
240-
event_loop_factory: Some(basic::event_loop),
240+
event_loop_factory: basic::event_loop,
241241
})
242242
}
243243

@@ -267,7 +267,7 @@ mod test {
267267
fn multi_thread() {
268268
let mut pool = SchedPool::new(PoolConfig {
269269
threads: 2,
270-
event_loop_factory: Some(basic::event_loop),
270+
event_loop_factory: basic::event_loop,
271271
});
272272

273273
for _ in range(0, 20) {

src/libgreen/lib.rs

+33-36
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,34 @@
116116
//! extern crate green;
117117
//!
118118
//! #[start]
119-
//! fn start(argc: int, argv: **u8) -> int { green::start(argc, argv, main) }
119+
//! fn start(argc: int, argv: **u8) -> int {
120+
//! green::start(argc, argv, green::basic::event_loop, main)
121+
//! }
120122
//!
121123
//! fn main() {
122124
//! // this code is running in a pool of schedulers
123125
//! }
124126
//! ```
125127
//!
128+
//! > **Note**: This `main` funciton in this example does *not* have I/O
129+
//! > support. The basic event loop does not provide any support
130+
//!
131+
//! # Starting with I/O support in libgreen
132+
//!
133+
//! ```rust
134+
//! extern crate green;
135+
//! extern crate rustuv;
136+
//!
137+
//! #[start]
138+
//! fn start(argc: int, argv: **u8) -> int {
139+
//! green::start(argc, argv, rustuv::event_loop, main)
140+
//! }
141+
//!
142+
//! fn main() {
143+
//! // this code is running in a pool of schedulers all powered by libuv
144+
//! }
145+
//! ```
146+
//!
126147
//! # Using a scheduler pool
127148
//!
128149
//! ```rust
@@ -176,11 +197,11 @@
176197
#[allow(visible_private_types)];
177198

178199
#[cfg(test)] #[phase(syntax, link)] extern crate log;
200+
#[cfg(test)] extern crate rustuv;
179201
extern crate rand;
180202

181203
use std::mem::replace;
182204
use std::os;
183-
use std::rt::crate_map;
184205
use std::rt::rtio;
185206
use std::rt::thread::Thread;
186207
use std::rt;
@@ -207,16 +228,6 @@ pub mod sleeper_list;
207228
pub mod stack;
208229
pub mod task;
209230

210-
#[lang = "start"]
211-
#[cfg(not(test), stage0)]
212-
pub fn lang_start(main: *u8, argc: int, argv: **u8) -> int {
213-
use std::cast;
214-
start(argc, argv, proc() {
215-
let main: extern "Rust" fn() = unsafe { cast::transmute(main) };
216-
main();
217-
})
218-
}
219-
220231
/// Set up a default runtime configuration, given compiler-supplied arguments.
221232
///
222233
/// This function will block until the entire pool of M:N schedulers have
@@ -235,12 +246,14 @@ pub fn lang_start(main: *u8, argc: int, argv: **u8) -> int {
235246
///
236247
/// The return value is used as the process return code. 0 on success, 101 on
237248
/// error.
238-
pub fn start(argc: int, argv: **u8, main: proc()) -> int {
249+
pub fn start(argc: int, argv: **u8,
250+
event_loop_factory: fn() -> ~rtio::EventLoop,
251+
main: proc()) -> int {
239252
rt::init(argc, argv);
240253
let mut main = Some(main);
241254
let mut ret = None;
242255
simple::task().run(|| {
243-
ret = Some(run(main.take_unwrap()));
256+
ret = Some(run(event_loop_factory, main.take_unwrap()));
244257
});
245258
// unsafe is ok b/c we're sure that the runtime is gone
246259
unsafe { rt::cleanup() }
@@ -255,10 +268,12 @@ pub fn start(argc: int, argv: **u8, main: proc()) -> int {
255268
///
256269
/// This function will not return until all schedulers in the associated pool
257270
/// have returned.
258-
pub fn run(main: proc()) -> int {
271+
pub fn run(event_loop_factory: fn() -> ~rtio::EventLoop, main: proc()) -> int {
259272
// Create a scheduler pool and spawn the main task into this pool. We will
260273
// get notified over a channel when the main task exits.
261-
let mut pool = SchedPool::new(PoolConfig::new());
274+
let mut cfg = PoolConfig::new();
275+
cfg.event_loop_factory = event_loop_factory;
276+
let mut pool = SchedPool::new(cfg);
262277
let (tx, rx) = channel();
263278
let mut opts = TaskOpts::new();
264279
opts.notify_chan = Some(tx);
@@ -283,7 +298,7 @@ pub struct PoolConfig {
283298
threads: uint,
284299
/// A factory function used to create new event loops. If this is not
285300
/// specified then the default event loop factory is used.
286-
event_loop_factory: Option<fn() -> ~rtio::EventLoop>,
301+
event_loop_factory: fn() -> ~rtio::EventLoop,
287302
}
288303

289304
impl PoolConfig {
@@ -292,7 +307,7 @@ impl PoolConfig {
292307
pub fn new() -> PoolConfig {
293308
PoolConfig {
294309
threads: rt::default_sched_threads(),
295-
event_loop_factory: None,
310+
event_loop_factory: basic::event_loop,
296311
}
297312
}
298313
}
@@ -334,7 +349,6 @@ impl SchedPool {
334349
threads: nscheds,
335350
event_loop_factory: factory
336351
} = config;
337-
let factory = factory.unwrap_or(default_event_loop_factory());
338352
assert!(nscheds > 0);
339353

340354
// The pool of schedulers that will be returned from this function
@@ -503,20 +517,3 @@ impl Drop for SchedPool {
503517
}
504518
}
505519
}
506-
507-
fn default_event_loop_factory() -> fn() -> ~rtio::EventLoop {
508-
match crate_map::get_crate_map() {
509-
None => {}
510-
Some(map) => {
511-
match map.event_loop_factory {
512-
None => {}
513-
Some(factory) => return factory
514-
}
515-
}
516-
}
517-
518-
// If the crate map didn't specify a factory to create an event loop, then
519-
// instead just use a basic event loop missing all I/O services to at least
520-
// get the scheduler running.
521-
return basic::event_loop;
522-
}

src/libgreen/sched.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,8 @@ fn new_sched_rng() -> XorShiftRng {
10031003

10041004
#[cfg(test)]
10051005
mod test {
1006+
use rustuv;
1007+
10061008
use std::comm;
10071009
use std::task::TaskOpts;
10081010
use std::rt::Runtime;
@@ -1017,7 +1019,7 @@ mod test {
10171019
fn pool() -> SchedPool {
10181020
SchedPool::new(PoolConfig {
10191021
threads: 1,
1020-
event_loop_factory: Some(basic::event_loop),
1022+
event_loop_factory: basic::event_loop,
10211023
})
10221024
}
10231025

@@ -1262,7 +1264,7 @@ mod test {
12621264

12631265
let mut pool = SchedPool::new(PoolConfig {
12641266
threads: 2,
1265-
event_loop_factory: None,
1267+
event_loop_factory: rustuv::event_loop,
12661268
});
12671269

12681270
// This is a regression test that when there are no schedulable tasks in
@@ -1413,7 +1415,7 @@ mod test {
14131415
fn dont_starve_1() {
14141416
let mut pool = SchedPool::new(PoolConfig {
14151417
threads: 2, // this must be > 1
1416-
event_loop_factory: Some(basic::event_loop),
1418+
event_loop_factory: basic::event_loop,
14171419
});
14181420
pool.spawn(TaskOpts::new(), proc() {
14191421
let (tx, rx) = channel();

src/libgreen/task.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ mod tests {
494494
fn spawn_opts(opts: TaskOpts, f: proc()) {
495495
let mut pool = SchedPool::new(PoolConfig {
496496
threads: 1,
497-
event_loop_factory: None,
497+
event_loop_factory: ::rustuv::event_loop,
498498
});
499499
pool.spawn(opts, f);
500500
pool.shutdown();

src/libnative/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static OS_DEFAULT_STACK_ESTIMATE: uint = 1 << 20;
6969
static OS_DEFAULT_STACK_ESTIMATE: uint = 2 * (1 << 20);
7070

7171
#[lang = "start"]
72-
#[cfg(not(test), not(stage0))]
72+
#[cfg(not(test))]
7373
pub fn lang_start(main: *u8, argc: int, argv: **u8) -> int {
7474
use std::cast;
7575
start(argc, argv, proc() {

src/librustuv/homing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ mod test {
167167
let (tx, rx) = channel();
168168
let mut pool = SchedPool::new(PoolConfig {
169169
threads: 1,
170-
event_loop_factory: None,
170+
event_loop_factory: ::event_loop,
171171
});
172172

173173
pool.spawn(TaskOpts::new(), proc() {
@@ -188,7 +188,7 @@ mod test {
188188
let (tx, rx) = channel();
189189
let mut pool = SchedPool::new(PoolConfig {
190190
threads: 1,
191-
event_loop_factory: None,
191+
event_loop_factory: ::event_loop,
192192
});
193193

194194
pool.spawn(TaskOpts::new(), proc() {

src/librustuv/lib.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use std::libc::{c_int, c_void};
5454
use std::ptr::null;
5555
use std::ptr;
5656
use std::rt::local::Local;
57+
use std::rt::rtio;
5758
use std::rt::task::{BlockedTask, Task};
5859
use std::str::raw::from_c_str;
5960
use std::str;
@@ -76,7 +77,7 @@ pub use self::tty::TtyWatcher;
7677
// '__test' module.
7778
#[cfg(test)] #[start]
7879
fn start(argc: int, argv: **u8) -> int {
79-
green::start(argc, argv, __test::main)
80+
green::start(argc, argv, event_loop, __test::main)
8081
}
8182

8283
mod macros;
@@ -104,6 +105,31 @@ pub mod tty;
104105
pub mod signal;
105106
pub mod stream;
106107

108+
/// Creates a new event loop which is powered by libuv
109+
///
110+
/// This function is used in tandem with libgreen's `PoolConfig` type as a value
111+
/// for the `event_loop_factory` field. Using this function as the event loop
112+
/// factory will power programs with libuv and enable green threading.
113+
///
114+
/// # Example
115+
///
116+
/// ```
117+
/// extern crate rustuv;
118+
/// extern crate green;
119+
///
120+
/// #[start]
121+
/// fn start(argc: int, argv: **u8) -> int {
122+
/// green::start(argc, argv, rustuv::event_loop, main)
123+
/// }
124+
///
125+
/// fn main() {
126+
/// // this code is running inside of a green task powered by libuv
127+
/// }
128+
/// ```
129+
pub fn event_loop() -> ~rtio::EventLoop {
130+
~uvio::UvEventLoop::new() as ~rtio::EventLoop
131+
}
132+
107133
/// A type that wraps a uv handle
108134
pub trait UvHandle<T> {
109135
fn uv_handle(&self) -> *T;

src/librustuv/uvio.rs

-6
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ impl rtio::EventLoop for UvEventLoop {
105105
}
106106
}
107107

108-
#[cfg(not(test))]
109-
#[lang = "event_loop_factory"]
110-
pub fn new_loop() -> ~rtio::EventLoop {
111-
~UvEventLoop::new() as ~rtio::EventLoop
112-
}
113-
114108
#[test]
115109
fn test_callback_run_once() {
116110
use std::rt::rtio::EventLoop;

src/libstd/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
// '__test' module.
8585
#[cfg(test)] #[start]
8686
fn start(argc: int, argv: **u8) -> int {
87-
green::start(argc, argv, __test::main)
87+
green::start(argc, argv, rustuv::event_loop, __test::main)
8888
}
8989

9090
pub mod macros;

src/test/run-make/bootstrap-from-c-with-green/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern crate green;
1616

1717
#[no_mangle] // this needs to get called from C
1818
pub extern "C" fn foo(argc: int, argv: **u8) -> int {
19-
green::start(argc, argv, proc() {
19+
green::start(argc, argv, rustuv::event_loop, proc() {
2020
spawn(proc() {
2121
println!("hello");
2222
});

src/test/run-pass/core-run-destroy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ macro_rules! iotest (
4949

5050
#[cfg(test)] #[start]
5151
fn start(argc: int, argv: **u8) -> int {
52-
green::start(argc, argv, __test::main)
52+
green::start(argc, argv, rustuv::event_loop, __test::main)
5353
}
5454

5555
iotest!(fn test_destroy_once() {

src/test/run-pass/issue-12684.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ extern crate green;
1515
extern crate rustuv;
1616

1717
#[start]
18-
fn start(argc: int, argv: **u8) -> int { green::start(argc, argv, main) }
18+
fn start(argc: int, argv: **u8) -> int {
19+
green::start(argc, argv, rustuv::event_loop, main)
20+
}
1921

2022
fn main() {
2123
native::task::spawn(proc() customtask());

src/test/run-pass/issue-8860.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static mut DROP_T: int = 0i;
1818

1919
#[start]
2020
fn start(argc: int, argv: **u8) -> int {
21-
let ret = green::start(argc, argv, main);
21+
let ret = green::start(argc, argv, green::basic::event_loop, main);
2222
unsafe {
2323
assert_eq!(2, DROP);
2424
assert_eq!(1, DROP_S);

src/test/run-pass/process-detach.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ use std::io::process;
2828
use std::io::signal::{Listener, Interrupt};
2929

3030
#[start]
31-
fn start(argc: int, argv: **u8) -> int { green::start(argc, argv, main) }
31+
fn start(argc: int, argv: **u8) -> int {
32+
green::start(argc, argv, rustuv::event_loop, main)
33+
}
3234

3335
fn main() {
3436
unsafe { libc::setsid(); }

0 commit comments

Comments
 (0)