Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PolledQueue.cpp Source File

PolledQueue.cpp

00001 /*
00002  * Copyright (c) 2018-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "drivers/internal/PolledQueue.h"
00019 
00020 #include "events/mbed_events.h"
00021 #include "platform/Callback.h"
00022 
00023 
00024 PolledQueue::PolledQueue(mbed::Callback<void()> cb): _cb(cb)
00025 {
00026 
00027 }
00028 
00029 PolledQueue::~PolledQueue()
00030 {
00031 
00032 }
00033 
00034 void PolledQueue::dispatch()
00035 {
00036     core_util_critical_section_enter();
00037     uint64_t buf[MBED_MAX_TASK_SIZE / sizeof(uint64_t)];
00038 
00039     while (true) {
00040 
00041         // Atomically dequeue the task and copy the callback
00042         TaskBase *task = _list.dequeue();
00043         if (!task) {
00044             break;
00045         }
00046         MBED_ASSERT(sizeof(buf) >= task_size(task));
00047         TaskBase::run_callback_t callback = task_start(task, (uint8_t *)buf, sizeof(buf));
00048 
00049         // Run the callback outside the critical section
00050         core_util_critical_section_exit();
00051         callback((uint8_t *)buf);
00052         core_util_critical_section_enter();
00053 
00054         // Finish
00055         task_finish(task);
00056         task = NULL;
00057 
00058     }
00059 
00060     core_util_critical_section_exit();
00061 }
00062 
00063 void PolledQueue::attach(mbed::Callback<void()> cb)
00064 {
00065     core_util_critical_section_enter();
00066 
00067     _cb = cb;
00068 
00069     core_util_critical_section_exit();
00070 }
00071 
00072 void PolledQueue::post(TaskBase *task)
00073 {
00074     core_util_critical_section_enter();
00075 
00076     bool empty = _list.head() == NULL;
00077     _list.remove(task);
00078     _list.enqueue(task);
00079     if (empty && _cb) {
00080         _cb();
00081     }
00082 
00083     core_util_critical_section_exit();
00084 }
00085 
00086 void PolledQueue::cancel(TaskBase *task)
00087 {
00088     core_util_critical_section_enter();
00089 
00090     _list.remove(task);
00091 
00092     core_util_critical_section_exit();
00093 }