Skip to content

Eventloop in main thread to master #6186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@

#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 uint64_t event_thread_stk[MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_THREAD_STACK_SIZE/8];
Expand All @@ -26,6 +39,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 = {
Expand Down Expand Up @@ -66,34 +81,62 @@ void eventOS_scheduler_signal(void)
// XXX why does signal set lock if called with irqs disabled?
//__enable_irq();
//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);
#endif
//tr_debug("signalled %p", (void*)event_thread_id);
}

void eventOS_scheduler_idle(void)
{
//tr_debug("idle");
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);
#endif

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 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);
MBED_ASSERT(event_thread_id != NULL);
}

void ns_event_loop_thread_start(void)
{
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}