diff --git a/lib/toolbox/SConscript b/lib/toolbox/SConscript index 0381ce5b9..42ccc9207 100644 --- a/lib/toolbox/SConscript +++ b/lib/toolbox/SConscript @@ -40,6 +40,7 @@ env.Append( File("pulse_protocols/pulse_glue.h"), File("md5_calc.h"), File("varint.h"), + File("run_parallel.h"), ], ) diff --git a/lib/toolbox/run_parallel.c b/lib/toolbox/run_parallel.c new file mode 100644 index 000000000..439949cf4 --- /dev/null +++ b/lib/toolbox/run_parallel.c @@ -0,0 +1,17 @@ +#include "run_parallel.h" + +#include + +static void run_parallel_thread_state(FuriThread* thread, FuriThreadState state, void* context) { + UNUSED(context); + + if(state == FuriThreadStateStopped) { + furi_thread_free(thread); + } +} + +void run_parallel(FuriThreadCallback callback, void* context, uint32_t stack_size) { + FuriThread* thread = furi_thread_alloc_ex(NULL, stack_size, callback, context); + furi_thread_set_state_callback(thread, run_parallel_thread_state); + furi_thread_start(thread); +} diff --git a/lib/toolbox/run_parallel.h b/lib/toolbox/run_parallel.h new file mode 100644 index 000000000..2722c54eb --- /dev/null +++ b/lib/toolbox/run_parallel.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +/** + * @brief Run function in thread, then automatically clean up thread. + * + * @param[in] callback pointer to a function to be executed in parallel + * @param[in] context pointer to a user-specified object (will be passed to the callback) + * @param[in] stack_size stack size in bytes + */ +void run_parallel(FuriThreadCallback callback, void* context, uint32_t stack_size); diff --git a/targets/f7/api_symbols.csv b/targets/f7/api_symbols.csv index 65f8d74e7..9175f8ade 100644 --- a/targets/f7/api_symbols.csv +++ b/targets/f7/api_symbols.csv @@ -241,6 +241,7 @@ Header,+,lib/toolbox/pipe.h,, Header,+,lib/toolbox/pretty_format.h,, Header,+,lib/toolbox/protocols/protocol_dict.h,, Header,+,lib/toolbox/pulse_protocols/pulse_glue.h,, +Header,+,lib/toolbox/run_parallel.h,, Header,+,lib/toolbox/saved_struct.h,, Header,+,lib/toolbox/simple_array.h,, Header,+,lib/toolbox/stream/buffered_file_stream.h,, @@ -3155,6 +3156,7 @@ Function,+,rpc_system_app_set_callback,void,"RpcAppSystem*, RpcAppSystemCallback Function,+,rpc_system_app_set_error_code,void,"RpcAppSystem*, uint32_t" Function,+,rpc_system_app_set_error_text,void,"RpcAppSystem*, const char*" Function,-,rpmatch,int,const char* +Function,+,run_parallel,void,"FuriThreadCallback, void*, uint32_t" Function,+,saved_struct_get_metadata,_Bool,"const char*, uint8_t*, uint8_t*, size_t*" Function,+,saved_struct_load,_Bool,"const char*, void*, size_t, uint8_t, uint8_t" Function,+,saved_struct_save,_Bool,"const char*, const void*, size_t, uint8_t, uint8_t"