Skip to content

Commit 4daf6e5

Browse files
kjbraceyCruz Monrreal II
authored andcommitted
Add option to make Nanostack use global event queue
1 parent 0110825 commit 4daf6e5

File tree

5 files changed

+207
-39
lines changed

5 files changed

+207
-39
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
"critical-section-usable-from-interrupt": {
1313
"help": "Make critical section API usable from interrupt context. Else a mutex is used as locking primitive. Consult arm_hal_interrupt.c for possible side effects on interrupt latency.",
1414
"value": false
15-
}
16-
,
15+
},
1716
"event-loop-dispatch-from-application": {
1817
"help": "Application is responsible of message dispatch loop. Else launch a separate thread for event-loop.",
1918
"value": false
19+
},
20+
"event-loop-use-mbed-events": {
21+
"help": "Use Mbed OS global event queue for Nanostack event loop, rather than our own thread.",
22+
"value": false
2023
}
2124
}
2225
}

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

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
* Copyright (c) 2016 ARM Limited, All Rights Reserved
33
*/
44

5-
#include <mbed_assert.h>
5+
#include "mbed_assert.h"
66
#include "cmsis.h"
77
#include "cmsis_os2.h"
88
#include "mbed_rtos_storage.h"
99
#include "ns_trace.h"
1010

1111
#include "eventOS_scheduler.h"
1212

13+
#include "ns_event_loop_mutex.h"
1314
#include "ns_event_loop.h"
1415

1516
#define TRACE_GROUP "evlp"
1617

18+
#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS
1719

1820
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
1921

