1
+ // copyright 2013 the rust project developers. see the copyright
2
+ // file at the top-level directory of this distribution and at
3
+ // http://rust-lang.org/copyright.
4
+ //
5
+ // licensed under the apache license, version 2.0 <license-apache or
6
+ // http://www.apache.org/licenses/license-2.0> or the mit license
7
+ // <license-mit or http://opensource.org/licenses/mit>, at your
8
+ // option. this file may not be copied, modified, or distributed
9
+ // except according to those terms.
10
+ use option:: { Option , Some , None } ;
11
+ use result:: { Ok , Err } ;
12
+ use rt:: io:: { io_error} ;
13
+ use rt:: rtio:: { IoFactory , IoFactoryObject ,
14
+ RtioTimer , RtioTimerObject } ;
15
+ use rt:: local:: Local ;
16
+
17
+ pub struct Timer ( ~RtioTimerObject ) ;
18
+
19
+ impl Timer {
20
+ fn new ( i : ~RtioTimerObject ) -> Timer {
21
+ Timer ( i)
22
+ }
23
+
24
+ pub fn init ( ) -> Option < Timer > {
25
+ let timer = unsafe {
26
+ rtdebug ! ( "Timer::init: borrowing io to init timer" ) ;
27
+ let io = Local :: unsafe_borrow :: < IoFactoryObject > ( ) ;
28
+ rtdebug ! ( "about to init timer" ) ;
29
+ ( * io) . timer_init ( )
30
+ } ;
31
+ match timer {
32
+ Ok ( t) => Some ( Timer :: new ( t) ) ,
33
+ Err ( ioerr) => {
34
+ rtdebug ! ( "Timer::init: failed to init: %?" , ioerr) ;
35
+ io_error:: cond. raise ( ioerr) ;
36
+ None
37
+ }
38
+ }
39
+ }
40
+ }
41
+
42
+ impl RtioTimer for Timer {
43
+ fn sleep ( & self , msecs : u64 ) {
44
+ ( * * self ) . sleep ( msecs) ;
45
+ }
46
+ }
47
+
48
+ #[ cfg( test) ]
49
+ mod test {
50
+ use super :: * ;
51
+ use rt:: test:: * ;
52
+ use option:: { Some , None } ;
53
+ #[ test]
54
+ fn test_io_timer_sleep_simple ( ) {
55
+ do run_in_newsched_task {
56
+ let timer = Timer :: init ( ) ;
57
+ match timer {
58
+ Some ( t) => t. sleep ( 1 ) ,
59
+ None => assert ! ( false )
60
+ }
61
+ }
62
+ }
63
+ }
0 commit comments