xref: /nrf52832-nimble/rt-thread/examples/kernel/semaphore_producer_consumer.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * 程序清单:生产者消费者例子
3  *
4  * 这个例子中将创建两个线程用于实现生产者消费者问题
5  */
6 #include <rtthread.h>
7 #include "tc_comm.h"
8 
9 /* 定义最大5个元素能够被产生 */
10 #define MAXSEM 5
11 
12 /* 用于放置生产的整数数组 */
13 rt_uint32_t array[MAXSEM];
14 /* 指向生产者、消费者在array数组中的读写位置 */
15 static rt_uint32_t set, get;
16 
17 /* 指向线程控制块的指针 */
18 static rt_thread_t producer_tid = RT_NULL;
19 static rt_thread_t consumer_tid = RT_NULL;
20 
21 struct rt_semaphore sem_lock;
22 struct rt_semaphore sem_empty, sem_full;
23 
24 /* 生成者线程入口 */
producer_thread_entry(void * parameter)25 void producer_thread_entry(void* parameter)
26 {
27     int cnt = 0;
28 
29     /* 运行100次 */
30     while( cnt < 100)
31     {
32         /* 获取一个空位 */
33         rt_sem_take(&sem_empty, RT_WAITING_FOREVER);
34 
35         /* 修改array内容,上锁 */
36         rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
37         array[set%MAXSEM] = cnt + 1;
38         rt_kprintf("the producer generates a number: %d\n", array[set%MAXSEM]);
39         set++;
40         rt_sem_release(&sem_lock);
41 
42         /* 发布一个满位 */
43         rt_sem_release(&sem_full);
44         cnt++;
45 
46         /* 暂停一段时间 */
47         rt_thread_delay(50);
48     }
49 
50     rt_kprintf("the producer exit!\n");
51 }
52 
53 /* 消费者线程入口 */
consumer_thread_entry(void * parameter)54 void consumer_thread_entry(void* parameter)
55 {
56     rt_uint32_t no;
57     rt_uint32_t sum;
58 
59     /* 第n个线程,由入口参数传进来 */
60     no = (rt_uint32_t)parameter;
61 
62     sum = 0;
63     while(1)
64     {
65         /* 获取一个满位 */
66         rt_sem_take(&sem_full, RT_WAITING_FOREVER);
67 
68         /* 临界区,上锁进行操作 */
69         rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
70         sum += array[get%MAXSEM];
71         rt_kprintf("the consumer[%d] get a number: %d\n", no, array[get%MAXSEM] );
72         get++;
73         rt_sem_release(&sem_lock);
74 
75         /* 释放一个空位 */
76         rt_sem_release(&sem_empty);
77 
78         /* 生产者生产到100个数目,停止,消费者线程相应停止 */
79         if (get == 100) break;
80 
81         /* 暂停一小会时间 */
82         rt_thread_delay(10);
83     }
84 
85     rt_kprintf("the consumer[%d] sum is %d \n ", no, sum);
86     rt_kprintf("the consumer[%d] exit!\n");
87 }
88 
semaphore_producer_consumer_init()89 int semaphore_producer_consumer_init()
90 {
91     /* 初始化3个信号量 */
92     rt_sem_init(&sem_lock , "lock",     1,      RT_IPC_FLAG_FIFO);
93     rt_sem_init(&sem_empty, "empty",    MAXSEM, RT_IPC_FLAG_FIFO);
94     rt_sem_init(&sem_full , "full",     0,      RT_IPC_FLAG_FIFO);
95 
96     /* 创建线程1 */
97     producer_tid = rt_thread_create("producer",
98                                     producer_thread_entry, RT_NULL, /* 线程入口是producer_thread_entry, 入口参数是RT_NULL */
99                                     THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
100     if (producer_tid != RT_NULL)
101         rt_thread_startup(producer_tid);
102     else
103         tc_stat(TC_STAT_END | TC_STAT_FAILED);
104 
105     /* 创建线程2 */
106     consumer_tid = rt_thread_create("consumer",
107                                     consumer_thread_entry, RT_NULL, /* 线程入口是consumer_thread_entry, 入口参数是RT_NULL */
108                                     THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
109     if (consumer_tid != RT_NULL)
110         rt_thread_startup(consumer_tid);
111     else
112         tc_stat(TC_STAT_END | TC_STAT_FAILED);
113 
114     return 0;
115 }
116 
117 #ifdef RT_USING_TC
_tc_cleanup()118 static void _tc_cleanup()
119 {
120     /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */
121     rt_enter_critical();
122 
123     rt_sem_detach(&sem_lock);
124     rt_sem_detach(&sem_empty);
125     rt_sem_detach(&sem_full);
126 
127     /* 删除线程 */
128     if (producer_tid != RT_NULL && producer_tid->stat != RT_THREAD_CLOSE)
129         rt_thread_delete(producer_tid);
130     if (consumer_tid != RT_NULL && consumer_tid->stat != RT_THREAD_CLOSE)
131         rt_thread_delete(consumer_tid);
132 
133     /* 调度器解锁 */
134     rt_exit_critical();
135 
136     /* 设置TestCase状态 */
137     tc_done(TC_STAT_PASSED);
138 }
139 
_tc_semaphore_producer_consumer()140 int _tc_semaphore_producer_consumer()
141 {
142     /* 设置TestCase清理回调函数 */
143     tc_cleanup(_tc_cleanup);
144     semaphore_producer_consumer_init();
145 
146     /* 返回TestCase运行的最长时间 */
147     return 100;
148 }
149 /* 输出函数命令到finsh shell中 */
150 FINSH_FUNCTION_EXPORT(_tc_semaphore_producer_consumer, producer and consumer example);
151 #else
152 /* 用户应用入口 */
rt_application_init()153 int rt_application_init()
154 {
155     semaphore_producer_consumer_init();
156 
157     return 0;
158 }
159 #endif
160