Skip to content

Commit 139efcf

Browse files
c1728p90xc0170
authored andcommitted
Add the PolledQueue implementation of TaskQueue
Add the PolledQueue class which provides a TaskQueue which is run by calling PolledQueue::process.
1 parent 87cb834 commit 139efcf

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* events
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "events/PolledQueue.h"
17+
18+
#include "events/mbed_events.h"
19+
#include "platform/Callback.h"
20+
21+
22+
PolledQueue::PolledQueue(mbed::Callback<void()> cb): _cb(cb)
23+
{
24+
25+
}
26+
27+
PolledQueue::~PolledQueue()
28+
{
29+
30+
}
31+
32+
void PolledQueue::dispatch()
33+
{
34+
core_util_critical_section_enter();
35+
uint64_t buf[MBED_MAX_TASK_SIZE / sizeof(uint64_t)];
36+
37+
while (true) {
38+
39+
// Atomically dequeue the task and copy the callback
40+
TaskBase *task = _list.dequeue();
41+
if (!task) {
42+
break;
43+
}
44+
MBED_ASSERT(sizeof(buf) >= task_size(task));
45+
TaskBase::run_callback_t callback = task_start(task, (uint8_t*)buf, sizeof(buf));
46+
47+
// Run the callback outside the critical section
48+
core_util_critical_section_exit();
49+
callback((uint8_t*)buf);
50+
core_util_critical_section_enter();
51+
52+
// Finish
53+
task_finish(task);
54+
task = NULL;
55+
56+
}
57+
58+
core_util_critical_section_exit();
59+
}
60+
61+
void PolledQueue::attach(mbed::Callback<void()> cb)
62+
{
63+
core_util_critical_section_enter();
64+
65+
_cb = cb;
66+
67+
core_util_critical_section_exit();
68+
}
69+
70+
void PolledQueue::post(TaskBase *task)
71+
{
72+
core_util_critical_section_enter();
73+
74+
bool empty = _list.head() == NULL;
75+
_list.remove(task);
76+
_list.enqueue(task);
77+
if (empty && _cb) {
78+
_cb();
79+
}
80+
81+
core_util_critical_section_exit();
82+
}
83+
84+
void PolledQueue::cancel(TaskBase *task)
85+
{
86+
core_util_critical_section_enter();
87+
88+
_list.remove(task);
89+
90+
core_util_critical_section_exit();
91+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* events
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef POLLED_QUEUE_H
18+
#define POLLED_QUEUE_H
19+
20+
#include "events/TaskQueue.h"
21+
#include "platform/Callback.h"
22+
#include "LinkedList.h"
23+
namespace events {
24+
/** \addtogroup events */
25+
26+
27+
/** PolledQueue
28+
*
29+
* This class is an implementation of TaskQueue which is
30+
* processed synchronously by calls to dispatch.
31+
* @ingroup events
32+
*/
33+
class PolledQueue: public TaskQueue {
34+
public:
35+
36+
/** Create a PolledQueue
37+
*
38+
* Create an event queue.
39+
*
40+
* @param cb Callback called when dispatch needs to be called
41+
*/
42+
PolledQueue(mbed::Callback<void()> cb=NULL);
43+
44+
virtual ~PolledQueue();
45+
46+
virtual void post(TaskBase *event);
47+
48+
virtual void cancel(TaskBase *event);
49+
50+
/**
51+
* Process all the events in this queue
52+
*/
53+
void dispatch();
54+
55+
/**
56+
* Attach a callback indicating that this queue needs to be processed
57+
*
58+
* @param cb Callback called when dispatch needs to be called
59+
*/
60+
void attach(mbed::Callback<void()> cb);
61+
62+
protected:
63+
64+
mbed::Callback<void()> _cb;
65+
LinkedList<TaskBase> _list;
66+
67+
};
68+
69+
}
70+
#endif
71+

0 commit comments

Comments
 (0)