flipperzero-firmware/core/furi/record.c
SG 274c12fc56
[FL-2274] Inventing streams and moving FFF to them (#981)
* 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>
2022-02-18 22:53:46 +03:00

159 lines
3.8 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_READY (0x1)
typedef struct {
osEventFlagsId_t flags;
void* data;
size_t holders_count;
} FuriRecordData;
DICT_DEF2(FuriRecordDataDict, string_t, STRING_OPLIST, FuriRecordData, M_POD_OPLIST)
typedef struct {
osMutexId_t mutex;
FuriRecordDataDict_t records;
} FuriRecord;
static FuriRecord* furi_record = NULL;
void furi_record_init() {
furi_record = malloc(sizeof(FuriRecord));
furi_record->mutex = osMutexNew(NULL);
furi_check(furi_record->mutex);
FuriRecordDataDict_init(furi_record->records);
}
static FuriRecordData* furi_record_data_get_or_create(string_t name_str) {
furi_assert(furi_record);
FuriRecordData* record_data = FuriRecordDataDict_get(furi_record->records, name_str);
if(!record_data) {
FuriRecordData new_record;
new_record.flags = osEventFlagsNew(NULL);
new_record.data = NULL;
new_record.holders_count = 0;
FuriRecordDataDict_set_at(furi_record->records, name_str, new_record);
record_data = FuriRecordDataDict_get(furi_record->records, name_str);
}
return record_data;
}
static void furi_record_lock() {
furi_check(osMutexAcquire(furi_record->mutex, osWaitForever) == osOK);
}
static void furi_record_unlock() {
furi_check(osMutexRelease(furi_record->mutex) == osOK);
}
bool furi_record_exists(const char* name) {
furi_assert(furi_record);
furi_assert(name);
bool ret = false;
string_t name_str;
string_init_set_str(name_str, name);
furi_record_lock();
ret = (FuriRecordDataDict_get(furi_record->records, name_str) != NULL);
furi_record_unlock();
string_clear(name_str);
return ret;
}
void furi_record_create(const char* name, void* data) {
furi_assert(furi_record);
string_t name_str;
string_init_set_str(name_str, name);
furi_record_lock();
// Get record data and fill it
FuriRecordData* record_data = furi_record_data_get_or_create(name_str);
furi_assert(record_data->data == NULL);
record_data->data = data;
osEventFlagsSet(record_data->flags, FURI_RECORD_FLAG_READY);
furi_record_unlock();
string_clear(name_str);
}
bool furi_record_destroy(const char* name) {
furi_assert(furi_record);
bool ret = false;
string_t name_str;
string_init_set_str(name_str, name);
furi_record_lock();
FuriRecordData* record_data = FuriRecordDataDict_get(furi_record->records, name_str);
furi_assert(record_data);
if(record_data->holders_count == 0) {
furi_check(osOK == osEventFlagsDelete(record_data->flags));
FuriRecordDataDict_erase(furi_record->records, name_str);
ret = true;
}
furi_record_unlock();
string_clear(name_str);
return ret;
}
void* furi_record_open(const char* name) {
furi_assert(furi_record);
string_t name_str;
string_init_set_str(name_str, name);
furi_record_lock();
FuriRecordData* record_data = furi_record_data_get_or_create(name_str);
record_data->holders_count++;
furi_record_unlock();
// Wait for record to become ready
furi_check(
osEventFlagsWait(
record_data->flags,
FURI_RECORD_FLAG_READY,
osFlagsWaitAny | osFlagsNoClear,
osWaitForever) == FURI_RECORD_FLAG_READY);
string_clear(name_str);
return record_data->data;
}
void furi_record_close(const char* name) {
furi_assert(furi_record);
string_t name_str;
string_init_set_str(name_str, name);
furi_record_lock();
FuriRecordData* record_data = FuriRecordDataDict_get(furi_record->records, name_str);
furi_assert(record_data);
record_data->holders_count--;
furi_record_unlock();
string_clear(name_str);
}