Skip to content

Commit 36a8b5f

Browse files
committed
tests: add simple semaphore test
1 parent 35a417d commit 36a8b5f

File tree

7 files changed

+66
-0
lines changed

7 files changed

+66
-0
lines changed

tests/semaphore/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
get_filename_component(ZEPHYR_RUST ${CMAKE_CURRENT_SOURCE_DIR}/../.. ABSOLUTE)
5+
list(APPEND ZEPHYR_MODULES ${ZEPHYR_RUST})
6+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
7+
project(rust)
8+
target_sources(app PRIVATE ./src/main.c)

tests/semaphore/Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/semaphore/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "app"
3+
version = "0.1.0"
4+
authors = ["Tyler Hall <tylerwhall@gmail.com>"]
5+
edition = "2018"
6+
7+
[dependencies]

tests/semaphore/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_RUST=y
2+
CONFIG_ZTEST=y

tests/semaphore/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
extern crate libc;
2+
extern crate zephyr;
3+
extern crate zephyr_macros;
4+
5+
use libc::c_void;
6+
use zephyr::context::Kernel as C;
7+
use zephyr::semaphore::*;
8+
9+
zephyr_macros::k_sem_define!(TEST_SEM, 0, 10);
10+
11+
#[no_mangle]
12+
pub extern "C" fn rust_sem_thread(_a: *const c_void, _b: *const c_void, _c: *const c_void) {
13+
for i in 0..10 {
14+
println!("Giving {}", i);
15+
TEST_SEM.give::<C>();
16+
}
17+
}
18+
19+
#[no_mangle]
20+
pub extern "C" fn rust_test_main() {
21+
for i in 0..10 {
22+
println!("Taking {}", i);
23+
TEST_SEM.take::<C>();
24+
println!("Took {}", i);
25+
}
26+
}

tests/semaphore/src/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <zephyr.h>
2+
3+
extern void rust_test_main(void);
4+
extern void rust_sem_thread(void *, void *, void *);
5+
6+
K_THREAD_DEFINE(sem_thread, 1024, rust_sem_thread, NULL, NULL, NULL,
7+
K_LOWEST_APPLICATION_THREAD_PRIO, 0, K_NO_WAIT);
8+
9+
void test_main(void)
10+
{
11+
rust_test_main();
12+
}

tests/semaphore/testcase.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
common:
2+
arch_whitelist: x86 arm posix
3+
tests:
4+
rust.semaphore:
5+
tags: rust

0 commit comments

Comments
 (0)