mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:9db0e321a9f4 1 /*
kenjiArai 1:9db0e321a9f4 2 * Copyright (c) 2018-2019, Arm Limited and affiliates.
kenjiArai 1:9db0e321a9f4 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 1:9db0e321a9f4 4 *
kenjiArai 1:9db0e321a9f4 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:9db0e321a9f4 6 * you may not use this file except in compliance with the License.
kenjiArai 1:9db0e321a9f4 7 * You may obtain a copy of the License at
kenjiArai 1:9db0e321a9f4 8 *
kenjiArai 1:9db0e321a9f4 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:9db0e321a9f4 10 *
kenjiArai 1:9db0e321a9f4 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:9db0e321a9f4 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:9db0e321a9f4 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:9db0e321a9f4 14 * See the License for the specific language governing permissions and
kenjiArai 1:9db0e321a9f4 15 * limitations under the License.
kenjiArai 1:9db0e321a9f4 16 */
kenjiArai 1:9db0e321a9f4 17
kenjiArai 1:9db0e321a9f4 18 #ifndef TASK_QUEUE_H
kenjiArai 1:9db0e321a9f4 19 #define TASK_QUEUE_H
kenjiArai 1:9db0e321a9f4 20
kenjiArai 1:9db0e321a9f4 21 #include "drivers/internal/TaskBase.h"
kenjiArai 1:9db0e321a9f4 22 #include "platform/Callback.h"
kenjiArai 1:9db0e321a9f4 23 #include "mbed_critical.h"
kenjiArai 1:9db0e321a9f4 24
kenjiArai 1:9db0e321a9f4 25 #define MBED_MAX_TASK_SIZE 32
kenjiArai 1:9db0e321a9f4 26
kenjiArai 1:9db0e321a9f4 27 namespace events {
kenjiArai 1:9db0e321a9f4 28
kenjiArai 1:9db0e321a9f4 29 /**
kenjiArai 1:9db0e321a9f4 30 * \defgroup drivers_TaskQueue TaskQueue class
kenjiArai 1:9db0e321a9f4 31 * \ingroup drivers-internal-api-usb
kenjiArai 1:9db0e321a9f4 32 * @{
kenjiArai 1:9db0e321a9f4 33 */
kenjiArai 1:9db0e321a9f4 34
kenjiArai 1:9db0e321a9f4 35 /** TaskQueue
kenjiArai 1:9db0e321a9f4 36 *
kenjiArai 1:9db0e321a9f4 37 * Flexible task queue for dispatching tasks
kenjiArai 1:9db0e321a9f4 38 */
kenjiArai 1:9db0e321a9f4 39 class TaskQueue {
kenjiArai 1:9db0e321a9f4 40 public:
kenjiArai 1:9db0e321a9f4 41
kenjiArai 1:9db0e321a9f4 42 /** Create a TaskQueue
kenjiArai 1:9db0e321a9f4 43 *
kenjiArai 1:9db0e321a9f4 44 * Create an event queue.
kenjiArai 1:9db0e321a9f4 45 */
kenjiArai 1:9db0e321a9f4 46 TaskQueue()
kenjiArai 1:9db0e321a9f4 47 {
kenjiArai 1:9db0e321a9f4 48
kenjiArai 1:9db0e321a9f4 49 }
kenjiArai 1:9db0e321a9f4 50
kenjiArai 1:9db0e321a9f4 51 /** Destroy a TaskQueue
kenjiArai 1:9db0e321a9f4 52 */
kenjiArai 1:9db0e321a9f4 53 virtual ~TaskQueue()
kenjiArai 1:9db0e321a9f4 54 {
kenjiArai 1:9db0e321a9f4 55
kenjiArai 1:9db0e321a9f4 56 }
kenjiArai 1:9db0e321a9f4 57
kenjiArai 1:9db0e321a9f4 58 /**
kenjiArai 1:9db0e321a9f4 59 * Add this event to the queue for execution
kenjiArai 1:9db0e321a9f4 60 *
kenjiArai 1:9db0e321a9f4 61 * If the event is already in the queue then it is canceled and
kenjiArai 1:9db0e321a9f4 62 * added to the end of the queue.
kenjiArai 1:9db0e321a9f4 63 *
kenjiArai 1:9db0e321a9f4 64 * @param event Pointer to the event
kenjiArai 1:9db0e321a9f4 65 */
kenjiArai 1:9db0e321a9f4 66 virtual void post(TaskBase *event) = 0;
kenjiArai 1:9db0e321a9f4 67
kenjiArai 1:9db0e321a9f4 68 /** Cancel an in-flight event
kenjiArai 1:9db0e321a9f4 69 *
kenjiArai 1:9db0e321a9f4 70 * Cancels the given event so the event's memory can be reused.
kenjiArai 1:9db0e321a9f4 71 *
kenjiArai 1:9db0e321a9f4 72 * The cancel function is IRQ safe.
kenjiArai 1:9db0e321a9f4 73 *
kenjiArai 1:9db0e321a9f4 74 * If called while the event queue's dispatch loop is active, the cancel
kenjiArai 1:9db0e321a9f4 75 * function does not guarantee that the event will not execute after it
kenjiArai 1:9db0e321a9f4 76 * returns, as the event may have already begun executing. It does
kenjiArai 1:9db0e321a9f4 77 * guarantee that the event queue is no longer using event data so
kenjiArai 1:9db0e321a9f4 78 * the event can be freed or reused.
kenjiArai 1:9db0e321a9f4 79 *
kenjiArai 1:9db0e321a9f4 80 * @param event Pointer to the event
kenjiArai 1:9db0e321a9f4 81 */
kenjiArai 1:9db0e321a9f4 82 virtual void cancel(TaskBase *event) = 0;
kenjiArai 1:9db0e321a9f4 83
kenjiArai 1:9db0e321a9f4 84 protected:
kenjiArai 1:9db0e321a9f4 85
kenjiArai 1:9db0e321a9f4 86 /**
kenjiArai 1:9db0e321a9f4 87 * Get the size required to run this task
kenjiArai 1:9db0e321a9f4 88 *
kenjiArai 1:9db0e321a9f4 89 * Get the minimum size required for TaskQueue::task_start
kenjiArai 1:9db0e321a9f4 90 *
kenjiArai 1:9db0e321a9f4 91 * @param task The task to check size on
kenjiArai 1:9db0e321a9f4 92 * @return required size
kenjiArai 1:9db0e321a9f4 93 * @note This call must be made in a critical section
kenjiArai 1:9db0e321a9f4 94 */
kenjiArai 1:9db0e321a9f4 95 static uint32_t task_size(TaskBase *task)
kenjiArai 1:9db0e321a9f4 96 {
kenjiArai 1:9db0e321a9f4 97
kenjiArai 1:9db0e321a9f4 98 return task->size();
kenjiArai 1:9db0e321a9f4 99 }
kenjiArai 1:9db0e321a9f4 100
kenjiArai 1:9db0e321a9f4 101 /**
kenjiArai 1:9db0e321a9f4 102 * Start processing this event by copying out its data
kenjiArai 1:9db0e321a9f4 103 *
kenjiArai 1:9db0e321a9f4 104 * Inform this event both that callback execution has started
kenjiArai 1:9db0e321a9f4 105 * and that the event is free to be posted again.
kenjiArai 1:9db0e321a9f4 106 *
kenjiArai 1:9db0e321a9f4 107 * @param task The task to start processing
kenjiArai 1:9db0e321a9f4 108 * @param dest The buffer to copy the callback arguments to
kenjiArai 1:9db0e321a9f4 109 * @param size maximum size to copy
kenjiArai 1:9db0e321a9f4 110 * @return Pointer to function run
kenjiArai 1:9db0e321a9f4 111 *
kenjiArai 1:9db0e321a9f4 112 * @note event_start must not be called on a canceled event as the
kenjiArai 1:9db0e321a9f4 113 * memory may have been freed already
kenjiArai 1:9db0e321a9f4 114 * @note Every call to event_start must be paired with event_finish
kenjiArai 1:9db0e321a9f4 115 * @note This call must be made in a critical section
kenjiArai 1:9db0e321a9f4 116 */
kenjiArai 1:9db0e321a9f4 117 static TaskBase::run_callback_t task_start(TaskBase *task, uint8_t *dest, uint32_t size)
kenjiArai 1:9db0e321a9f4 118 {
kenjiArai 1:9db0e321a9f4 119
kenjiArai 1:9db0e321a9f4 120 return task->_start(dest, size);
kenjiArai 1:9db0e321a9f4 121 }
kenjiArai 1:9db0e321a9f4 122
kenjiArai 1:9db0e321a9f4 123 /**
kenjiArai 1:9db0e321a9f4 124 * Finish processing this event
kenjiArai 1:9db0e321a9f4 125 *
kenjiArai 1:9db0e321a9f4 126 * Inform this event that the callback has run to completion.
kenjiArai 1:9db0e321a9f4 127 *
kenjiArai 1:9db0e321a9f4 128 * @param task The task to finish processing
kenjiArai 1:9db0e321a9f4 129 *
kenjiArai 1:9db0e321a9f4 130 * @note Every call to event_finish must be preceded by a call to event_start
kenjiArai 1:9db0e321a9f4 131 * @note This call must be made in a critical section
kenjiArai 1:9db0e321a9f4 132 */
kenjiArai 1:9db0e321a9f4 133 static void task_finish(TaskBase *task)
kenjiArai 1:9db0e321a9f4 134 {
kenjiArai 1:9db0e321a9f4 135 task->_finish();
kenjiArai 1:9db0e321a9f4 136 }
kenjiArai 1:9db0e321a9f4 137 };
kenjiArai 1:9db0e321a9f4 138
kenjiArai 1:9db0e321a9f4 139 /** @}*/
kenjiArai 1:9db0e321a9f4 140
kenjiArai 1:9db0e321a9f4 141 }
kenjiArai 1:9db0e321a9f4 142 #endif