thread.c 4.5 KB

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