thread.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "thread.h"
  2. #include "memmgr.h"
  3. #include "memmgr_heap.h"
  4. #include "check.h"
  5. #include <m-string.h>
  6. struct FuriThread {
  7. FuriThreadState state;
  8. int32_t ret;
  9. FuriThreadCallback callback;
  10. void* context;
  11. FuriThreadStateCallback state_callback;
  12. void* state_context;
  13. osThreadAttr_t attr;
  14. volatile osThreadId_t id;
  15. bool heap_trace_enabled;
  16. size_t heap_size;
  17. };
  18. void furi_thread_set_state(FuriThread* thread, FuriThreadState state) {
  19. furi_assert(thread);
  20. thread->state = state;
  21. if(thread->state_callback) {
  22. thread->state_callback(state, thread->state_context);
  23. }
  24. }
  25. static void furi_thread_body(void* context) {
  26. furi_assert(context);
  27. FuriThread* thread = context;
  28. furi_assert(thread->state == FuriThreadStateStarting);
  29. furi_thread_set_state(thread, FuriThreadStateRunning);
  30. osThreadId_t thread_id = osThreadGetId();
  31. if(thread->heap_trace_enabled == true) {
  32. memmgr_heap_enable_thread_trace(thread_id);
  33. }
  34. thread->ret = thread->callback(thread->context);
  35. if(thread->heap_trace_enabled == true) {
  36. osDelay(33);
  37. thread->heap_size = memmgr_heap_get_thread_memory(thread_id);
  38. memmgr_heap_disable_thread_trace(thread_id);
  39. }
  40. furi_assert(thread->state == FuriThreadStateRunning);
  41. furi_thread_set_state(thread, FuriThreadStateStopped);
  42. osThreadExit();
  43. }
  44. FuriThread* furi_thread_alloc() {
  45. FuriThread* thread = malloc(sizeof(FuriThread));
  46. return thread;
  47. }
  48. void furi_thread_free(FuriThread* thread) {
  49. furi_assert(thread);
  50. furi_assert(thread->state == FuriThreadStateStopped);
  51. if(thread->attr.name) free((void*)thread->attr.name);
  52. free(thread);
  53. }
  54. void furi_thread_set_name(FuriThread* thread, const char* name) {
  55. furi_assert(thread);
  56. furi_assert(thread->state == FuriThreadStateStopped);
  57. if(thread->attr.name) free((void*)thread->attr.name);
  58. thread->attr.name = strdup(name);
  59. }
  60. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size) {
  61. furi_assert(thread);
  62. furi_assert(thread->state == FuriThreadStateStopped);
  63. thread->attr.stack_size = stack_size;
  64. }
  65. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback) {
  66. furi_assert(thread);
  67. furi_assert(thread->state == FuriThreadStateStopped);
  68. thread->callback = callback;
  69. }
  70. void furi_thread_set_context(FuriThread* thread, void* context) {
  71. furi_assert(thread);
  72. furi_assert(thread->state == FuriThreadStateStopped);
  73. thread->context = context;
  74. }
  75. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback) {
  76. furi_assert(thread);
  77. furi_assert(thread->state == FuriThreadStateStopped);
  78. thread->state_callback = callback;
  79. }
  80. void furi_thread_set_state_context(FuriThread* thread, void* context) {
  81. furi_assert(thread);
  82. furi_assert(thread->state == FuriThreadStateStopped);
  83. thread->state_context = context;
  84. }
  85. FuriThreadState furi_thread_get_state(FuriThread* thread) {
  86. furi_assert(thread);
  87. return thread->state;
  88. }
  89. bool furi_thread_start(FuriThread* thread) {
  90. furi_assert(thread);
  91. furi_assert(thread->callback);
  92. furi_assert(thread->state == FuriThreadStateStopped);
  93. furi_assert(thread->attr.stack_size > 0);
  94. furi_thread_set_state(thread, FuriThreadStateStarting);
  95. thread->id = osThreadNew(furi_thread_body, thread, &thread->attr);
  96. if(thread->id) {
  97. return true;
  98. } else {
  99. furi_assert(thread->state == FuriThreadStateStarting);
  100. furi_thread_set_state(thread, FuriThreadStateStopped);
  101. return false;
  102. }
  103. }
  104. osStatus_t furi_thread_terminate(FuriThread* thread) {
  105. furi_assert(thread);
  106. osStatus_t ret = osThreadTerminate(thread->id);
  107. if(ret == osOK) {
  108. furi_thread_set_state(thread, FuriThreadStateStopped);
  109. }
  110. return ret;
  111. }
  112. osStatus_t furi_thread_join(FuriThread* thread) {
  113. furi_assert(thread);
  114. while(thread->state != FuriThreadStateStopped) {
  115. osDelay(10);
  116. }
  117. return osOK;
  118. }
  119. osThreadId_t furi_thread_get_thread_id(FuriThread* thread) {
  120. return thread->id;
  121. }
  122. void furi_thread_enable_heap_trace(FuriThread* thread) {
  123. furi_assert(thread);
  124. furi_assert(thread->state == FuriThreadStateStopped);
  125. furi_assert(thread->heap_trace_enabled == false);
  126. thread->heap_trace_enabled = true;
  127. }
  128. void furi_thread_disable_heap_trace(FuriThread* thread) {
  129. furi_assert(thread);
  130. furi_assert(thread->state == FuriThreadStateStopped);
  131. furi_assert(thread->heap_trace_enabled == true);
  132. thread->heap_trace_enabled = false;
  133. }
  134. size_t furi_thread_get_heap_size(FuriThread* thread) {
  135. furi_assert(thread);
  136. furi_assert(thread->heap_trace_enabled == true);
  137. return thread->heap_size;
  138. }
  139. int32_t furi_thread_get_return_code(FuriThread* thread) {
  140. furi_assert(thread);
  141. furi_assert(thread->state == FuriThreadStateStopped);
  142. return thread->ret;
  143. }