thread.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <cmsis_os2.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /** FuriThreadState */
  9. typedef enum {
  10. FuriThreadStateStopped,
  11. FuriThreadStateStarting,
  12. FuriThreadStateRunning,
  13. } FuriThreadState;
  14. /** FuriThread anonymous structure */
  15. typedef struct FuriThread FuriThread;
  16. /**
  17. * FuriThreadCallback
  18. * Your callback to run in new thread
  19. * @warning don't use osThreadExit
  20. */
  21. typedef int32_t (*FuriThreadCallback)(void* context);
  22. /**
  23. * FuriThread state change calback
  24. * called upon thread state change
  25. * @param state - new thread state
  26. * @param context - callback context
  27. */
  28. typedef void (*FuriThreadStateCallback)(FuriThreadState state, void* context);
  29. /**
  30. * Allocate FuriThread
  31. * @return FuriThread instance
  32. */
  33. FuriThread* furi_thread_alloc();
  34. /**
  35. * Release FuriThread
  36. * @param thread - FuriThread instance
  37. */
  38. void furi_thread_free(FuriThread* thread);
  39. /**
  40. * Set FuriThread name
  41. * @param thread - FuriThread instance
  42. * @param name - string
  43. */
  44. void furi_thread_set_name(FuriThread* thread, const char* name);
  45. /**
  46. * Set FuriThread stack size
  47. * @param thread - FuriThread instance
  48. * @param stack_size - stack size in bytes
  49. */
  50. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
  51. /**
  52. * Set FuriThread callback
  53. * @param thread - FuriThread instance
  54. * @param callback - FuriThreadCallback, called upon thread run
  55. */
  56. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
  57. /**
  58. * Set FuriThread context
  59. * @param thread - FuriThread instance
  60. * @param context - pointer to context for thread callback
  61. */
  62. void furi_thread_set_context(FuriThread* thread, void* context);
  63. /**
  64. * Set FuriThread state change callback
  65. * @param thread - FuriThread instance
  66. * @param callack - state change callback
  67. */
  68. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
  69. /**
  70. * Set FuriThread state change context
  71. * @param thread - FuriThread instance
  72. * @param context - pointer to context
  73. */
  74. void furi_thread_set_state_context(FuriThread* thread, void* context);
  75. /**
  76. * Get FuriThread state
  77. * @param thread - FuriThread instance
  78. * @return thread state from FuriThreadState
  79. */
  80. FuriThreadState furi_thread_get_state(FuriThread* thread);
  81. /**
  82. * Start FuriThread
  83. * @param thread - FuriThread instance
  84. * @return true on success
  85. */
  86. bool furi_thread_start(FuriThread* thread);
  87. /**
  88. * Treminate FuriThread
  89. * @param thread - FuriThread instance
  90. * @return osStatus_t
  91. * @warning terminating statefull thread is dangerous
  92. * use only if you know what you doing
  93. */
  94. osStatus_t furi_thread_terminate(FuriThread* thread);
  95. /**
  96. * Join FuriThread
  97. * @param thread - FuriThread instance
  98. * @return osStatus_t
  99. */
  100. osStatus_t furi_thread_join(FuriThread* thread);
  101. #ifdef __cplusplus
  102. }
  103. #endif