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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.