mutex_sample.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * 2020-10-17 Meco Man translate to English comment
  10. */
  11. /*
  12. * Demo: mutex(es)
  13. *
  14. * This demo demonstrates how the mutex manage the shared resource.
  15. *
  16. * read more:
  17. * https://www.rt-thread.io/document/site/programming-manual/ipc1/ipc1/#mutex
  18. */
  19. #include <rtthread.h>
  20. #define THREAD_PRIORITY 8
  21. #define THREAD_TIMESLICE 5
  22. /* mutex handler */
  23. static rt_mutex_t dynamic_mutex = RT_NULL;
  24. static rt_uint8_t number1, number2 = 0;
  25. #ifdef rt_align
  26. rt_align(RT_ALIGN_SIZE)
  27. #else
  28. ALIGN(RT_ALIGN_SIZE)
  29. #endif
  30. static char thread1_stack[1024];
  31. static struct rt_thread thread1;
  32. static void rt_thread_entry1(void *parameter)
  33. {
  34. while (1)
  35. {
  36. /* pending the mutex */
  37. rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
  38. /* protect and deal with public variables */
  39. number1++;
  40. rt_thread_mdelay(10);
  41. number2++;
  42. /* release the mutex */
  43. rt_mutex_release(dynamic_mutex);
  44. }
  45. }
  46. #ifdef rt_align
  47. rt_align(RT_ALIGN_SIZE)
  48. #else
  49. ALIGN(RT_ALIGN_SIZE)
  50. #endif
  51. static char thread2_stack[1024];
  52. static struct rt_thread thread2;
  53. static void rt_thread_entry2(void *parameter)
  54. {
  55. while (1)
  56. {
  57. rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
  58. if (number1 != number2)
  59. {
  60. rt_kprintf("not protect.number1 = %d, mumber2 = %d \n", number1, number2);
  61. }
  62. else
  63. {
  64. rt_kprintf("mutex protect ,number1 = mumber2 is %d\n", number1);
  65. }
  66. number1++;
  67. number2++;
  68. rt_mutex_release(dynamic_mutex);
  69. if (number1 >= 50)
  70. return;
  71. }
  72. }
  73. int mutex_sample(void)
  74. {
  75. /* create mutex */
  76. dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_PRIO);
  77. if (dynamic_mutex == RT_NULL)
  78. {
  79. rt_kprintf("create dynamic mutex failed.\n");
  80. return -1;
  81. }
  82. rt_thread_init(&thread1,
  83. "thread1",
  84. rt_thread_entry1,
  85. RT_NULL,
  86. &thread1_stack[0],
  87. sizeof(thread1_stack),
  88. THREAD_PRIORITY, THREAD_TIMESLICE);
  89. #ifdef RT_USING_SMP
  90. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  91. rt_thread_control(&thread1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  92. #endif
  93. rt_thread_startup(&thread1);
  94. rt_thread_init(&thread2,
  95. "thread2",
  96. rt_thread_entry2,
  97. RT_NULL,
  98. &thread2_stack[0],
  99. sizeof(thread2_stack),
  100. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  101. #ifdef RT_USING_SMP
  102. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  103. rt_thread_control(&thread2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  104. #endif
  105. rt_thread_startup(&thread2);
  106. return 0;
  107. }
  108. /* export the msh command */
  109. MSH_CMD_EXPORT(mutex_sample, mutex sample);