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