mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
drivers/internal/TaskBase.h@1:9db0e321a9f4, 2019-12-31 (annotated)
- 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?
User | Revision | Line number | New 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_BASE_H |
kenjiArai | 1:9db0e321a9f4 | 19 | #define TASK_BASE_H |
kenjiArai | 1:9db0e321a9f4 | 20 | |
kenjiArai | 1:9db0e321a9f4 | 21 | #include "platform/Callback.h" |
kenjiArai | 1:9db0e321a9f4 | 22 | #include "platform/mbed_assert.h" |
kenjiArai | 1:9db0e321a9f4 | 23 | #include "LinkEntry.h" |
kenjiArai | 1:9db0e321a9f4 | 24 | |
kenjiArai | 1:9db0e321a9f4 | 25 | namespace rtos { |
kenjiArai | 1:9db0e321a9f4 | 26 | class Semaphore; |
kenjiArai | 1:9db0e321a9f4 | 27 | } |
kenjiArai | 1:9db0e321a9f4 | 28 | |
kenjiArai | 1:9db0e321a9f4 | 29 | namespace events { |
kenjiArai | 1:9db0e321a9f4 | 30 | |
kenjiArai | 1:9db0e321a9f4 | 31 | class TaskQueue; |
kenjiArai | 1:9db0e321a9f4 | 32 | |
kenjiArai | 1:9db0e321a9f4 | 33 | /** |
kenjiArai | 1:9db0e321a9f4 | 34 | * \defgroup drivers_TaskBase TaskBase class |
kenjiArai | 1:9db0e321a9f4 | 35 | * \ingroup drivers-internal-api-usb |
kenjiArai | 1:9db0e321a9f4 | 36 | * @{ |
kenjiArai | 1:9db0e321a9f4 | 37 | */ |
kenjiArai | 1:9db0e321a9f4 | 38 | |
kenjiArai | 1:9db0e321a9f4 | 39 | /** TaskBase |
kenjiArai | 1:9db0e321a9f4 | 40 | * |
kenjiArai | 1:9db0e321a9f4 | 41 | * Representation of a caller allocated task |
kenjiArai | 1:9db0e321a9f4 | 42 | */ |
kenjiArai | 1:9db0e321a9f4 | 43 | class TaskBase : public LinkEntry { |
kenjiArai | 1:9db0e321a9f4 | 44 | public: |
kenjiArai | 1:9db0e321a9f4 | 45 | |
kenjiArai | 1:9db0e321a9f4 | 46 | typedef void (*run_callback_t)(void *data); |
kenjiArai | 1:9db0e321a9f4 | 47 | |
kenjiArai | 1:9db0e321a9f4 | 48 | /** |
kenjiArai | 1:9db0e321a9f4 | 49 | * Construct a new TaskBase object |
kenjiArai | 1:9db0e321a9f4 | 50 | * |
kenjiArai | 1:9db0e321a9f4 | 51 | * @param q Queue for posting to |
kenjiArai | 1:9db0e321a9f4 | 52 | */ |
kenjiArai | 1:9db0e321a9f4 | 53 | TaskBase(TaskQueue *q); |
kenjiArai | 1:9db0e321a9f4 | 54 | |
kenjiArai | 1:9db0e321a9f4 | 55 | /** |
kenjiArai | 1:9db0e321a9f4 | 56 | * Destroy this TaskBase |
kenjiArai | 1:9db0e321a9f4 | 57 | */ |
kenjiArai | 1:9db0e321a9f4 | 58 | virtual ~TaskBase(); |
kenjiArai | 1:9db0e321a9f4 | 59 | |
kenjiArai | 1:9db0e321a9f4 | 60 | /** |
kenjiArai | 1:9db0e321a9f4 | 61 | * Set the queue of this task |
kenjiArai | 1:9db0e321a9f4 | 62 | * |
kenjiArai | 1:9db0e321a9f4 | 63 | * @param q TaskQueue to post to |
kenjiArai | 1:9db0e321a9f4 | 64 | */ |
kenjiArai | 1:9db0e321a9f4 | 65 | void set(TaskQueue *q); |
kenjiArai | 1:9db0e321a9f4 | 66 | |
kenjiArai | 1:9db0e321a9f4 | 67 | /** |
kenjiArai | 1:9db0e321a9f4 | 68 | * Cancel the execution of this task |
kenjiArai | 1:9db0e321a9f4 | 69 | * |
kenjiArai | 1:9db0e321a9f4 | 70 | * Once cancelled the task can be posted again. Previous |
kenjiArai | 1:9db0e321a9f4 | 71 | * calls to post may still run. If you need to ensure the |
kenjiArai | 1:9db0e321a9f4 | 72 | * callback has finished the function wait() can be used. |
kenjiArai | 1:9db0e321a9f4 | 73 | * |
kenjiArai | 1:9db0e321a9f4 | 74 | * @note This function is interrupt safe |
kenjiArai | 1:9db0e321a9f4 | 75 | */ |
kenjiArai | 1:9db0e321a9f4 | 76 | void cancel(); |
kenjiArai | 1:9db0e321a9f4 | 77 | |
kenjiArai | 1:9db0e321a9f4 | 78 | /** |
kenjiArai | 1:9db0e321a9f4 | 79 | * Return true if this task is ready to be posted |
kenjiArai | 1:9db0e321a9f4 | 80 | * |
kenjiArai | 1:9db0e321a9f4 | 81 | * Check if this task is on a queue waiting to be run. |
kenjiArai | 1:9db0e321a9f4 | 82 | * |
kenjiArai | 1:9db0e321a9f4 | 83 | * @return true if it is safe to call post |
kenjiArai | 1:9db0e321a9f4 | 84 | */ |
kenjiArai | 1:9db0e321a9f4 | 85 | bool ready(); |
kenjiArai | 1:9db0e321a9f4 | 86 | |
kenjiArai | 1:9db0e321a9f4 | 87 | /** |
kenjiArai | 1:9db0e321a9f4 | 88 | * Wait for this task to finish execution |
kenjiArai | 1:9db0e321a9f4 | 89 | * |
kenjiArai | 1:9db0e321a9f4 | 90 | * When this function returns then this task is in the finished state. |
kenjiArai | 1:9db0e321a9f4 | 91 | */ |
kenjiArai | 1:9db0e321a9f4 | 92 | void wait(); |
kenjiArai | 1:9db0e321a9f4 | 93 | |
kenjiArai | 1:9db0e321a9f4 | 94 | /** |
kenjiArai | 1:9db0e321a9f4 | 95 | * Check if the callback has run to completion or been fully canceled |
kenjiArai | 1:9db0e321a9f4 | 96 | * |
kenjiArai | 1:9db0e321a9f4 | 97 | * When an task is finished the queue is completely done with it and the |
kenjiArai | 1:9db0e321a9f4 | 98 | * callback is either fully complete or has been canceled and will not run. |
kenjiArai | 1:9db0e321a9f4 | 99 | * |
kenjiArai | 1:9db0e321a9f4 | 100 | * @return true if this task has been flushed from the queue, false otherwise |
kenjiArai | 1:9db0e321a9f4 | 101 | */ |
kenjiArai | 1:9db0e321a9f4 | 102 | bool finished(); |
kenjiArai | 1:9db0e321a9f4 | 103 | |
kenjiArai | 1:9db0e321a9f4 | 104 | protected: |
kenjiArai | 1:9db0e321a9f4 | 105 | |
kenjiArai | 1:9db0e321a9f4 | 106 | /** |
kenjiArai | 1:9db0e321a9f4 | 107 | * Size of buffer required for TaskBase::start |
kenjiArai | 1:9db0e321a9f4 | 108 | * |
kenjiArai | 1:9db0e321a9f4 | 109 | * @return requested buffer size |
kenjiArai | 1:9db0e321a9f4 | 110 | */ |
kenjiArai | 1:9db0e321a9f4 | 111 | virtual uint32_t size() = 0; |
kenjiArai | 1:9db0e321a9f4 | 112 | |
kenjiArai | 1:9db0e321a9f4 | 113 | /** |
kenjiArai | 1:9db0e321a9f4 | 114 | * Copy any callback data and return a callback to run |
kenjiArai | 1:9db0e321a9f4 | 115 | * |
kenjiArai | 1:9db0e321a9f4 | 116 | * @param data Buffer to copy data to. Do not copy more than TaskBase::size() data. |
kenjiArai | 1:9db0e321a9f4 | 117 | * @param size Maximum size to copy |
kenjiArai | 1:9db0e321a9f4 | 118 | */ |
kenjiArai | 1:9db0e321a9f4 | 119 | virtual run_callback_t start(void *data, uint32_t size) = 0; |
kenjiArai | 1:9db0e321a9f4 | 120 | |
kenjiArai | 1:9db0e321a9f4 | 121 | /** |
kenjiArai | 1:9db0e321a9f4 | 122 | * Inform this task that execution has finished. |
kenjiArai | 1:9db0e321a9f4 | 123 | * |
kenjiArai | 1:9db0e321a9f4 | 124 | */ |
kenjiArai | 1:9db0e321a9f4 | 125 | virtual void finish(); |
kenjiArai | 1:9db0e321a9f4 | 126 | |
kenjiArai | 1:9db0e321a9f4 | 127 | /** |
kenjiArai | 1:9db0e321a9f4 | 128 | * Post this task to the set TaskQueue for execution |
kenjiArai | 1:9db0e321a9f4 | 129 | */ |
kenjiArai | 1:9db0e321a9f4 | 130 | void post(); |
kenjiArai | 1:9db0e321a9f4 | 131 | |
kenjiArai | 1:9db0e321a9f4 | 132 | private: |
kenjiArai | 1:9db0e321a9f4 | 133 | |
kenjiArai | 1:9db0e321a9f4 | 134 | TaskQueue *_queue; |
kenjiArai | 1:9db0e321a9f4 | 135 | bool _posted; |
kenjiArai | 1:9db0e321a9f4 | 136 | uint16_t _start_count; |
kenjiArai | 1:9db0e321a9f4 | 137 | rtos::Semaphore *_flush_sem; |
kenjiArai | 1:9db0e321a9f4 | 138 | |
kenjiArai | 1:9db0e321a9f4 | 139 | friend class TaskQueue; |
kenjiArai | 1:9db0e321a9f4 | 140 | |
kenjiArai | 1:9db0e321a9f4 | 141 | /* |
kenjiArai | 1:9db0e321a9f4 | 142 | * Must be called in a critical section |
kenjiArai | 1:9db0e321a9f4 | 143 | * |
kenjiArai | 1:9db0e321a9f4 | 144 | * This function should not be called directly. Instead |
kenjiArai | 1:9db0e321a9f4 | 145 | * TaskQueue::task_start should be used instead. |
kenjiArai | 1:9db0e321a9f4 | 146 | */ |
kenjiArai | 1:9db0e321a9f4 | 147 | run_callback_t _start(void *buffer, uint32_t size); |
kenjiArai | 1:9db0e321a9f4 | 148 | |
kenjiArai | 1:9db0e321a9f4 | 149 | /* |
kenjiArai | 1:9db0e321a9f4 | 150 | * Must be called in a critical section |
kenjiArai | 1:9db0e321a9f4 | 151 | * |
kenjiArai | 1:9db0e321a9f4 | 152 | * This function should not be called directly. Instead |
kenjiArai | 1:9db0e321a9f4 | 153 | * TaskQueue::task_finish should be used instead. |
kenjiArai | 1:9db0e321a9f4 | 154 | * |
kenjiArai | 1:9db0e321a9f4 | 155 | */ |
kenjiArai | 1:9db0e321a9f4 | 156 | void _finish(); |
kenjiArai | 1:9db0e321a9f4 | 157 | |
kenjiArai | 1:9db0e321a9f4 | 158 | /* |
kenjiArai | 1:9db0e321a9f4 | 159 | * Unblock wait if this task is finished |
kenjiArai | 1:9db0e321a9f4 | 160 | */ |
kenjiArai | 1:9db0e321a9f4 | 161 | void _wake_check(); |
kenjiArai | 1:9db0e321a9f4 | 162 | }; |
kenjiArai | 1:9db0e321a9f4 | 163 | |
kenjiArai | 1:9db0e321a9f4 | 164 | /** @}*/ |
kenjiArai | 1:9db0e321a9f4 | 165 | |
kenjiArai | 1:9db0e321a9f4 | 166 | } |
kenjiArai | 1:9db0e321a9f4 | 167 | |
kenjiArai | 1:9db0e321a9f4 | 168 | #endif |