thread.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @file thread.h
  3. * Furi: Furi Thread API
  4. */
  5. #pragma once
  6. #include <stdint.h>
  7. #include <stdbool.h>
  8. #include <cmsis_os2.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /** FuriThreadState */
  13. typedef enum {
  14. FuriThreadStateStopped,
  15. FuriThreadStateStarting,
  16. FuriThreadStateRunning,
  17. } FuriThreadState;
  18. /** FuriThread anonymous structure */
  19. typedef struct FuriThread FuriThread;
  20. /** FuriThreadCallback Your callback to run in new thread
  21. * @warning never use osThreadExit in FuriThread
  22. */
  23. typedef int32_t (*FuriThreadCallback)(void* context);
  24. /** FuriThread state change calback called upon thread state change
  25. * @param state new thread state
  26. * @param context callback context
  27. */
  28. typedef void (*FuriThreadStateCallback)(FuriThreadState state, void* context);
  29. /** Allocate FuriThread
  30. *
  31. * @return FuriThread instance
  32. */
  33. FuriThread* furi_thread_alloc();
  34. /** Release FuriThread
  35. *
  36. * @param thread FuriThread instance
  37. */
  38. void furi_thread_free(FuriThread* thread);
  39. /** Set FuriThread name
  40. *
  41. * @param thread FuriThread instance
  42. * @param name string
  43. */
  44. void furi_thread_set_name(FuriThread* thread, const char* name);
  45. /** Set FuriThread stack size
  46. *
  47. * @param thread FuriThread instance
  48. * @param stack_size stack size in bytes
  49. */
  50. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
  51. /** Set FuriThread callback
  52. *
  53. * @param thread FuriThread instance
  54. * @param callback FuriThreadCallback, called upon thread run
  55. */
  56. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
  57. /** Set FuriThread context
  58. *
  59. * @param thread FuriThread instance
  60. * @param context pointer to context for thread callback
  61. */
  62. void furi_thread_set_context(FuriThread* thread, void* context);
  63. /** Set FuriThread state change callback
  64. *
  65. * @param thread FuriThread instance
  66. * @param callback state change callback
  67. */
  68. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
  69. /** Set FuriThread state change context
  70. *
  71. * @param thread FuriThread instance
  72. * @param context pointer to context
  73. */
  74. void furi_thread_set_state_context(FuriThread* thread, void* context);
  75. /** Get FuriThread state
  76. *
  77. * @param thread FuriThread instance
  78. *
  79. * @return thread state from FuriThreadState
  80. */
  81. FuriThreadState furi_thread_get_state(FuriThread* thread);
  82. /** Start FuriThread
  83. *
  84. * @param thread FuriThread instance
  85. *
  86. * @return true on success
  87. */
  88. bool furi_thread_start(FuriThread* thread);
  89. /** Treminate FuriThread
  90. *
  91. * @param thread FuriThread instance
  92. *
  93. * @return osStatus_t
  94. * @warning terminating statefull thread is dangerous use only if you know
  95. * what you doing
  96. */
  97. osStatus_t furi_thread_terminate(FuriThread* thread);
  98. /** Join FuriThread
  99. *
  100. * @param thread FuriThread instance
  101. *
  102. * @return osStatus_t
  103. */
  104. osStatus_t furi_thread_join(FuriThread* thread);
  105. /** Get CMSIS Thread ID
  106. *
  107. * @param thread FuriThread instance
  108. *
  109. * @return osThreadId_t or NULL
  110. */
  111. osThreadId_t furi_thread_get_thread_id(FuriThread* thread);
  112. /** Enable heap tracing
  113. *
  114. * @param thread FuriThread instance
  115. */
  116. void furi_thread_enable_heap_trace(FuriThread* thread);
  117. /** Disable heap tracing
  118. *
  119. * @param thread FuriThread instance
  120. */
  121. void furi_thread_disable_heap_trace(FuriThread* thread);
  122. /** Get thread heap size
  123. *
  124. * @param thread FuriThread instance
  125. *
  126. * @return size in bytes
  127. */
  128. size_t furi_thread_get_heap_size(FuriThread* thread);
  129. #ifdef __cplusplus
  130. }
  131. #endif