mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
Toolbox: Add run_parallel() util
Runs a self-cleaning-up thread without boilerplate Same paradigm used in existing code like region and rpc services Not replaced there to avoid merge conflicts Not exposed to API for now
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <cli/cli.h>
|
||||
#include <toolbox/run_parallel.h>
|
||||
|
||||
#include "test_runner.h"
|
||||
|
||||
@@ -13,21 +14,14 @@ void unit_tests_cli(Cli* cli, FuriString* args, void* context) {
|
||||
test_runner_free(test_runner);
|
||||
}
|
||||
|
||||
static void unit_tests_pending(void* context, uint32_t arg) {
|
||||
UNUSED(arg);
|
||||
FuriThread* thread = context;
|
||||
furi_thread_join(thread);
|
||||
furi_thread_free(thread);
|
||||
}
|
||||
|
||||
static int32_t unit_tests_thread(void* context) {
|
||||
UNUSED(context);
|
||||
furi_delay_ms(5000);
|
||||
FuriString* args = furi_string_alloc();
|
||||
TestRunner* test_runner = test_runner_alloc(NULL, args);
|
||||
test_runner_run(test_runner);
|
||||
test_runner_free(test_runner);
|
||||
furi_string_free(args);
|
||||
furi_timer_pending_callback(unit_tests_pending, context, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -38,8 +32,6 @@ void unit_tests_on_system_start(void) {
|
||||
furi_record_close(RECORD_CLI);
|
||||
#endif
|
||||
if(furi_hal_is_normal_boot()) {
|
||||
FuriThread* thread = furi_thread_alloc_ex("UnitTests", 4 * 1024, unit_tests_thread, NULL);
|
||||
furi_thread_set_context(thread, thread);
|
||||
furi_thread_start(thread);
|
||||
run_parallel(unit_tests_thread, NULL, 4 * 1024);
|
||||
}
|
||||
}
|
||||
|
||||
26
lib/toolbox/run_parallel.c
Normal file
26
lib/toolbox/run_parallel.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "run_parallel.h"
|
||||
|
||||
#include <core/timer.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void run_parallel_pending_callback(void* context, uint32_t arg) {
|
||||
UNUSED(arg);
|
||||
|
||||
FuriThread* thread = context;
|
||||
furi_thread_join(thread);
|
||||
furi_thread_free(thread);
|
||||
}
|
||||
|
||||
static void run_parallel_thread_state(FuriThreadState state, void* context) {
|
||||
UNUSED(context);
|
||||
|
||||
if(state == FuriThreadStateStopped) {
|
||||
furi_timer_pending_callback(run_parallel_pending_callback, furi_thread_get_current(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
12
lib/toolbox/run_parallel.h
Normal file
12
lib/toolbox/run_parallel.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <core/thread.h>
|
||||
|
||||
/**
|
||||
* @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);
|
||||
Reference in New Issue
Block a user