Furi: refactor records (#736)

Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
This commit is contained in:
あく 2021-10-02 20:05:43 +03:00 committed by GitHub
parent 61aaed8abb
commit 05b610f265
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 84 deletions

View file

@ -6,127 +6,134 @@
#include <m-string.h> #include <m-string.h>
#include <m-dict.h> #include <m-dict.h>
#define FURI_RECORD_FLAG_UPDATED 0x00000001U #define FURI_RECORD_FLAG_READY (0x1)
DICT_SET_DEF(osThreadIdSet, uint32_t)
typedef struct { typedef struct {
osEventFlagsId_t flags;
void* data; void* data;
osThreadId_t owner; size_t holders_count;
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;
static FuriRecordData* furi_record_data = NULL; 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() { void furi_record_init() {
furi_record_data = furi_alloc(sizeof(FuriRecordData)); furi_record = furi_alloc(sizeof(FuriRecord));
furi_record_data->records_mutex = osMutexNew(NULL); furi_record->mutex = osMutexNew(NULL);
furi_check(furi_record_data->records_mutex); furi_check(furi_record->mutex);
FuriRecordDict_init(furi_record_data->records); FuriRecordDataDict_init(furi_record->records);
} }
FuriRecord* furi_record_get_or_create(string_t name_str) { static FuriRecordData* furi_record_data_get_or_create(string_t name_str) {
furi_assert(furi_record_data); furi_assert(furi_record);
FuriRecord* record = FuriRecordDict_get(furi_record_data->records, name_str); FuriRecordData* record_data = FuriRecordDataDict_get(furi_record->records, name_str);
if(!record) { if(!record_data) {
FuriRecord new_record; FuriRecordData new_record;
new_record.flags = osEventFlagsNew(NULL);
new_record.data = NULL; new_record.data = NULL;
new_record.owner = NULL; new_record.holders_count = 0;
osThreadIdSet_init(new_record.holders); FuriRecordDataDict_set_at(furi_record->records, name_str, new_record);
FuriRecordDict_set_at(furi_record_data->records, name_str, new_record); record_data = FuriRecordDataDict_get(furi_record->records, name_str);
record = FuriRecordDict_get(furi_record_data->records, name_str);
} }
return record; 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);
} }
void furi_record_create(const char* name, void* data) { void furi_record_create(const char* name, void* data) {
furi_assert(furi_record_data); furi_assert(furi_record);
osThreadId_t thread_id = osThreadGetId();
string_t name_str; string_t name_str;
string_init_set_str(name_str, name); string_init_set_str(name_str, name);
// Acquire mutex furi_record_lock();
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 // Get record data and fill it
osThreadIdSet_it_t it; FuriRecordData* record_data = furi_record_data_get_or_create(name_str);
for(osThreadIdSet_it(it, record->holders); !osThreadIdSet_end_p(it); osThreadIdSet_next(it)) { furi_assert(record_data->data == NULL);
osThreadFlagsSet((osThreadId_t)*osThreadIdSet_ref(it), FURI_RECORD_FLAG_UPDATED); record_data->data = data;
} osEventFlagsSet(record_data->flags, FURI_RECORD_FLAG_READY);
// Release mutex
furi_check(osMutexRelease(furi_record_data->records_mutex) == osOK); furi_record_unlock();
string_clear(name_str); string_clear(name_str);
} }
bool furi_record_destroy(const char* name) { bool furi_record_destroy(const char* name) {
furi_assert(furi_record_data); furi_assert(furi_record);
osThreadId_t thread_id = osThreadGetId();
bool ret = false;
string_t name_str; string_t name_str;
string_init_set_str(name_str, name); string_init_set_str(name_str, name);
bool destroyed = false; furi_record_lock();
furi_check(osMutexAcquire(furi_record_data->records_mutex, osWaitForever) == osOK);
FuriRecord* record = FuriRecordDict_get(furi_record_data->records, name_str); FuriRecordData* record_data = FuriRecordDataDict_get(furi_record->records, name_str);
if(record && record->owner == thread_id && osThreadIdSet_size(record->holders) == 0) { furi_assert(record_data);
osThreadIdSet_clear(record->holders); if(record_data->holders_count == 0) {
FuriRecordDict_erase(furi_record_data->records, name_str); FuriRecordDataDict_erase(furi_record->records, name_str);
destroyed = true; ret = true;
} }
furi_check(osMutexRelease(furi_record_data->records_mutex) == osOK);
furi_record_unlock();
string_clear(name_str); string_clear(name_str);
return destroyed;
return ret;
} }
void* furi_record_open(const char* name) { void* furi_record_open(const char* name) {
furi_assert(furi_record_data); furi_assert(furi_record);
osThreadId_t thread_id = osThreadGetId();
string_t name_str; string_t name_str;
string_init_set_str(name_str, name); string_init_set_str(name_str, name);
FuriRecord* record = NULL; furi_record_lock();
while(1) {
furi_check(osMutexAcquire(furi_record_data->records_mutex, osWaitForever) == osOK); FuriRecordData* record_data = furi_record_data_get_or_create(name_str);
record = furi_record_get_or_create(name_str); record_data->holders_count++;
osThreadIdSet_push(record->holders, (uint32_t)thread_id);
furi_check(osMutexRelease(furi_record_data->records_mutex) == osOK); furi_record_unlock();
// Check if owner is already arrived
if(record->owner) { // Wait for record to become ready
break; furi_check(
} osEventFlagsWait(
// Wait for thread flag to appear record_data->flags,
osThreadFlagsWait(FURI_RECORD_FLAG_UPDATED, osFlagsWaitAny, osWaitForever); FURI_RECORD_FLAG_READY,
} osFlagsWaitAny | osFlagsNoClear,
osWaitForever) == FURI_RECORD_FLAG_READY);
string_clear(name_str); string_clear(name_str);
return record->data;
return record_data->data;
} }
void furi_record_close(const char* name) { void furi_record_close(const char* name) {
furi_assert(furi_record_data); furi_assert(furi_record);
osThreadId_t thread_id = osThreadGetId();
string_t name_str; string_t name_str;
string_init_set_str(name_str, name); string_init_set_str(name_str, name);
furi_check(osMutexAcquire(furi_record_data->records_mutex, osWaitForever) == osOK); furi_record_lock();
FuriRecord* record = FuriRecordDict_get(furi_record_data->records, name_str);
osThreadIdSet_erase(record->holders, (uint32_t)thread_id); FuriRecordData* record_data = FuriRecordDataDict_get(furi_record->records, name_str);
furi_check(osMutexRelease(furi_record_data->records_mutex) == osOK); furi_assert(record_data);
record_data->holders_count--;
furi_record_unlock();
string_clear(name_str); string_clear(name_str);
} }

View file

@ -6,30 +6,26 @@
extern "C" { extern "C" {
#endif #endif
/** /** Initialize record storage
* Initialize record storage
* For internal use only. * For internal use only.
*/ */
void furi_record_init(); void furi_record_init();
/** /** Create record
* Create record
* @param name - record name * @param name - record name
* @param data - data pointer * @param data - data pointer
* @note Thread safe. Create and destroy must be executed from the same thread. * @note Thread safe. Create and destroy must be executed from the same thread.
*/ */
void furi_record_create(const char* name, void* data); void furi_record_create(const char* name, void* data);
/** /** Destroy record
* Destroy record
* @param name - record name * @param name - record name
* @return true if successful, false if still have holders or thread is not owner. * @return true if successful, false if still have holders or thread is not owner.
* @note Thread safe. Create and destroy must be executed from the same thread. * @note Thread safe. Create and destroy must be executed from the same thread.
*/ */
bool furi_record_destroy(const char* name); bool furi_record_destroy(const char* name);
/** /** Open record
* Open record
* @param name - record name * @param name - record name
* @return pointer to the record * @return pointer to the record
* @note Thread safe. Open and close must be executed from the same thread. * @note Thread safe. Open and close must be executed from the same thread.
@ -37,8 +33,7 @@ bool furi_record_destroy(const char* name);
*/ */
void* furi_record_open(const char* name); void* furi_record_open(const char* name);
/** /** Close record
* Close record
* @param name - record name * @param name - record name
* @note Thread safe. Open and close must be executed from the same thread. * @note Thread safe. Open and close must be executed from the same thread.
*/ */