thread.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. thread->heap_size = memmgr_heap_get_thread_memory(thread_id);
  37. memmgr_heap_disable_thread_trace(thread_id);
  38. }
  39. furi_assert(thread->state == FuriThreadStateRunning);
  40. furi_thread_set_state(thread, FuriThreadStateStopped);
  41. osThreadExit();
  42. }
  43. FuriThread* furi_thread_alloc() {
  44. FuriThread* thread = malloc(sizeof(FuriThread));
  45. return thread;
  46. }
  47. void furi_thread_free(FuriThread* thread) {
  48. furi_assert(thread);
  49. furi_assert(thread->state == FuriThreadStateStopped);
  50. if(thread->attr.name) free((void*)thread->attr.name);
  51. free(thread);
  52. }
  53. void furi_thread_set_name(FuriThread* thread, const char* name) {
  54. furi_assert(thread);
  55. furi_assert(thread->state == FuriThreadStateStopped);
  56. if(thread->attr.name) free((void*)thread->attr.name);
  57. thread->attr.name = strdup(name);
  58. }
  59. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size) {
  60. furi_assert(thread);
  61. furi_assert(thread->state == FuriThreadStateStopped);
  62. thread->attr.stack_size = stack_size;
  63. }
  64. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback) {
  65. furi_assert(thread);
  66. furi_assert(thread->state == FuriThreadStateStopped);
  67. thread->callback = callback;
  68. }
  69. void furi_thread_set_context(FuriThread* thread, void* context) {
  70. furi_assert(thread);
  71. furi_assert(thread->state == FuriThreadStateStopped);
  72. thread->context = context;
  73. }
  74. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback) {
  75. furi_assert(thread);
  76. furi_assert(thread->state == FuriThreadStateStopped);
  77. thread->state_callback = callback;
  78. }
  79. void furi_thread_set_state_context(FuriThread* thread, void* context) {
  80. furi_assert(thread);
  81. furi_assert(thread->state == FuriThreadStateStopped);
  82. thread->state_context = context;
  83. }
  84. FuriThreadState furi_thread_get_state(FuriThread* thread) {
  85. furi_assert(thread);
  86. return thread->state;
  87. }
  88. bool furi_thread_start(FuriThread* thread) {
  89. furi_assert(thread);
  90. furi_assert(thread->callback);
  91. furi_assert(thread->state == FuriThreadStateStopped);
  92. furi_assert(thread->attr.stack_size > 0);
  93. furi_thread_set_state(thread, FuriThreadStateStarting);
  94. thread->id = osThreadNew(furi_thread_body, thread, &thread->attr);
  95. if(thread->id) {
  96. return true;
  97. } else {
  98. furi_assert(thread->state == FuriThreadStateStarting);
  99. furi_thread_set_state(thread, FuriThreadStateStopped);
  100. return false;
  101. }
  102. }
  103. osStatus_t furi_thread_terminate(FuriThread* thread) {
  104. furi_assert(thread);
  105. osStatus_t ret = osThreadTerminate(thread->id);
  106. if(ret == osOK) {
  107. furi_thread_set_state(thread, FuriThreadStateStopped);
  108. }
  109. return ret;
  110. }
  111. osStatus_t furi_thread_join(FuriThread* thread) {
  112. furi_assert(thread);
  113. while(thread->state != FuriThreadStateStopped) {
  114. osDelay(10);
  115. }
  116. return osOK;
  117. }
  118. osThreadId_t furi_thread_get_thread_id(FuriThread* thread) {
  119. return thread->id;
  120. }
  121. void furi_thread_enable_heap_trace(FuriThread* thread) {
  122. furi_assert(thread);
  123. furi_assert(thread->state == FuriThreadStateStopped);
  124. furi_assert(thread->heap_trace_enabled == false);
  125. thread->heap_trace_enabled = true;
  126. }
  127. void furi_thread_disable_heap_trace(FuriThread* thread) {
  128. furi_assert(thread);
  129. furi_assert(thread->state == FuriThreadStateStopped);
  130. furi_assert(thread->heap_trace_enabled == true);
  131. thread->heap_trace_enabled = false;
  132. }
  133. size_t furi_thread_get_heap_size(FuriThread* thread) {
  134. furi_assert(thread);
  135. furi_assert(thread->heap_trace_enabled == true);
  136. return thread->heap_size;
  137. }
  138. int32_t furi_thread_get_return_code(FuriThread* thread) {
  139. furi_assert(thread);
  140. furi_assert(thread->state == FuriThreadStateStopped);
  141. return thread->ret;
  142. }