File tree Expand file tree Collapse file tree 3 files changed +28
-3
lines changed
Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change 11[package ]
22name = " interchange"
3- version = " 0.1.0 "
3+ version = " 0.1.1 "
44authors = [
" Nicolas Stalder <[email protected] >" ]
55edition = " 2018"
66description = " Request/response mechanism for embedded development, using atomics"
Original file line number Diff line number Diff line change @@ -160,6 +160,16 @@ pub trait Interchange: Sized {
160160 /// Returns singleton static instances until all that were allocated are
161161 /// used up, thereafter, `None` is returned.
162162 fn claim ( ) -> Option < ( Requester < Self > , Responder < Self > ) > ;
163+ /// Method purely for testing - do not use in production
164+ ///
165+ /// Rationale: In production, interchanges are supposed to be set up
166+ /// as global singletons during intialization. In testing however, multiple
167+ /// test cases are run serially; without this reset, such tests would need
168+ /// to allocate an extremely large amount of clients.
169+ ///
170+ /// It does not work to put this behind a feature flag, as macro expansion
171+ /// happens at call site and can't see the feature.
172+ unsafe fn reset_claims ( ) ;
163173
164174 #[ doc( hidden) ]
165175 unsafe fn rq_ref ( & self ) -> & Self :: REQUEST ;
Original file line number Diff line number Diff line change @@ -75,16 +75,31 @@ macro_rules! interchange {
7575 )
7676 }
7777 }
78+
79+ fn last_claimed( ) -> & ' static core:: sync:: atomic:: AtomicUsize {
80+ use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
81+ static LAST_CLAIMED : AtomicUsize = AtomicUsize :: new( 0 ) ;
82+ & LAST_CLAIMED
83+ }
7884 }
7985
8086 impl $crate:: Interchange for $Name {
8187 type REQUEST = $REQUEST;
8288 type RESPONSE = $RESPONSE;
8389
90+ unsafe fn reset_claims( ) {
91+ // debug!("last claimed was {}",
92+ // Self::last_claimed().load( core::sync::atomic::Ordering::SeqCst));
93+ Self :: last_claimed( ) . store( 0 , core:: sync:: atomic:: Ordering :: SeqCst ) ;
94+ // debug!("last claimed is {}",
95+ // Self::last_claimed().load( core::sync::atomic::Ordering::SeqCst));
96+ }
97+
8498 fn claim( ) -> Option <( $crate:: Requester <Self >, $crate:: Responder <Self >) > {
8599 use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
86- static LAST_CLAIMED : AtomicUsize = AtomicUsize :: new( 0 ) ;
87- let index = LAST_CLAIMED . fetch_add( 1 , Ordering :: SeqCst ) ;
100+ let last_claimed = Self :: last_claimed( ) ;
101+ // static LAST_CLAIMED: AtomicUsize = AtomicUsize::new(0);
102+ let index = last_claimed. fetch_add( 1 , Ordering :: SeqCst ) ;
88103 if index > $N {
89104 None
90105 } else {
You can’t perform that action at this time.
0 commit comments