Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ns_event_loop_mbed.cpp Source File

ns_event_loop_mbed.cpp

00001 /*
00002  * Copyright (c) 2018, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "mbed_assert.h"
00019 #include "platform/arm_hal_interrupt.h"
00020 #ifdef MBED_CONF_RTOS_PRESENT
00021 #include "cmsis.h"
00022 #include "cmsis_os2.h"
00023 #include "mbed_rtos_storage.h"
00024 #endif
00025 #include "ns_trace.h"
00026 
00027 #include "eventOS_scheduler.h"
00028 
00029 #include "mbed_error.h"
00030 #include "mbed_shared_queues.h"
00031 #include "events/Event.h"
00032 #include "ns_event_loop_mutex.h"
00033 #include "ns_event_loop.h"
00034 
00035 #define TRACE_GROUP "evlp"
00036 
00037 #if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS
00038 
00039 using events::EventQueue;
00040 using events::Event;
00041 
00042 static Event<void()> *event;
00043 static volatile int event_pending;
00044 static volatile bool started;
00045 
00046 void eventOS_scheduler_signal(void)
00047 {
00048     platform_enter_critical();
00049     if (started && event_pending == 0) {
00050         event_pending = event->post();
00051         MBED_ASSERT(event_pending != 0);
00052     }
00053     platform_exit_critical();
00054 }
00055 
00056 void eventOS_scheduler_idle(void)
00057 {
00058     error("Shouldn't be called");
00059 }
00060 
00061 static void do_dispatch_with_mutex_held()
00062 {
00063     platform_enter_critical();
00064     event_pending = 0;
00065     platform_exit_critical();
00066 
00067     /* Process only 1 Nanostack event at a time, to try to be nice to
00068      * others on the global queue.
00069      */
00070     eventOS_scheduler_mutex_wait();
00071     bool dispatched = eventOS_scheduler_dispatch_event();
00072     eventOS_scheduler_mutex_release();
00073 
00074     /* Go round again if (potentially) more */
00075     if (dispatched) {
00076         eventOS_scheduler_signal();
00077     }
00078 }
00079 
00080 void ns_event_loop_init(void)
00081 {
00082     ns_event_loop_mutex_init();
00083 }
00084 
00085 void ns_event_loop_thread_create(void)
00086 {
00087     EventQueue *equeue = mbed::mbed_event_queue();
00088     MBED_ASSERT(equeue != NULL);
00089 
00090     event = new Event<void()>(equeue, do_dispatch_with_mutex_held);
00091     MBED_ASSERT(event != NULL);
00092 }
00093 
00094 void ns_event_loop_thread_start(void)
00095 {
00096     started = true;
00097     eventOS_scheduler_signal();
00098 }
00099 
00100 #endif // MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS