thread.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @file thread.h
  3. * Furi: Furi Thread API
  4. */
  5. #pragma once
  6. #include "base.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /** FuriThreadState */
  11. typedef enum {
  12. FuriThreadStateStopped,
  13. FuriThreadStateStarting,
  14. FuriThreadStateRunning,
  15. } FuriThreadState;
  16. /** FuriThreadPriority */
  17. typedef enum {
  18. FuriThreadPriorityNone = 0, /**< Uninitialized, choose system default */
  19. FuriThreadPriorityIdle = 1, /**< Idle priority */
  20. FuriThreadPriorityLowest = 14, /**< Lowest */
  21. FuriThreadPriorityLow = 15, /**< Low */
  22. FuriThreadPriorityNormal = 16, /**< Normal */
  23. FuriThreadPriorityHigh = 17, /**< High */
  24. FuriThreadPriorityHighest = 18, /**< Highest */
  25. FuriThreadPriorityIsr = 32, /**< Deffered Isr (highest possible) */
  26. } FuriThreadPriority;
  27. /** FuriThread anonymous structure */
  28. typedef struct FuriThread FuriThread;
  29. /** FuriThreadId proxy type to OS low level functions */
  30. typedef void* FuriThreadId;
  31. /** FuriThreadCallback Your callback to run in new thread
  32. * @warning never use osThreadExit in FuriThread
  33. */
  34. typedef int32_t (*FuriThreadCallback)(void* context);
  35. /** FuriThread state change calback called upon thread state change
  36. * @param state new thread state
  37. * @param context callback context
  38. */
  39. typedef void (*FuriThreadStateCallback)(FuriThreadState state, void* context);
  40. /** Allocate FuriThread
  41. *
  42. * @return FuriThread instance
  43. */
  44. FuriThread* furi_thread_alloc();
  45. /** Release FuriThread
  46. *
  47. * @param thread FuriThread instance
  48. */
  49. void furi_thread_free(FuriThread* thread);
  50. /** Set FuriThread name
  51. *
  52. * @param thread FuriThread instance
  53. * @param name string
  54. */
  55. void furi_thread_set_name(FuriThread* thread, const char* name);
  56. /** Set FuriThread stack size
  57. *
  58. * @param thread FuriThread instance
  59. * @param stack_size stack size in bytes
  60. */
  61. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
  62. /** Set FuriThread callback
  63. *
  64. * @param thread FuriThread instance
  65. * @param callback FuriThreadCallback, called upon thread run
  66. */
  67. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
  68. /** Set FuriThread context
  69. *
  70. * @param thread FuriThread instance
  71. * @param context pointer to context for thread callback
  72. */
  73. void furi_thread_set_context(FuriThread* thread, void* context);
  74. /** Set FuriThread priority
  75. *
  76. * @param thread FuriThread instance
  77. * @param priority FuriThreadPriority value
  78. */
  79. void furi_thread_set_priority(FuriThread* thread, FuriThreadPriority priority);
  80. /** Set FuriThread state change callback
  81. *
  82. * @param thread FuriThread instance
  83. * @param callback state change callback
  84. */
  85. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
  86. /** Set FuriThread state change context
  87. *
  88. * @param thread FuriThread instance
  89. * @param context pointer to context
  90. */
  91. void furi_thread_set_state_context(FuriThread* thread, void* context);
  92. /** Get FuriThread state
  93. *
  94. * @param thread FuriThread instance
  95. *
  96. * @return thread state from FuriThreadState
  97. */
  98. FuriThreadState furi_thread_get_state(FuriThread* thread);
  99. /** Start FuriThread
  100. *
  101. * @param thread FuriThread instance
  102. */
  103. void furi_thread_start(FuriThread* thread);
  104. /** Join FuriThread
  105. *
  106. * @param thread FuriThread instance
  107. *
  108. * @return bool
  109. */
  110. bool furi_thread_join(FuriThread* thread);
  111. /** Get FreeRTOS FuriThreadId for FuriThread instance
  112. *
  113. * @param thread FuriThread instance
  114. *
  115. * @return FuriThreadId or NULL
  116. */
  117. FuriThreadId furi_thread_get_id(FuriThread* thread);
  118. /** Enable heap tracing
  119. *
  120. * @param thread FuriThread instance
  121. */
  122. void furi_thread_enable_heap_trace(FuriThread* thread);
  123. /** Disable heap tracing
  124. *
  125. * @param thread FuriThread instance
  126. */
  127. void furi_thread_disable_heap_trace(FuriThread* thread);
  128. /** Get thread heap size
  129. *
  130. * @param thread FuriThread instance
  131. *
  132. * @return size in bytes
  133. */
  134. size_t furi_thread_get_heap_size(FuriThread* thread);
  135. /** Get thread return code
  136. *
  137. * @param thread FuriThread instance
  138. *
  139. * @return return code
  140. */
  141. int32_t furi_thread_get_return_code(FuriThread* thread);
  142. /** Thread releated methods that doesn't involve FuriThread directly */
  143. /** Get FreeRTOS FuriThreadId for current thread
  144. *
  145. * @param thread FuriThread instance
  146. *
  147. * @return FuriThreadId or NULL
  148. */
  149. FuriThreadId furi_thread_get_current_id();
  150. /** Return control to scheduler */
  151. void furi_thread_yield();
  152. uint32_t furi_thread_flags_set(FuriThreadId thread_id, uint32_t flags);
  153. uint32_t furi_thread_flags_clear(uint32_t flags);
  154. uint32_t furi_thread_flags_get(void);
  155. uint32_t furi_thread_flags_wait(uint32_t flags, uint32_t options, uint32_t timeout);
  156. uint32_t furi_thread_enumerate(FuriThreadId* thread_array, uint32_t array_items);
  157. const char* furi_thread_get_name(FuriThreadId thread_id);
  158. uint32_t furi_thread_get_stack_space(FuriThreadId thread_id);
  159. #ifdef __cplusplus
  160. }
  161. #endif