116
116
//! extern crate green;
117
117
//!
118
118
//! #[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
+ //! }
120
122
//!
121
123
//! fn main() {
122
124
//! // this code is running in a pool of schedulers
123
125
//! }
124
126
//! ```
125
127
//!
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
+ //!
126
147
//! # Using a scheduler pool
127
148
//!
128
149
//! ```rust
176
197
#[ allow( visible_private_types) ] ;
177
198
178
199
#[ cfg( test) ] #[ phase( syntax, link) ] extern crate log;
200
+ #[ cfg( test) ] extern crate rustuv;
179
201
extern crate rand;
180
202
181
203
use std:: mem:: replace;
182
204
use std:: os;
183
- use std:: rt:: crate_map;
184
205
use std:: rt:: rtio;
185
206
use std:: rt:: thread:: Thread ;
186
207
use std:: rt;
@@ -207,16 +228,6 @@ pub mod sleeper_list;
207
228
pub mod stack;
208
229
pub mod task;
209
230
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
-
220
231
/// Set up a default runtime configuration, given compiler-supplied arguments.
221
232
///
222
233
/// 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 {
235
246
///
236
247
/// The return value is used as the process return code. 0 on success, 101 on
237
248
/// 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 {
239
252
rt:: init ( argc, argv) ;
240
253
let mut main = Some ( main) ;
241
254
let mut ret = None ;
242
255
simple:: task ( ) . run ( || {
243
- ret = Some ( run ( main. take_unwrap ( ) ) ) ;
256
+ ret = Some ( run ( event_loop_factory , main. take_unwrap ( ) ) ) ;
244
257
} ) ;
245
258
// unsafe is ok b/c we're sure that the runtime is gone
246
259
unsafe { rt:: cleanup ( ) }
@@ -255,10 +268,12 @@ pub fn start(argc: int, argv: **u8, main: proc()) -> int {
255
268
///
256
269
/// This function will not return until all schedulers in the associated pool
257
270
/// have returned.
258
- pub fn run ( main : proc ( ) ) -> int {
271
+ pub fn run ( event_loop_factory : fn ( ) -> ~rtio :: EventLoop , main : proc ( ) ) -> int {
259
272
// Create a scheduler pool and spawn the main task into this pool. We will
260
273
// 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) ;
262
277
let ( tx, rx) = channel ( ) ;
263
278
let mut opts = TaskOpts :: new ( ) ;
264
279
opts. notify_chan = Some ( tx) ;
@@ -283,7 +298,7 @@ pub struct PoolConfig {
283
298
threads : uint ,
284
299
/// A factory function used to create new event loops. If this is not
285
300
/// specified then the default event loop factory is used.
286
- event_loop_factory : Option < fn ( ) -> ~rtio:: EventLoop > ,
301
+ event_loop_factory : fn ( ) -> ~rtio:: EventLoop ,
287
302
}
288
303
289
304
impl PoolConfig {
@@ -292,7 +307,7 @@ impl PoolConfig {
292
307
pub fn new ( ) -> PoolConfig {
293
308
PoolConfig {
294
309
threads : rt:: default_sched_threads ( ) ,
295
- event_loop_factory : None ,
310
+ event_loop_factory : basic :: event_loop ,
296
311
}
297
312
}
298
313
}
@@ -334,7 +349,6 @@ impl SchedPool {
334
349
threads : nscheds,
335
350
event_loop_factory : factory
336
351
} = config;
337
- let factory = factory. unwrap_or ( default_event_loop_factory ( ) ) ;
338
352
assert ! ( nscheds > 0 ) ;
339
353
340
354
// The pool of schedulers that will be returned from this function
@@ -503,20 +517,3 @@ impl Drop for SchedPool {
503
517
}
504
518
}
505
519
}
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