Skip to content

Commit

Permalink
Core: thread allocation shortcut (#2007)
Browse files Browse the repository at this point in the history
* Core: thread alloc+set shortcut
* Apps: use thread allocation shortcut
* Mark some service threads as services
* Init BT as soon as possible

Co-authored-by: あく <[email protected]>
  • Loading branch information
DrZlo13 and skotopes authored Nov 23, 2022
1 parent b9c483f commit c511c67
Show file tree
Hide file tree
Showing 38 changed files with 94 additions and 195 deletions.
6 changes: 1 addition & 5 deletions applications/debug/uart_echo/uart_echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,7 @@ static UartEchoApp* uart_echo_app_alloc() {
furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, uart_echo_on_irq_cb, app);

app->worker_thread = furi_thread_alloc();
furi_thread_set_name(app->worker_thread, "UsbUartWorker");
furi_thread_set_stack_size(app->worker_thread, 1024);
furi_thread_set_context(app->worker_thread, app);
furi_thread_set_callback(app->worker_thread, uart_echo_worker);
app->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 1024, uart_echo_worker, app);
furi_thread_start(app->worker_thread);

return app;
Expand Down
14 changes: 4 additions & 10 deletions applications/debug/unit_tests/storage/storage_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ MU_TEST(storage_file_open_lock) {
File* file = storage_file_alloc(storage);

// file_locker thread start
FuriThread* locker_thread = furi_thread_alloc();
furi_thread_set_name(locker_thread, "StorageFileLocker");
furi_thread_set_stack_size(locker_thread, 2048);
furi_thread_set_context(locker_thread, semaphore);
furi_thread_set_callback(locker_thread, storage_file_locker);
FuriThread* locker_thread =
furi_thread_alloc_ex("StorageFileLocker", 2048, storage_file_locker, semaphore);
furi_thread_start(locker_thread);

// wait for file lock
Expand Down Expand Up @@ -133,11 +130,8 @@ MU_TEST(storage_dir_open_lock) {
File* file = storage_file_alloc(storage);

// file_locker thread start
FuriThread* locker_thread = furi_thread_alloc();
furi_thread_set_name(locker_thread, "StorageDirLocker");
furi_thread_set_stack_size(locker_thread, 2048);
furi_thread_set_context(locker_thread, semaphore);
furi_thread_set_callback(locker_thread, storage_dir_locker);
FuriThread* locker_thread =
furi_thread_alloc_ex("StorageDirLocker", 2048, storage_dir_locker, semaphore);
furi_thread_start(locker_thread);

// wait for dir lock
Expand Down
7 changes: 1 addition & 6 deletions applications/main/bad_usb/bad_usb_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,7 @@ BadUsbScript* bad_usb_script_open(FuriString* file_path) {
bad_usb->st.state = BadUsbStateInit;
bad_usb->st.error[0] = '\0';

bad_usb->thread = furi_thread_alloc();
furi_thread_set_name(bad_usb->thread, "BadUsbWorker");
furi_thread_set_stack_size(bad_usb->thread, 2048);
furi_thread_set_context(bad_usb->thread, bad_usb);
furi_thread_set_callback(bad_usb->thread, bad_usb_worker);

bad_usb->thread = furi_thread_alloc_ex("BadUsbWorker", 2048, bad_usb_worker, bad_usb);
furi_thread_start(bad_usb->thread);
return bad_usb;
}
Expand Down
13 changes: 3 additions & 10 deletions applications/main/gpio/usb_uart_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,8 @@ static int32_t usb_uart_worker(void* context) {
usb_uart->tx_sem = furi_semaphore_alloc(1, 1);
usb_uart->usb_mutex = furi_mutex_alloc(FuriMutexTypeNormal);

usb_uart->tx_thread = furi_thread_alloc();
furi_thread_set_name(usb_uart->tx_thread, "UsbUartTxWorker");
furi_thread_set_stack_size(usb_uart->tx_thread, 512);
furi_thread_set_context(usb_uart->tx_thread, usb_uart);
furi_thread_set_callback(usb_uart->tx_thread, usb_uart_tx_thread);
usb_uart->tx_thread =
furi_thread_alloc_ex("UsbUartTxWorker", 512, usb_uart_tx_thread, usb_uart);

usb_uart_vcp_init(usb_uart, usb_uart->cfg.vcp_ch);
usb_uart_serial_init(usb_uart, usb_uart->cfg.uart_ch);
Expand Down Expand Up @@ -338,11 +335,7 @@ UsbUartBridge* usb_uart_enable(UsbUartConfig* cfg) {

memcpy(&(usb_uart->cfg_new), cfg, sizeof(UsbUartConfig));

usb_uart->thread = furi_thread_alloc();
furi_thread_set_name(usb_uart->thread, "UsbUartWorker");
furi_thread_set_stack_size(usb_uart->thread, 1024);
furi_thread_set_context(usb_uart->thread, usb_uart);
furi_thread_set_callback(usb_uart->thread, usb_uart_worker);
usb_uart->thread = furi_thread_alloc_ex("UsbUartWorker", 1024, usb_uart_worker, usb_uart);

furi_thread_start(usb_uart->thread);
return usb_uart;
Expand Down
7 changes: 2 additions & 5 deletions applications/main/subghz/helpers/subghz_chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ SubGhzChatWorker* subghz_chat_worker_alloc(Cli* cli) {

instance->cli = cli;

instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "SubGhzChat");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
furi_thread_set_callback(instance->thread, subghz_chat_worker_thread);
instance->thread =
furi_thread_alloc_ex("SubGhzChat", 2048, subghz_chat_worker_thread, instance);
instance->subghz_txrx = subghz_tx_rx_worker_alloc();
instance->event_queue = furi_message_queue_alloc(80, sizeof(SubGhzChatEvent));
return instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,8 @@ SubGhzFrequencyAnalyzerWorker* subghz_frequency_analyzer_worker_alloc(void* cont
furi_assert(context);
SubGhzFrequencyAnalyzerWorker* instance = malloc(sizeof(SubGhzFrequencyAnalyzerWorker));

instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "SubGhzFAWorker");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
furi_thread_set_callback(instance->thread, subghz_frequency_analyzer_worker_thread);

instance->thread = furi_thread_alloc_ex(
"SubGhzFAWorker", 2048, subghz_frequency_analyzer_worker_thread, instance);
SubGhz* subghz = context;
instance->setting = subghz->setting;
return instance;
Expand Down
6 changes: 1 addition & 5 deletions applications/main/u2f/u2f_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,7 @@ U2fHid* u2f_hid_start(U2fData* u2f_inst) {

u2f_hid->u2f_instance = u2f_inst;

u2f_hid->thread = furi_thread_alloc();
furi_thread_set_name(u2f_hid->thread, "U2fHidWorker");
furi_thread_set_stack_size(u2f_hid->thread, 2048);
furi_thread_set_context(u2f_hid->thread, u2f_hid);
furi_thread_set_callback(u2f_hid->thread, u2f_hid_worker);
u2f_hid->thread = furi_thread_alloc_ex("U2fHidWorker", 2048, u2f_hid_worker, u2f_hid);
furi_thread_start(u2f_hid->thread);
return u2f_hid;
}
Expand Down
13 changes: 0 additions & 13 deletions applications/plugins/dap_link/dap_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,19 +441,6 @@ static int32_t cdc_process(void* p) {
/******************************* MAIN APP **********************************/
/***************************************************************************/

static FuriThread* furi_thread_alloc_ex(
const char* name,
uint32_t stack_size,
FuriThreadCallback callback,
void* context) {
FuriThread* thread = furi_thread_alloc();
furi_thread_set_name(thread, name);
furi_thread_set_stack_size(thread, stack_size);
furi_thread_set_callback(thread, callback);
furi_thread_set_context(thread, context);
return thread;
}

static DapApp* dap_app_alloc() {
DapApp* dap_app = malloc(sizeof(DapApp));
dap_app->dap_thread = furi_thread_alloc_ex("DAP Process", 1024, dap_process, dap_app);
Expand Down
7 changes: 2 additions & 5 deletions applications/plugins/music_player/music_player_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ MusicPlayerWorker* music_player_worker_alloc() {

NoteBlockArray_init(instance->notes);

instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "MusicPlayerWorker");
furi_thread_set_stack_size(instance->thread, 1024);
furi_thread_set_context(instance->thread, instance);
furi_thread_set_callback(instance->thread, music_player_worker_thread_callback);
instance->thread = furi_thread_alloc_ex(
"MusicPlayerWorker", 1024, music_player_worker_thread_callback, instance);

instance->volume = 1.0f;

Expand Down
7 changes: 2 additions & 5 deletions applications/plugins/nfc_magic/nfc_magic_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ NfcMagicWorker* nfc_magic_worker_alloc() {
NfcMagicWorker* nfc_magic_worker = malloc(sizeof(NfcMagicWorker));

// Worker thread attributes
nfc_magic_worker->thread = furi_thread_alloc();
furi_thread_set_name(nfc_magic_worker->thread, "NfcMagicWorker");
furi_thread_set_stack_size(nfc_magic_worker->thread, 8192);
furi_thread_set_callback(nfc_magic_worker->thread, nfc_magic_worker_task);
furi_thread_set_context(nfc_magic_worker->thread, nfc_magic_worker);
nfc_magic_worker->thread =
furi_thread_alloc_ex("NfcMagicWorker", 8192, nfc_magic_worker_task, nfc_magic_worker);

nfc_magic_worker->callback = NULL;
nfc_magic_worker->context = NULL;
Expand Down
7 changes: 2 additions & 5 deletions applications/plugins/picopass/picopass_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ PicopassWorker* picopass_worker_alloc() {
PicopassWorker* picopass_worker = malloc(sizeof(PicopassWorker));

// Worker thread attributes
picopass_worker->thread = furi_thread_alloc();
furi_thread_set_name(picopass_worker->thread, "PicopassWorker");
furi_thread_set_stack_size(picopass_worker->thread, 8192);
furi_thread_set_callback(picopass_worker->thread, picopass_worker_task);
furi_thread_set_context(picopass_worker->thread, picopass_worker);
picopass_worker->thread =
furi_thread_alloc_ex("PicopassWorker", 8192, picopass_worker_task, picopass_worker);

picopass_worker->callback = NULL;
picopass_worker->context = NULL;
Expand Down
5 changes: 1 addition & 4 deletions applications/services/cli/cli_vcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ static void cli_vcp_init() {

vcp->connected = false;

vcp->thread = furi_thread_alloc();
furi_thread_set_name(vcp->thread, "CliVcpWorker");
furi_thread_set_stack_size(vcp->thread, 1024);
furi_thread_set_callback(vcp->thread, vcp_worker);
vcp->thread = furi_thread_alloc_ex("CliVcpWorker", 1024, vcp_worker, NULL);
furi_thread_start(vcp->thread);

FURI_LOG_I(TAG, "Init OK");
Expand Down
6 changes: 1 addition & 5 deletions applications/services/gui/modules/file_browser_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,7 @@ BrowserWorker*
browser->skip_assets = skip_assets;
browser->path_next = furi_string_alloc_set(path);

browser->thread = furi_thread_alloc();
furi_thread_set_name(browser->thread, "BrowserWorker");
furi_thread_set_stack_size(browser->thread, 2048);
furi_thread_set_context(browser->thread, browser);
furi_thread_set_callback(browser->thread, browser_worker);
browser->thread = furi_thread_alloc_ex("BrowserWorker", 2048, browser_worker, browser);
furi_thread_start(browser->thread);

return browser;
Expand Down
6 changes: 1 addition & 5 deletions applications/services/rpc/rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,7 @@ RpcSession* rpc_session_open(Rpc* rpc) {
};
rpc_add_handler(session, PB_Main_stop_session_tag, &rpc_handler);

session->thread = furi_thread_alloc();
furi_thread_set_name(session->thread, "RpcSessionWorker");
furi_thread_set_stack_size(session->thread, 3072);
furi_thread_set_context(session->thread, session);
furi_thread_set_callback(session->thread, rpc_session_worker);
session->thread = furi_thread_alloc_ex("RpcSessionWorker", 3072, rpc_session_worker, session);

furi_thread_set_state_context(session->thread, session);
furi_thread_set_state_callback(session->thread, rpc_session_free_callback);
Expand Down
8 changes: 2 additions & 6 deletions applications/services/rpc/rpc_gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,8 @@ static void rpc_system_gui_start_screen_stream_process(const PB_Main* request, v
malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(framebuffer_size));
rpc_gui->transmit_frame->content.gui_screen_frame.data->size = framebuffer_size;
// Transmission thread for async TX
rpc_gui->transmit_thread = furi_thread_alloc();
furi_thread_set_name(rpc_gui->transmit_thread, "GuiRpcWorker");
furi_thread_set_callback(
rpc_gui->transmit_thread, rpc_system_gui_screen_stream_frame_transmit_thread);
furi_thread_set_context(rpc_gui->transmit_thread, rpc_gui);
furi_thread_set_stack_size(rpc_gui->transmit_thread, 1024);
rpc_gui->transmit_thread = furi_thread_alloc_ex(
"GuiRpcWorker", 1024, rpc_system_gui_screen_stream_frame_transmit_thread, rpc_gui);
furi_thread_start(rpc_gui->transmit_thread);
// GUI framebuffer callback
gui_add_framebuffer_callback(
Expand Down
7 changes: 2 additions & 5 deletions applications/system/updater/cli/updater_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,8 @@ static void updater_start_app() {
* inside loader process, at startup.
* So, accessing its record would cause a deadlock
*/
FuriThread* thread = furi_thread_alloc();

furi_thread_set_name(thread, "UpdateAppSpawner");
furi_thread_set_stack_size(thread, 768);
furi_thread_set_callback(thread, updater_spawner_thread_worker);
FuriThread* thread =
furi_thread_alloc_ex("UpdateAppSpawner", 768, updater_spawner_thread_worker, NULL);
furi_thread_set_state_callback(thread, updater_spawner_thread_cleanup);
furi_thread_set_state_context(thread, thread);
furi_thread_start(thread);
Expand Down
7 changes: 2 additions & 5 deletions applications/system/updater/util/update_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,8 @@ UpdateTask* update_task_alloc() {
update_task->boot_mode = furi_hal_rtc_get_boot_mode();
update_task->update_path = furi_string_alloc();

FuriThread* thread = update_task->thread = furi_thread_alloc();

furi_thread_set_name(thread, "UpdateWorker");
furi_thread_set_stack_size(thread, 5120);
furi_thread_set_context(thread, update_task);
FuriThread* thread = update_task->thread =
furi_thread_alloc_ex("UpdateWorker", 5120, NULL, update_task);

furi_thread_set_state_callback(thread, update_task_worker_thread_cb);
furi_thread_set_state_context(thread, update_task);
Expand Down
5 changes: 1 addition & 4 deletions firmware/targets/f7/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ int main() {
// Flipper critical FURI HAL
furi_hal_init_early();

FuriThread* main_thread = furi_thread_alloc();
furi_thread_set_name(main_thread, "Init");
furi_thread_set_stack_size(main_thread, 4096);
furi_thread_set_callback(main_thread, init_task);
FuriThread* main_thread = furi_thread_alloc_ex("Init", 4096, init_task, NULL);

#ifdef FURI_RAM_EXEC
furi_thread_start(main_thread);
Expand Down
1 change: 1 addition & 0 deletions firmware/targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,7 @@ Function,+,furi_string_utf8_length,size_t,FuriString*
Function,+,furi_string_utf8_push,void,"FuriString*, FuriStringUnicodeValue"
Function,+,furi_string_vprintf,int,"FuriString*, const char[], va_list"
Function,+,furi_thread_alloc,FuriThread*,
Function,+,furi_thread_alloc_ex,FuriThread*,"const char*, uint32_t, FuriThreadCallback, void*"
Function,+,furi_thread_catch,void,
Function,-,furi_thread_disable_heap_trace,void,FuriThread*
Function,+,furi_thread_enable_heap_trace,void,FuriThread*
Expand Down
6 changes: 1 addition & 5 deletions firmware/targets/f7/ble_glue/ble_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ bool ble_app_init() {
ble_app->hci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
ble_app->hci_sem = furi_semaphore_alloc(1, 0);
// HCI transport layer thread to handle user asynch events
ble_app->thread = furi_thread_alloc();
furi_thread_set_name(ble_app->thread, "BleHciDriver");
furi_thread_set_stack_size(ble_app->thread, 1024);
furi_thread_set_context(ble_app->thread, ble_app);
furi_thread_set_callback(ble_app->thread, ble_app_hci_thread);
ble_app->thread = furi_thread_alloc_ex("BleHciDriver", 1024, ble_app_hci_thread, ble_app);
furi_thread_start(ble_app->thread);

// Initialize Ble Transport Layer
Expand Down
6 changes: 1 addition & 5 deletions firmware/targets/f7/ble_glue/ble_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ void ble_glue_init() {
ble_glue->shci_sem = furi_semaphore_alloc(1, 0);

// FreeRTOS system task creation
ble_glue->thread = furi_thread_alloc();
furi_thread_set_name(ble_glue->thread, "BleShciDriver");
furi_thread_set_stack_size(ble_glue->thread, 1024);
furi_thread_set_context(ble_glue->thread, ble_glue);
furi_thread_set_callback(ble_glue->thread, ble_glue_shci_thread);
ble_glue->thread = furi_thread_alloc_ex("BleShciDriver", 1024, ble_glue_shci_thread, ble_glue);
furi_thread_start(ble_glue->thread);

// System channel initialization
Expand Down
6 changes: 1 addition & 5 deletions firmware/targets/f7/ble_glue/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,7 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
gap->enable_adv = true;

// Thread configuration
gap->thread = furi_thread_alloc();
furi_thread_set_name(gap->thread, "BleGapDriver");
furi_thread_set_stack_size(gap->thread, 1024);
furi_thread_set_context(gap->thread, gap);
furi_thread_set_callback(gap->thread, gap_app);
gap->thread = furi_thread_alloc_ex("BleGapDriver", 1024, gap_app, gap);
furi_thread_start(gap->thread);

// Command queue allocation
Expand Down
17 changes: 8 additions & 9 deletions firmware/targets/f7/furi_hal/furi_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,25 @@ void furi_hal_init() {

furi_hal_crypto_init();

// USB
#ifndef FURI_RAM_EXEC
furi_hal_usb_init();
FURI_LOG_I(TAG, "USB OK");
#endif

furi_hal_i2c_init();

// High Level
furi_hal_power_init();
furi_hal_light_init();

furi_hal_bt_init();
furi_hal_memory_init();
furi_hal_compress_icon_init();

#ifndef FURI_RAM_EXEC
// USB
furi_hal_usb_init();
FURI_LOG_I(TAG, "USB OK");
furi_hal_vibro_init();
furi_hal_subghz_init();
furi_hal_nfc_init();
furi_hal_rfid_init();
#endif
furi_hal_bt_init();
furi_hal_memory_init();
furi_hal_compress_icon_init();

// FatFS driver initialization
MX_FATFS_Init();
Expand Down
6 changes: 2 additions & 4 deletions firmware/targets/f7/furi_hal/furi_hal_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ void furi_hal_usb_init(void) {
NVIC_EnableIRQ(USB_LP_IRQn);
NVIC_EnableIRQ(USB_HP_IRQn);

usb.thread = furi_thread_alloc();
furi_thread_set_name(usb.thread, "UsbDriver");
furi_thread_set_stack_size(usb.thread, 1024);
furi_thread_set_callback(usb.thread, furi_hal_usb_thread);
usb.thread = furi_thread_alloc_ex("UsbDriver", 1024, furi_hal_usb_thread, NULL);
furi_thread_mark_as_service(usb.thread);
furi_thread_start(usb.thread);

FURI_LOG_I(TAG, "Init OK");
Expand Down
13 changes: 13 additions & 0 deletions furi/core/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ FuriThread* furi_thread_alloc() {
return thread;
}

FuriThread* furi_thread_alloc_ex(
const char* name,
uint32_t stack_size,
FuriThreadCallback callback,
void* context) {
FuriThread* thread = furi_thread_alloc();
furi_thread_set_name(thread, name);
furi_thread_set_stack_size(thread, stack_size);
furi_thread_set_callback(thread, callback);
furi_thread_set_context(thread, context);
return thread;
}

void furi_thread_free(FuriThread* thread) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
Expand Down
Loading

0 comments on commit c511c67

Please sign in to comment.