ksync_kernel_tests/
kernel.rs1#![no_std]
6
7#[ksync::guarded]
8struct GuardedMutexObj {
9 #[mutex]
10 mu: ksync::KMutex,
11 #[guarded_by(mu)]
12 value: u32,
13}
14
15#[ksync::guarded]
16struct GuardedSpinlockObj {
17 #[mutex]
18 mu: ksync::KMutex<ksync::RawSpinlock>,
19 #[guarded_by(mu)]
20 value: u32,
21}
22
23#[unsafe(no_mangle)]
24pub extern "C" fn test_ksync_spinlock() -> bool {
25 ksync::pin_init::stack_pin_init!(let obj = ksync::pin_init::pin_init!(GuardedSpinlockObj {
26 mu <- ksync::KMutex::init(),
27 value: 100.into(),
28 }));
29
30 {
31 ksync::lock!(let mut guard = obj.lock_mu());
32 if *guard.value() != 100 {
33 return false;
34 }
35 *guard.as_mut().value_mut() = 101;
36 }
37
38 {
39 ksync::lock!(let guard = obj.lock_mu());
40 if *guard.value() != 101 {
41 return false;
42 }
43 }
44 true
45}
46
47#[unsafe(no_mangle)]
48pub extern "C" fn test_ksync_mutex() -> bool {
49 ksync::pin_init::stack_pin_init!(let obj = ksync::pin_init::pin_init!(GuardedMutexObj {
50 mu <- ksync::KMutex::init(),
51 value: 42.into(),
52 }));
53
54 {
55 ksync::lock!(let mut guard = obj.lock_mu());
56 if *guard.value() != 42 {
57 return false;
58 }
59 *guard.as_mut().value_mut() = 43;
60 }
61
62 {
63 ksync::lock!(let guard = obj.lock_mu());
64 if *guard.value() != 43 {
65 return false;
66 }
67 }
68 true
69}
70
71#[unsafe(no_mangle)]
72pub extern "C" fn test_ksync_event() -> bool {
73 ksync::pin_init::stack_pin_init!(let event = ksync::KEvent::init(false));
74 if event.wait_deadline(0).is_ok() {
75 return false;
76 }
77 event.signal();
78 if event.wait_deadline(0).is_err() {
79 return false;
80 }
81 event.unsignal();
82 if event.wait_deadline(0).is_ok() {
83 return false;
84 }
85 true
86}