2021-10-03 10:36:05 +00:00
|
|
|
/**
|
|
|
|
* @file thread.h
|
|
|
|
* Furi: Furi Thread API
|
|
|
|
*/
|
|
|
|
|
2021-02-12 17:24:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
#include "base.h"
|
2022-07-20 10:56:33 +00:00
|
|
|
#include "common_defines.h"
|
2021-02-12 17:24:34 +00:00
|
|
|
|
2023-11-01 07:24:11 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2021-02-12 17:24:34 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-03-24 09:35:33 +00:00
|
|
|
/** FuriThreadState */
|
2021-02-12 17:24:34 +00:00
|
|
|
typedef enum {
|
|
|
|
FuriThreadStateStopped,
|
|
|
|
FuriThreadStateStarting,
|
|
|
|
FuriThreadStateRunning,
|
|
|
|
} FuriThreadState;
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
/** FuriThreadPriority */
|
|
|
|
typedef enum {
|
|
|
|
FuriThreadPriorityNone = 0, /**< Uninitialized, choose system default */
|
|
|
|
FuriThreadPriorityIdle = 1, /**< Idle priority */
|
|
|
|
FuriThreadPriorityLowest = 14, /**< Lowest */
|
|
|
|
FuriThreadPriorityLow = 15, /**< Low */
|
|
|
|
FuriThreadPriorityNormal = 16, /**< Normal */
|
|
|
|
FuriThreadPriorityHigh = 17, /**< High */
|
|
|
|
FuriThreadPriorityHighest = 18, /**< Highest */
|
2023-11-01 07:24:11 +00:00
|
|
|
FuriThreadPriorityIsr =
|
|
|
|
(FURI_CONFIG_THREAD_MAX_PRIORITIES - 1), /**< Deferred ISR (highest possible) */
|
2022-06-20 14:54:48 +00:00
|
|
|
} FuriThreadPriority;
|
|
|
|
|
2021-03-24 09:35:33 +00:00
|
|
|
/** FuriThread anonymous structure */
|
2021-02-12 17:24:34 +00:00
|
|
|
typedef struct FuriThread FuriThread;
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
/** FuriThreadId proxy type to OS low level functions */
|
|
|
|
typedef void* FuriThreadId;
|
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** FuriThreadCallback Your callback to run in new thread
|
|
|
|
* @warning never use osThreadExit in FuriThread
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
typedef int32_t (*FuriThreadCallback)(void* context);
|
|
|
|
|
2022-08-03 16:00:17 +00:00
|
|
|
/** Write to stdout callback
|
|
|
|
* @param data pointer to data
|
|
|
|
* @param size data size @warning your handler must consume everything
|
|
|
|
*/
|
|
|
|
typedef void (*FuriThreadStdoutWriteCallback)(const char* data, size_t size);
|
|
|
|
|
2022-11-28 16:51:51 +00:00
|
|
|
/** FuriThread state change callback called upon thread state change
|
2021-10-03 10:36:05 +00:00
|
|
|
* @param state new thread state
|
|
|
|
* @param context callback context
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
typedef void (*FuriThreadStateCallback)(FuriThreadState state, void* context);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Allocate FuriThread
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @return FuriThread instance
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
FuriThread* furi_thread_alloc();
|
|
|
|
|
2022-11-23 12:49:17 +00:00
|
|
|
/** Allocate FuriThread, shortcut version
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param stack_size
|
|
|
|
* @param callback
|
|
|
|
* @param context
|
|
|
|
* @return FuriThread*
|
|
|
|
*/
|
|
|
|
FuriThread* furi_thread_alloc_ex(
|
|
|
|
const char* name,
|
|
|
|
uint32_t stack_size,
|
|
|
|
FuriThreadCallback callback,
|
|
|
|
void* context);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Release FuriThread
|
2023-03-28 07:34:49 +00:00
|
|
|
*
|
|
|
|
* @warning see furi_thread_join
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_free(FuriThread* thread);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Set FuriThread name
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param name string
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_set_name(FuriThread* thread, const char* name);
|
|
|
|
|
2023-03-01 17:57:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Set FuriThread appid
|
|
|
|
* Technically, it is like a "process id", but it is not a system-wide unique identifier.
|
|
|
|
* All threads spawned by the same app will have the same appid.
|
|
|
|
*
|
|
|
|
* @param thread
|
|
|
|
* @param appid
|
|
|
|
*/
|
|
|
|
void furi_thread_set_appid(FuriThread* thread, const char* appid);
|
|
|
|
|
2022-08-27 04:25:47 +00:00
|
|
|
/** Mark thread as service
|
|
|
|
* The service cannot be stopped or removed, and cannot exit from the thread body
|
|
|
|
*
|
|
|
|
* @param thread
|
|
|
|
*/
|
|
|
|
void furi_thread_mark_as_service(FuriThread* thread);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Set FuriThread stack size
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param stack_size stack size in bytes
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Set FuriThread callback
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param callback FuriThreadCallback, called upon thread run
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Set FuriThread context
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param context pointer to context for thread callback
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_set_context(FuriThread* thread, void* context);
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
/** Set FuriThread priority
|
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param priority FuriThreadPriority value
|
|
|
|
*/
|
|
|
|
void furi_thread_set_priority(FuriThread* thread, FuriThreadPriority priority);
|
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
/** Get FuriThread priority
|
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @return FuriThreadPriority value
|
|
|
|
*/
|
|
|
|
FuriThreadPriority furi_thread_get_priority(FuriThread* thread);
|
|
|
|
|
2023-02-08 04:41:22 +00:00
|
|
|
/** Set current thread priority
|
|
|
|
*
|
|
|
|
* @param priority FuriThreadPriority value
|
|
|
|
*/
|
|
|
|
void furi_thread_set_current_priority(FuriThreadPriority priority);
|
|
|
|
|
|
|
|
/** Get current thread priority
|
|
|
|
*
|
|
|
|
* @return FuriThreadPriority value
|
|
|
|
*/
|
|
|
|
FuriThreadPriority furi_thread_get_current_priority();
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Set FuriThread state change callback
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param callback state change callback
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Set FuriThread state change context
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param context pointer to context
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_set_state_context(FuriThread* thread, void* context);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Get FuriThread state
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
|
|
|
* @return thread state from FuriThreadState
|
2021-03-18 07:58:16 +00:00
|
|
|
*/
|
|
|
|
FuriThreadState furi_thread_get_state(FuriThread* thread);
|
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Start FuriThread
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
2022-06-20 14:54:48 +00:00
|
|
|
void furi_thread_start(FuriThread* thread);
|
2021-02-12 17:24:34 +00:00
|
|
|
|
2021-06-23 14:48:44 +00:00
|
|
|
/** Join FuriThread
|
2023-03-28 07:34:49 +00:00
|
|
|
*
|
|
|
|
* @warning Use this method only when CPU is not busy(Idle task receives
|
|
|
|
* control), otherwise it will wait forever.
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
2022-06-20 14:54:48 +00:00
|
|
|
* @return bool
|
2021-02-12 17:24:34 +00:00
|
|
|
*/
|
2022-06-20 14:54:48 +00:00
|
|
|
bool furi_thread_join(FuriThread* thread);
|
2021-02-12 17:24:34 +00:00
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
/** Get FreeRTOS FuriThreadId for FuriThread instance
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
2022-06-20 14:54:48 +00:00
|
|
|
* @return FuriThreadId or NULL
|
2021-06-23 14:48:44 +00:00
|
|
|
*/
|
2022-06-20 14:54:48 +00:00
|
|
|
FuriThreadId furi_thread_get_id(FuriThread* thread);
|
2021-06-23 14:48:44 +00:00
|
|
|
|
|
|
|
/** Enable heap tracing
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
2021-06-23 14:48:44 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_enable_heap_trace(FuriThread* thread);
|
|
|
|
|
|
|
|
/** Disable heap tracing
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
2021-06-23 14:48:44 +00:00
|
|
|
*/
|
|
|
|
void furi_thread_disable_heap_trace(FuriThread* thread);
|
|
|
|
|
|
|
|
/** Get thread heap size
|
2021-10-03 10:36:05 +00:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
|
|
|
* @return size in bytes
|
2021-06-23 14:48:44 +00:00
|
|
|
*/
|
|
|
|
size_t furi_thread_get_heap_size(FuriThread* thread);
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
/** Get thread return code
|
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
|
|
|
* @return return code
|
|
|
|
*/
|
|
|
|
int32_t furi_thread_get_return_code(FuriThread* thread);
|
|
|
|
|
2022-11-28 16:51:51 +00:00
|
|
|
/** Thread related methods that doesn't involve FuriThread directly */
|
2022-06-20 14:54:48 +00:00
|
|
|
|
|
|
|
/** Get FreeRTOS FuriThreadId for current thread
|
|
|
|
*
|
|
|
|
* @return FuriThreadId or NULL
|
|
|
|
*/
|
|
|
|
FuriThreadId furi_thread_get_current_id();
|
|
|
|
|
2022-08-03 16:00:17 +00:00
|
|
|
/** Get FuriThread instance for current thread
|
|
|
|
*
|
2023-05-05 12:40:55 +00:00
|
|
|
* @return pointer to FuriThread or NULL if this thread doesn't belongs to Furi
|
2022-08-03 16:00:17 +00:00
|
|
|
*/
|
|
|
|
FuriThread* furi_thread_get_current();
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
/** Return control to scheduler */
|
|
|
|
void furi_thread_yield();
|
|
|
|
|
|
|
|
uint32_t furi_thread_flags_set(FuriThreadId thread_id, uint32_t flags);
|
|
|
|
|
|
|
|
uint32_t furi_thread_flags_clear(uint32_t flags);
|
|
|
|
|
|
|
|
uint32_t furi_thread_flags_get(void);
|
|
|
|
|
|
|
|
uint32_t furi_thread_flags_wait(uint32_t flags, uint32_t options, uint32_t timeout);
|
|
|
|
|
2023-03-01 17:57:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Enumerate threads
|
|
|
|
*
|
|
|
|
* @param thread_array array of FuriThreadId, where thread ids will be stored
|
2024-03-06 06:25:21 +00:00
|
|
|
* @param array_item_count array size
|
2023-03-01 17:57:27 +00:00
|
|
|
* @return uint32_t threads count
|
|
|
|
*/
|
2024-02-16 07:20:45 +00:00
|
|
|
uint32_t furi_thread_enumerate(FuriThreadId* thread_array, uint32_t array_item_count);
|
2022-06-20 14:54:48 +00:00
|
|
|
|
2023-03-01 17:57:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Get thread name
|
|
|
|
*
|
|
|
|
* @param thread_id
|
|
|
|
* @return const char* name or NULL
|
|
|
|
*/
|
2022-06-20 14:54:48 +00:00
|
|
|
const char* furi_thread_get_name(FuriThreadId thread_id);
|
|
|
|
|
2023-03-01 17:57:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Get thread appid
|
|
|
|
*
|
|
|
|
* @param thread_id
|
|
|
|
* @return const char* appid
|
|
|
|
*/
|
|
|
|
const char* furi_thread_get_appid(FuriThreadId thread_id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get thread stack watermark
|
|
|
|
*
|
|
|
|
* @param thread_id
|
|
|
|
* @return uint32_t
|
|
|
|
*/
|
2022-06-20 14:54:48 +00:00
|
|
|
uint32_t furi_thread_get_stack_space(FuriThreadId thread_id);
|
|
|
|
|
2023-01-30 07:54:15 +00:00
|
|
|
/** Get STDOUT callback for thead
|
|
|
|
*
|
|
|
|
* @return STDOUT callback
|
|
|
|
*/
|
|
|
|
FuriThreadStdoutWriteCallback furi_thread_get_stdout_callback();
|
|
|
|
|
2022-08-03 16:00:17 +00:00
|
|
|
/** Set STDOUT callback for thread
|
2023-05-05 12:40:55 +00:00
|
|
|
*
|
2022-08-03 16:00:17 +00:00
|
|
|
* @param callback callback or NULL to clear
|
|
|
|
*/
|
2023-05-05 12:40:55 +00:00
|
|
|
void furi_thread_set_stdout_callback(FuriThreadStdoutWriteCallback callback);
|
2022-08-03 16:00:17 +00:00
|
|
|
|
|
|
|
/** Write data to buffered STDOUT
|
|
|
|
*
|
|
|
|
* @param data input data
|
|
|
|
* @param size input data size
|
|
|
|
*
|
|
|
|
* @return size_t written data size
|
|
|
|
*/
|
|
|
|
size_t furi_thread_stdout_write(const char* data, size_t size);
|
|
|
|
|
|
|
|
/** Flush data to STDOUT
|
|
|
|
*
|
|
|
|
* @return int32_t error code
|
|
|
|
*/
|
|
|
|
int32_t furi_thread_stdout_flush();
|
|
|
|
|
2022-09-21 14:42:59 +00:00
|
|
|
/** Suspend thread
|
|
|
|
*
|
|
|
|
* @param thread_id thread id
|
|
|
|
*/
|
|
|
|
void furi_thread_suspend(FuriThreadId thread_id);
|
|
|
|
|
|
|
|
/** Resume thread
|
|
|
|
*
|
|
|
|
* @param thread_id thread id
|
|
|
|
*/
|
|
|
|
void furi_thread_resume(FuriThreadId thread_id);
|
|
|
|
|
|
|
|
/** Get thread suspended state
|
|
|
|
*
|
|
|
|
* @param thread_id thread id
|
|
|
|
* @return true if thread is suspended
|
|
|
|
*/
|
|
|
|
bool furi_thread_is_suspended(FuriThreadId thread_id);
|
|
|
|
|
2021-02-12 17:24:34 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2022-05-06 13:37:10 +00:00
|
|
|
#endif
|