1
0

semaphore_producer_consumer.c 4.5 KB

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