thread.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "thread.h"
  2. #include "memmgr.h"
  3. #include "check.h"
  4. #include <m-string.h>
  5. struct FuriThread {
  6. FuriThreadState state;
  7. int32_t ret;
  8. FuriThreadCallback callback;
  9. void* context;
  10. FuriThreadStateCallback state_callback;
  11. void* state_context;
  12. osThreadAttr_t attr;
  13. osThreadId_t id;
  14. };
  15. void furi_thread_set_state(FuriThread* thread, FuriThreadState state) {
  16. furi_assert(thread);
  17. thread->state = state;
  18. if(thread->state_callback) {
  19. thread->state_callback(state, thread->state_context);
  20. }
  21. }
  22. void furi_thread_body(void* context) {
  23. furi_assert(context);
  24. FuriThread* thread = context;
  25. furi_assert(thread->state == FuriThreadStateStarting);
  26. furi_thread_set_state(thread, FuriThreadStateRunning);
  27. thread->ret = thread->callback(thread->context);
  28. furi_assert(thread->state == FuriThreadStateRunning);
  29. furi_thread_set_state(thread, FuriThreadStateStopped);
  30. osThreadExit();
  31. }
  32. FuriThread* furi_thread_alloc() {
  33. FuriThread* thread = furi_alloc(sizeof(FuriThread));
  34. return thread;
  35. }
  36. void furi_thread_free(FuriThread* thread) {
  37. furi_assert(thread);
  38. furi_assert(thread->state == FuriThreadStateStopped);
  39. if(thread->attr.name) free((void*)thread->attr.name);
  40. free(thread);
  41. }
  42. void furi_thread_set_name(FuriThread* thread, const char* name) {
  43. furi_assert(thread);
  44. furi_assert(thread->state == FuriThreadStateStopped);
  45. if(thread->attr.name) free((void*)thread->attr.name);
  46. thread->attr.name = strdup(name);
  47. }
  48. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size) {
  49. furi_assert(thread);
  50. furi_assert(thread->state == FuriThreadStateStopped);
  51. thread->attr.stack_size = stack_size;
  52. }
  53. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback) {
  54. furi_assert(thread);
  55. furi_assert(thread->state == FuriThreadStateStopped);
  56. thread->callback = callback;
  57. }
  58. void furi_thread_set_context(FuriThread* thread, void* context) {
  59. furi_assert(thread);
  60. furi_assert(thread->state == FuriThreadStateStopped);
  61. thread->context = context;
  62. }
  63. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback) {
  64. furi_assert(thread);
  65. furi_assert(thread->state == FuriThreadStateStopped);
  66. thread->state_callback = callback;
  67. }
  68. void furi_thread_set_state_context(FuriThread* thread, void* context) {
  69. furi_assert(thread);
  70. furi_assert(thread->state == FuriThreadStateStopped);
  71. thread->state_context = context;
  72. }
  73. FuriThreadState furi_thread_get_state(FuriThread* thread) {
  74. furi_assert(thread);
  75. return thread->state;
  76. }
  77. bool furi_thread_start(FuriThread* thread) {
  78. furi_assert(thread);
  79. furi_assert(thread->callback);
  80. furi_assert(thread->state == FuriThreadStateStopped);
  81. furi_thread_set_state(thread, FuriThreadStateStarting);
  82. thread->id = osThreadNew(furi_thread_body, thread, &thread->attr);
  83. if(thread->id) {
  84. return true;
  85. } else {
  86. furi_assert(thread->state == FuriThreadStateStarting);
  87. furi_thread_set_state(thread, FuriThreadStateStopped);
  88. return false;
  89. }
  90. }
  91. osStatus_t furi_thread_terminate(FuriThread* thread) {
  92. furi_assert(thread);
  93. osStatus_t ret = osThreadTerminate(thread->id);
  94. if(ret == osOK) {
  95. furi_thread_set_state(thread, FuriThreadStateStopped);
  96. }
  97. return ret;
  98. }
  99. osStatus_t furi_thread_join(FuriThread* thread) {
  100. furi_assert(thread);
  101. return osThreadJoin(thread->id);
  102. }