Skip to content

Commit

Permalink
Fix and tests mutex wrapper
Browse files Browse the repository at this point in the history
Merges: #42
  • Loading branch information
chrysn authored Feb 13, 2023
2 parents 50e170a + 3814945 commit 9c29faf
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 7 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/buildtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
done
cd ..
echo "::echo ::off"
- name: Build tests
- name: Build and run tests
run: |
export BOARDS='native sltb001a samr21-xpro'
DIRS=$(echo tests/*/)
Expand All @@ -66,7 +66,12 @@ jobs:
cd ${D}
echo "::group::Building ${D}"
make buildtest BUILDTEST_MAKE_REDIRECT=''
cd ../..
echo "::endgroup::"
if make test/available BOARD=native; then
echo "::group::Testing ${D}"
make all test BOARD=native
echo "::endgroup::"
fi
cd ../..
done
echo "::echo ::off"
6 changes: 3 additions & 3 deletions src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<T> Mutex<T> {
pub fn lock(&self) -> MutexGuard<T> {
crate::thread::InThread::new()
.expect("Mutex::lock may only be called outside of interrupt contexts")
.promote(&self)
.promote(self)
.lock()
}

Expand Down Expand Up @@ -97,13 +97,13 @@ impl<T> Mutex<T> {
}
}

impl<T> crate::thread::ValueInThread<&Mutex<T>> {
impl<'a, T> crate::thread::ValueInThread<&'a Mutex<T>> {
/// Get an accessor to the mutex when the mutex is available
///
/// Through the [crate::thread::ValueInThread], this is already guaranteed to run in a thread
/// context, so no additional check is performed.
#[doc(alias = "mutex_lock")]
pub fn lock(&self) -> MutexGuard<T> {
pub fn lock(self) -> MutexGuard<'a, T> {
// unsafe: All preconditions of the C function are met (not-NULL through taking a &self,
// being initialized through RAII guarantees, thread context is in the InThread).
unsafe { riot_sys::mutex_lock(crate::inline_cast_mut(self.mutex.get())) };
Expand Down
1 change: 1 addition & 0 deletions src/thread/tokenparts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl InIsr {
/// This does barely implement anything on its own, but the module implementing `T` might provide
/// extra methods.
// Making the type fundamental results in ValueInThread<&Mutex<T>> being shown at Mutex's page.
#[derive(Copy, Clone)]
#[cfg_attr(feature = "nightly_docs", fundamental)]
pub struct ValueInThread<T> {
value: T,
Expand Down
7 changes: 5 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ Tests for riot-wrappers

Running these tests requires setting the RIOTBASE environment variable to a checkout of RIOT.

The tests are mainly intended as build tests;
they can be run safely though, provided the board files are correct.
All tests work as build tests.

The tests can all safely be run (`make flash term`) on any board (provided the board files are correct),
and some have test runners (`make test`) that work for some or all boards.
These are run in CI for native.
16 changes: 16 additions & 0 deletions tests/led/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env python3

import os
import sys
from testrunner import run

def test(child):
# Both LEDs light up at some point
child.expect("LED_RED_TOGGLE")
child.expect("LED_GREEN_TOGGLE")

if __name__ == "__main__":
if os.environ['BOARD'] != 'native':
print("Automated test only works on native (other boards don't report hteir LED activity)", file=sys.stderr)
sys.exit(1)
sys.exit(run(test))
15 changes: 15 additions & 0 deletions tests/mutex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "riot-wrappers-test-mutex"
version = "0.1.0"
authors = ["Christian Amsüss <chrysn@fsfe.org>"]
edition = "2021"
publish = false

[lib]
crate-type = ["staticlib"]

[profile.release]
panic = "abort"

[dependencies]
riot-wrappers = { version = "*", features = [ "set_panic_handler" ] }
7 changes: 7 additions & 0 deletions tests/mutex/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
APPLICATION = riot-wrappers-test-mutex
BOARD ?= native
APPLICATION_RUST_MODULE = riot_wrappers_test_mutex
BASELIBS += $(APPLICATION_RUST_MODULE).module
FEATURES_REQUIRED += rust_target

include $(RIOTBASE)/Makefile.include
28 changes: 28 additions & 0 deletions tests/mutex/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![no_std]

use riot_wrappers::mutex::Mutex;
use riot_wrappers::println;
use riot_wrappers::riot_main;
use riot_wrappers::thread::{InThread, ValueInThread};

riot_main!(main);

static M1: Mutex<()> = Mutex::new(());

fn main() {
let l1 = M1.lock();
drop(l1);
let m1 = InThread::new().unwrap().promote(&M1);
let l2 = m1.lock();
assert!(m1.try_lock().is_none());
drop(l2);
let l3 = m1.try_lock();
assert!(l3.is_some());
drop(l3);

let m2 = Mutex::new(0u8);
*m2.lock() = 4;
assert!(*m2.lock() == 4);

println!("SUCCESS");
}
10 changes: 10 additions & 0 deletions tests/mutex/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python3

import sys
from testrunner import run

def test(child):
child.expect("SUCCESS")

if __name__ == "__main__":
sys.exit(run(test))

0 comments on commit 9c29faf

Please sign in to comment.