@@ -50,40 +52,6 @@ static const osThreadAttr_t event_thread_attr = {
5052
static osThreadId_t event_thread_id;
5153
#endif
5254

53-
static mbed_rtos_storage_mutex_t event_mutex;
54-
static const osMutexAttr_t event_mutex_attr = {
55-
.name = "nanostack_event_mutex",
56-
.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust,
57-
.cb_mem = &event_mutex,
58-
.cb_size = sizeof event_mutex,
59-
};
60-
static osMutexId_t event_mutex_id;
61-
static osThreadId_t event_mutex_owner_id = NULL;
62-
static uint32_t owner_count = 0;
63-
64-
void eventOS_scheduler_mutex_wait(void)
65-
{
66-
osMutexAcquire(event_mutex_id, osWaitForever);
67-
if (0 == owner_count) {
68-
event_mutex_owner_id = osThreadGetId();
69-
}
70-
owner_count++;
71-
}
72-
73-
void eventOS_scheduler_mutex_release(void)
74-
{
75-
owner_count--;
76-
if (0 == owner_count) {
77-
event_mutex_owner_id = NULL;
78-
}
79-
osMutexRelease(event_mutex_id);
80-
}
81-
82-
uint8_t eventOS_scheduler_mutex_is_owner(void)
83-
{
84-
return osThreadGetId() == event_mutex_owner_id ? 1 : 0;
85-
}
86-
8755
void eventOS_scheduler_signal(void)
8856
{
8957
// XXX why does signal set lock if called with irqs disabled?
@@ -124,8 +92,7 @@ static void event_loop_thread(void *arg)
12492
// if it is not ran in a separate thread.
12593
void ns_event_loop_init(void)
12694
{
127-
event_mutex_id = osMutexNew(&event_mutex_attr);
128-
MBED_ASSERT(event_mutex_id != NULL);
95+
ns_event_loop_mutex_init();
12996

13097
// If a separate event loop thread is not used, the signaling
13198
// happens via event flags instead of thread flags. This allows one to
@@ -148,3 +115,5 @@ void ns_event_loop_thread_start(void)
148115
{
149116
}
150117
#endif
118+
119+
#endif // !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "mbed_assert.h"
18+
#include "platform/arm_hal_interrupt.h"
19+
#include "cmsis.h"
20+
#include "cmsis_os2.h"
21+
#include "mbed_rtos_storage.h"
22+
#include "ns_trace.h"
23+
24+
#include "eventOS_scheduler.h"
25+
26+
#include "mbed_error.h"
27+
#include "mbed_shared_queues.h"
28+
#include "events/Event.h"
29+
#include "ns_event_loop_mutex.h"
30+
#include "ns_event_loop.h"
31+
32+
#define TRACE_GROUP "evlp"
33+
34+
#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS
35+
36+
using events::EventQueue;
37+
using events::Event;
38+
39+
static Event<void()> *event;
40+
static volatile int event_pending;
41+
static volatile bool started;
42+
43+
void eventOS_scheduler_signal(void)
44+
{
45+
platform_enter_critical();
46+
if (started && event_pending == 0) {
47+
event_pending = event->post();
48+
MBED_ASSERT(event_pending != 0);
49+
}
50+
platform_exit_critical();
51+
}
52+
53+
void eventOS_scheduler_idle(void)
54+
{
55+
error("Shouldn't be called");
56+
}
57+
58+
static void do_dispatch_with_mutex_held()
59+
{
60+
platform_enter_critical();
61+
event_pending = 0;
62+
platform_exit_critical();
63+
64+
/* Process only 1 Nanostack event at a time, to try to be nice to
65+
* others on the global queue.
66+
*/
67+
eventOS_scheduler_mutex_wait();
68+
bool dispatched = eventOS_scheduler_dispatch_event();
69+
eventOS_scheduler_mutex_release();
70+
71+
/* Go round again if (potentially) more */
72+
if (dispatched) {
73+
eventOS_scheduler_signal();
74+
}
75+
}
76+
77+
void ns_event_loop_init(void)
78+
{
79+
ns_event_loop_mutex_init();
80+
}
81+
82+
void ns_event_loop_thread_create(void)
83+
{
84+
EventQueue *equeue = mbed::mbed_event_queue();
85+
MBED_ASSERT(equeue != NULL);
86+
87+
event = new Event<void()>(equeue, do_dispatch_with_mutex_held);
88+
MBED_ASSERT(event != NULL);
89+
}
90+
91+
void ns_event_loop_thread_start(void)
92+
{
93+
started = true;
94+
eventOS_scheduler_signal();
95+
}
96+
97+
#endif // MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "mbed_assert.h"
18+
#include "cmsis.h"
19+
#include "cmsis_os2.h"
20+
#include "mbed_rtos_storage.h"
21+
#include "ns_trace.h"
22+
23+
#include "eventOS_scheduler.h"
24+
25+
#include "ns_event_loop_mutex.h"
26+
27+
#define TRACE_GROUP "evlm"
28+
29+
static mbed_rtos_storage_mutex_t event_mutex;
30+
static const osMutexAttr_t event_mutex_attr = {
31+
.name = "nanostack_event_mutex",
32+
.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust,
33+
.cb_mem = &event_mutex,
34+
.cb_size = sizeof event_mutex,
35+
};
36+
static osMutexId_t event_mutex_id;
37+
static osThreadId_t event_mutex_owner_id = NULL;
38+
static uint32_t owner_count = 0;
39+
40+
void eventOS_scheduler_mutex_wait(void)
41+
{
42+
osMutexAcquire(event_mutex_id, osWaitForever);
43+
if (0 == owner_count) {
44+
event_mutex_owner_id = osThreadGetId();
45+
}
46+
owner_count++;
47+
}
48+
49+
void eventOS_scheduler_mutex_release(void)
50+
{
51+
owner_count--;
52+
if (0 == owner_count) {
53+
event_mutex_owner_id = NULL;
54+
}
55+
osMutexRelease(event_mutex_id);
56+
}
57+
58+
uint8_t eventOS_scheduler_mutex_is_owner(void)
59+
{
60+
return osThreadGetId() == event_mutex_owner_id ? 1 : 0;
61+
}
62+
63+
void ns_event_loop_mutex_init(void)
64+
{
65+
event_mutex_id = osMutexNew(&event_mutex_attr);
66+
MBED_ASSERT(event_mutex_id != NULL);
67+
}
68+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
/** \internal Initialise the scheduler mutex
22+
*
23+
* Initialises the mutex used by the implementation of
24+
* eventOS_scheduler_mutex_wait(). Must be called before scheduler is used.
25+
*/
26+
void ns_event_loop_mutex_init(void);
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif
31+

0 commit comments

Comments
 (0)