Mistake on this page?
Report an issue in GitHub or email us
TaskQueue.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_QUEUE_H
19 #define TASK_QUEUE_H
20 
21 #include "drivers/internal/TaskBase.h"
22 #include "platform/Callback.h"
23 #include "mbed_critical.h"
24 
25 #define MBED_MAX_TASK_SIZE 32
26 
27 namespace events {
28 
29 /**
30  * \defgroup drivers_TaskQueue TaskQueue class
31  * \ingroup drivers-internal-api-usb
32  * @{
33  */
34 
35 /** TaskQueue
36  *
37  * Flexible task queue for dispatching tasks
38  */
39 class TaskQueue {
40 public:
41 
42  /** Create a TaskQueue
43  *
44  * Create an event queue.
45  */
47  {
48 
49  }
50 
51  /** Destroy a TaskQueue
52  */
53  virtual ~TaskQueue()
54  {
55 
56  }
57 
58  /**
59  * Add this event to the queue for execution
60  *
61  * If the event is already in the queue then it is canceled and
62  * added to the end of the queue.
63  *
64  * @param event Pointer to the event
65  */
66  virtual void post(TaskBase *event) = 0;
67 
68  /** Cancel an in-flight event
69  *
70  * Cancels the given event so the event's memory can be reused.
71  *
72  * The cancel function is IRQ safe.
73  *
74  * If called while the event queue's dispatch loop is active, the cancel
75  * function does not guarantee that the event will not execute after it
76  * returns, as the event may have already begun executing. It does
77  * guarantee that the event queue is no longer using event data so
78  * the event can be freed or reused.
79  *
80  * @param event Pointer to the event
81  */
82  virtual void cancel(TaskBase *event) = 0;
83 
84 protected:
85 
86  /**
87  * Get the size required to run this task
88  *
89  * Get the minimum size required for TaskQueue::task_start
90  *
91  * @param task The task to check size on
92  * @return required size
93  * @note This call must be made in a critical section
94  */
95  static uint32_t task_size(TaskBase *task)
96  {
97 
98  return task->size();
99  }
100 
101  /**
102  * Start processing this event by copying out its data
103  *
104  * Inform this event both that callback execution has started
105  * and that the event is free to be posted again.
106  *
107  * @param task The task to start processing
108  * @param dest The buffer to copy the callback arguments to
109  * @param size maximum size to copy
110  * @return Pointer to function run
111  *
112  * @note event_start must not be called on a canceled event as the
113  * memory may have been freed already
114  * @note Every call to event_start must be paired with event_finish
115  * @note This call must be made in a critical section
116  */
117  static TaskBase::run_callback_t task_start(TaskBase *task, uint8_t *dest, uint32_t size)
118  {
119 
120  return task->_start(dest, size);
121  }
122 
123  /**
124  * Finish processing this event
125  *
126  * Inform this event that the callback has run to completion.
127  *
128  * @param task The task to finish processing
129  *
130  * @note Every call to event_finish must be preceded by a call to event_start
131  * @note This call must be made in a critical section
132  */
133  static void task_finish(TaskBase *task)
134  {
135  task->_finish();
136  }
137 };
138 
139 /** @}*/
140 
141 }
142 #endif
virtual uint32_t size()=0
Size of buffer required for TaskBase::start.
TaskBase.
Definition: TaskBase.h:43
virtual ~TaskQueue()
Destroy a TaskQueue.
Definition: TaskQueue.h:53
TaskQueue()
Create a TaskQueue.
Definition: TaskQueue.h:46
static TaskBase::run_callback_t task_start(TaskBase *task, uint8_t *dest, uint32_t size)
Start processing this event by copying out its data.
Definition: TaskQueue.h:117
virtual void post(TaskBase *event)=0
Add this event to the queue for execution.
TaskQueue.
Definition: TaskQueue.h:39
static void task_finish(TaskBase *task)
Finish processing this event.
Definition: TaskQueue.h:133
virtual void cancel(TaskBase *event)=0
Cancel an in-flight event.
static uint32_t task_size(TaskBase *task)
Get the size required to run this task.
Definition: TaskQueue.h:95
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.