thread.h 6.0 KB

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