thread.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 callback 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. /** Allocate FuriThread, shortcut version
  52. *
  53. * @param name
  54. * @param stack_size
  55. * @param callback
  56. * @param context
  57. * @return FuriThread*
  58. */
  59. FuriThread* furi_thread_alloc_ex(
  60. const char* name,
  61. uint32_t stack_size,
  62. FuriThreadCallback callback,
  63. void* context);
  64. /** Release FuriThread
  65. *
  66. * @param thread FuriThread instance
  67. */
  68. void furi_thread_free(FuriThread* thread);
  69. /** Set FuriThread name
  70. *
  71. * @param thread FuriThread instance
  72. * @param name string
  73. */
  74. void furi_thread_set_name(FuriThread* thread, const char* name);
  75. /** Mark thread as service
  76. * The service cannot be stopped or removed, and cannot exit from the thread body
  77. *
  78. * @param thread
  79. */
  80. void furi_thread_mark_as_service(FuriThread* thread);
  81. /** Set FuriThread stack size
  82. *
  83. * @param thread FuriThread instance
  84. * @param stack_size stack size in bytes
  85. */
  86. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
  87. /** Set FuriThread callback
  88. *
  89. * @param thread FuriThread instance
  90. * @param callback FuriThreadCallback, called upon thread run
  91. */
  92. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
  93. /** Set FuriThread context
  94. *
  95. * @param thread FuriThread instance
  96. * @param context pointer to context for thread callback
  97. */
  98. void furi_thread_set_context(FuriThread* thread, void* context);
  99. /** Set FuriThread priority
  100. *
  101. * @param thread FuriThread instance
  102. * @param priority FuriThreadPriority value
  103. */
  104. void furi_thread_set_priority(FuriThread* thread, FuriThreadPriority priority);
  105. /** Set current thread priority
  106. *
  107. * @param priority FuriThreadPriority value
  108. */
  109. void furi_thread_set_current_priority(FuriThreadPriority priority);
  110. /** Get current thread priority
  111. *
  112. * @return FuriThreadPriority value
  113. */
  114. FuriThreadPriority furi_thread_get_current_priority();
  115. /** Set FuriThread state change callback
  116. *
  117. * @param thread FuriThread instance
  118. * @param callback state change callback
  119. */
  120. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
  121. /** Set FuriThread state change context
  122. *
  123. * @param thread FuriThread instance
  124. * @param context pointer to context
  125. */
  126. void furi_thread_set_state_context(FuriThread* thread, void* context);
  127. /** Get FuriThread state
  128. *
  129. * @param thread FuriThread instance
  130. *
  131. * @return thread state from FuriThreadState
  132. */
  133. FuriThreadState furi_thread_get_state(FuriThread* thread);
  134. /** Start FuriThread
  135. *
  136. * @param thread FuriThread instance
  137. */
  138. void furi_thread_start(FuriThread* thread);
  139. /** Join FuriThread
  140. *
  141. * @param thread FuriThread instance
  142. *
  143. * @return bool
  144. */
  145. bool furi_thread_join(FuriThread* thread);
  146. /** Get FreeRTOS FuriThreadId for FuriThread instance
  147. *
  148. * @param thread FuriThread instance
  149. *
  150. * @return FuriThreadId or NULL
  151. */
  152. FuriThreadId furi_thread_get_id(FuriThread* thread);
  153. /** Enable heap tracing
  154. *
  155. * @param thread FuriThread instance
  156. */
  157. void furi_thread_enable_heap_trace(FuriThread* thread);
  158. /** Disable heap tracing
  159. *
  160. * @param thread FuriThread instance
  161. */
  162. void furi_thread_disable_heap_trace(FuriThread* thread);
  163. /** Get thread heap size
  164. *
  165. * @param thread FuriThread instance
  166. *
  167. * @return size in bytes
  168. */
  169. size_t furi_thread_get_heap_size(FuriThread* thread);
  170. /** Get thread return code
  171. *
  172. * @param thread FuriThread instance
  173. *
  174. * @return return code
  175. */
  176. int32_t furi_thread_get_return_code(FuriThread* thread);
  177. /** Thread related methods that doesn't involve FuriThread directly */
  178. /** Get FreeRTOS FuriThreadId for current thread
  179. *
  180. * @param thread FuriThread instance
  181. *
  182. * @return FuriThreadId or NULL
  183. */
  184. FuriThreadId furi_thread_get_current_id();
  185. /** Get FuriThread instance for current thread
  186. *
  187. * @return FuriThread*
  188. */
  189. FuriThread* furi_thread_get_current();
  190. /** Return control to scheduler */
  191. void furi_thread_yield();
  192. uint32_t furi_thread_flags_set(FuriThreadId thread_id, uint32_t flags);
  193. uint32_t furi_thread_flags_clear(uint32_t flags);
  194. uint32_t furi_thread_flags_get(void);
  195. uint32_t furi_thread_flags_wait(uint32_t flags, uint32_t options, uint32_t timeout);
  196. uint32_t furi_thread_enumerate(FuriThreadId* thread_array, uint32_t array_items);
  197. const char* furi_thread_get_name(FuriThreadId thread_id);
  198. uint32_t furi_thread_get_stack_space(FuriThreadId thread_id);
  199. /** Get STDOUT callback for thead
  200. *
  201. * @return STDOUT callback
  202. */
  203. FuriThreadStdoutWriteCallback furi_thread_get_stdout_callback();
  204. /** Set STDOUT callback for thread
  205. *
  206. * @param callback callback or NULL to clear
  207. *
  208. * @return true on success, otherwise fail
  209. */
  210. bool furi_thread_set_stdout_callback(FuriThreadStdoutWriteCallback callback);
  211. /** Write data to buffered STDOUT
  212. *
  213. * @param data input data
  214. * @param size input data size
  215. *
  216. * @return size_t written data size
  217. */
  218. size_t furi_thread_stdout_write(const char* data, size_t size);
  219. /** Flush data to STDOUT
  220. *
  221. * @return int32_t error code
  222. */
  223. int32_t furi_thread_stdout_flush();
  224. /** Suspend thread
  225. *
  226. * @param thread_id thread id
  227. */
  228. void furi_thread_suspend(FuriThreadId thread_id);
  229. /** Resume thread
  230. *
  231. * @param thread_id thread id
  232. */
  233. void furi_thread_resume(FuriThreadId thread_id);
  234. /** Get thread suspended state
  235. *
  236. * @param thread_id thread id
  237. * @return true if thread is suspended
  238. */
  239. bool furi_thread_is_suspended(FuriThreadId thread_id);
  240. #ifdef __cplusplus
  241. }
  242. #endif