|
| 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 | +} |
0 commit comments