Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
rcore-os
RCore Tutorial V3
提交
2bd7dcf0
R
RCore Tutorial V3
项目概览
rcore-os
/
RCore Tutorial V3
上一次同步 1 年多
通知
15
Star
793
Fork
260
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
RCore Tutorial V3
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2bd7dcf0
编写于
7月 19, 2021
作者:
Y
Yifan Wu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Exclusive UPSafeCell: A RefCell wrapper
上级
a70fbfdb
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
26 addition
and
20 deletion
+26
-20
os/src/main.rs
os/src/main.rs
+1
-1
os/src/sync/up.rs
os/src/sync/up.rs
+10
-10
os/src/task/mod.rs
os/src/task/mod.rs
+15
-9
未找到文件。
os/src/main.rs
浏览文件 @
2bd7dcf0
...
@@ -26,7 +26,7 @@ fn clear_bss() {
...
@@ -26,7 +26,7 @@ fn clear_bss() {
}
}
unsafe
{
unsafe
{
core
::
slice
::
from_raw_parts_mut
(
core
::
slice
::
from_raw_parts_mut
(
sbss
as
usize
as
*
mut
u8
,
sbss
as
usize
as
*
mut
u8
,
ebss
as
usize
-
sbss
as
usize
,
ebss
as
usize
-
sbss
as
usize
,
)
.fill
(
0
);
)
.fill
(
0
);
}
}
...
...
os/src/sync/up.rs
浏览文件 @
2bd7dcf0
/// Wrap a static data structure inside it so that we are
use
core
::
cell
::{
RefCell
,
RefMut
};
/// Wrap a static data structure inside it so that we are
/// able to access it without any `unsafe`.
/// able to access it without any `unsafe`.
///
///
/// We should only use it in uniprocessor.
/// We should only use it in uniprocessor.
///
///
/// In order to get mutable reference of inner data, call
/// In order to get mutable reference of inner data, call
/// `
upsafe_access`.
/// `
exclusive_access`.
pub
struct
UPSafeCell
<
T
>
{
pub
struct
UPSafeCell
<
T
>
{
/// inner data
/// inner data
data
:
T
,
inner
:
RefCell
<
T
>
,
}
}
unsafe
impl
<
T
>
Sync
for
UPSafeCell
<
T
>
{}
unsafe
impl
<
T
>
Sync
for
UPSafeCell
<
T
>
{}
...
@@ -16,12 +18,10 @@ impl<T> UPSafeCell<T> {
...
@@ -16,12 +18,10 @@ impl<T> UPSafeCell<T> {
/// User is responsible to guarantee that inner struct is only used in
/// User is responsible to guarantee that inner struct is only used in
/// uniprocessor.
/// uniprocessor.
pub
unsafe
fn
new
(
value
:
T
)
->
Self
{
pub
unsafe
fn
new
(
value
:
T
)
->
Self
{
Self
{
data
:
value
,
}
Self
{
inner
:
RefCell
::
new
(
value
)
}
}
}
/// Mention that user should hold exactly one &mut T at a time.
/// Panic if the data has been borrowed.
pub
fn
upsafe_access
(
&
self
)
->
&
mut
T
{
pub
fn
exclusive_access
(
&
self
)
->
RefMut
<
'_
,
T
>
{
unsafe
{
self
.inner
.borrow_mut
()
&
mut
*
(
&
self
.data
as
*
const
_
as
usize
as
*
mut
T
)
}
}
}
}
}
\ No newline at end of file
os/src/task/mod.rs
浏览文件 @
2bd7dcf0
...
@@ -27,12 +27,12 @@ lazy_static! {
...
@@ -27,12 +27,12 @@ lazy_static! {
let
mut
tasks
=
[
let
mut
tasks
=
[
TaskControlBlock
{
TaskControlBlock
{
task_cx
:
TaskContext
::
zero_init
(),
task_cx
:
TaskContext
::
zero_init
(),
task_status
:
TaskStatus
::
UnInit
task_status
:
TaskStatus
::
UnInit
};
};
MAX_APP_NUM
MAX_APP_NUM
];
];
for
i
in
0
..
num_app
{
for
i
in
0
..
num_app
{
tasks
[
i
]
.task_cx
=
TaskContext
::
goto_restore
(
init_app_cx
(
i
));
tasks
[
i
]
.task_cx
=
TaskContext
::
goto_restore
(
init_app_cx
(
i
));
tasks
[
i
]
.task_status
=
TaskStatus
::
Ready
;
tasks
[
i
]
.task_status
=
TaskStatus
::
Ready
;
}
}
TaskManager
{
TaskManager
{
...
@@ -46,33 +46,37 @@ lazy_static! {
...
@@ -46,33 +46,37 @@ lazy_static! {
}
}
impl
TaskManager
{
impl
TaskManager
{
fn
run_first_task
(
&
self
)
{
fn
run_first_task
(
&
self
)
->
!
{
let
task0
=
&
mut
self
.inner
.upsafe_access
()
.tasks
[
0
];
let
mut
inner
=
self
.inner
.exclusive_access
();
let
task0
=
&
mut
inner
.tasks
[
0
];
task0
.task_status
=
TaskStatus
::
Running
;
task0
.task_status
=
TaskStatus
::
Running
;
let
next_task_cx_ptr
=
&
task0
.task_cx
as
*
const
TaskContext
;
let
next_task_cx_ptr
=
&
task0
.task_cx
as
*
const
TaskContext
;
drop
(
inner
);
let
mut
_u
nused
=
TaskContext
::
zero_init
();
let
mut
_u
nused
=
TaskContext
::
zero_init
();
// before this, we should drop local variables that must be dropped manually
unsafe
{
unsafe
{
__
switch
(
__
switch
(
&
mut
_u
nused
as
*
mut
TaskContext
,
&
mut
_u
nused
as
*
mut
TaskContext
,
next_task_cx_ptr
,
next_task_cx_ptr
,
);
);
}
}
panic!
(
"unreachable in run_first_task!"
);
}
}
fn
mark_current_suspended
(
&
self
)
{
fn
mark_current_suspended
(
&
self
)
{
let
mut
inner
=
self
.inner
.
upsaf
e_access
();
let
mut
inner
=
self
.inner
.
exclusiv
e_access
();
let
current
=
inner
.current_task
;
let
current
=
inner
.current_task
;
inner
.tasks
[
current
]
.task_status
=
TaskStatus
::
Ready
;
inner
.tasks
[
current
]
.task_status
=
TaskStatus
::
Ready
;
}
}
fn
mark_current_exited
(
&
self
)
{
fn
mark_current_exited
(
&
self
)
{
let
mut
inner
=
self
.inner
.
upsaf
e_access
();
let
mut
inner
=
self
.inner
.
exclusiv
e_access
();
let
current
=
inner
.current_task
;
let
current
=
inner
.current_task
;
inner
.tasks
[
current
]
.task_status
=
TaskStatus
::
Exited
;
inner
.tasks
[
current
]
.task_status
=
TaskStatus
::
Exited
;
}
}
fn
find_next_task
(
&
self
)
->
Option
<
usize
>
{
fn
find_next_task
(
&
self
)
->
Option
<
usize
>
{
let
inner
=
self
.inner
.
upsaf
e_access
();
let
inner
=
self
.inner
.
exclusiv
e_access
();
let
current
=
inner
.current_task
;
let
current
=
inner
.current_task
;
(
current
+
1
..
current
+
self
.num_app
+
1
)
(
current
+
1
..
current
+
self
.num_app
+
1
)
.map
(|
id
|
id
%
self
.num_app
)
.map
(|
id
|
id
%
self
.num_app
)
...
@@ -83,19 +87,21 @@ impl TaskManager {
...
@@ -83,19 +87,21 @@ impl TaskManager {
fn
run_next_task
(
&
self
)
{
fn
run_next_task
(
&
self
)
{
if
let
Some
(
next
)
=
self
.find_next_task
()
{
if
let
Some
(
next
)
=
self
.find_next_task
()
{
let
mut
inner
=
self
.inner
.
upsaf
e_access
();
let
mut
inner
=
self
.inner
.
exclusiv
e_access
();
let
current
=
inner
.current_task
;
let
current
=
inner
.current_task
;
inner
.tasks
[
next
]
.task_status
=
TaskStatus
::
Running
;
inner
.tasks
[
next
]
.task_status
=
TaskStatus
::
Running
;
inner
.current_task
=
next
;
inner
.current_task
=
next
;
let
current_task_cx_ptr
=
&
mut
inner
.tasks
[
current
]
.task_cx
as
*
mut
TaskContext
;
let
current_task_cx_ptr
=
&
mut
inner
.tasks
[
current
]
.task_cx
as
*
mut
TaskContext
;
let
next_task_cx_ptr
=
&
inner
.tasks
[
next
]
.task_cx
as
*
const
TaskContext
;
let
next_task_cx_ptr
=
&
inner
.tasks
[
next
]
.task_cx
as
*
const
TaskContext
;
drop
(
inner
);
drop
(
inner
);
// before this, we should drop local variables that must be dropped manually
unsafe
{
unsafe
{
__
switch
(
__
switch
(
current_task_cx_ptr
,
current_task_cx_ptr
,
next_task_cx_ptr
,
next_task_cx_ptr
,
);
);
}
}
// go back to user mode
}
else
{
}
else
{
panic!
(
"All applications completed!"
);
panic!
(
"All applications completed!"
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录