Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
rtfm-async-await
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
rtfm-async-await
Commits
e746d14b
Commit
e746d14b
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
async POC, mutable resource proxies
parent
c3326600
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
qemu/examples/1-rtic2.rs
+79
-0
79 additions, 0 deletions
qemu/examples/1-rtic2.rs
qemu/examples/1-rtic3.rs
+115
-0
115 additions, 0 deletions
qemu/examples/1-rtic3.rs
with
194 additions
and
0 deletions
qemu/examples/1-rtic2.rs
0 → 100644
+
79
−
0
View file @
e746d14b
#![deny(unsafe_code)]
// #![deny(warnings)]
#![no_main]
#![no_std]
use
async_cortex_m
::{
task
,
unsync
::
Mutex
};
use
cortex_m_semihosting
::{
debug
,
hprintln
};
use
panic_halt
as
_
;
// panic handler
use
rtic
::
app
;
#[rtic::app(device
=
lm3s6965)]
mod
app
{
#[resources]
struct
Resources
{
#[init(
9
)]
x
:
u32
,
}
#[idle(resources
=
[
x]
)]
fn
idle
(
mut
cx
:
idle
::
Context
)
->
!
{
static
mut
X
:
Mutex
<
i64
>
=
Mutex
::
new
(
0
);
let
c
:
&
'static
_
=
X
;
cx
.resources.x
.lock
(|
x
|
{
hprintln!
(
"x {}"
,
x
)
.ok
();
});
task
::
spawn
(
a1
(
c
));
task
::
block_on
(
async
{
loop
{
hprintln!
(
"B: before lock"
)
.ok
();
// cannot immediately make progress; context switch to A
let
mut
lock
=
c
.lock
()
.await
;
hprintln!
(
"B: {}"
,
*
lock
)
.ok
();
*
lock
+=
1
;
if
*
lock
>
3
{
debug
::
exit
(
debug
::
EXIT_SUCCESS
);
}
else
{
drop
(
lock
);
hprintln!
(
"B: yield"
)
.ok
();
task
::
r
#
yield
()
.await
;
}
}
});
loop
{
continue
;
}
}
#[task(binds
=
UART0,
resources
=
[
x]
)]
fn
rt_task
(
cx
:
rt_task
::
Context
)
{
hprintln!
(
"x {}"
,
cx
.resources.x
)
.ok
();
}
}
async
fn
a1
(
c
:
&
Mutex
<
i64
>
)
{
loop
{
hprintln!
(
"A: before lock"
)
.ok
();
let
mut
lock
=
c
.lock
()
.await
;
hprintln!
(
"A: before write {}"
,
*
lock
)
.ok
();
*
lock
+=
1
;
drop
(
lock
);
hprintln!
(
"A: after releasing the lock"
)
.ok
();
hprintln!
(
"A: manual yield"
)
.ok
();
task
::
r
#
yield
()
.await
;
}
}
// fn some_driver() -> impl Future {
// task::r#yield()
// }
This diff is collapsed.
Click to expand it.
qemu/examples/1-rtic3.rs
0 → 100644
+
115
−
0
View file @
e746d14b
// #![deny(unsafe_code)]
// #![deny(warnings)]
#![no_main]
#![no_std]
use
async_cortex_m
::{
task
,
unsync
::
Mutex
};
use
cortex_m_semihosting
::{
debug
,
hprintln
};
use
panic_halt
as
_
;
// panic handler
use
rtic
::
app
;
#[rtic::app(device
=
lm3s6965)]
mod
app
{
#[resources]
struct
Resources
{
#[init(
9
)]
x
:
u32
,
}
#[idle(resources
=
[
x]
)]
fn
idle
(
mut
cx
:
idle
::
Context
)
->
!
{
let
mut
x
=
cx
.resources.x
;
task
::
spawn
(
a1
());
task
::
spawn
(
spawn_a1a
());
// task::spawn(a1a(&mut x)); // needs to be static
task
::
spawn
(
a2
());
task
::
block_on
(
async
{
let
mut
local
=
0
;
loop
{
local
+=
1
;
x
.lock
(|
x
|
{
hprintln!
(
"B: x {:?}"
,
x
)
.ok
();
});
if
local
>
3
{
debug
::
exit
(
debug
::
EXIT_SUCCESS
);
}
else
{
hprintln!
(
"B: yield"
)
.ok
();
task
::
r
#
yield
()
.await
;
}
}
});
loop
{
continue
;
}
}
#[task(binds
=
UART0,
resources
=
[
x]
)]
fn
rt_task
(
cx
:
rt_task
::
Context
)
{
hprintln!
(
"x {}"
,
cx
.resources.x
)
.ok
();
}
}
// static mut r: resources::x = resources::x::new(&rtic::export::Priority::new(1));
// some uggly code
fn
spawn_a1a
()
->
impl
core
::
future
::
Future
+
'static
{
static
prio
:
rtic
::
export
::
Priority
=
unsafe
{
rtic
::
export
::
Priority
::
new
(
0
)
};
static
mut
X
:
resources
::
x
=
resources
::
x
{
priority
:
&
prio
};
a1a
(
unsafe
{
&
mut
X
})
}
async
fn
a1a
<
'a
>
(
x
:
&
mut
resources
::
x
<
'a
>
)
{
use
rtic
::
Mutex
;
loop
{
hprintln!
(
"A1: before lock"
)
.ok
();
x
.lock
(|
x
|
{
hprintln!
(
"A1: x {:?}"
,
x
)
.ok
();
*
x
=
56
;
// task::r#yield().await; // we are protected by the compiler
});
hprintln!
(
"A1: manual yield"
)
.ok
();
task
::
r
#
yield
()
.await
;
}
}
async
fn
a1
()
{
// this code must be injected by RTIC
use
rtic
::
Mutex
;
let
prio
=
unsafe
{
rtic
::
export
::
Priority
::
new
(
0
)
};
let
xr
=
&
mut
unsafe
{
resources
::
x
::
new
(
&
prio
)
};
// here is the user code
let
mut
nr_calls
=
1
;
loop
{
hprintln!
(
"A1: before lock"
)
.ok
();
xr
.lock
(|
x
|
{
hprintln!
(
"A1: x {:?}, nr_calls {:?}"
,
x
,
nr_calls
)
.ok
();
*
x
+=
nr_calls
;
nr_calls
+=
1
;
// xr.lock(|x| {
// // we are protected by the compiler
// hprintln!("A1: x {:?}", x).ok();
// *x = 56;
// });
});
hprintln!
(
"A1: manual yield"
)
.ok
();
task
::
r
#
yield
()
.await
;
}
}
async
fn
a2
()
{
loop
{
hprintln!
(
"A2: before lock"
)
.ok
();
hprintln!
(
"A2: manual yield"
)
.ok
();
task
::
r
#
yield
()
.await
;
}
}
// fn some_driver() -> impl Future {
// task::r#yield()
// }
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment