Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2301_76519099
rt-thread
提交
55c0a9f2
R
rt-thread
项目概览
2301_76519099
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
3
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
55c0a9f2
编写于
10月 25, 2020
作者:
St0pH@CK
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the conflicting file.
上级
bd91d549
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
469 addition
and
0 deletion
+469
-0
examples/kernel/SConscript
examples/kernel/SConscript
+1
-0
examples/kernel/mutex_priority.c
examples/kernel/mutex_priority.c
+425
-0
src/ipc.c
src/ipc.c
+43
-0
未找到文件。
examples/kernel/SConscript
100644 → 100755
浏览文件 @
55c0a9f2
...
...
@@ -19,6 +19,7 @@ semaphore_priority.c
semaphore_buffer_worker.c
semaphore_producer_consumer.c
mutex_simple.c
mutex_priority.c
event_simple.c
mbox_simple.c
mbox_send_wait.c
...
...
examples/kernel/mutex_priority.c
0 → 100755
浏览文件 @
55c0a9f2
/*
* 创建7个进程,tid20、tid21、tid22、tid23、tid24、tid25、tid26、tid27、tid28、tid29
* 各任务优先级分别为20、21、22、23、24、25、26、27、28、29,
* 其中tid26最先获得锁,tid22不需要使用到互斥锁,其他所有进程均需要使用互斥锁,
* 通过各个进程的打印信息,观察各个进程获取到互斥锁后,优先级变化情况
*/
#include <rtthread.h>
#include "tc_comm.h"
static
rt_thread_t
tid20
=
RT_NULL
;
static
rt_thread_t
tid21
=
RT_NULL
;
static
rt_thread_t
tid22
=
RT_NULL
;
static
rt_thread_t
tid23
=
RT_NULL
;
static
rt_thread_t
tid24
=
RT_NULL
;
static
rt_thread_t
tid25
=
RT_NULL
;
static
rt_thread_t
tid26
=
RT_NULL
;
static
rt_thread_t
tid27
=
RT_NULL
;
static
rt_thread_t
tid28
=
RT_NULL
;
static
rt_thread_t
tid29
=
RT_NULL
;
static
rt_mutex_t
mutex
=
RT_NULL
;
static
void
thread20_entry
(
void
*
parameter
)
{
rt_err_t
result
;
rt_tick_t
tick
;
rt_kprintf
(
"[%d] thread20 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
30
);
rt_kprintf
(
"[%d] thread20 wake!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread20 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
5
)
{
rt_kprintf
(
"[%d] thread20, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid20
->
init_priority
,
tid20
->
current_priority
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread20 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread21_entry
(
void
*
parameter
)
{
rt_err_t
result
;
rt_tick_t
tick
;
rt_kprintf
(
"[%d] thread21 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
25
);
rt_kprintf
(
"[%d] thread21 wake!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread21 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
5
)
{
rt_kprintf
(
"[%d] thread21, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid21
->
init_priority
,
tid21
->
current_priority
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread21 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread22_entry
(
void
*
parameter
)
{
rt_tick_t
tick
;
rt_kprintf
(
"[%d] thread22 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
20
);
rt_kprintf
(
"[%d] thread22 wake!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
100
)
{
rt_kprintf
(
"[%d] thread22 running..., init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid22
->
init_priority
,
tid22
->
current_priority
);
rt_thread_delay
(
2
);
}
rt_kprintf
(
"[%d] thread22 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread23_entry
(
void
*
parameter
)
{
rt_err_t
result
;
rt_tick_t
tick
;
rt_kprintf
(
"[%d] thread23 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
15
);
rt_kprintf
(
"[%d] thread23 wake!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread23 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
5
)
{
rt_kprintf
(
"[%d] thread23, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid23
->
init_priority
,
tid23
->
current_priority
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread23 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread24_entry
(
void
*
parameter
)
{
rt_err_t
result
;
rt_tick_t
tick
;
rt_kprintf
(
"[%d] thread24 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
10
);
rt_kprintf
(
"[%d] thread24 wake!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread24 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
5
)
{
rt_kprintf
(
"[%d] thread24, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid24
->
init_priority
,
tid24
->
current_priority
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread24 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread25_entry
(
void
*
parameter
)
{
rt_err_t
result
;
rt_tick_t
tick
;
rt_kprintf
(
"[%d] thread25 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
5
);
rt_kprintf
(
"[%d] thread25 wake!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread25 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
5
)
{
rt_kprintf
(
"[%d] thread25, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid25
->
init_priority
,
tid25
->
current_priority
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread25 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread26_entry
(
void
*
parameter
)
{
rt_tick_t
tick
;
rt_err_t
result
;
rt_kprintf
(
"[%d] thread26 run!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread26 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
50
)
{
rt_kprintf
(
"[%d] thread26, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid26
->
init_priority
,
tid26
->
current_priority
);
rt_thread_delay
(
1
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread26 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread27_entry
(
void
*
parameter
)
{
rt_tick_t
tick
;
rt_err_t
result
;
rt_kprintf
(
"[%d] thread27 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
35
);
rt_kprintf
(
"[%d] thread27 wake!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread27 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
5
)
{
rt_kprintf
(
"[%d] thread27, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid27
->
init_priority
,
tid27
->
current_priority
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread27 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread28_entry
(
void
*
parameter
)
{
rt_tick_t
tick
;
rt_err_t
result
;
rt_kprintf
(
"[%d] thread28 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
40
);
rt_kprintf
(
"[%d] thread28 wake!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread28 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
5
)
{
rt_kprintf
(
"[%d] thread28, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid28
->
init_priority
,
tid28
->
current_priority
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread28 exit!
\n
"
,
rt_tick_get
());
}
static
void
thread29_entry
(
void
*
parameter
)
{
rt_tick_t
tick
;
rt_err_t
result
;
rt_kprintf
(
"[%d] thread29 run!
\n
"
,
rt_tick_get
());
rt_thread_delay
(
45
);
rt_kprintf
(
"[%d] thread29 wake!
\n
"
,
rt_tick_get
());
result
=
rt_mutex_take
(
mutex
,
RT_WAITING_FOREVER
);
if
(
result
!=
RT_EOK
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
}
rt_kprintf
(
"[%d] thread29 take!
\n
"
,
rt_tick_get
());
tick
=
rt_tick_get
();
while
(
rt_tick_get
()
-
tick
<
5
)
{
rt_kprintf
(
"[%d] thread29, init_priority=%d, current_priority=%d
\n
"
,
rt_tick_get
(),
tid29
->
init_priority
,
tid29
->
current_priority
);
}
rt_mutex_release
(
mutex
);
rt_kprintf
(
"[%d] thread29 exit!
\n
"
,
rt_tick_get
());
}
static
int
mutex_simple_init
()
{
/* 创建互斥锁 */
mutex
=
rt_mutex_create
(
"mutex"
,
RT_IPC_FLAG_FIFO
);
if
(
mutex
==
RT_NULL
)
{
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
return
0
;
}
tid29
=
rt_thread_create
(
"t29"
,
thread29_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
29
,
THREAD_TIMESLICE
);
if
(
tid29
!=
RT_NULL
)
rt_thread_startup
(
tid29
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid28
=
rt_thread_create
(
"t28"
,
thread28_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
28
,
THREAD_TIMESLICE
);
if
(
tid28
!=
RT_NULL
)
rt_thread_startup
(
tid28
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid27
=
rt_thread_create
(
"t27"
,
thread27_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
27
,
THREAD_TIMESLICE
);
if
(
tid27
!=
RT_NULL
)
rt_thread_startup
(
tid27
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid26
=
rt_thread_create
(
"t26"
,
thread26_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
26
,
THREAD_TIMESLICE
);
if
(
tid26
!=
RT_NULL
)
rt_thread_startup
(
tid26
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid25
=
rt_thread_create
(
"t25"
,
thread25_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
25
,
THREAD_TIMESLICE
);
if
(
tid25
!=
RT_NULL
)
rt_thread_startup
(
tid25
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid24
=
rt_thread_create
(
"t24"
,
thread24_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
24
,
THREAD_TIMESLICE
);
if
(
tid24
!=
RT_NULL
)
rt_thread_startup
(
tid24
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid23
=
rt_thread_create
(
"t23"
,
thread23_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
23
,
THREAD_TIMESLICE
);
if
(
tid23
!=
RT_NULL
)
rt_thread_startup
(
tid23
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid22
=
rt_thread_create
(
"t22"
,
thread22_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
22
,
THREAD_TIMESLICE
);
if
(
tid22
!=
RT_NULL
)
rt_thread_startup
(
tid22
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid21
=
rt_thread_create
(
"t21"
,
thread21_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
21
,
THREAD_TIMESLICE
);
if
(
tid21
!=
RT_NULL
)
rt_thread_startup
(
tid21
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
tid20
=
rt_thread_create
(
"t20"
,
thread20_entry
,
RT_NULL
,
THREAD_STACK_SIZE
,
20
,
THREAD_TIMESLICE
);
if
(
tid20
!=
RT_NULL
)
rt_thread_startup
(
tid20
);
else
tc_stat
(
TC_STAT_END
|
TC_STAT_FAILED
);
return
0
;
}
#ifdef RT_USING_TC
static
void
_tc_cleanup
()
{
/* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
rt_enter_critical
();
/* 删除线程 */
if
(
tid20
!=
RT_NULL
&&
tid20
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid20
);
if
(
tid21
!=
RT_NULL
&&
tid21
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid21
);
if
(
tid22
!=
RT_NULL
&&
tid22
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid22
);
if
(
tid23
!=
RT_NULL
&&
tid23
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid23
);
if
(
tid24
!=
RT_NULL
&&
tid24
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid24
);
if
(
tid25
!=
RT_NULL
&&
tid25
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid25
);
if
(
tid26
!=
RT_NULL
&&
tid26
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid26
);
if
(
tid27
!=
RT_NULL
&&
tid27
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid27
);
if
(
tid28
!=
RT_NULL
&&
tid28
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid28
);
if
(
tid29
!=
RT_NULL
&&
tid29
->
stat
!=
RT_THREAD_CLOSE
)
rt_thread_delete
(
tid29
);
if
(
mutex
!=
RT_NULL
)
{
rt_mutex_delete
(
mutex
);
}
rt_kprintf
(
"test_done!
\n
"
);
/* 调度器解锁 */
rt_exit_critical
();
/* 设置TestCase状态 */
tc_done
(
TC_STAT_PASSED
);
}
int
_tc_mutex_priority
()
{
/* 设置TestCase清理回调函数 */
tc_cleanup
(
_tc_cleanup
);
mutex_simple_init
();
/* 返回TestCase运行的最长时间 */
return
100
;
}
/* 输出函数命令到finsh shell中 */
FINSH_FUNCTION_EXPORT
(
_tc_mutex_priority
,
a
priority
rollover
example
of
mutex
);
#else
/* 用户应用入口 */
int
rt_application_init
()
{
mutex_simple_init
();
return
0
;
}
#endif
src/ipc.c
100644 → 100755
浏览文件 @
55c0a9f2
...
...
@@ -37,6 +37,7 @@
* 2020-07-29 Meco Man fix thread->event_set/event_info when received an
* event without pending
* 2020-10-11 Meco Man add value overflow-check code
* 2020-10-25 hupu fix priority inversion bug of mutex
*/
#include <rtthread.h>
...
...
@@ -190,6 +191,31 @@ rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t *list)
return
RT_EOK
;
}
/**
* This function will get the highest priority from the specified
* list of threads
*
* @param list of the threads
*
* @return the highest priority
*/
rt_uint8_t
rt_ipc_get_highest_priority
(
rt_list_t
*
list
)
{
struct
rt_list_node
*
n
;
struct
rt_thread
*
sthread
;
rt_uint8_t
priority
=
RT_THREAD_PRIORITY_MAX
-
1
;
for
(
n
=
list
->
next
;
n
!=
list
;
n
=
n
->
next
)
{
sthread
=
rt_list_entry
(
n
,
struct
rt_thread
,
tlist
);
priority
=
priority
<
sthread
->
current_priority
?
priority
:
sthread
->
current_priority
;
}
return
priority
;
}
#ifdef RT_USING_SEMAPHORE
/**
* This function will initialize a semaphore and put it under control of
...
...
@@ -828,6 +854,7 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex)
register
rt_base_t
temp
;
struct
rt_thread
*
thread
;
rt_bool_t
need_schedule
;
rt_uint8_t
max_priority_in_queue
=
RT_THREAD_PRIORITY_MAX
-
1
;
/* parameter check */
RT_ASSERT
(
mutex
!=
RT_NULL
);
...
...
@@ -888,6 +915,22 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex)
/* set new owner and priority */
mutex
->
owner
=
thread
;
mutex
->
original_priority
=
thread
->
current_priority
;
/* Priority adjustment occurs only when the following conditions
* are met simultaneously:
* 1.The type of mutex is RT_IPC_FLAG_FIFO;
* 2.The priority of the thread to be resumed is not equal to the
* highest priority in the queue;
*/
max_priority_in_queue
=
rt_ipc_get_highest_priority
(
&
mutex
->
parent
.
suspend_thread
);
if
(
mutex
->
parent
.
parent
.
flag
==
RT_IPC_FLAG_FIFO
&&
thread
->
current_priority
!=
max_priority_in_queue
)
{
rt_thread_control
(
thread
,
RT_THREAD_CTRL_CHANGE_PRIORITY
,
&
(
max_priority_in_queue
));
}
if
(
mutex
->
hold
<
RT_MUTEX_HOLD_MAX
)
{
mutex
->
hold
++
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录