Merge pull request #6186 from TeroJaasko/eventloop_in_main_thread_to_master

Eventloop in main thread to master
pull/6169/head
Kevin Bracey 2018-02-27 14:01:40 +02:00 committed by GitHub
commit d24c7c454b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 1 deletions

View File

@ -9,5 +9,10 @@
"help": "Define event-loop thread stack size.", "help": "Define event-loop thread stack size.",
"value": 6144 "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,19 @@
#define TRACE_GROUP "evlp" #define TRACE_GROUP "evlp"
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
static mbed_rtos_storage_event_flags_t event_flag_cb;
static const osEventFlagsAttr_t event_flags_attr = {
.name = "nanostack_event_flags",
.cb_mem = &event_flag_cb,
.cb_size = sizeof event_flag_cb
};
static osEventFlagsId_t event_flag_id;
#else
static void event_loop_thread(void *arg); static void event_loop_thread(void *arg);
static uint64_t event_thread_stk[MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_THREAD_STACK_SIZE/8]; static uint64_t event_thread_stk[MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_THREAD_STACK_SIZE/8];
@ -26,6 +39,8 @@ static const osThreadAttr_t event_thread_attr = {
.cb_mem = &event_thread_tcb, .cb_mem = &event_thread_tcb,
.cb_size = sizeof event_thread_tcb, .cb_size = sizeof event_thread_tcb,
}; };
#endif
static osThreadId_t event_thread_id; static osThreadId_t event_thread_id;
static mbed_rtos_storage_mutex_t event_mutex; static mbed_rtos_storage_mutex_t event_mutex;
static const osMutexAttr_t event_mutex_attr = { static const osMutexAttr_t event_mutex_attr = {
@ -66,7 +81,11 @@ void eventOS_scheduler_signal(void)
// XXX why does signal set lock if called with irqs disabled? // XXX why does signal set lock if called with irqs disabled?
//__enable_irq(); //__enable_irq();
//tr_debug("signal %p", (void*)event_thread_id); //tr_debug("signal %p", (void*)event_thread_id);
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
osEventFlagsSet(event_flag_id, 1);
#else
osThreadFlagsSet(event_thread_id, 1); osThreadFlagsSet(event_thread_id, 1);
#endif
//tr_debug("signalled %p", (void*)event_thread_id); //tr_debug("signalled %p", (void*)event_thread_id);
} }
@ -74,22 +93,45 @@ void eventOS_scheduler_idle(void)
{ {
//tr_debug("idle"); //tr_debug("idle");
eventOS_scheduler_mutex_release(); eventOS_scheduler_mutex_release();
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
osEventFlagsWait(event_flag_id, 1, osFlagsWaitAny, osWaitForever);
#else
osThreadFlagsWait(1, 0, osWaitForever); osThreadFlagsWait(1, 0, osWaitForever);
#endif
eventOS_scheduler_mutex_wait(); eventOS_scheduler_mutex_wait();
} }
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
static void event_loop_thread(void *arg) static void event_loop_thread(void *arg)
{ {
(void)arg; (void)arg;
eventOS_scheduler_mutex_wait(); eventOS_scheduler_mutex_wait();
eventOS_scheduler_run(); //Does not return 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); event_mutex_id = osMutexNew(&event_mutex_attr);
MBED_ASSERT(event_mutex_id != NULL); MBED_ASSERT(event_mutex_id != NULL);
// If a separate event loop thread is not used, the signaling
// happens via event flags instead of thread flags. This allows one to
// perform the initialization from any thread and removes need to know the id
// of event loop dispatch thread.
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
event_flag_id = osEventFlagsNew(&event_flags_attr);
MBED_ASSERT(event_flag_id != NULL);
#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); event_thread_id = osThreadNew(event_loop_thread, NULL, &event_thread_attr);
MBED_ASSERT(event_thread_id != NULL); MBED_ASSERT(event_thread_id != NULL);
} }
@ -97,3 +139,4 @@ void ns_event_loop_thread_create(void)
void ns_event_loop_thread_start(void) void ns_event_loop_thread_start(void)
{ {
} }
#endif

View File

@ -6,6 +6,7 @@
extern "C" { extern "C" {
#endif #endif
void ns_event_loop_init(void);
void ns_event_loop_thread_create(void); void ns_event_loop_thread_create(void);
void ns_event_loop_thread_start(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); ns_dyn_mem_init(heap, h_size, passed_fptr, info_ptr);
platform_timer_enable(); platform_timer_enable();
eventOS_scheduler_init(); eventOS_scheduler_init();
// We do not initialise randlib, as it should be done after // We do not initialise randlib, as it should be done after
// RF driver has started, to get MAC address and RF noise as seed. // RF driver has started, to get MAC address and RF noise as seed.
// We do not initialise trace - left to application. // 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_create();
ns_event_loop_thread_start(); ns_event_loop_thread_start();
#endif
initted = true; initted = true;
} }