This is a fork of the `events` subdirectory of https://github.com/ARMmbed/mbed-os
Dependents: HelloWorld_CCA01M1 HelloWorld_CCA02M1 CI-data-logger-server HelloWorld_CCA02M1 ... more
This is a fork of the events
subdirectory of https://github.com/ARMmbed/mbed-os.
Note, you must import this library with import name: events
!!!
mbed_shared_queues.cpp@9831:68f03f5d2dd2, 2017-05-30 (annotated)
- Committer:
- Kevin Bracey
- Date:
- Tue May 30 16:12:51 2017 +0300
- Revision:
- 9831:68f03f5d2dd2
Add ability to request a shared event queue
To allow components with a simple need to schedule a few events to not
have to create their own threads, with all the associated memory
overhead, add 2 central calls to get shared normal and an
interrupt-deferral event queues, each dispatched on their own shared
threads.
For non-RTOS systems, just the normal event queue is provided, and the
application would have to dispatch this itself. This
application-dispatch is also available via a config option, to
potentially save memory by reusing the main thread.
Possible future improvement: the ability for separate components to
request a minimum stack size, and have the JSON combine these requests.
(Analogous tooling has already been mooted for mbed TLS config options
like key size).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kevin Bracey |
9831:68f03f5d2dd2 | 1 | /* events |
Kevin Bracey |
9831:68f03f5d2dd2 | 2 | * Copyright (c) 2017 ARM Limited |
Kevin Bracey |
9831:68f03f5d2dd2 | 3 | * |
Kevin Bracey |
9831:68f03f5d2dd2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kevin Bracey |
9831:68f03f5d2dd2 | 5 | * you may not use this file except in compliance with the License. |
Kevin Bracey |
9831:68f03f5d2dd2 | 6 | * You may obtain a copy of the License at |
Kevin Bracey |
9831:68f03f5d2dd2 | 7 | * |
Kevin Bracey |
9831:68f03f5d2dd2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kevin Bracey |
9831:68f03f5d2dd2 | 9 | * |
Kevin Bracey |
9831:68f03f5d2dd2 | 10 | * Unless required by applicable law or agreed to in writing, software |
Kevin Bracey |
9831:68f03f5d2dd2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kevin Bracey |
9831:68f03f5d2dd2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kevin Bracey |
9831:68f03f5d2dd2 | 13 | * See the License for the specific language governing permissions and |
Kevin Bracey |
9831:68f03f5d2dd2 | 14 | * limitations under the License. |
Kevin Bracey |
9831:68f03f5d2dd2 | 15 | */ |
Kevin Bracey |
9831:68f03f5d2dd2 | 16 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 17 | #include "events/mbed_shared_queues.h" |
Kevin Bracey |
9831:68f03f5d2dd2 | 18 | #include "mbed.h" |
Kevin Bracey |
9831:68f03f5d2dd2 | 19 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 20 | using namespace events; |
Kevin Bracey |
9831:68f03f5d2dd2 | 21 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 22 | namespace mbed { |
Kevin Bracey |
9831:68f03f5d2dd2 | 23 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 24 | #ifdef MBED_CONF_RTOS_PRESENT |
Kevin Bracey |
9831:68f03f5d2dd2 | 25 | /* Create an event queue, and start the thread that dispatches it. Static |
Kevin Bracey |
9831:68f03f5d2dd2 | 26 | * variables mean this happens once the first time each template instantiation |
Kevin Bracey |
9831:68f03f5d2dd2 | 27 | * is called. This is currently instantiated no more than twice. |
Kevin Bracey |
9831:68f03f5d2dd2 | 28 | */ |
Kevin Bracey |
9831:68f03f5d2dd2 | 29 | template |
Kevin Bracey |
9831:68f03f5d2dd2 | 30 | <osPriority Priority, size_t QueueSize, size_t StackSize> |
Kevin Bracey |
9831:68f03f5d2dd2 | 31 | EventQueue *do_shared_event_queue_with_thread() |
Kevin Bracey |
9831:68f03f5d2dd2 | 32 | { |
Kevin Bracey |
9831:68f03f5d2dd2 | 33 | static uint64_t queue_buffer[QueueSize / sizeof(uint64_t)]; |
Kevin Bracey |
9831:68f03f5d2dd2 | 34 | static EventQueue queue(sizeof queue_buffer, (unsigned char *) queue_buffer); |
Kevin Bracey |
9831:68f03f5d2dd2 | 35 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 36 | static uint64_t stack[StackSize / sizeof(uint64_t)]; |
Kevin Bracey |
9831:68f03f5d2dd2 | 37 | static Thread thread(Priority, StackSize, (unsigned char *) stack); |
Kevin Bracey |
9831:68f03f5d2dd2 | 38 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 39 | Thread::State state = thread.get_state(); |
Kevin Bracey |
9831:68f03f5d2dd2 | 40 | if (state == Thread::Inactive || state == Thread::Deleted) { |
Kevin Bracey |
9831:68f03f5d2dd2 | 41 | osStatus status = thread.start(callback(&queue, &EventQueue::dispatch_forever)); |
Kevin Bracey |
9831:68f03f5d2dd2 | 42 | MBED_ASSERT(status == osOK); |
Kevin Bracey |
9831:68f03f5d2dd2 | 43 | if (status != osOK) { |
Kevin Bracey |
9831:68f03f5d2dd2 | 44 | return NULL; |
Kevin Bracey |
9831:68f03f5d2dd2 | 45 | } |
Kevin Bracey |
9831:68f03f5d2dd2 | 46 | } |
Kevin Bracey |
9831:68f03f5d2dd2 | 47 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 48 | return &queue; |
Kevin Bracey |
9831:68f03f5d2dd2 | 49 | } |
Kevin Bracey |
9831:68f03f5d2dd2 | 50 | #endif |
Kevin Bracey |
9831:68f03f5d2dd2 | 51 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 52 | EventQueue *mbed_event_queue() |
Kevin Bracey |
9831:68f03f5d2dd2 | 53 | { |
Kevin Bracey |
9831:68f03f5d2dd2 | 54 | #if MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION || !defined MBED_CONF_RTOS_PRESENT |
Kevin Bracey |
9831:68f03f5d2dd2 | 55 | /* Only create the EventQueue, but no dispatching thread */ |
Kevin Bracey |
9831:68f03f5d2dd2 | 56 | static unsigned char queue_buffer[MBED_CONF_EVENTS_SHARED_EVENTSIZE]; |
Kevin Bracey |
9831:68f03f5d2dd2 | 57 | static EventQueue queue(sizeof queue_buffer, queue_buffer); |
Kevin Bracey |
9831:68f03f5d2dd2 | 58 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 59 | return &queue; |
Kevin Bracey |
9831:68f03f5d2dd2 | 60 | #else |
Kevin Bracey |
9831:68f03f5d2dd2 | 61 | return do_shared_event_queue_with_thread<osPriorityNormal, MBED_CONF_EVENTS_SHARED_EVENTSIZE, MBED_CONF_EVENTS_SHARED_STACKSIZE>(); |
Kevin Bracey |
9831:68f03f5d2dd2 | 62 | #endif |
Kevin Bracey |
9831:68f03f5d2dd2 | 63 | } |
Kevin Bracey |
9831:68f03f5d2dd2 | 64 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 65 | #ifdef MBED_CONF_RTOS_PRESENT |
Kevin Bracey |
9831:68f03f5d2dd2 | 66 | EventQueue *mbed_highprio_event_queue() |
Kevin Bracey |
9831:68f03f5d2dd2 | 67 | { |
Kevin Bracey |
9831:68f03f5d2dd2 | 68 | return do_shared_event_queue_with_thread<osPriorityHigh, MBED_CONF_EVENTS_SHARED_HIGHPRIO_EVENTSIZE, MBED_CONF_EVENTS_SHARED_HIGHPRIO_STACKSIZE>(); |
Kevin Bracey |
9831:68f03f5d2dd2 | 69 | } |
Kevin Bracey |
9831:68f03f5d2dd2 | 70 | #endif |
Kevin Bracey |
9831:68f03f5d2dd2 | 71 | |
Kevin Bracey |
9831:68f03f5d2dd2 | 72 | } |