@@ -12,7 +12,10 @@ use std::sync::{
1212 mpsc:: { channel, Receiver , Sender } ,
1313 Arc , Mutex ,
1414} ;
15- use std:: { thread, thread:: JoinHandle } ;
15+ use std:: {
16+ time:: Duration ,
17+ thread:: { self , JoinHandle } ,
18+ } ;
1619
1720/// A thread that sends a notification every 10th second
1821///
@@ -33,7 +36,7 @@ pub struct Timer {
3336
3437impl Timer {
3538 /// Initialize Timer and run a thread to send events to attached listeners
36- pub fn initialize ( self ) -> Result < Self > {
39+ pub fn initialize ( self , accumulation_cycle : Duration ) -> Result < Self > {
3740 let txs = Arc :: clone ( & self . txs ) ;
3841
3942 // Add Default tx
@@ -44,11 +47,11 @@ impl Timer {
4447
4548 let handle = Some ( thread:: spawn ( move || {
4649 // Wait for initial expiration
47- let initial_event = Timer :: register_initial_expiration ( kqueue) ?;
50+ let initial_event = Timer :: register_initial_expiration ( kqueue, Duration :: from_millis ( 0 ) ) ?;
4851 Timer :: wait_event ( kqueue, [ initial_event] . as_mut_ptr ( ) ) ?;
4952
5053 // Register loop event
51- let loop_event = Timer :: register_loop_expiration ( kqueue) ?;
54+ let loop_event = Timer :: register_loop_expiration ( kqueue, accumulation_cycle ) ?;
5255
5356 // Loop 10s
5457 loop {
@@ -132,14 +135,14 @@ impl Timer {
132135 }
133136
134137 /// Register a loop expiration event
135- fn register_loop_expiration ( kqueue : i32 ) -> Result < libc:: kevent > {
138+ fn register_loop_expiration ( kqueue : i32 , duration : Duration ) -> Result < libc:: kevent > {
136139 let loop_event = libc:: kevent {
137140 ident : 1 ,
138141 filter : libc:: EVFILT_TIMER ,
139142 flags : libc:: EV_ADD | libc:: EV_ENABLE ,
140143 fflags : 0 ,
141- data : 10000 ,
142- udata : 0 as * mut libc :: c_void ,
144+ data : duration . as_millis ( ) ,
145+ udata : std :: ptr :: null ( ) ,
143146 } ;
144147
145148 // add loop event
0 commit comments