@@ -29,6 +29,7 @@ extern crate core;
29
29
#[ cfg( feature="replayer" ) ] use std:: fmt;
30
30
#[ cfg( feature="replayer" ) ] use core:: cmp:: PartialEq ;
31
31
#[ cfg( feature="replayer" ) ] use core:: clone:: Clone ;
32
+ #[ cfg( feature="replayer" ) ] use core:: cell:: RefCell ;
32
33
33
34
#[ cfg( not( feature="replayer" ) ) ] use core:: intrinsics:: { volatile_load, volatile_store} ;
34
35
#[ cfg( feature="replayer" ) ] use core:: intrinsics:: transmute;
@@ -72,13 +73,13 @@ impl<T> VolatileCell<T> {
72
73
impl VolatileCell < u32 > {
73
74
pub fn get ( & self ) -> u32 {
74
75
unsafe {
75
- ( * GlobalReplayer ) . get_cell ( transmute ( & self . value ) )
76
+ GLOBAL_REPLAYER . with ( |gr| { gr . borrow_mut ( ) . get_cell ( transmute ( & self . value ) ) } )
76
77
}
77
78
}
78
79
79
80
pub fn set ( & self , value : u32 ) {
80
81
unsafe {
81
- ( * GlobalReplayer ) . set_cell ( transmute ( & self . value ) , value)
82
+ GLOBAL_REPLAYER . with ( |gr| { gr . borrow_mut ( ) . set_cell ( transmute ( & self . value ) , value) } )
82
83
}
83
84
}
84
85
}
@@ -87,13 +88,13 @@ impl VolatileCell<u32> {
87
88
impl VolatileCell < u16 > {
88
89
pub fn get ( & self ) -> u16 {
89
90
unsafe {
90
- ( * GlobalReplayer ) . get_cell ( transmute ( & self . value ) ) as u16
91
+ GLOBAL_REPLAYER . with ( |gr| { gr . borrow_mut ( ) . get_cell ( transmute ( & self . value ) ) } ) as u16
91
92
}
92
93
}
93
94
94
95
pub fn set ( & self , value : u16 ) {
95
96
unsafe {
96
- ( * GlobalReplayer ) . set_cell ( transmute ( & self . value ) , value as u32 )
97
+ GLOBAL_REPLAYER . with ( |gr| { gr . borrow_mut ( ) . set_cell ( transmute ( & self . value ) , value as u32 ) } )
97
98
}
98
99
}
99
100
}
@@ -102,13 +103,13 @@ impl VolatileCell<u16> {
102
103
impl VolatileCell < u8 > {
103
104
pub fn get ( & self ) -> u8 {
104
105
unsafe {
105
- ( * GlobalReplayer ) . get_cell ( transmute ( & self . value ) ) as u8
106
+ GLOBAL_REPLAYER . with ( |gr| { gr . borrow_mut ( ) . get_cell ( transmute ( & self . value ) ) } ) as u8
106
107
}
107
108
}
108
109
109
110
pub fn set ( & self , value : u8 ) {
110
111
unsafe {
111
- ( * GlobalReplayer ) . set_cell ( transmute ( & self . value ) , value as u32 )
112
+ GLOBAL_REPLAYER . with ( |gr| { gr . borrow_mut ( ) . set_cell ( transmute ( & self . value ) , value as u32 ) } )
112
113
}
113
114
}
114
115
}
@@ -234,13 +235,22 @@ impl VolatileCellReplayer {
234
235
}
235
236
236
237
#[ cfg( feature="replayer" ) ]
237
- static mut GlobalReplayer : * mut VolatileCellReplayer = 0 as * mut VolatileCellReplayer ;
238
+ thread_local ! ( static GLOBAL_REPLAYER : RefCell < VolatileCellReplayer > = RefCell :: new ( VolatileCellReplayer :: new ( ) ) ) ;
238
239
239
240
#[ cfg( feature="replayer" ) ]
240
- pub fn set_replayer ( replayer : & mut VolatileCellReplayer ) {
241
- unsafe {
242
- GlobalReplayer = replayer;
243
- }
241
+ pub fn set_replayer ( replayer : VolatileCellReplayer ) {
242
+ GLOBAL_REPLAYER . with ( |gr| {
243
+ let mut bm = gr. borrow_mut ( ) ;
244
+ * bm = replayer;
245
+ } ) ;
246
+ }
247
+
248
+ #[ cfg( feature="replayer" ) ]
249
+ pub fn with_mut_replayer < F > ( f : F ) where F : core:: ops:: FnOnce ( & mut VolatileCellReplayer ) {
250
+ GLOBAL_REPLAYER . with ( |gr| {
251
+ let mut bm = gr. borrow_mut ( ) ;
252
+ f ( & mut * bm) ;
253
+ } ) ;
244
254
}
245
255
246
256
struct BeEqualToWithContext < E > {
@@ -271,23 +281,34 @@ impl<A, E> Matcher<A, E> for BeEqualToWithContext<E>
271
281
272
282
#[ macro_export]
273
283
macro_rules! expect_volatile_read {
274
- ( $replayer: ident, $addr: expr, $val: expr) => (
275
- $replayer. expect_read( $addr, $val,
276
- expectest:: core:: SourceLocation :: new( file!( ) , line!( ) ) )
277
- ) ;
284
+ ( $addr: expr, $val: expr) => (
285
+ $crate:: with_mut_replayer( |r| {
286
+ r. expect_read( $addr, $val, expectest:: core:: SourceLocation :: new( file!( ) , line!( ) ) ) ;
287
+ } )
288
+ ) ;
278
289
}
279
290
280
291
#[ macro_export]
281
292
macro_rules! expect_volatile_write {
282
- ( $replayer: ident, $addr: expr, $val: expr) => (
283
- $replayer. expect_write( $addr, $val,
284
- expectest:: core:: SourceLocation :: new( file!( ) , line!( ) ) )
285
- ) ;
293
+ ( $addr: expr, $val: expr) => (
294
+ $crate:: with_mut_replayer( |r| {
295
+ r. expect_write( $addr, $val, expectest:: core:: SourceLocation :: new( file!( ) , line!( ) ) ) ;
296
+ } )
297
+ ) ;
286
298
}
287
299
288
300
#[ macro_export]
289
301
macro_rules! expect_replayer_valid {
290
- ( $replayer: ident) => (
291
- $replayer. verify( expectest:: core:: SourceLocation :: new( file!( ) , line!( ) ) )
292
- ) ;
302
+ ( ) => (
303
+ $crate:: with_mut_replayer( |r| {
304
+ r. verify( expectest:: core:: SourceLocation :: new( file!( ) , line!( ) ) ) ;
305
+ } )
306
+ ) ;
307
+ }
308
+
309
+ #[ macro_export]
310
+ macro_rules! init_replayer {
311
+ ( ) => (
312
+ set_replayer( VolatileCellReplayer :: new( ) ) ;
313
+ ) ;
293
314
}
0 commit comments