thread.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include "thread.h"
  2. #include "memmgr.h"
  3. #include "memmgr_heap.h"
  4. #include "check.h"
  5. #include "common_defines.h"
  6. #include <task.h>
  7. #include <m-string.h>
  8. #define THREAD_NOTIFY_INDEX 1 // Index 0 is used for stream buffers
  9. struct FuriThread {
  10. FuriThreadState state;
  11. int32_t ret;
  12. FuriThreadCallback callback;
  13. void* context;
  14. FuriThreadStateCallback state_callback;
  15. void* state_context;
  16. char* name;
  17. configSTACK_DEPTH_TYPE stack_size;
  18. FuriThreadPriority priority;
  19. TaskHandle_t task_handle;
  20. bool heap_trace_enabled;
  21. size_t heap_size;
  22. };
  23. /** Catch threads that are trying to exit wrong way */
  24. __attribute__((__noreturn__)) void furi_thread_catch() {
  25. asm volatile("nop"); // extra magic
  26. furi_crash("You are doing it wrong");
  27. }
  28. static void furi_thread_set_state(FuriThread* thread, FuriThreadState state) {
  29. furi_assert(thread);
  30. thread->state = state;
  31. if(thread->state_callback) {
  32. thread->state_callback(state, thread->state_context);
  33. }
  34. }
  35. static void furi_thread_body(void* context) {
  36. furi_assert(context);
  37. FuriThread* thread = context;
  38. furi_assert(thread->state == FuriThreadStateStarting);
  39. furi_thread_set_state(thread, FuriThreadStateRunning);
  40. TaskHandle_t task_handle = xTaskGetCurrentTaskHandle();
  41. if(thread->heap_trace_enabled == true) {
  42. memmgr_heap_enable_thread_trace((FuriThreadId)task_handle);
  43. }
  44. thread->ret = thread->callback(thread->context);
  45. if(thread->heap_trace_enabled == true) {
  46. osDelay(33);
  47. thread->heap_size = memmgr_heap_get_thread_memory((FuriThreadId)task_handle);
  48. memmgr_heap_disable_thread_trace((FuriThreadId)task_handle);
  49. }
  50. furi_assert(thread->state == FuriThreadStateRunning);
  51. furi_thread_set_state(thread, FuriThreadStateStopped);
  52. vTaskDelete(thread->task_handle);
  53. furi_thread_catch();
  54. }
  55. FuriThread* furi_thread_alloc() {
  56. FuriThread* thread = malloc(sizeof(FuriThread));
  57. return thread;
  58. }
  59. void furi_thread_free(FuriThread* thread) {
  60. furi_assert(thread);
  61. furi_assert(thread->state == FuriThreadStateStopped);
  62. if(thread->name) free((void*)thread->name);
  63. free(thread);
  64. }
  65. void furi_thread_set_name(FuriThread* thread, const char* name) {
  66. furi_assert(thread);
  67. furi_assert(thread->state == FuriThreadStateStopped);
  68. if(thread->name) free((void*)thread->name);
  69. thread->name = strdup(name);
  70. }
  71. void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size) {
  72. furi_assert(thread);
  73. furi_assert(thread->state == FuriThreadStateStopped);
  74. furi_assert(stack_size % 4 == 0);
  75. thread->stack_size = stack_size;
  76. }
  77. void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback) {
  78. furi_assert(thread);
  79. furi_assert(thread->state == FuriThreadStateStopped);
  80. thread->callback = callback;
  81. }
  82. void furi_thread_set_context(FuriThread* thread, void* context) {
  83. furi_assert(thread);
  84. furi_assert(thread->state == FuriThreadStateStopped);
  85. thread->context = context;
  86. }
  87. void furi_thread_set_priority(FuriThread* thread, FuriThreadPriority priority) {
  88. furi_assert(thread);
  89. furi_assert(thread->state == FuriThreadStateStopped);
  90. furi_assert(priority >= FuriThreadPriorityIdle && priority <= FuriThreadPriorityIsr);
  91. thread->priority = priority;
  92. }
  93. void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback) {
  94. furi_assert(thread);
  95. furi_assert(thread->state == FuriThreadStateStopped);
  96. thread->state_callback = callback;
  97. }
  98. void furi_thread_set_state_context(FuriThread* thread, void* context) {
  99. furi_assert(thread);
  100. furi_assert(thread->state == FuriThreadStateStopped);
  101. thread->state_context = context;
  102. }
  103. FuriThreadState furi_thread_get_state(FuriThread* thread) {
  104. furi_assert(thread);
  105. return thread->state;
  106. }
  107. void furi_thread_start(FuriThread* thread) {
  108. furi_assert(thread);
  109. furi_assert(thread->callback);
  110. furi_assert(thread->state == FuriThreadStateStopped);
  111. furi_assert(thread->stack_size > 0 && thread->stack_size < 0xFFFF * 4);
  112. furi_thread_set_state(thread, FuriThreadStateStarting);
  113. BaseType_t ret = xTaskCreate(
  114. furi_thread_body,
  115. thread->name,
  116. thread->stack_size / 4,
  117. thread,
  118. thread->priority ? thread->priority : FuriThreadPriorityNormal,
  119. &thread->task_handle);
  120. furi_check(ret == pdPASS);
  121. furi_check(thread->task_handle);
  122. }
  123. bool furi_thread_join(FuriThread* thread) {
  124. furi_assert(thread);
  125. while(thread->state != FuriThreadStateStopped) {
  126. osDelay(10);
  127. }
  128. return osOK;
  129. }
  130. FuriThreadId furi_thread_get_id(FuriThread* thread) {
  131. furi_assert(thread);
  132. return thread->task_handle;
  133. }
  134. void furi_thread_enable_heap_trace(FuriThread* thread) {
  135. furi_assert(thread);
  136. furi_assert(thread->state == FuriThreadStateStopped);
  137. furi_assert(thread->heap_trace_enabled == false);
  138. thread->heap_trace_enabled = true;
  139. }
  140. void furi_thread_disable_heap_trace(FuriThread* thread) {
  141. furi_assert(thread);
  142. furi_assert(thread->state == FuriThreadStateStopped);
  143. furi_assert(thread->heap_trace_enabled == true);
  144. thread->heap_trace_enabled = false;
  145. }
  146. size_t furi_thread_get_heap_size(FuriThread* thread) {
  147. furi_assert(thread);
  148. furi_assert(thread->heap_trace_enabled == true);
  149. return thread->heap_size;
  150. }
  151. int32_t furi_thread_get_return_code(FuriThread* thread) {
  152. furi_assert(thread);
  153. furi_assert(thread->state == FuriThreadStateStopped);
  154. return thread->ret;
  155. }
  156. FuriThreadId furi_thread_get_current_id() {
  157. return xTaskGetCurrentTaskHandle();
  158. }
  159. void furi_thread_yield() {
  160. furi_assert(!FURI_IS_IRQ_MODE());
  161. taskYIELD();
  162. }
  163. /* Limits */
  164. #define MAX_BITS_TASK_NOTIFY 31U
  165. #define MAX_BITS_EVENT_GROUPS 24U
  166. #define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY) - 1U))
  167. #define EVENT_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_EVENT_GROUPS) - 1U))
  168. uint32_t furi_thread_flags_set(FuriThreadId thread_id, uint32_t flags) {
  169. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  170. uint32_t rflags;
  171. BaseType_t yield;
  172. if((hTask == NULL) || ((flags & THREAD_FLAGS_INVALID_BITS) != 0U)) {
  173. rflags = (uint32_t)osErrorParameter;
  174. } else {
  175. rflags = (uint32_t)osError;
  176. if(FURI_IS_IRQ_MODE()) {
  177. yield = pdFALSE;
  178. (void)xTaskNotifyIndexedFromISR(hTask, THREAD_NOTIFY_INDEX, flags, eSetBits, &yield);
  179. (void)xTaskNotifyAndQueryIndexedFromISR(
  180. hTask, THREAD_NOTIFY_INDEX, 0, eNoAction, &rflags, NULL);
  181. portYIELD_FROM_ISR(yield);
  182. } else {
  183. (void)xTaskNotifyIndexed(hTask, THREAD_NOTIFY_INDEX, flags, eSetBits);
  184. (void)xTaskNotifyAndQueryIndexed(hTask, THREAD_NOTIFY_INDEX, 0, eNoAction, &rflags);
  185. }
  186. }
  187. /* Return flags after setting */
  188. return (rflags);
  189. }
  190. uint32_t furi_thread_flags_clear(uint32_t flags) {
  191. TaskHandle_t hTask;
  192. uint32_t rflags, cflags;
  193. if(FURI_IS_IRQ_MODE()) {
  194. rflags = (uint32_t)osErrorISR;
  195. } else if((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
  196. rflags = (uint32_t)osErrorParameter;
  197. } else {
  198. hTask = xTaskGetCurrentTaskHandle();
  199. if(xTaskNotifyAndQueryIndexed(hTask, THREAD_NOTIFY_INDEX, 0, eNoAction, &cflags) ==
  200. pdPASS) {
  201. rflags = cflags;
  202. cflags &= ~flags;
  203. if(xTaskNotifyIndexed(hTask, THREAD_NOTIFY_INDEX, cflags, eSetValueWithOverwrite) !=
  204. pdPASS) {
  205. rflags = (uint32_t)osError;
  206. }
  207. } else {
  208. rflags = (uint32_t)osError;
  209. }
  210. }
  211. /* Return flags before clearing */
  212. return (rflags);
  213. }
  214. uint32_t furi_thread_flags_get(void) {
  215. TaskHandle_t hTask;
  216. uint32_t rflags;
  217. if(FURI_IS_IRQ_MODE()) {
  218. rflags = (uint32_t)osErrorISR;
  219. } else {
  220. hTask = xTaskGetCurrentTaskHandle();
  221. if(xTaskNotifyAndQueryIndexed(hTask, THREAD_NOTIFY_INDEX, 0, eNoAction, &rflags) !=
  222. pdPASS) {
  223. rflags = (uint32_t)osError;
  224. }
  225. }
  226. return (rflags);
  227. }
  228. uint32_t furi_thread_flags_wait(uint32_t flags, uint32_t options, uint32_t timeout) {
  229. uint32_t rflags, nval;
  230. uint32_t clear;
  231. TickType_t t0, td, tout;
  232. BaseType_t rval;
  233. if(FURI_IS_IRQ_MODE()) {
  234. rflags = (uint32_t)osErrorISR;
  235. } else if((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
  236. rflags = (uint32_t)osErrorParameter;
  237. } else {
  238. if((options & osFlagsNoClear) == osFlagsNoClear) {
  239. clear = 0U;
  240. } else {
  241. clear = flags;
  242. }
  243. rflags = 0U;
  244. tout = timeout;
  245. t0 = xTaskGetTickCount();
  246. do {
  247. rval = xTaskNotifyWaitIndexed(THREAD_NOTIFY_INDEX, 0, clear, &nval, tout);
  248. if(rval == pdPASS) {
  249. rflags &= flags;
  250. rflags |= nval;
  251. if((options & osFlagsWaitAll) == osFlagsWaitAll) {
  252. if((flags & rflags) == flags) {
  253. break;
  254. } else {
  255. if(timeout == 0U) {
  256. rflags = (uint32_t)osErrorResource;
  257. break;
  258. }
  259. }
  260. } else {
  261. if((flags & rflags) != 0) {
  262. break;
  263. } else {
  264. if(timeout == 0U) {
  265. rflags = (uint32_t)osErrorResource;
  266. break;
  267. }
  268. }
  269. }
  270. /* Update timeout */
  271. td = xTaskGetTickCount() - t0;
  272. if(td > tout) {
  273. tout = 0;
  274. } else {
  275. tout -= td;
  276. }
  277. } else {
  278. if(timeout == 0) {
  279. rflags = (uint32_t)osErrorResource;
  280. } else {
  281. rflags = (uint32_t)osErrorTimeout;
  282. }
  283. }
  284. } while(rval != pdFAIL);
  285. }
  286. /* Return flags before clearing */
  287. return (rflags);
  288. }
  289. uint32_t furi_thread_enumerate(FuriThreadId* thread_array, uint32_t array_items) {
  290. uint32_t i, count;
  291. TaskStatus_t* task;
  292. if(FURI_IS_IRQ_MODE() || (thread_array == NULL) || (array_items == 0U)) {
  293. count = 0U;
  294. } else {
  295. vTaskSuspendAll();
  296. count = uxTaskGetNumberOfTasks();
  297. task = pvPortMalloc(count * sizeof(TaskStatus_t));
  298. if(task != NULL) {
  299. count = uxTaskGetSystemState(task, count, NULL);
  300. for(i = 0U; (i < count) && (i < array_items); i++) {
  301. thread_array[i] = (FuriThreadId)task[i].xHandle;
  302. }
  303. count = i;
  304. }
  305. (void)xTaskResumeAll();
  306. vPortFree(task);
  307. }
  308. return (count);
  309. }
  310. const char* furi_thread_get_name(FuriThreadId thread_id) {
  311. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  312. const char* name;
  313. if(FURI_IS_IRQ_MODE() || (hTask == NULL)) {
  314. name = NULL;
  315. } else {
  316. name = pcTaskGetName(hTask);
  317. }
  318. return (name);
  319. }
  320. uint32_t furi_thread_get_stack_space(FuriThreadId thread_id) {
  321. TaskHandle_t hTask = (TaskHandle_t)thread_id;
  322. uint32_t sz;
  323. if(FURI_IS_IRQ_MODE() || (hTask == NULL)) {
  324. sz = 0U;
  325. } else {
  326. sz = (uint32_t)(uxTaskGetStackHighWaterMark(hTask) * sizeof(StackType_t));
  327. }
  328. return (sz);
  329. }