memp_sample.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: memory pool
  13. *
  14. * This demo creates one memory pool and two threads:
  15. * 1) thread #1: allocate memory pool
  16. * 2) thread #2: free memory pool
  17. *
  18. * read more:
  19. * https://www.rt-thread.io/document/site/programming-manual/memory/memory/#memory-pool
  20. */
  21. #include <rtthread.h>
  22. static rt_uint8_t *ptr[50];
  23. static rt_uint8_t mempool[4096];
  24. static struct rt_mempool mp;
  25. #define THREAD_PRIORITY 25
  26. #define THREAD_STACK_SIZE 512
  27. #define THREAD_TIMESLICE 5
  28. /* thread handler */
  29. static rt_thread_t tid1 = RT_NULL;
  30. static rt_thread_t tid2 = RT_NULL;
  31. /* thread #1 entry function */
  32. static void thread1_mp_alloc(void *parameter)
  33. {
  34. int i;
  35. for (i = 0 ; i < 50 ; i++)
  36. {
  37. if (ptr[i] == RT_NULL)
  38. {
  39. /* allocate memory from the memory pool */
  40. ptr[i] = rt_mp_alloc(&mp, RT_WAITING_FOREVER);
  41. if (ptr[i] != RT_NULL)
  42. rt_kprintf("allocate No.%d\n", i);
  43. }
  44. }
  45. }
  46. /* thread #2 entry function */
  47. static void thread2_mp_release(void *parameter)
  48. {
  49. int i;
  50. rt_kprintf("thread2 try to release block\n");
  51. for (i = 0; i < 50 ; i++)
  52. {
  53. /* free all memory */
  54. if (ptr[i] != RT_NULL)
  55. {
  56. rt_kprintf("release block %d\n", i);
  57. rt_mp_free(ptr[i]);
  58. ptr[i] = RT_NULL;
  59. }
  60. }
  61. }
  62. int mempool_sample(void)
  63. {
  64. int i;
  65. for (i = 0; i < 50; i ++) ptr[i] = RT_NULL;
  66. /* initiate memory pool */
  67. rt_mp_init(&mp, "mp1", &mempool[0], sizeof(mempool), 80);
  68. /* create thread #1 */
  69. tid1 = rt_thread_create("thread1", thread1_mp_alloc, RT_NULL,
  70. THREAD_STACK_SIZE,
  71. THREAD_PRIORITY, THREAD_TIMESLICE);
  72. #ifdef RT_USING_SMP
  73. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  74. rt_thread_control(tid1, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  75. #endif
  76. if (tid1 != RT_NULL)
  77. rt_thread_startup(tid1); /* start thread #1 */
  78. /* create thread #2 */
  79. tid2 = rt_thread_create("thread2", thread2_mp_release, RT_NULL,
  80. THREAD_STACK_SIZE,
  81. THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  82. #ifdef RT_USING_SMP
  83. /* Bind threads to the same core to avoid messy log output when multiple cores are enabled */
  84. rt_thread_control(tid2, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  85. #endif
  86. if (tid2 != RT_NULL)
  87. rt_thread_startup(tid2); /* start thread #2 */
  88. return 0;
  89. }
  90. /* export the msh command */
  91. MSH_CMD_EXPORT(mempool_sample, mempool sample);