@@ -80,26 +80,17 @@ pub struct CPUTimer {}
80
80
81
81
impl CPUTimer {
82
82
#[ cfg( not( target_os = "linux" ) ) ]
83
- pub fn get_or_init ( _: u64 ) -> Result < Self , Error > {
83
+ pub fn new ( _: u64 ) -> Result < Self , Error > {
84
84
log:: error!( "CPU timer: not enabled (need Linux)" ) ;
85
85
Ok ( Self { } )
86
86
}
87
87
88
88
#[ cfg( target_os = "linux" ) ]
89
- pub fn get_or_init ( ) -> Result < Self , Error > {
90
- use std:: cell:: RefCell ;
89
+ pub fn new ( ) -> Result < Self , Error > {
91
90
use std:: sync:: atomic:: Ordering ;
92
91
93
92
use linux:: * ;
94
93
95
- thread_local ! {
96
- static TIMER : RefCell <Option <CPUTimer >> = RefCell :: new( Option :: default ( ) ) ;
97
- }
98
-
99
- if let Some ( timer) = TIMER . with_borrow ( |v| v. clone ( ) ) {
100
- return Ok ( timer) ;
101
- }
102
-
103
94
let id = TIMER_COUNTER . fetch_add ( 1 , Ordering :: SeqCst ) ;
104
95
let mut tid = TimerId ( std:: ptr:: null_mut ( ) ) ;
105
96
let mut sigev: libc:: sigevent = unsafe { std:: mem:: zeroed ( ) } ;
@@ -135,10 +126,6 @@ impl CPUTimer {
135
126
. send ( SignalMsg :: Add ( ( id, this. clone ( ) ) ) )
136
127
. unwrap ( ) ;
137
128
138
- TIMER . with_borrow_mut ( |v| {
139
- assert ! ( v. replace( this. clone( ) ) . is_none( ) ) ;
140
- } ) ;
141
-
142
129
Ok ( this)
143
130
}
144
131
@@ -159,13 +146,9 @@ impl CPUTimer {
159
146
pub fn reset ( & self , initial_expiry : u64 , interval : u64 ) -> Result < ( ) , Error > {
160
147
use anyhow:: Context ;
161
148
use linux:: * ;
162
- use log:: error;
163
149
164
- error ! ( "try get lock: {:?}" , std:: thread:: current( ) ) ;
165
150
let timer = self . timer . try_lock ( ) . context ( "failed to get the lock" ) ?;
166
151
167
- error ! ( "get lock: {:?}" , std:: thread:: current( ) ) ;
168
-
169
152
let initial_expiry_secs = initial_expiry / 1000 ;
170
153
let initial_expiry_msecs = initial_expiry % 1000 ;
171
154
let interval_secs = interval / 1000 ;
@@ -182,12 +165,9 @@ impl CPUTimer {
182
165
libc:: timer_settime ( timer. tid . 0 , 0 , & tmspec, std:: ptr:: null_mut ( ) )
183
166
} < 0
184
167
{
185
- error ! ( "leave lock: {:?}" , std:: thread:: current( ) ) ;
186
168
bail ! ( std:: io:: Error :: last_os_error( ) )
187
169
}
188
170
189
- error ! ( "leave lock: {:?}" , std:: thread:: current( ) ) ;
190
-
191
171
Ok ( ( ) )
192
172
}
193
173
@@ -264,22 +244,22 @@ fn register_sigalrm() {
264
244
Some ( msg) = sig_msg_rx. recv( ) => {
265
245
match msg {
266
246
SignalMsg :: Alarm ( ref timer_id) => {
267
- if let Some ( cpu_timer) = registry. get( timer_id) {
268
- if let Some ( tx) = ( * cpu_timer. cpu_alarm_val. cpu_alarms_tx. lock( ) . await ) . clone( ) {
269
- if tx. send( ( ) ) . is_err( ) {
270
- debug!( "failed to send cpu alarm to the provided channel" ) ;
271
- }
272
- }
273
- } else {
274
- // NOTE: Unix signals are being delivered asynchronously,
275
- // and there are no guarantees to cancel the signal after
276
- // a timer has been deleted, and after a signal is
277
- // received, there may no longer be a target to accept it.
278
- error!(
279
- "can't find the cpu alarm signal matched with the received timer id: {}" ,
280
- * timer_id
281
- ) ;
247
+ if let Some ( cpu_timer) = registry. get( timer_id) {
248
+ if let Some ( tx) = ( * cpu_timer. cpu_alarm_val. cpu_alarms_tx. lock( ) . await ) . clone( ) {
249
+ if tx. send( ( ) ) . is_err( ) {
250
+ debug!( "failed to send cpu alarm to the provided channel" ) ;
251
+ }
282
252
}
253
+ } else {
254
+ // NOTE: Unix signals are being delivered asynchronously,
255
+ // and there are no guarantees to cancel the signal after
256
+ // a timer has been deleted, and after a signal is
257
+ // received, there may no longer be a target to accept it.
258
+ error!(
259
+ "can't find the cpu alarm signal matched with the received timer id: {}" ,
260
+ * timer_id
261
+ ) ;
262
+ }
283
263
}
284
264
285
265
SignalMsg :: Add ( ( timer_id, cpu_timer) ) => {
0 commit comments