Skip to content

Commit d24c7c4

Browse files
authored
Merge pull request #6186 from TeroJaasko/eventloop_in_main_thread_to_master
Eventloop in main thread to master
2 parents 4139f13 + 09b8245 commit d24c7c4

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-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
@@ -9,5 +9,10 @@
99
"help": "Define event-loop thread stack size.",
1010
"value": 6144
1111
}
12+
,
13+
"event-loop-dispatch-from-application": {
14+
"help": "Application is responsible of message dispatch loop. Else launch a separate thread for event-loop.",
15+
"value": false
16+
}
1217
}
1318
}

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

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

1515
#define TRACE_GROUP "evlp"
1616

17+
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
29+
1730
static void event_loop_thread(void *arg);
1831

1932
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 = {
2639
.cb_mem = &event_thread_tcb,
2740
.cb_size = sizeof event_thread_tcb,
2841
};
42+
#endif
43+
2944
static osThreadId_t event_thread_id;
3045
static mbed_rtos_storage_mutex_t event_mutex;
3146
static const osMutexAttr_t event_mutex_attr = {
@@ -66,34 +81,62 @@ void eventOS_scheduler_signal(void)
6681
// XXX why does signal set lock if called with irqs disabled?
6782
//__enable_irq();
6883
//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
6987
osThreadFlagsSet(event_thread_id, 1);
88+
#endif
7089
//tr_debug("signalled %p", (void*)event_thread_id);
7190
}
7291

7392
void eventOS_scheduler_idle(void)
7493
{
7594
//tr_debug("idle");
7695
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
77100
osThreadFlagsWait(1, 0, osWaitForever);
101+
#endif
102+
78103
eventOS_scheduler_mutex_wait();
79104
}
80105

106+
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
81107
static void event_loop_thread(void *arg)
82108
{
83109
(void)arg;
84110
eventOS_scheduler_mutex_wait();
85111
eventOS_scheduler_run(); //Does not return
86112
}
113+
#endif
87114

88-
void ns_event_loop_thread_create(void)
115+
// This is used to initialize the lock used by event loop even
116+
// if it is not ran in a separate thread.
117+
void ns_event_loop_init(void)
89118
{
90119
event_mutex_id = osMutexNew(&event_mutex_attr);
91120
MBED_ASSERT(event_mutex_id != NULL);
92121

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.
126+
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
127+
event_flag_id = osEventFlagsNew(&event_flags_attr);
128+
MBED_ASSERT(event_flag_id != NULL);
129+
#endif
130+
}
131+
132+
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
133+
void ns_event_loop_thread_create(void)
134+
{
93135
event_thread_id = osThreadNew(event_loop_thread, NULL, &event_thread_attr);
94136
MBED_ASSERT(event_thread_id != NULL);
95137
}
96138

97139
void ns_event_loop_thread_start(void)
98140
{
99141
}
142+
#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)