mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-12-18 00:33:09 +00:00
274c12fc56
* Streams: string stream * String stream: updated insert/delete api * Streams: generic stream interface and string stream implementation * Streams: helpers for insert and delete_and_insert * FFF: now compatible with streams * MinUnit: introduced tests with arguments * FFF: stream access violation * Streams: copy data between streams * Streams: file stream * FFF: documentation * FFStream: documentation * FFF: alloc as file * MinUnit: support for nested tests * Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout. * FFF: simplified file open function * Streams: unit tests * FFF: tests * Streams: declare cache_size constant as define, to allow variable modified arrays * FFF: lib moved to a separate folder * iButton: new FFF * RFID: new FFF * Animations: new FFF * IR: new FFF * NFC: new FFF * Flipper file format: delete lib * U2F: new FFF * Subghz: new FFF and streams * Streams: read line * Streams: split * FuriCore: implement memset with extra asserts * FuriCore: implement extra heap asserts without inventing memset * Scene manager: protected access to the scene id stack with a size check * NFC worker: dirty fix for issue where hal_nfc was busy on app start * Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc. * FuriCore: cleanup memmgr code. * Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console. * Memmgr: added ability to track allocations and deallocations through console. * FFStream: some speedup * Streams, FF: minor fixes * Tests: restore * File stream: a slightly more thread-safe version of file_stream_delete_and_insert Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
169 lines
4.6 KiB
C
169 lines
4.6 KiB
C
#include "thread.h"
|
|
#include "memmgr.h"
|
|
#include "memmgr_heap.h"
|
|
#include "check.h"
|
|
|
|
#include <m-string.h>
|
|
|
|
struct FuriThread {
|
|
FuriThreadState state;
|
|
int32_t ret;
|
|
|
|
FuriThreadCallback callback;
|
|
void* context;
|
|
|
|
FuriThreadStateCallback state_callback;
|
|
void* state_context;
|
|
|
|
osThreadAttr_t attr;
|
|
volatile osThreadId_t id;
|
|
|
|
bool heap_trace_enabled;
|
|
size_t heap_size;
|
|
};
|
|
|
|
void furi_thread_set_state(FuriThread* thread, FuriThreadState state) {
|
|
furi_assert(thread);
|
|
thread->state = state;
|
|
if(thread->state_callback) {
|
|
thread->state_callback(state, thread->state_context);
|
|
}
|
|
}
|
|
|
|
void furi_thread_body(void* context) {
|
|
furi_assert(context);
|
|
FuriThread* thread = context;
|
|
|
|
furi_assert(thread->state == FuriThreadStateStarting);
|
|
furi_thread_set_state(thread, FuriThreadStateRunning);
|
|
|
|
osThreadId_t thread_id = osThreadGetId();
|
|
if(thread->heap_trace_enabled == true) {
|
|
memmgr_heap_enable_thread_trace(thread_id);
|
|
}
|
|
|
|
thread->ret = thread->callback(thread->context);
|
|
|
|
if(thread->heap_trace_enabled == true) {
|
|
thread->heap_size = memmgr_heap_get_thread_memory(thread_id);
|
|
memmgr_heap_disable_thread_trace(thread_id);
|
|
}
|
|
|
|
furi_assert(thread->state == FuriThreadStateRunning);
|
|
furi_thread_set_state(thread, FuriThreadStateStopped);
|
|
|
|
osThreadExit();
|
|
}
|
|
|
|
FuriThread* furi_thread_alloc() {
|
|
FuriThread* thread = malloc(sizeof(FuriThread));
|
|
|
|
return thread;
|
|
}
|
|
|
|
void furi_thread_free(FuriThread* thread) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
|
|
if(thread->attr.name) free((void*)thread->attr.name);
|
|
free(thread);
|
|
}
|
|
|
|
void furi_thread_set_name(FuriThread* thread, const char* name) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
if(thread->attr.name) free((void*)thread->attr.name);
|
|
thread->attr.name = strdup(name);
|
|
}
|
|
|
|
void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
thread->attr.stack_size = stack_size;
|
|
}
|
|
|
|
void furi_thread_set_callback(FuriThread* thread, FuriThreadCallback callback) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
thread->callback = callback;
|
|
}
|
|
|
|
void furi_thread_set_context(FuriThread* thread, void* context) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
thread->context = context;
|
|
}
|
|
|
|
void furi_thread_set_state_callback(FuriThread* thread, FuriThreadStateCallback callback) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
thread->state_callback = callback;
|
|
}
|
|
|
|
void furi_thread_set_state_context(FuriThread* thread, void* context) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
thread->state_context = context;
|
|
}
|
|
|
|
FuriThreadState furi_thread_get_state(FuriThread* thread) {
|
|
furi_assert(thread);
|
|
return thread->state;
|
|
}
|
|
|
|
bool furi_thread_start(FuriThread* thread) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->callback);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
furi_assert(thread->attr.stack_size > 0);
|
|
furi_thread_set_state(thread, FuriThreadStateStarting);
|
|
thread->id = osThreadNew(furi_thread_body, thread, &thread->attr);
|
|
if(thread->id) {
|
|
return true;
|
|
} else {
|
|
furi_assert(thread->state == FuriThreadStateStarting);
|
|
furi_thread_set_state(thread, FuriThreadStateStopped);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
osStatus_t furi_thread_terminate(FuriThread* thread) {
|
|
furi_assert(thread);
|
|
osStatus_t ret = osThreadTerminate(thread->id);
|
|
if(ret == osOK) {
|
|
furi_thread_set_state(thread, FuriThreadStateStopped);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
osStatus_t furi_thread_join(FuriThread* thread) {
|
|
furi_assert(thread);
|
|
while(thread->state != FuriThreadStateStopped) {
|
|
osDelay(10);
|
|
}
|
|
return osOK;
|
|
}
|
|
|
|
osThreadId_t furi_thread_get_thread_id(FuriThread* thread) {
|
|
return thread->id;
|
|
}
|
|
|
|
void furi_thread_enable_heap_trace(FuriThread* thread) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
furi_assert(thread->heap_trace_enabled == false);
|
|
thread->heap_trace_enabled = true;
|
|
}
|
|
|
|
void furi_thread_disable_heap_trace(FuriThread* thread) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->state == FuriThreadStateStopped);
|
|
furi_assert(thread->heap_trace_enabled == true);
|
|
thread->heap_trace_enabled = false;
|
|
}
|
|
|
|
size_t furi_thread_get_heap_size(FuriThread* thread) {
|
|
furi_assert(thread);
|
|
furi_assert(thread->heap_trace_enabled == true);
|
|
return thread->heap_size;
|
|
}
|