unleashed-firmware/core/furi/record.c
あく 584c0962d8
[FL-781] FURI, CLI, stdlib: stdout hooks, integration between subsystems, uniform printf usage (#311)
* FURI stdglue: stdout hooks, local and global, ISR safe printf. Uniform newlines for terminal/debug output. Power: prevent sleep while core 2 has not started.
* Furi record, stdglue: check mutex allocation
* remove unused test
* Furi stdglue: buferized output, dynamically allocated state. Furi record: dynamically allocated state. Input dump: proper line ending. Hal VCP: dynamically allocated state.
* Interrupt manager: explicitly init list.
* Makefile: cleanup rules, fix broken dfu upload. F4: add compiler stack protection options.
* BLE: call debug uart callback on transmission complete
* FreeRTOS: add configUSE_NEWLIB_REENTRANT
* API HAL Timebase: fix issue with idle thread stack corruption caused by systick interrupt. BT: cleanup debug info output. FreeRTOS: disable reentry for newlib.
* F4: update stack protection CFLAGS to match used compiller
* F4: disable compiller stack protection because of incompatibility with current compiller
* Makefile: return openocd logs to gdb
* BLE: fixed pin, moar power, ble trace info.
* Prevent sleep when connection is active
* Makefile: return serial port to upload rule, add workaround for mac os
* Furi: prevent usage of stack for cmsis functions.
* F4: add missing includes, add debugger breakpoints
* Applications: per app stack size.
* Furi: honor kernel state in stdglue
* FreeRTOS: remove unused hooks
* Cleanup and format sources

Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
2021-01-29 03:09:33 +03:00

132 lines
4.1 KiB
C

#include "record.h"
#include "check.h"
#include "memmgr.h"
#include <cmsis_os2.h>
#include <m-string.h>
#include <m-dict.h>
#define FURI_RECORD_FLAG_UPDATED 0x00000001U
DICT_SET_DEF(osThreadIdSet, uint32_t)
typedef struct {
void* data;
osThreadId_t owner;
osThreadIdSet_t holders;
} FuriRecord;
DICT_DEF2(FuriRecordDict, string_t, STRING_OPLIST, FuriRecord, M_POD_OPLIST)
typedef struct {
osMutexId_t records_mutex;
FuriRecordDict_t records;
} FuriRecordData;
static FuriRecordData* furi_record_data = NULL;
void furi_record_init() {
furi_record_data = furi_alloc(sizeof(FuriRecordData));
furi_record_data->records_mutex = osMutexNew(NULL);
furi_check(furi_record_data->records_mutex);
FuriRecordDict_init(furi_record_data->records);
}
FuriRecord* furi_record_get_or_create(string_t name_str) {
assert(furi_record_data);
FuriRecord* record = FuriRecordDict_get(furi_record_data->records, name_str);
if(!record) {
FuriRecord new_record;
new_record.data = NULL;
new_record.owner = NULL;
osThreadIdSet_init(new_record.holders);
FuriRecordDict_set_at(furi_record_data->records, name_str, new_record);
record = FuriRecordDict_get(furi_record_data->records, name_str);
}
return record;
}
void furi_record_create(const char* name, void* data) {
assert(furi_record_data);
osThreadId_t thread_id = osThreadGetId();
string_t name_str;
string_init_set_str(name_str, name);
// Acquire mutex
furi_check(osMutexAcquire(furi_record_data->records_mutex, osWaitForever) == osOK);
FuriRecord* record = furi_record_get_or_create(name_str);
record->data = data;
record->owner = thread_id;
// For each holder set event flag
osThreadIdSet_it_t it;
for(osThreadIdSet_it(it, record->holders); !osThreadIdSet_end_p(it); osThreadIdSet_next(it)) {
osThreadFlagsSet((osThreadId_t)*osThreadIdSet_ref(it), FURI_RECORD_FLAG_UPDATED);
}
// Release mutex
furi_check(osMutexRelease(furi_record_data->records_mutex) == osOK);
string_clear(name_str);
}
bool furi_record_destroy(const char* name) {
assert(furi_record_data);
osThreadId_t thread_id = osThreadGetId();
string_t name_str;
string_init_set_str(name_str, name);
bool destroyed = false;
furi_check(osMutexAcquire(furi_record_data->records_mutex, osWaitForever) == osOK);
FuriRecord* record = FuriRecordDict_get(furi_record_data->records, name_str);
if(record && record->owner == thread_id && osThreadIdSet_size(record->holders) == 0) {
osThreadIdSet_clear(record->holders);
FuriRecordDict_erase(furi_record_data->records, name_str);
destroyed = true;
}
furi_check(osMutexRelease(furi_record_data->records_mutex) == osOK);
string_clear(name_str);
return destroyed;
}
void* furi_record_open(const char* name) {
assert(furi_record_data);
osThreadId_t thread_id = osThreadGetId();
string_t name_str;
string_init_set_str(name_str, name);
FuriRecord* record = NULL;
while(1) {
furi_check(osMutexAcquire(furi_record_data->records_mutex, osWaitForever) == osOK);
record = furi_record_get_or_create(name_str);
osThreadIdSet_push(record->holders, (uint32_t)thread_id);
furi_check(osMutexRelease(furi_record_data->records_mutex) == osOK);
// Check if owner is already arrived
if(record->owner) {
break;
}
// Wait for thread flag to appear
osThreadFlagsWait(FURI_RECORD_FLAG_UPDATED, osFlagsWaitAny, osWaitForever);
}
string_clear(name_str);
return record->data;
}
void furi_record_close(const char* name) {
assert(furi_record_data);
osThreadId_t thread_id = osThreadGetId();
string_t name_str;
string_init_set_str(name_str, name);
furi_check(osMutexAcquire(furi_record_data->records_mutex, osWaitForever) == osOK);
FuriRecord* record = FuriRecordDict_get(furi_record_data->records, name_str);
osThreadIdSet_erase(record->holders, (uint32_t)thread_id);
furi_check(osMutexRelease(furi_record_data->records_mutex) == osOK);
string_clear(name_str);
}