Skip to content

Commit

Permalink
Possibility to reset claims for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
nickray committed Feb 6, 2021
1 parent 1109819 commit f6ee324
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "interchange"
version = "0.1.0"
version = "0.1.1"
authors = ["Nicolas Stalder <[email protected]>"]
edition = "2018"
description = "Request/response mechanism for embedded development, using atomics"
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ pub trait Interchange: Sized {
/// Returns singleton static instances until all that were allocated are
/// used up, thereafter, `None` is returned.
fn claim() -> Option<(Requester<Self>, Responder<Self>)>;
/// Method purely for testing - do not use in production
///
/// Rationale: In production, interchanges are supposed to be set up
/// as global singletons during intialization. In testing however, multiple
/// test cases are run serially; without this reset, such tests would need
/// to allocate an extremely large amount of clients.
///
/// It does not work to put this behind a feature flag, as macro expansion
/// happens at call site and can't see the feature.
unsafe fn reset_claims();

#[doc(hidden)]
unsafe fn rq_ref(&self) -> &Self::REQUEST;
Expand Down
19 changes: 17 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,31 @@ macro_rules! interchange {
)
}
}

fn last_claimed() -> &'static core::sync::atomic::AtomicUsize {
use core::sync::atomic::{AtomicUsize, Ordering};
static LAST_CLAIMED: AtomicUsize = AtomicUsize::new(0);
&LAST_CLAIMED
}
}

impl $crate::Interchange for $Name {
type REQUEST = $REQUEST;
type RESPONSE = $RESPONSE;

unsafe fn reset_claims() {
// debug!("last claimed was {}",
// Self::last_claimed().load( core::sync::atomic::Ordering::SeqCst));
Self::last_claimed().store(0, core::sync::atomic::Ordering::SeqCst);
// debug!("last claimed is {}",
// Self::last_claimed().load( core::sync::atomic::Ordering::SeqCst));
}

fn claim() -> Option<($crate::Requester<Self>, $crate::Responder<Self>)> {
use core::sync::atomic::{AtomicUsize, Ordering};
static LAST_CLAIMED: AtomicUsize = AtomicUsize::new(0);
let index = LAST_CLAIMED.fetch_add(1, Ordering::SeqCst);
let last_claimed = Self::last_claimed();
// static LAST_CLAIMED: AtomicUsize = AtomicUsize::new(0);
let index = last_claimed.fetch_add(1, Ordering::SeqCst);
if index > $N {
None
} else {
Expand Down

0 comments on commit f6ee324

Please sign in to comment.