flipperzero-firmware/core/furi/record.c
DrZlo13 8f9b2513ff
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 19:09:26 +03:00

124 lines
3.8 KiB
C

#include "record.h"
#include "check.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;
FuriRecordData furi_record_data;
void furi_record_init() {
furi_record_data.records_mutex = osMutexNew(NULL);
FuriRecordDict_init(furi_record_data.records);
}
FuriRecord* furi_record_get_or_create(string_t name_str) {
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) {
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) {
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);
}
furi_check(osMutexRelease(furi_record_data.records_mutex) == osOK);
string_clear(name_str);
return destroyed;
}
void* furi_record_open(const char* name) {
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) {
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);
}