Skip to content

Commit 2ff9e34

Browse files
committed
Merge remote-tracking branch 'OFW/dev' into dev
2 parents 9f1eca2 + 4895ae5 commit 2ff9e34

File tree

4 files changed

+72
-16
lines changed

4 files changed

+72
-16
lines changed

applications/debug/unit_tests/tests/furi/furi_event_loop_test.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,55 @@ static int32_t test_furi_event_loop_consumer(void* p) {
446446
return 0;
447447
}
448448

449+
typedef struct {
450+
FuriEventLoop* event_loop;
451+
FuriSemaphore* semaphore;
452+
size_t counter;
453+
} SelfUnsubTestTimerContext;
454+
455+
static void test_self_unsub_semaphore_callback(FuriEventLoopObject* object, void* context) {
456+
furi_event_loop_unsubscribe(context, object); // shouldn't crash here
457+
}
458+
459+
static void test_self_unsub_timer_callback(void* arg) {
460+
SelfUnsubTestTimerContext* context = arg;
461+
462+
if(context->counter == 0) {
463+
furi_semaphore_release(context->semaphore);
464+
} else if(context->counter == 1) {
465+
furi_event_loop_stop(context->event_loop);
466+
}
467+
468+
context->counter++;
469+
}
470+
471+
void test_furi_event_loop_self_unsubscribe(void) {
472+
FuriEventLoop* event_loop = furi_event_loop_alloc();
473+
474+
FuriSemaphore* semaphore = furi_semaphore_alloc(1, 0);
475+
furi_event_loop_subscribe_semaphore(
476+
event_loop,
477+
semaphore,
478+
FuriEventLoopEventIn,
479+
test_self_unsub_semaphore_callback,
480+
event_loop);
481+
482+
SelfUnsubTestTimerContext timer_context = {
483+
.event_loop = event_loop,
484+
.semaphore = semaphore,
485+
.counter = 0,
486+
};
487+
FuriEventLoopTimer* timer = furi_event_loop_timer_alloc(
488+
event_loop, test_self_unsub_timer_callback, FuriEventLoopTimerTypePeriodic, &timer_context);
489+
furi_event_loop_timer_start(timer, furi_ms_to_ticks(20));
490+
491+
furi_event_loop_run(event_loop);
492+
493+
furi_event_loop_timer_free(timer);
494+
furi_semaphore_free(semaphore);
495+
furi_event_loop_free(event_loop);
496+
}
497+
449498
void test_furi_event_loop(void) {
450499
TestFuriEventLoopData data = {};
451500

applications/debug/unit_tests/tests/furi/furi_test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ void test_furi_concurrent_access(void);
88
void test_furi_pubsub(void);
99
void test_furi_memmgr(void);
1010
void test_furi_event_loop(void);
11+
void test_furi_event_loop_self_unsubscribe(void);
1112
void test_errno_saving(void);
1213
void test_furi_primitives(void);
1314
void test_stdin(void);
@@ -46,6 +47,10 @@ MU_TEST(mu_test_furi_event_loop) {
4647
test_furi_event_loop();
4748
}
4849

50+
MU_TEST(mu_test_furi_event_loop_self_unsubscribe) {
51+
test_furi_event_loop_self_unsubscribe();
52+
}
53+
4954
MU_TEST(mu_test_errno_saving) {
5055
test_errno_saving();
5156
}
@@ -68,6 +73,7 @@ MU_TEST_SUITE(test_suite) {
6873
MU_RUN_TEST(mu_test_furi_pubsub);
6974
MU_RUN_TEST(mu_test_furi_memmgr);
7075
MU_RUN_TEST(mu_test_furi_event_loop);
76+
MU_RUN_TEST(mu_test_furi_event_loop_self_unsubscribe);
7177
MU_RUN_TEST(mu_test_stdio);
7278
MU_RUN_TEST(mu_test_errno_saving);
7379
MU_RUN_TEST(mu_test_furi_primitives);

furi/core/event_loop.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ static void furi_event_loop_item_notify(FuriEventLoopItem* instance);
3232

3333
static bool furi_event_loop_item_is_waiting(FuriEventLoopItem* instance);
3434

35-
static void furi_event_loop_process_pending_callbacks(FuriEventLoop* instance) {
36-
for(; !PendingQueue_empty_p(instance->pending_queue);
37-
PendingQueue_pop_back(NULL, instance->pending_queue)) {
38-
const FuriEventLoopPendingQueueItem* item = PendingQueue_back(instance->pending_queue);
39-
item->callback(item->context);
40-
}
41-
}
42-
4335
static bool furi_event_loop_signal_callback(uint32_t signal, void* arg, void* context) {
4436
furi_assert(context);
4537
FuriEventLoop* instance = context;
@@ -130,12 +122,16 @@ static inline FuriEventLoopProcessStatus
130122
furi_event_loop_unsubscribe(instance, item->object);
131123
}
132124

125+
instance->current_item = item;
126+
133127
if(item->event & FuriEventLoopEventFlagEdge) {
134128
status = furi_event_loop_process_edge_event(item);
135129
} else {
136130
status = furi_event_loop_process_level_event(item);
137131
}
138132

133+
instance->current_item = NULL;
134+
139135
if(item->owner == NULL) {
140136
status = FuriEventLoopProcessStatusFreeLater;
141137
}
@@ -193,6 +189,14 @@ static void furi_event_loop_process_waiting_list(FuriEventLoop* instance) {
193189
furi_event_loop_sync_flags(instance);
194190
}
195191

192+
static void furi_event_loop_process_pending_callbacks(FuriEventLoop* instance) {
193+
for(; !PendingQueue_empty_p(instance->pending_queue);
194+
PendingQueue_pop_back(NULL, instance->pending_queue)) {
195+
const FuriEventLoopPendingQueueItem* item = PendingQueue_back(instance->pending_queue);
196+
item->callback(item->context);
197+
}
198+
}
199+
196200
static void furi_event_loop_restore_flags(FuriEventLoop* instance, uint32_t flags) {
197201
if(flags) {
198202
xTaskNotifyIndexed(
@@ -203,7 +207,6 @@ static void furi_event_loop_restore_flags(FuriEventLoop* instance, uint32_t flag
203207
void furi_event_loop_run(FuriEventLoop* instance) {
204208
furi_check(instance);
205209
furi_check(instance->thread_id == furi_thread_get_current_id());
206-
207210
FuriThread* thread = furi_thread_get_current();
208211

209212
// Set the default signal callback if none was previously set
@@ -213,9 +216,9 @@ void furi_event_loop_run(FuriEventLoop* instance) {
213216

214217
furi_event_loop_init_tick(instance);
215218

216-
while(true) {
217-
instance->state = FuriEventLoopStateIdle;
219+
instance->state = FuriEventLoopStateRunning;
218220

221+
while(true) {
219222
const TickType_t ticks_to_sleep =
220223
MIN(furi_event_loop_get_timer_wait_time(instance),
221224
furi_event_loop_get_tick_wait_time(instance));
@@ -224,8 +227,6 @@ void furi_event_loop_run(FuriEventLoop* instance) {
224227
BaseType_t ret = xTaskNotifyWaitIndexed(
225228
FURI_EVENT_LOOP_FLAG_NOTIFY_INDEX, 0, FuriEventLoopFlagAll, &flags, ticks_to_sleep);
226229

227-
instance->state = FuriEventLoopStateProcessing;
228-
229230
if(ret == pdTRUE) {
230231
if(flags & FuriEventLoopFlagStop) {
231232
instance->state = FuriEventLoopStateStopped;
@@ -448,7 +449,7 @@ void furi_event_loop_unsubscribe(FuriEventLoop* instance, FuriEventLoopObject* o
448449
WaitingList_unlink(item);
449450
}
450451

451-
if(instance->state == FuriEventLoopStateProcessing) {
452+
if(instance->current_item == item) {
452453
furi_event_loop_item_free_later(item);
453454
} else {
454455
furi_event_loop_item_free(item);

furi/core/event_loop_i.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ typedef enum {
6464

6565
typedef enum {
6666
FuriEventLoopStateStopped,
67-
FuriEventLoopStateIdle,
68-
FuriEventLoopStateProcessing,
67+
FuriEventLoopStateRunning,
6968
} FuriEventLoopState;
7069

7170
typedef struct {
@@ -81,6 +80,7 @@ struct FuriEventLoop {
8180

8281
// Poller state
8382
volatile FuriEventLoopState state;
83+
volatile FuriEventLoopItem* current_item;
8484

8585
// Event handling
8686
FuriEventLoopTree_t tree;

0 commit comments

Comments
 (0)