thread.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * @warning see furi_thread_join
  67. *
  68. * @param thread FuriThread instance
  69. */
  70. void furi_thread_free(FuriThread* thread);
  71. /** Set FuriThread name
  72. *
  73. * @param thread FuriThread instance
  74. * @param name string
  75. */
  76. void furi_thread_set_name(FuriThread* thread, const char* name);
  77. /**
  78. * @brief Set FuriThread appid
  79. * Technically, it is like a "process id", but it is not a system-wide unique identifier.
  80. * All threads spawned by the same app will have the same appid.
  81. *
  82. * @param thread
  83. * @param appid
  84. */
  85. void furi_thread_set_appid(FuriThread* thread, const char* appid);
  86. /** Mark thread as service
  87. * The service cannot be stopped or removed, and cannot exit from the thread body
  88. *
  89. * @param thread
  90. */
  91. void furi_thread_mark_as_service(FuriThread* thread);
  92. /** Set FuriThread stack size
  93. *
  94. * @param thread FuriThread instance
  95. * @param stack_size stack size in bytes
  96. */
  97. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
  98. /** Set FuriThread callback
  99. *
  100. * @param thread FuriThread instance
  101. * @param callback FuriThreadCallback, called upon thread run
  102. */
  103. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
  104. /** Set FuriThread context
  105. *
  106. * @param thread FuriThread instance
  107. * @param context pointer to context for thread callback
  108. */
  109. void furi_thread_set_context(FuriThread* thread, void* context);
  110. /** Set FuriThread priority
  111. *
  112. * @param thread FuriThread instance
  113. * @param priority FuriThreadPriority value
  114. */
  115. void furi_thread_set_priority(FuriThread* thread, FuriThreadPriority priority);
  116. /** Set current thread priority
  117. *
  118. * @param priority FuriThreadPriority value
  119. */
  120. void furi_thread_set_current_priority(FuriThreadPriority priority);
  121. /** Get current thread priority
  122. *
  123. * @return FuriThreadPriority value
  124. */
  125. FuriThreadPriority furi_thread_get_current_priority();
  126. /** Set FuriThread state change callback
  127. *
  128. * @param thread FuriThread instance
  129. * @param callback state change callback
  130. */
  131. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
  132. /** Set FuriThread state change context
  133. *
  134. * @param thread FuriThread instance
  135. * @param context pointer to context
  136. */
  137. void furi_thread_set_state_context(FuriThread* thread, void* context);
  138. /** Get FuriThread state
  139. *
  140. * @param thread FuriThread instance
  141. *
  142. * @return thread state from FuriThreadState
  143. */
  144. FuriThreadState furi_thread_get_state(FuriThread* thread);
  145. /** Start FuriThread
  146. *
  147. * @param thread FuriThread instance
  148. */
  149. void furi_thread_start(FuriThread* thread);
  150. /** Join FuriThread
  151. *
  152. * @warning Use this method only when CPU is not busy(Idle task receives
  153. * control), otherwise it will wait forever.
  154. *
  155. * @param thread FuriThread instance
  156. *
  157. * @return bool
  158. */
  159. bool furi_thread_join(FuriThread* thread);
  160. /** Get FreeRTOS FuriThreadId for FuriThread instance
  161. *
  162. * @param thread FuriThread instance
  163. *
  164. * @return FuriThreadId or NULL
  165. */
  166. FuriThreadId furi_thread_get_id(FuriThread* thread);
  167. /** Enable heap tracing
  168. *
  169. * @param thread FuriThread instance
  170. */
  171. void furi_thread_enable_heap_trace(FuriThread* thread);
  172. /** Disable heap tracing
  173. *
  174. * @param thread FuriThread instance
  175. */
  176. void furi_thread_disable_heap_trace(FuriThread* thread);
  177. /** Get thread heap size
  178. *
  179. * @param thread FuriThread instance
  180. *
  181. * @return size in bytes
  182. */
  183. size_t furi_thread_get_heap_size(FuriThread* thread);
  184. /** Get thread return code
  185. *
  186. * @param thread FuriThread instance
  187. *
  188. * @return return code
  189. */
  190. int32_t furi_thread_get_return_code(FuriThread* thread);
  191. /** Thread related methods that doesn't involve FuriThread directly */
  192. /** Get FreeRTOS FuriThreadId for current thread
  193. *
  194. * @param thread FuriThread instance
  195. *
  196. * @return FuriThreadId or NULL
  197. */
  198. FuriThreadId furi_thread_get_current_id();
  199. /** Get FuriThread instance for current thread
  200. *
  201. * @return FuriThread*
  202. */
  203. FuriThread* furi_thread_get_current();
  204. /** Return control to scheduler */
  205. void furi_thread_yield();
  206. uint32_t furi_thread_flags_set(FuriThreadId thread_id, uint32_t flags);
  207. uint32_t furi_thread_flags_clear(uint32_t flags);
  208. uint32_t furi_thread_flags_get(void);
  209. uint32_t furi_thread_flags_wait(uint32_t flags, uint32_t options, uint32_t timeout);
  210. /**
  211. * @brief Enumerate threads
  212. *
  213. * @param thread_array array of FuriThreadId, where thread ids will be stored
  214. * @param array_items array size
  215. * @return uint32_t threads count
  216. */
  217. uint32_t furi_thread_enumerate(FuriThreadId* thread_array, uint32_t array_items);
  218. /**
  219. * @brief Get thread name
  220. *
  221. * @param thread_id
  222. * @return const char* name or NULL
  223. */
  224. const char* furi_thread_get_name(FuriThreadId thread_id);
  225. /**
  226. * @brief Get thread appid
  227. *
  228. * @param thread_id
  229. * @return const char* appid
  230. */
  231. const char* furi_thread_get_appid(FuriThreadId thread_id);
  232. /**
  233. * @brief Get thread stack watermark
  234. *
  235. * @param thread_id
  236. * @return uint32_t
  237. */
  238. uint32_t furi_thread_get_stack_space(FuriThreadId thread_id);
  239. /** Get STDOUT callback for thead
  240. *
  241. * @return STDOUT callback
  242. */
  243. FuriThreadStdoutWriteCallback furi_thread_get_stdout_callback();
  244. /** Set STDOUT callback for thread
  245. *
  246. * @param callback callback or NULL to clear
  247. *
  248. * @return true on success, otherwise fail
  249. */
  250. bool furi_thread_set_stdout_callback(FuriThreadStdoutWriteCallback callback);
  251. /** Write data to buffered STDOUT
  252. *
  253. * @param data input data
  254. * @param size input data size
  255. *
  256. * @return size_t written data size
  257. */
  258. size_t furi_thread_stdout_write(const char* data, size_t size);
  259. /** Flush data to STDOUT
  260. *
  261. * @return int32_t error code
  262. */
  263. int32_t furi_thread_stdout_flush();
  264. /** Suspend thread
  265. *
  266. * @param thread_id thread id
  267. */
  268. void furi_thread_suspend(FuriThreadId thread_id);
  269. /** Resume thread
  270. *
  271. * @param thread_id thread id
  272. */
  273. void furi_thread_resume(FuriThreadId thread_id);
  274. /** Get thread suspended state
  275. *
  276. * @param thread_id thread id
  277. * @return true if thread is suspended
  278. */
  279. bool furi_thread_is_suspended(FuriThreadId thread_id);
  280. #ifdef __cplusplus
  281. }
  282. #endif