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.
pull/6186/head
Tero Jääskö 2017-12-01 19:10:53 +02:00
parent 107976773c
commit aed2a0cd9f
4 changed files with 36 additions and 1 deletions

View File

@ -9,5 +9,10 @@
"help": "Define event-loop thread stack size.",
"value": 6144
}
,
"event-loop-dispatch-from-application": {
"help": "Application is responsible of message dispatch loop. Else launch a separate thread for event-loop.",
"value": false
}
}
}

View File

@ -14,6 +14,9 @@
#define TRACE_GROUP "evlp"
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
static void event_loop_thread(void *arg);
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 = {
.cb_mem = &event_thread_tcb,
.cb_size = sizeof event_thread_tcb,
};
#endif
static osThreadId_t event_thread_id;
static mbed_rtos_storage_mutex_t event_mutex;
static const osMutexAttr_t event_mutex_attr = {
@ -78,18 +83,32 @@ void eventOS_scheduler_idle(void)
eventOS_scheduler_mutex_wait();
}
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
static void event_loop_thread(void *arg)
{
(void)arg;
eventOS_scheduler_mutex_wait();
eventOS_scheduler_run(); //Does not return
}
#endif
void ns_event_loop_thread_create(void)
// This is used to initialize the lock used by event loop even
// if it is not ran in a separate thread.
void ns_event_loop_init(void)
{
event_mutex_id = osMutexNew(&event_mutex_attr);
MBED_ASSERT(event_mutex_id != NULL);
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
// The thread id of the current thread is stored so it can be used to signal
// the waiting thread from other threads or from a interrupt.
event_thread_id = osThreadGetId();
#endif
}
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
void ns_event_loop_thread_create(void)
{
event_thread_id = osThreadNew(event_loop_thread, NULL, &event_thread_attr);
MBED_ASSERT(event_thread_id != NULL);
}
@ -97,3 +116,4 @@ void ns_event_loop_thread_create(void)
void ns_event_loop_thread_start(void)
{
}
#endif

View File

@ -6,6 +6,7 @@
extern "C" {
#endif
void ns_event_loop_init(void);
void ns_event_loop_thread_create(void);
void ns_event_loop_thread_start(void);

View File

@ -32,10 +32,19 @@ void ns_hal_init(void *heap, size_t h_size, void (*passed_fptr)(heap_fail_t), me
ns_dyn_mem_init(heap, h_size, passed_fptr, info_ptr);
platform_timer_enable();
eventOS_scheduler_init();
// We do not initialise randlib, as it should be done after
// RF driver has started, to get MAC address and RF noise as seed.
// We do not initialise trace - left to application.
// Prepare the event loop lock which is used even if the loop
// is not ran in a separate thread.
ns_event_loop_init();
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
ns_event_loop_thread_create();
ns_event_loop_thread_start();
#endif
initted = true;
}