Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
rcore-os
RCore Tutorial V3
提交
43c6b7cf
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,发现更多精彩内容 >>
提交
43c6b7cf
编写于
10月 07, 2021
作者:
Y
Yifan Wu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add race_adder_{atomic,loop}.
上级
136e26ae
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
99 addition
and
6 deletion
+99
-6
user/src/bin/race_adder.rs
user/src/bin/race_adder.rs
+6
-6
user/src/bin/race_adder_atomic.rs
user/src/bin/race_adder_atomic.rs
+46
-0
user/src/bin/race_adder_loop.rs
user/src/bin/race_adder_loop.rs
+47
-0
未找到文件。
user/src/bin/race_adder.rs
浏览文件 @
43c6b7cf
...
@@ -9,21 +9,23 @@ use user_lib::{exit, thread_create, waittid, get_time};
...
@@ -9,21 +9,23 @@ use user_lib::{exit, thread_create, waittid, get_time};
use
alloc
::
vec
::
Vec
;
use
alloc
::
vec
::
Vec
;
static
mut
A
:
usize
=
0
;
static
mut
A
:
usize
=
0
;
const
PER_THREAD
:
usize
=
200
0000
;
const
PER_THREAD
:
usize
=
1
0000
;
const
THREAD_COUNT
:
usize
=
8
;
const
THREAD_COUNT
:
usize
=
8
;
unsafe
fn
f
()
->
!
{
unsafe
fn
f
()
->
!
{
let
start
=
get_time
()
;
let
mut
t
=
2u
size
;
for
_
in
0
..
PER_THREAD
{
for
_
in
0
..
PER_THREAD
{
let
a
=
&
mut
A
as
*
mut
usize
;
let
a
=
&
mut
A
as
*
mut
usize
;
let
cur
=
a
.read_volatile
();
let
cur
=
a
.read_volatile
();
for
_
in
0
..
500
{
t
=
t
*
t
%
10007
;
}
a
.write_volatile
(
cur
+
1
);
a
.write_volatile
(
cur
+
1
);
}
}
exit
(
(
get_time
()
-
start
)
as
i32
)
exit
(
t
as
i32
)
}
}
#[no_mangle]
#[no_mangle]
pub
fn
main
()
->
i32
{
pub
fn
main
()
->
i32
{
let
start
=
get_time
();
let
mut
v
=
Vec
::
new
();
let
mut
v
=
Vec
::
new
();
for
_
in
0
..
THREAD_COUNT
{
for
_
in
0
..
THREAD_COUNT
{
v
.push
(
thread_create
(
f
as
usize
)
as
usize
);
v
.push
(
thread_create
(
f
as
usize
)
as
usize
);
...
@@ -32,9 +34,7 @@ pub fn main() -> i32 {
...
@@ -32,9 +34,7 @@ pub fn main() -> i32 {
for
tid
in
v
.iter
()
{
for
tid
in
v
.iter
()
{
time_cost
.push
(
waittid
(
*
tid
));
time_cost
.push
(
waittid
(
*
tid
));
}
}
for
(
i
,
cost
)
in
time_cost
.iter
()
.enumerate
()
{
println!
(
"time cost is {}ms"
,
get_time
()
-
start
);
println!
(
"cost of thread#{} is {}ms"
,
i
,
cost
);
}
assert_eq!
(
unsafe
{
A
},
PER_THREAD
*
THREAD_COUNT
);
assert_eq!
(
unsafe
{
A
},
PER_THREAD
*
THREAD_COUNT
);
0
0
}
}
user/src/bin/race_adder_atomic.rs
0 → 100644
浏览文件 @
43c6b7cf
#![no_std]
#![no_main]
#[macro_use]
extern
crate
user_lib
;
extern
crate
alloc
;
use
user_lib
::{
exit
,
thread_create
,
waittid
,
get_time
,
yield_
};
use
alloc
::
vec
::
Vec
;
use
core
::
sync
::
atomic
::{
AtomicBool
,
Ordering
};
static
mut
A
:
usize
=
0
;
static
OCCUPIED
:
AtomicBool
=
AtomicBool
::
new
(
false
);
const
PER_THREAD
:
usize
=
100000
;
const
THREAD_COUNT
:
usize
=
8
;
unsafe
fn
f
()
->
!
{
let
mut
t
=
2u
size
;
for
_
in
0
..
PER_THREAD
{
while
OCCUPIED
.compare_exchange
(
false
,
true
,
Ordering
::
Relaxed
,
Ordering
::
Relaxed
)
.is_err
()
{
yield_
();
}
let
a
=
&
mut
A
as
*
mut
usize
;
let
cur
=
a
.read_volatile
();
for
_
in
0
..
500
{
t
=
t
*
t
%
10007
;
}
a
.write_volatile
(
cur
+
1
);
OCCUPIED
.store
(
false
,
Ordering
::
Relaxed
);
}
exit
(
t
as
i32
)
}
#[no_mangle]
pub
fn
main
()
->
i32
{
let
start
=
get_time
();
let
mut
v
=
Vec
::
new
();
for
_
in
0
..
THREAD_COUNT
{
v
.push
(
thread_create
(
f
as
usize
)
as
usize
);
}
let
mut
time_cost
=
Vec
::
new
();
for
tid
in
v
.iter
()
{
time_cost
.push
(
waittid
(
*
tid
));
}
println!
(
"time cost is {}ms"
,
get_time
()
-
start
);
assert_eq!
(
unsafe
{
A
},
PER_THREAD
*
THREAD_COUNT
);
0
}
user/src/bin/race_adder_loop.rs
0 → 100644
浏览文件 @
43c6b7cf
#![no_std]
#![no_main]
#[macro_use]
extern
crate
user_lib
;
extern
crate
alloc
;
use
user_lib
::{
exit
,
thread_create
,
waittid
,
get_time
,
yield_
};
use
alloc
::
vec
::
Vec
;
static
mut
A
:
usize
=
0
;
static
mut
OCCUPIED
:
bool
=
false
;
const
PER_THREAD
:
usize
=
10000
;
const
THREAD_COUNT
:
usize
=
8
;
unsafe
fn
f
()
->
!
{
let
mut
t
=
2u
size
;
for
_
in
0
..
PER_THREAD
{
while
OCCUPIED
{
yield_
();
}
OCCUPIED
=
true
;
// enter critical section
let
a
=
&
mut
A
as
*
mut
usize
;
let
cur
=
a
.read_volatile
();
for
_
in
0
..
500
{
t
=
t
*
t
%
10007
;
}
a
.write_volatile
(
cur
+
1
);
// exit critical section
OCCUPIED
=
false
;
}
exit
(
t
as
i32
)
}
#[no_mangle]
pub
fn
main
()
->
i32
{
let
start
=
get_time
();
let
mut
v
=
Vec
::
new
();
for
_
in
0
..
THREAD_COUNT
{
v
.push
(
thread_create
(
f
as
usize
)
as
usize
);
}
let
mut
time_cost
=
Vec
::
new
();
for
tid
in
v
.iter
()
{
time_cost
.push
(
waittid
(
*
tid
));
}
println!
(
"time cost is {}ms"
,
get_time
()
-
start
);
assert_eq!
(
unsafe
{
A
},
PER_THREAD
*
THREAD_COUNT
);
0
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录