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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
| #include "rtthread.h"
#include "main.h"
/*
*************************************************************************
* 变量
*************************************************************************
*/
/* 定义线程控制块 */
static rt_thread_t LowPriority_thread = RT_NULL;
static rt_thread_t MidPriority_thread = RT_NULL;
static rt_thread_t HighPriority_thread = RT_NULL;
/* 定义互斥量控制块 */
static rt_mutex_t test_mux = RT_NULL;
/*
*************************************************************************
* 函数声明
*************************************************************************
*/
static void LowPriority_thread_entry(void* parameter);
static void MidPriority_thread_entry(void* parameter);
static void HighPriority_thread_entry(void* parameter);
/************************* 全局变量声明 ****************************/
uint8_t ucValue [ 2 ] = { 0x00, 0x00 };
int main(void)
{
/* 创建一个互斥量 */
test_mux = rt_mutex_create("test_mux",RT_IPC_FLAG_PRIO);
if (test_mux != RT_NULL)
rt_kprintf("mux \n\n");
LowPriority_thread = /* 线程控制块指针 */
rt_thread_create( "LowPriority", /* 线程名字 */
LowPriority_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
5, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (LowPriority_thread != RT_NULL)
rt_thread_startup(LowPriority_thread);
else
return -1;
MidPriority_thread = /* 线程控制块指针 */
rt_thread_create( "MidPriority", /* 线程名字 */
MidPriority_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
4, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (MidPriority_thread != RT_NULL)
rt_thread_startup(MidPriority_thread);
else
return -1;
HighPriority_thread = /* 线程控制块指针 */
rt_thread_create( "HighPriority", /* 线程名字 */
HighPriority_thread_entry, /* 线程入口函数 */
RT_NULL, /* 线程入口函数参数 */
512, /* 线程栈大小 */
3, /* 线程的优先级 */
20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (HighPriority_thread != RT_NULL)
rt_thread_startup(HighPriority_thread);
else
return -1;
}
/**
* @brief LowPriority_thread线程主体
* @param parameter 参数
* @retval 无
*/
static void LowPriority_thread_entry(void* parameter)
{
static uint32_t i;
/* 任务都是一个无限循环,不能返回 */
while (1)
{
rt_kprintf("LowPriority take\n\n");
rt_mutex_take(test_mux, /* 获取互斥量 */
RT_WAITING_FOREVER); /* 等待时间:一直等 */
for(i=0; i<2000000; i++) //模拟低优先级任务占用信号量
{
rt_thread_yield();
}
rt_kprintf("LowPriority release\n\n");
rt_mutex_release( test_mux ); //释放互斥量
rt_thread_delay ( 500 );
}
}
/**
* @brief MidPriority_thread线程主体
* @param parameter 参数
* @retval 无
*/
static void MidPriority_thread_entry(void* parameter)
{
while (1)
{
rt_kprintf("MidPriority runing\n\n");
rt_thread_delay ( 500 );
}
}
/**
* @brief HighPriority_thread线程主体
* @param parameter 参数
* @retval 无
*/
static void HighPriority_thread_entry(void* parameter)
{
while (1)
{
rt_kprintf("HighPriority take\n\n");
rt_mutex_take(test_mux, /* 获取互斥量 */
RT_WAITING_FOREVER); /* 等待时间:一直等 */
rt_kprintf("HighPriority give\n\n");
rt_mutex_release( test_mux ); //释放互斥量
rt_thread_delay ( 500 );
}
}
|