From 8ad3e47a7e1cab942b14993bf89adb6cd20a8fb6 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Mon, 4 Jun 2018 17:25:14 +0300 Subject: [PATCH 1/2] Add option to make Nanostack use global event queue --- .../mbed_lib.json | 7 +- .../ns_event_loop.c | 43 ++------ .../ns_event_loop_mbed.cpp | 97 +++++++++++++++++++ .../ns_event_loop_mutex.c | 68 +++++++++++++ .../ns_event_loop_mutex.h | 31 ++++++ 5 files changed, 207 insertions(+), 39 deletions(-) create mode 100644 features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mbed.cpp create mode 100644 features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mutex.c create mode 100644 features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mutex.h diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/mbed_lib.json b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/mbed_lib.json index 091fa46ae63..86e1c91c8c6 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/mbed_lib.json +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/mbed_lib.json @@ -12,11 +12,14 @@ "critical-section-usable-from-interrupt": { "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.", "value": false - } - , + }, "event-loop-dispatch-from-application": { "help": "Application is responsible of message dispatch loop. Else launch a separate thread for event-loop.", "value": false + }, + "event-loop-use-mbed-events": { + "help": "Use Mbed OS global event queue for Nanostack event loop, rather than our own thread.", + "value": false } } } diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c index 5883fe623ec..7b2332c21a2 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c @@ -2,7 +2,7 @@ * Copyright (c) 2016 ARM Limited, All Rights Reserved */ -#include +#include "mbed_assert.h" #include "cmsis.h" #include "cmsis_os2.h" #include "mbed_rtos_storage.h" @@ -10,10 +10,12 @@ #include "eventOS_scheduler.h" +#include "ns_event_loop_mutex.h" #include "ns_event_loop.h" #define TRACE_GROUP "evlp" +#if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS #if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION @@ -50,40 +52,6 @@ static const osThreadAttr_t event_thread_attr = { static osThreadId_t event_thread_id; #endif -static mbed_rtos_storage_mutex_t event_mutex; -static const osMutexAttr_t event_mutex_attr = { - .name = "nanostack_event_mutex", - .attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust, - .cb_mem = &event_mutex, - .cb_size = sizeof event_mutex, -}; -static osMutexId_t event_mutex_id; -static osThreadId_t event_mutex_owner_id = NULL; -static uint32_t owner_count = 0; - -void eventOS_scheduler_mutex_wait(void) -{ - osMutexAcquire(event_mutex_id, osWaitForever); - if (0 == owner_count) { - event_mutex_owner_id = osThreadGetId(); - } - owner_count++; -} - -void eventOS_scheduler_mutex_release(void) -{ - owner_count--; - if (0 == owner_count) { - event_mutex_owner_id = NULL; - } - osMutexRelease(event_mutex_id); -} - -uint8_t eventOS_scheduler_mutex_is_owner(void) -{ - return osThreadGetId() == event_mutex_owner_id ? 1 : 0; -} - void eventOS_scheduler_signal(void) { // XXX why does signal set lock if called with irqs disabled? @@ -124,8 +92,7 @@ static void event_loop_thread(void *arg) // 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); + ns_event_loop_mutex_init(); // If a separate event loop thread is not used, the signaling // happens via event flags instead of thread flags. This allows one to @@ -148,3 +115,5 @@ void ns_event_loop_thread_start(void) { } #endif + +#endif // !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mbed.cpp b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mbed.cpp new file mode 100644 index 00000000000..6953cb660d4 --- /dev/null +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mbed.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed_assert.h" +#include "platform/arm_hal_interrupt.h" +#include "cmsis.h" +#include "cmsis_os2.h" +#include "mbed_rtos_storage.h" +#include "ns_trace.h" + +#include "eventOS_scheduler.h" + +#include "mbed_error.h" +#include "mbed_shared_queues.h" +#include "events/Event.h" +#include "ns_event_loop_mutex.h" +#include "ns_event_loop.h" + +#define TRACE_GROUP "evlp" + +#if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS + +using events::EventQueue; +using events::Event; + +static Event *event; +static volatile int event_pending; +static volatile bool started; + +void eventOS_scheduler_signal(void) +{ + platform_enter_critical(); + if (started && event_pending == 0) { + event_pending = event->post(); + MBED_ASSERT(event_pending != 0); + } + platform_exit_critical(); +} + +void eventOS_scheduler_idle(void) +{ + error("Shouldn't be called"); +} + +static void do_dispatch_with_mutex_held() +{ + platform_enter_critical(); + event_pending = 0; + platform_exit_critical(); + + /* Process only 1 Nanostack event at a time, to try to be nice to + * others on the global queue. + */ + eventOS_scheduler_mutex_wait(); + bool dispatched = eventOS_scheduler_dispatch_event(); + eventOS_scheduler_mutex_release(); + + /* Go round again if (potentially) more */ + if (dispatched) { + eventOS_scheduler_signal(); + } +} + +void ns_event_loop_init(void) +{ + ns_event_loop_mutex_init(); +} + +void ns_event_loop_thread_create(void) +{ + EventQueue *equeue = mbed::mbed_event_queue(); + MBED_ASSERT(equeue != NULL); + + event = new Event(equeue, do_dispatch_with_mutex_held); + MBED_ASSERT(event != NULL); +} + +void ns_event_loop_thread_start(void) +{ + started = true; + eventOS_scheduler_signal(); +} + +#endif // MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mutex.c b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mutex.c new file mode 100644 index 00000000000..f4d91236fa2 --- /dev/null +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mutex.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed_assert.h" +#include "cmsis.h" +#include "cmsis_os2.h" +#include "mbed_rtos_storage.h" +#include "ns_trace.h" + +#include "eventOS_scheduler.h" + +#include "ns_event_loop_mutex.h" + +#define TRACE_GROUP "evlm" + +static mbed_rtos_storage_mutex_t event_mutex; +static const osMutexAttr_t event_mutex_attr = { + .name = "nanostack_event_mutex", + .attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust, + .cb_mem = &event_mutex, + .cb_size = sizeof event_mutex, +}; +static osMutexId_t event_mutex_id; +static osThreadId_t event_mutex_owner_id = NULL; +static uint32_t owner_count = 0; + +void eventOS_scheduler_mutex_wait(void) +{ + osMutexAcquire(event_mutex_id, osWaitForever); + if (0 == owner_count) { + event_mutex_owner_id = osThreadGetId(); + } + owner_count++; +} + +void eventOS_scheduler_mutex_release(void) +{ + owner_count--; + if (0 == owner_count) { + event_mutex_owner_id = NULL; + } + osMutexRelease(event_mutex_id); +} + +uint8_t eventOS_scheduler_mutex_is_owner(void) +{ + return osThreadGetId() == event_mutex_owner_id ? 1 : 0; +} + +void ns_event_loop_mutex_init(void) +{ + event_mutex_id = osMutexNew(&event_mutex_attr); + MBED_ASSERT(event_mutex_id != NULL); +} + diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mutex.h b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mutex.h new file mode 100644 index 00000000000..ce3ac9beb6f --- /dev/null +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop_mutex.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** \internal Initialise the scheduler mutex + * + * Initialises the mutex used by the implementation of + * eventOS_scheduler_mutex_wait(). Must be called before scheduler is used. + */ +void ns_event_loop_mutex_init(void); + +#ifdef __cplusplus +} +#endif + From 229dfe5ce2f36a6afe72ac854725321c41c3b2f0 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Mon, 2 Jul 2018 13:47:29 +0300 Subject: [PATCH 2/2] Add Apache licenses to ns_hal files --- .../arm_hal_interrupt.c | 14 +++++++++++++- .../arm_hal_interrupt_private.h | 14 +++++++++++++- .../arm_hal_timer.cpp | 14 +++++++++++++- .../nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c | 14 +++++++++++++- .../nanostack-hal-mbed-cmsis-rtos/ns_event_loop.h | 14 +++++++++++++- .../nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c | 14 +++++++++++++- .../nanostack-hal-mbed-cmsis-rtos/ns_hal_init.h | 14 +++++++++++++- 7 files changed, 91 insertions(+), 7 deletions(-) diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt.c b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt.c index fa13de658d0..21c352996c4 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt.c +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt.c @@ -1,5 +1,17 @@ /* - * Copyright (c) 2016 ARM Limited, All Rights Reserved + * Copyright (c) 2016-2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #include "arm_hal_interrupt.h" diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt_private.h b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt_private.h index 4ef518b7c8d..586e2e96049 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt_private.h +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_interrupt_private.h @@ -1,5 +1,17 @@ /* - * Copyright (c) 2016 ARM Limited, All Rights Reserved + * Copyright (c) 2016-2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef ARM_HAL_INTERRUPT_PRIVATE_H_ diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp index a6255c9d9f4..d5191f66bf5 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp @@ -1,5 +1,17 @@ /* - * Copyright (c) 2016 ARM Limited, All Rights Reserved + * Copyright (c) 2016-2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Include before mbed.h to properly get UINT*_C() diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c index 7b2332c21a2..35e0c3e25dd 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.c @@ -1,5 +1,17 @@ /* - * Copyright (c) 2016 ARM Limited, All Rights Reserved + * Copyright (c) 2016-2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #include "mbed_assert.h" diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.h b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.h index a22ca7c752a..8dc55bd7378 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.h +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_event_loop.h @@ -1,5 +1,17 @@ /* - * Copyright (c) 2016 ARM Limited, All Rights Reserved + * Copyright (c) 2016-2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifdef __cplusplus diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c index 54838c28bfa..566472a0dc8 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.c @@ -1,5 +1,17 @@ /* - * Copyright (c) 2016 ARM Limited, All Rights Reserved + * Copyright (c) 2016-2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #include "ns_types.h" diff --git a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.h b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.h index 38425e117ee..10991ce4c70 100644 --- a/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.h +++ b/features/nanostack/nanostack-hal-mbed-cmsis-rtos/ns_hal_init.h @@ -1,5 +1,17 @@ /* - * Copyright (c) 2016 ARM Limited, All Rights Reserved + * Copyright (c) 2016-2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef NS_HAL_INIT_H_