thread.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /** FuriThread state change calback called upon thread state change
  37. * @param state new thread state
  38. * @param context callback context
  39. */
  40. typedef void (*FuriThreadStateCallback)(FuriThreadState state, void* context);
  41. /** Allocate FuriThread
  42. *
  43. * @return FuriThread instance
  44. */
  45. FuriThread* furi_thread_alloc();
  46. /** Release FuriThread
  47. *
  48. * @param thread FuriThread instance
  49. */
  50. void furi_thread_free(FuriThread* thread);
  51. /** Set FuriThread name
  52. *
  53. * @param thread FuriThread instance
  54. * @param name string
  55. */
  56. void furi_thread_set_name(FuriThread* thread, const char* name);
  57. /** Set FuriThread stack size
  58. *
  59. * @param thread FuriThread instance
  60. * @param stack_size stack size in bytes
  61. */
  62. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
  63. /** Set FuriThread callback
  64. *
  65. * @param thread FuriThread instance
  66. * @param callback FuriThreadCallback, called upon thread run
  67. */
  68. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
  69. /** Set FuriThread context
  70. *
  71. * @param thread FuriThread instance
  72. * @param context pointer to context for thread callback
  73. */
  74. void furi_thread_set_context(FuriThread* thread, void* context);
  75. /** Set FuriThread priority
  76. *
  77. * @param thread FuriThread instance
  78. * @param priority FuriThreadPriority value
  79. */
  80. void furi_thread_set_priority(FuriThread* thread, FuriThreadPriority priority);
  81. /** Set FuriThread state change callback
  82. *
  83. * @param thread FuriThread instance
  84. * @param callback state change callback
  85. */
  86. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
  87. /** Set FuriThread state change context
  88. *
  89. * @param thread FuriThread instance
  90. * @param context pointer to context
  91. */
  92. void furi_thread_set_state_context(FuriThread* thread, void* context);
  93. /** Get FuriThread state
  94. *
  95. * @param thread FuriThread instance
  96. *
  97. * @return thread state from FuriThreadState
  98. */
  99. FuriThreadState furi_thread_get_state(FuriThread* thread);
  100. /** Start FuriThread
  101. *
  102. * @param thread FuriThread instance
  103. */
  104. void furi_thread_start(FuriThread* thread);
  105. /** Join FuriThread
  106. *
  107. * @param thread FuriThread instance
  108. *
  109. * @return bool
  110. */
  111. bool furi_thread_join(FuriThread* thread);
  112. /** Get FreeRTOS FuriThreadId for FuriThread instance
  113. *
  114. * @param thread FuriThread instance
  115. *
  116. * @return FuriThreadId or NULL
  117. */
  118. FuriThreadId furi_thread_get_id(FuriThread* thread);
  119. /** Enable heap tracing
  120. *
  121. * @param thread FuriThread instance
  122. */
  123. void furi_thread_enable_heap_trace(FuriThread* thread);
  124. /** Disable heap tracing
  125. *
  126. * @param thread FuriThread instance
  127. */
  128. void furi_thread_disable_heap_trace(FuriThread* thread);
  129. /** Get thread heap size
  130. *
  131. * @param thread FuriThread instance
  132. *
  133. * @return size in bytes
  134. */
  135. size_t furi_thread_get_heap_size(FuriThread* thread);
  136. /** Get thread return code
  137. *
  138. * @param thread FuriThread instance
  139. *
  140. * @return return code
  141. */
  142. int32_t furi_thread_get_return_code(FuriThread* thread);
  143. /** Thread releated methods that doesn't involve FuriThread directly */
  144. /** Get FreeRTOS FuriThreadId for current thread
  145. *
  146. * @param thread FuriThread instance
  147. *
  148. * @return FuriThreadId or NULL
  149. */
  150. FuriThreadId furi_thread_get_current_id();
  151. /** Return control to scheduler */
  152. void furi_thread_yield();
  153. uint32_t furi_thread_flags_set(FuriThreadId thread_id, uint32_t flags);
  154. uint32_t furi_thread_flags_clear(uint32_t flags);
  155. uint32_t furi_thread_flags_get(void);
  156. uint32_t furi_thread_flags_wait(uint32_t flags, uint32_t options, uint32_t timeout);
  157. uint32_t furi_thread_enumerate(FuriThreadId* thread_array, uint32_t array_items);
  158. const char* furi_thread_get_name(FuriThreadId thread_id);
  159. uint32_t furi_thread_get_stack_space(FuriThreadId thread_id);
  160. #ifdef __cplusplus
  161. }
  162. #endif