Skip to content

Commit 931cb95

Browse files
committed
samples/rust: add static mutex and condvar to sync sample
The sample includes local instances, this patch adds an example of how to use static mutexes and conditional varibles. Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
1 parent 56acc03 commit 931cb95

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

samples/rust/rust_sync.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ module! {
1616
license: b"GPL v2",
1717
}
1818

19+
kernel::init_static_sync! {
20+
static SAMPLE_MUTEX: Mutex<u32> = 10;
21+
static SAMPLE_CONDVAR: CondVar;
22+
}
23+
1924
struct RustSync;
2025

2126
impl KernelModule for RustSync {
@@ -45,6 +50,16 @@ impl KernelModule for RustSync {
4550
cv.free_waiters();
4651
}
4752

53+
// Test static mutex + condvar.
54+
*SAMPLE_MUTEX.lock() = 20;
55+
56+
{
57+
let mut guard = SAMPLE_MUTEX.lock();
58+
while *guard != 20 {
59+
let _ = SAMPLE_CONDVAR.wait(&mut guard);
60+
}
61+
}
62+
4863
// Test spinlocks.
4964
{
5065
// SAFETY: `init` is called below.

0 commit comments

Comments
 (0)