Denislam Valeev / Mbed OS Nucleo_rtos_basic
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_shared_queues.cpp Source File

mbed_shared_queues.cpp

00001 /* events
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "events/mbed_shared_queues.h"
00018 #include "mbed.h"
00019 
00020 using namespace events;
00021 
00022 namespace mbed {
00023 
00024 #ifdef MBED_CONF_RTOS_PRESENT
00025 /* Create an event queue, and start the thread that dispatches it. Static
00026  * variables mean this happens once the first time each template instantiation
00027  * is called. This is currently instantiated no more than twice.
00028  */
00029 template
00030 <osPriority Priority, size_t QueueSize, size_t StackSize>
00031 EventQueue *do_shared_event_queue_with_thread()
00032 {
00033     static uint64_t queue_buffer[QueueSize / sizeof(uint64_t)];
00034     static EventQueue queue(sizeof queue_buffer, (unsigned char *) queue_buffer);
00035 
00036     static uint64_t stack[StackSize / sizeof(uint64_t)];
00037     static Thread thread(Priority, StackSize, (unsigned char *) stack);
00038 
00039     Thread::State state = thread.get_state();
00040     if (state == Thread::Inactive || state == Thread::Deleted) {
00041         osStatus status = thread.start(callback(&queue, &EventQueue::dispatch_forever));
00042         MBED_ASSERT(status == osOK);
00043         if (status != osOK) {
00044             return NULL;
00045         }
00046     }
00047 
00048     return &queue;
00049 }
00050 #endif
00051 
00052 EventQueue *mbed_event_queue()
00053 {
00054 #if MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION || !defined MBED_CONF_RTOS_PRESENT
00055     /* Only create the EventQueue, but no dispatching thread */
00056     static unsigned char queue_buffer[MBED_CONF_EVENTS_SHARED_EVENTSIZE];
00057     static EventQueue queue(sizeof queue_buffer, queue_buffer);
00058 
00059     return &queue;
00060 #else
00061     return do_shared_event_queue_with_thread<osPriorityNormal, MBED_CONF_EVENTS_SHARED_EVENTSIZE, MBED_CONF_EVENTS_SHARED_STACKSIZE>();
00062 #endif
00063 }
00064 
00065 #ifdef MBED_CONF_RTOS_PRESENT
00066 EventQueue *mbed_highprio_event_queue()
00067 {
00068     return do_shared_event_queue_with_thread<osPriorityHigh, MBED_CONF_EVENTS_SHARED_HIGHPRIO_EVENTSIZE, MBED_CONF_EVENTS_SHARED_HIGHPRIO_STACKSIZE>();
00069 }
00070 #endif
00071 
00072 }