Skip to content

Commit 09b8245

Browse files
committed
nanostack-hal: remove connection from event loop init and dispatch thread
The thread flag signaling mechanism is problematic if a separate event loop thread is not used. The problem is, that one needs to know the thread id of the loop dispathcer, and that knowledge can be derived only from the caller thread of the initialization function. Remove the magic connection from caller thread to dispatcher thread by using event flags instead of thread flags on signaling. As the event flags require the massive amount 20 bytes of RAM whereas thread flags requires none, keep the code behind flag.
1 parent aed2a0c commit 09b8245

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@
1515
#define TRACE_GROUP "evlp"
1616

1717

18-
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
18+
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
19+
20+
static mbed_rtos_storage_event_flags_t event_flag_cb;
21+
static const osEventFlagsAttr_t event_flags_attr = {
22+
.name = "nanostack_event_flags",
23+
.cb_mem = &event_flag_cb,
24+
.cb_size = sizeof event_flag_cb
25+
};
26+
static osEventFlagsId_t event_flag_id;
27+
28+
#else
1929

2030
static void event_loop_thread(void *arg);
2131

@@ -71,15 +81,25 @@ void eventOS_scheduler_signal(void)
7181
// XXX why does signal set lock if called with irqs disabled?
7282
//__enable_irq();
7383
//tr_debug("signal %p", (void*)event_thread_id);
84+
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
85+
osEventFlagsSet(event_flag_id, 1);
86+
#else
7487
osThreadFlagsSet(event_thread_id, 1);
88+
#endif
7589
//tr_debug("signalled %p", (void*)event_thread_id);
7690
}
7791

7892
void eventOS_scheduler_idle(void)
7993
{
8094
//tr_debug("idle");
8195
eventOS_scheduler_mutex_release();
96+
97+
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
98+
osEventFlagsWait(event_flag_id, 1, osFlagsWaitAny, osWaitForever);
99+
#else
82100
osThreadFlagsWait(1, 0, osWaitForever);
101+
#endif
102+
83103
eventOS_scheduler_mutex_wait();
84104
}
85105

@@ -99,10 +119,13 @@ void ns_event_loop_init(void)
99119
event_mutex_id = osMutexNew(&event_mutex_attr);
100120
MBED_ASSERT(event_mutex_id != NULL);
101121

122+
// If a separate event loop thread is not used, the signaling
123+
// happens via event flags instead of thread flags. This allows one to
124+
// perform the initialization from any thread and removes need to know the id
125+
// of event loop dispatch thread.
102126
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
103-
// The thread id of the current thread is stored so it can be used to signal
104-
// the waiting thread from other threads or from a interrupt.
105-
event_thread_id = osThreadGetId();
127+
event_flag_id = osEventFlagsNew(&event_flags_attr);
128+
MBED_ASSERT(event_flag_id != NULL);
106129
#endif
107130
}
108131

0 commit comments

Comments
 (0)