Skip to content

Commit 097a5d0

Browse files
TeroJaasko0xc0170
authored andcommitted
nanostack-hal: modify eventloop to allow running it in a main thread
The separate eventloop thread may not be necessary on all uses, as one can use the existing main thread for event dispatching. Add a conditional nanostack-hal.event-loop-dispatch-from-application, which disables the thread creation. Note: the ns_hal_init must be ran from the same thread which will be used to execute the event loop later.
1 parent 7f021e7 commit 097a5d0

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/mbed_lib.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@
1313
"help": "Make critical section API usable from interrupt context. Else a mutex is used as locking primitive.",
1414
"value": false
1515
}
16+
,
17+
"event-loop-dispatch-from-application": {
18+
"help": "Application is responsible of message dispatch loop. Else launch a separate thread for event-loop.",
19+
"value": false
20+
}
1621
}
1722
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
#define TRACE_GROUP "evlp"
1616

17+
18+
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
19+
1720
static void event_loop_thread(void *arg);
1821

1922
static uint64_t event_thread_stk[MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_THREAD_STACK_SIZE/8];
@@ -26,6 +29,8 @@ static const osThreadAttr_t event_thread_attr = {
2629
.cb_mem = &event_thread_tcb,
2730
.cb_size = sizeof event_thread_tcb,
2831
};
32+
#endif
33+
2934
static osThreadId_t event_thread_id;
3035
static mbed_rtos_storage_mutex_t event_mutex;
3136
static const osMutexAttr_t event_mutex_attr = {
@@ -78,22 +83,37 @@ void eventOS_scheduler_idle(void)
7883
eventOS_scheduler_mutex_wait();
7984
}
8085

86+
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
8187
static void event_loop_thread(void *arg)
8288
{
8389
(void)arg;
8490
eventOS_scheduler_mutex_wait();
8591
eventOS_scheduler_run(); //Does not return
8692
}
93+
#endif
8794

88-
void ns_event_loop_thread_create(void)
95+
// This is used to initialize the lock used by event loop even
96+
// if it is not ran in a separate thread.
97+
void ns_event_loop_init(void)
8998
{
9099
event_mutex_id = osMutexNew(&event_mutex_attr);
91100
MBED_ASSERT(event_mutex_id != NULL);
92101

102+
#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();
106+
#endif
107+
}
108+
109+
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
110+
void ns_event_loop_thread_create(void)
111+
{
93112
event_thread_id = osThreadNew(event_loop_thread, NULL, &event_thread_attr);
94113
MBED_ASSERT(event_thread_id != NULL);
95114
}
96115

97116
void ns_event_loop_thread_start(void)
98117
{
99118
}
119+
#endif

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
extern "C" {
77
#endif
88

9+
void ns_event_loop_init(void);
910
void ns_event_loop_thread_create(void);
1011
void ns_event_loop_thread_start(void);
1112

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@ void ns_hal_init(void *heap, size_t h_size, void (*passed_fptr)(heap_fail_t), me
3232
ns_dyn_mem_init(heap, h_size, passed_fptr, info_ptr);
3333
platform_timer_enable();
3434
eventOS_scheduler_init();
35+
3536
// We do not initialise randlib, as it should be done after
3637
// RF driver has started, to get MAC address and RF noise as seed.
3738
// We do not initialise trace - left to application.
39+
40+
// Prepare the event loop lock which is used even if the loop
41+
// is not ran in a separate thread.
42+
ns_event_loop_init();
43+
44+
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
3845
ns_event_loop_thread_create();
3946
ns_event_loop_thread_start();
47+
#endif
48+
4049
initted = true;
4150
}

0 commit comments

Comments
 (0)