Skip to content

Commit bc7a6db

Browse files
committed
digital_io: test: use thread local storage instead of global line list
Cargo test runs test in parallel using one thread per test. Usually parallelism in rust is safe enough to not make this an issue but here we built something intentionally hacky (to keep the non-test/ mock code simple) in the form of a global index of mock GPIO lines whos values can be read out in test code. The next commit will introduce a second dut_power thread that uses the same GPIO lines as the first test. As all tests run in the same process this means that they share the same list of mock GPIO lines, resulting in havoc when the tests run in parallel. This time we can get around this issue by using a list of mock GPIO lines per thread so we do not have to complicate the non-test code. Signed-off-by: Leonard Göhrs <[email protected]>
1 parent 6b17dbf commit bc7a6db

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/digital_io/gpio/test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use std::cell::RefCell;
1819
use std::iter::Iterator;
1920
use std::ops::BitOr;
2021
use std::sync::atomic::{AtomicU8, Ordering};
2122
use std::thread::sleep;
2223
use std::time::Duration;
2324

2425
use anyhow::Result;
25-
use async_std::sync::{Arc, Mutex};
26-
use async_std::task::block_on;
26+
use async_std::sync::Arc;
2727

28-
static LINES: Mutex<Vec<(String, Arc<AtomicU8>)>> = Mutex::new(Vec::new());
28+
std::thread_local! {
29+
static LINES: RefCell<Vec<(String, Arc<AtomicU8>)>> = RefCell::new(Vec::new());
30+
}
2931

3032
pub struct LineHandle {
3133
name: String,
@@ -115,17 +117,15 @@ impl FindDecoy {
115117
}
116118

117119
pub fn find_line(name: &str) -> Option<FindDecoy> {
118-
let val = {
119-
let mut lines = block_on(LINES.lock());
120-
120+
let val = LINES.with_borrow_mut(|lines| {
121121
if let Some((_, v)) = lines.iter().find(|(n, _)| n == name) {
122122
v.clone()
123123
} else {
124124
let v = Arc::new(AtomicU8::new(0));
125125
lines.push((name.to_string(), v.clone()));
126126
v
127127
}
128-
};
128+
});
129129

130130
Some(FindDecoy {
131131
name: name.to_string(),

0 commit comments

Comments
 (0)