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
176197#[ allow( visible_private_types) ] ;
177198
178199#[ cfg( test) ] #[ phase( syntax, link) ] extern crate log;
200+ #[ cfg( test) ] extern crate rustuv;
179201extern crate rand;
180202
181203use std:: mem:: replace;
182204use std:: os;
183- use std:: rt:: crate_map;
184205use std:: rt:: rtio;
185206use std:: rt:: thread:: Thread ;
186207use std:: rt;
@@ -207,16 +228,6 @@ pub mod sleeper_list;
207228pub mod stack;
208229pub 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
289304impl 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- }
0 commit comments