Mbed OS Reference
Loading...
Searching...
No Matches
TaskBase.h
1/*
2 * Copyright (c) 2018-2019, Arm Limited and affiliates.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef TASK_BASE_H
19#define TASK_BASE_H
20
21#include "platform/Callback.h"
22#include "platform/mbed_assert.h"
23#include "LinkEntry.h"
24
25namespace rtos {
26class Semaphore;
27}
28
29namespace events {
30
31class TaskQueue;
32
33/**
34 * \defgroup drivers_TaskBase TaskBase class
35 * \ingroup drivers-internal-api-usb
36 * @{
37 */
38
39/** TaskBase
40 *
41 * Representation of a caller allocated task
42 */
43class TaskBase : public LinkEntry {
44public:
45
46 typedef void (*run_callback_t)(void *data);
47
48 /**
49 * Construct a new TaskBase object
50 *
51 * @param q Queue for posting to
52 */
54
55 /**
56 * Destroy this TaskBase
57 */
58 virtual ~TaskBase();
59
60 /**
61 * Set the queue of this task
62 *
63 * @param q TaskQueue to post to
64 */
65 void set(TaskQueue *q);
66
67 /**
68 * Cancel the execution of this task
69 *
70 * Once cancelled the task can be posted again. Previous
71 * calls to post may still run. If you need to ensure the
72 * callback has finished the function wait() can be used.
73 *
74 * @note This function is interrupt safe
75 */
76 void cancel();
77
78 /**
79 * Return true if this task is ready to be posted
80 *
81 * Check if this task is on a queue waiting to be run.
82 *
83 * @return true if it is safe to call post
84 */
85 bool ready();
86
87 /**
88 * Wait for this task to finish execution
89 *
90 * When this function returns then this task is in the finished state.
91 */
92 void wait();
93
94 /**
95 * Check if the callback has run to completion or been fully canceled
96 *
97 * When an task is finished the queue is completely done with it and the
98 * callback is either fully complete or has been canceled and will not run.
99 *
100 * @return true if this task has been flushed from the queue, false otherwise
101 */
102 bool finished();
103
104protected:
105
106 /**
107 * Size of buffer required for TaskBase::start
108 *
109 * @return requested buffer size
110 */
111 virtual uint32_t size() = 0;
112
113 /**
114 * Copy any callback data and return a callback to run
115 *
116 * @param data Buffer to copy data to. Do not copy more than TaskBase::size() data.
117 * @param size Maximum size to copy
118 */
119 virtual run_callback_t start(void *data, uint32_t size) = 0;
120
121 /**
122 * Inform this task that execution has finished.
123 *
124 */
125 virtual void finish();
126
127 /**
128 * Post this task to the set TaskQueue for execution
129 */
130 void post();
131
132private:
133
134 TaskQueue *_queue;
135 bool _posted;
136 uint16_t _start_count;
137 rtos::Semaphore *_flush_sem;
138
139 friend class TaskQueue;
140
141 /*
142 * Must be called in a critical section
143 *
144 * This function should not be called directly. Instead
145 * TaskQueue::task_start should be used instead.
146 */
147 run_callback_t _start(void *buffer, uint32_t size);
148
149 /*
150 * Must be called in a critical section
151 *
152 * This function should not be called directly. Instead
153 * TaskQueue::task_finish should be used instead.
154 *
155 */
156 void _finish();
157
158 /*
159 * Unblock wait if this task is finished
160 */
161 void _wake_check();
162};
163
164/** @}*/
165
166}
167
168#endif
Definition: LinkEntry.h:28
TaskBase.
Definition: TaskBase.h:43
void cancel()
Cancel the execution of this task.
virtual uint32_t size()=0
Size of buffer required for TaskBase::start.
TaskBase(TaskQueue *q)
Construct a new TaskBase object.
void wait()
Wait for this task to finish execution.
void set(TaskQueue *q)
Set the queue of this task.
void post()
Post this task to the set TaskQueue for execution.
bool finished()
Check if the callback has run to completion or been fully canceled.
virtual ~TaskBase()
Destroy this TaskBase.
virtual run_callback_t start(void *data, uint32_t size)=0
Copy any callback data and return a callback to run.
virtual void finish()
Inform this task that execution has finished.
bool ready()
Return true if this task is ready to be posted.
TaskQueue.
Definition: TaskQueue.h:39
The Semaphore class is used to manage and protect access to a set of shared resources.
Definition: Semaphore.h:50