Skip to content

Commit 3c77ae2

Browse files
authored
Nfc: replace cmsis thread with furi, fix condition race in thread stop routine. (#1003)
* Nfc: replace cmsis thread with furi, fix condition race on thread stop routine. * Nfc: fix memory leak * FuriCore, CMSIS: properly handle thread names before scheduler start
1 parent ddd909f commit 3c77ae2

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

applications/nfc/nfc_worker.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99

1010
NfcWorker* nfc_worker_alloc() {
1111
NfcWorker* nfc_worker = malloc(sizeof(NfcWorker));
12+
1213
// Worker thread attributes
13-
nfc_worker->thread_attr.name = "NfcWorker";
14-
nfc_worker->thread_attr.stack_size = 8192;
14+
nfc_worker->thread = furi_thread_alloc();
15+
furi_thread_set_name(nfc_worker->thread, "NfcWorker");
16+
furi_thread_set_stack_size(nfc_worker->thread, 8192);
17+
furi_thread_set_callback(nfc_worker->thread, nfc_worker_task);
18+
furi_thread_set_context(nfc_worker->thread, nfc_worker);
19+
1520
nfc_worker->callback = NULL;
1621
nfc_worker->context = NULL;
22+
1723
// Initialize rfal
1824
while(furi_hal_nfc_is_busy()) {
1925
osDelay(10);
@@ -25,6 +31,7 @@ NfcWorker* nfc_worker_alloc() {
2531

2632
void nfc_worker_free(NfcWorker* nfc_worker) {
2733
furi_assert(nfc_worker);
34+
furi_thread_free(nfc_worker->thread);
2835
free(nfc_worker);
2936
}
3037

@@ -48,7 +55,7 @@ void nfc_worker_start(
4855
nfc_worker->context = context;
4956
nfc_worker->dev_data = dev_data;
5057
nfc_worker_change_state(nfc_worker, state);
51-
nfc_worker->thread = osThreadNew(nfc_worker_task, nfc_worker, &nfc_worker->thread_attr);
58+
furi_thread_start(nfc_worker->thread);
5259
}
5360

5461
void nfc_worker_stop(NfcWorker* nfc_worker) {
@@ -58,6 +65,7 @@ void nfc_worker_stop(NfcWorker* nfc_worker) {
5865
}
5966
furi_hal_nfc_stop();
6067
nfc_worker_change_state(nfc_worker, NfcWorkerStateStop);
68+
furi_thread_join(nfc_worker->thread);
6169
}
6270

6371
void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state) {
@@ -66,7 +74,7 @@ void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state) {
6674

6775
/***************************** NFC Worker Thread *******************************/
6876

69-
void nfc_worker_task(void* context) {
77+
int32_t nfc_worker_task(void* context) {
7078
NfcWorker* nfc_worker = context;
7179

7280
furi_hal_power_insomnia_enter();
@@ -92,7 +100,8 @@ void nfc_worker_task(void* context) {
92100
furi_hal_nfc_deactivate();
93101
nfc_worker_change_state(nfc_worker, NfcWorkerStateReady);
94102
furi_hal_power_insomnia_exit();
95-
osThreadExit();
103+
104+
return 0;
96105
}
97106

98107
void nfc_worker_detect(NfcWorker* nfc_worker) {

applications/nfc/nfc_worker_i.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#include <st25r3916_irq.h>
1818

1919
struct NfcWorker {
20-
osThreadAttr_t thread_attr;
21-
osThreadId_t thread;
20+
FuriThread* thread;
2221

2322
NfcDeviceData* dev_data;
2423

@@ -30,7 +29,7 @@ struct NfcWorker {
3029

3130
void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state);
3231

33-
void nfc_worker_task(void* context);
32+
int32_t nfc_worker_task(void* context);
3433

3534
void nfc_worker_read_emv_app(NfcWorker* nfc_worker);
3635

core/furi/memmgr_heap.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ static void print_heap_init() {
288288
static void print_heap_malloc(void* ptr, size_t size) {
289289
char tmp_str[33];
290290
const char* name = osThreadGetName(osThreadGetId());
291+
if(!name) {
292+
name = "";
293+
}
291294

292295
// {thread name|m|address|size}
293296
FURI_CRITICAL_ENTER();
@@ -306,6 +309,9 @@ static void print_heap_malloc(void* ptr, size_t size) {
306309
static void print_heap_free(void* ptr) {
307310
char tmp_str[33];
308311
const char* name = osThreadGetName(osThreadGetId());
312+
if(!name) {
313+
name = "";
314+
}
309315

310316
// {thread name|f|address}
311317
FURI_CRITICAL_ENTER();

lib/FreeRTOS-glue/cmsis_os2.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,10 @@ const char *osThreadGetName (osThreadId_t thread_id) {
578578

579579
if ((IRQ_Context() != 0U) || (hTask == NULL)) {
580580
name = NULL;
581-
} else {
581+
} else if(osKernelGetState() == osKernelRunning) {
582582
name = pcTaskGetName (hTask);
583+
} else {
584+
name = NULL;
583585
}
584586

585587
/* Return name as null-terminated string */

0 commit comments

Comments
 (0)