event_sample.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. */
  10. /*
  11. * 程序清单:事件例程
  12. *
  13. * 程序会初始化2个线程及初始化一个静态事件对象
  14. * 一个线程等待于事件对象上,以接收事件;
  15. * 一个线程发送事件 (事件3/事件5)
  16. */
  17. #include <rtthread.h>
  18. #define THREAD_PRIORITY 9
  19. #define THREAD_TIMESLICE 5
  20. #define EVENT_FLAG3 (1 << 3)
  21. #define EVENT_FLAG5 (1 << 5)
  22. /* 事件控制块 */
  23. static struct rt_event event;
  24. #ifdef rt_align
  25. rt_align(RT_ALIGN_SIZE)
  26. #else
  27. ALIGN(RT_ALIGN_SIZE)
  28. #endif
  29. static char thread1_stack[1024];
  30. static struct rt_thread thread1;
  31. /* 线程1入口函数 */
  32. static void thread1_recv_event(void *param)
  33. {
  34. rt_uint32_t e;
  35. /* 第一次接收事件,事件3或事件5任意一个可以触发线程1,接收完后清除事件标志 */
  36. if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
  37. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  38. RT_WAITING_FOREVER, &e) == RT_EOK)
  39. {
  40. rt_kprintf("thread1: OR recv event 0x%x\n", e);
  41. }
  42. rt_kprintf("thread1: delay 1s to prepare the second event\n");
  43. rt_thread_mdelay(1000);
  44. /* 第二次接收事件,事件3和事件5均发生时才可以触发线程1,接收完后清除事件标志 */
  45. if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
  46. RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
  47. RT_WAITING_FOREVER, &e) == RT_EOK)
  48. {
  49. rt_kprintf("thread1: AND recv event 0x%x\n", e);
  50. }
  51. rt_kprintf("thread1 leave.\n");
  52. }
  53. #ifdef rt_align
  54. rt_align(RT_ALIGN_SIZE)
  55. #else
  56. ALIGN(RT_ALIGN_SIZE)
  57. #endif
  58. static char thread2_stack[1024];
  59. static struct rt_thread thread2;
  60. /* 线程2入口 */
  61. static void thread2_send_event(void *param)
  62. {
  63. rt_kprintf("thread2: send event3\n");
  64. rt_event_send(&event, EVENT_FLAG3);
  65. rt_thread_mdelay(200);
  66. rt_kprintf("thread2: send event5\n");
  67. rt_event_send(&event, EVENT_FLAG5);
  68. rt_thread_mdelay(200);
  69. rt_kprintf("thread2: send event3\n");
  70. rt_event_send(&event, EVENT_FLAG3);
  71. rt_kprintf("thread2 leave.\n");
  72. }
  73. int event_sample(void)
  74. {
  75. rt_err_t result;
  76. /* 初始化事件对象 */
  77. result = rt_event_init(&event, "event", RT_IPC_FLAG_PRIO);
  78. if (result != RT_EOK)
  79. {
  80. rt_kprintf("init event failed.\n");
  81. return -1;
  82. }
  83. rt_thread_init(&thread1,
  84. "thread1",
  85. thread1_recv_event,
  86. RT_NULL,
  87. &thread1_stack[0],
  88. sizeof(thread1_stack),
  89. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  90. #ifdef RT_USING_SMP
  91. /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */
  92. rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  93. #endif
  94. rt_thread_startup(&thread1);
  95. rt_thread_init(&thread2,
  96. "thread2",
  97. thread2_send_event,
  98. RT_NULL,
  99. &thread2_stack[0],
  100. sizeof(thread2_stack),
  101. THREAD_PRIORITY, THREAD_TIMESLICE);
  102. #ifdef RT_USING_SMP
  103. /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */
  104. rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  105. #endif
  106. rt_thread_startup(&thread2);
  107. return 0;
  108. }
  109. /* 导出到 msh 命令列表中 */
  110. MSH_CMD_EXPORT(event_sample, event sample);