aboutsummaryrefslogtreecommitdiff
path: root/examples/multilock.rs
blob: a6985dfa0c093f21be70f2ae85a4914279acd150 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! examples/mutlilock.rs
//!
//! The multi-lock feature example.

#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]

use panic_semihosting as _;

#[rtic::app(device = lm3s6965)]
mod app {
    use cortex_m_semihosting::{debug, hprintln};
    use lm3s6965::Interrupt;

    #[resources]
    struct Resources {
        #[init(0)]
        shared1: u32,
        #[init(0)]
        shared2: u32,
        #[init(0)]
        shared3: u32,
    }

    #[init]
    fn init(_: init::Context) -> init::LateResources {
        rtic::pend(Interrupt::GPIOA);

        init::LateResources {}
    }

    // when omitted priority is assumed to be `1`
    #[task(binds = GPIOA, resources = [shared1, shared2, shared3])]
    fn locks(c: locks::Context) {
        let mut s1 = c.resources.shared1;
        let mut s2 = c.resources.shared2;
        let mut s3 = c.resources.shared3;

        hprintln!("Multiple single locks").unwrap();
        s1.lock(|s1| {
            s2.lock(|s2| {
                s3.lock(|s3| {
                    *s1 += 1;
                    *s2 += 1;
                    *s3 += 1;

                    hprintln!(
                        "Multiple single locks, s1: {}, s2: {}, s3: {}",
                        *s1,
                        *s2,
                        *s3
                    )
                    .unwrap();
                })
            })
        });

        hprintln!("Multilock!").unwrap();

        (s1, s2, s3).lock(|s1, s2, s3| {
            *s1 += 1;
            *s2 += 1;
            *s3 += 1;

            hprintln!(
                "Multiple single locks, s1: {}, s2: {}, s3: {}",
                *s1,
                *s2,
                *s3
            )
            .unwrap();
        });

        debug::exit(debug::EXIT_SUCCESS);
    }
}