2021-10-03 13:36:05 +03:00
|
|
|
/**
|
|
|
|
* @file thread.h
|
|
|
|
* Furi: Furi Thread API
|
|
|
|
*/
|
|
|
|
|
2021-02-12 20:24:34 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-06-20 17:54:48 +03:00
|
|
|
#include "base.h"
|
2022-07-20 13:56:33 +03:00
|
|
|
#include "common_defines.h"
|
2021-02-12 20:24:34 +03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/** FuriThreadState */
|
2021-02-12 20:24:34 +03:00
|
|
|
typedef enum {
|
|
|
|
FuriThreadStateStopped,
|
|
|
|
FuriThreadStateStarting,
|
|
|
|
FuriThreadStateRunning,
|
|
|
|
} FuriThreadState;
|
|
|
|
|
2022-06-20 17:54:48 +03: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 */
|
|
|
|
FuriThreadPriorityIsr = 32, /**< Deffered Isr (highest possible) */
|
|
|
|
} FuriThreadPriority;
|
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/** FuriThread anonymous structure */
|
2021-02-12 20:24:34 +03:00
|
|
|
typedef struct FuriThread FuriThread;
|
|
|
|
|
2022-06-20 17:54:48 +03:00
|
|
|
/** FuriThreadId proxy type to OS low level functions */
|
|
|
|
typedef void* FuriThreadId;
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** FuriThreadCallback Your callback to run in new thread
|
|
|
|
* @warning never use osThreadExit in FuriThread
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
typedef int32_t (*FuriThreadCallback)(void* context);
|
|
|
|
|
2022-08-03 19:00:17 +03: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 19:51:51 +03:00
|
|
|
/** FuriThread state change callback called upon thread state change
|
2021-10-03 13:36:05 +03:00
|
|
|
* @param state new thread state
|
|
|
|
* @param context callback context
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
typedef void (*FuriThreadStateCallback)(FuriThreadState state, void* context);
|
|
|
|
|
2021-06-23 17:48:44 +03:00
|
|
|
/** Allocate FuriThread
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @return FuriThread instance
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
FuriThread* furi_thread_alloc();
|
|
|
|
|
2022-11-23 15:49:17 +03: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 17:48:44 +03:00
|
|
|
/** Release FuriThread
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_free(FuriThread* thread);
|
|
|
|
|
2021-06-23 17:48:44 +03:00
|
|
|
/** Set FuriThread name
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param name string
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_set_name(FuriThread* thread, const char* name);
|
|
|
|
|
2023-03-01 20:57:27 +03: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 07:25:47 +03: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 17:48:44 +03:00
|
|
|
/** Set FuriThread stack size
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param stack_size stack size in bytes
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size);
|
|
|
|
|
2021-06-23 17:48:44 +03:00
|
|
|
/** Set FuriThread callback
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param callback FuriThreadCallback, called upon thread run
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback);
|
|
|
|
|
2021-06-23 17:48:44 +03:00
|
|
|
/** Set FuriThread context
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param context pointer to context for thread callback
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_set_context(FuriThread* thread, void* context);
|
|
|
|
|
2022-06-20 17:54:48 +03:00
|
|
|
/** Set FuriThread priority
|
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param priority FuriThreadPriority value
|
|
|
|
*/
|
|
|
|
void furi_thread_set_priority(FuriThread* thread, FuriThreadPriority priority);
|
|
|
|
|
2023-02-08 07:41:22 +03: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 17:48:44 +03:00
|
|
|
/** Set FuriThread state change callback
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param callback state change callback
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback);
|
|
|
|
|
2021-06-23 17:48:44 +03:00
|
|
|
/** Set FuriThread state change context
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
* @param context pointer to context
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_set_state_context(FuriThread* thread, void* context);
|
|
|
|
|
2021-06-23 17:48:44 +03:00
|
|
|
/** Get FuriThread state
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
|
|
|
* @return thread state from FuriThreadState
|
2021-03-18 10:58:16 +03:00
|
|
|
*/
|
|
|
|
FuriThreadState furi_thread_get_state(FuriThread* thread);
|
|
|
|
|
2021-06-23 17:48:44 +03:00
|
|
|
/** Start FuriThread
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
2022-06-20 17:54:48 +03:00
|
|
|
void furi_thread_start(FuriThread* thread);
|
2021-02-12 20:24:34 +03:00
|
|
|
|
2021-06-23 17:48:44 +03:00
|
|
|
/** Join FuriThread
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
2022-06-20 17:54:48 +03:00
|
|
|
* @return bool
|
2021-02-12 20:24:34 +03:00
|
|
|
*/
|
2022-06-20 17:54:48 +03:00
|
|
|
bool furi_thread_join(FuriThread* thread);
|
2021-02-12 20:24:34 +03:00
|
|
|
|
2022-06-20 17:54:48 +03:00
|
|
|
/** Get FreeRTOS FuriThreadId for FuriThread instance
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
2022-06-20 17:54:48 +03:00
|
|
|
* @return FuriThreadId or NULL
|
2021-06-23 17:48:44 +03:00
|
|
|
*/
|
2022-06-20 17:54:48 +03:00
|
|
|
FuriThreadId furi_thread_get_id(FuriThread* thread);
|
2021-06-23 17:48:44 +03:00
|
|
|
|
|
|
|
/** Enable heap tracing
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
2021-06-23 17:48:44 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_enable_heap_trace(FuriThread* thread);
|
|
|
|
|
|
|
|
/** Disable heap tracing
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
2021-06-23 17:48:44 +03:00
|
|
|
*/
|
|
|
|
void furi_thread_disable_heap_trace(FuriThread* thread);
|
|
|
|
|
|
|
|
/** Get thread heap size
|
2021-10-03 13:36:05 +03:00
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
|
|
|
* @return size in bytes
|
2021-06-23 17:48:44 +03:00
|
|
|
*/
|
|
|
|
size_t furi_thread_get_heap_size(FuriThread* thread);
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
/** Get thread return code
|
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
|
|
|
* @return return code
|
|
|
|
*/
|
|
|
|
int32_t furi_thread_get_return_code(FuriThread* thread);
|
|
|
|
|
2022-11-28 19:51:51 +03:00
|
|
|
/** Thread related methods that doesn't involve FuriThread directly */
|
2022-06-20 17:54:48 +03:00
|
|
|
|
|
|
|
/** Get FreeRTOS FuriThreadId for current thread
|
|
|
|
*
|
|
|
|
* @param thread FuriThread instance
|
|
|
|
*
|
|
|
|
* @return FuriThreadId or NULL
|
|
|
|
*/
|
|
|
|
FuriThreadId furi_thread_get_current_id();
|
|
|
|
|
2022-08-03 19:00:17 +03:00
|
|
|
/** Get FuriThread instance for current thread
|
|
|
|
*
|
|
|
|
* @return FuriThread*
|
|
|
|
*/
|
|
|
|
FuriThread* furi_thread_get_current();
|
|
|
|
|
2022-06-20 17:54:48 +03: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 20:57:27 +03:00
|
|
|
/**
|
|
|
|
* @brief Enumerate threads
|
|
|
|
*
|
|
|
|
* @param thread_array array of FuriThreadId, where thread ids will be stored
|
|
|
|
* @param array_items array size
|
|
|
|
* @return uint32_t threads count
|
|
|
|
*/
|
2022-06-20 17:54:48 +03:00
|
|
|
uint32_t furi_thread_enumerate(FuriThreadId* thread_array, uint32_t array_items);
|
|
|
|
|
2023-03-01 20:57:27 +03:00
|
|
|
/**
|
|
|
|
* @brief Get thread name
|
|
|
|
*
|
|
|
|
* @param thread_id
|
|
|
|
* @return const char* name or NULL
|
|
|
|
*/
|
2022-06-20 17:54:48 +03:00
|
|
|
const char* furi_thread_get_name(FuriThreadId thread_id);
|
|
|
|
|
2023-03-01 20:57:27 +03: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 17:54:48 +03:00
|
|
|
uint32_t furi_thread_get_stack_space(FuriThreadId thread_id);
|
|
|
|
|
2023-01-30 10:54:15 +03:00
|
|
|
/** Get STDOUT callback for thead
|
|
|
|
*
|
|
|
|
* @return STDOUT callback
|
|
|
|
*/
|
|
|
|
FuriThreadStdoutWriteCallback furi_thread_get_stdout_callback();
|
|
|
|
|
2022-08-03 19:00:17 +03:00
|
|
|
/** Set STDOUT callback for thread
|
|
|
|
*
|
|
|
|
* @param callback callback or NULL to clear
|
|
|
|
*
|
|
|
|
* @return true on success, otherwise fail
|
|
|
|
*/
|
|
|
|
bool furi_thread_set_stdout_callback(FuriThreadStdoutWriteCallback callback);
|
|
|
|
|
|
|
|
/** 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 17:42:59 +03: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 20:24:34 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2022-05-06 16:37:10 +03:00
|
|
|
#endif
|