c++ - 调用 xSemaphoreGive 后,xSemaphoreTake 不会恢复任务
问题描述
我想在 C++ 中实现 FreeRTOS 队列,以便能够将 C++ 类与队列一起使用,而标准的 FreeRTOS 队列实现是不可能的。
这个类看起来像这样:
template <typename T> class os_queue
{
public:
explicit os_queue(unsigned size);
~os_queue();
template <typename U> bool send(U &&u);
T receive(TickType_t timeout);
private:
std::vector<T> m_queue;
const unsigned m_size;
SemaphoreHandle_t m_mux;
SemaphoreHandle_t m_queue_num_elems_sem;
};
构造函数和析构函数:
template <typename T> os_queue<T>::os_queue(unsigned size) : m_size(size)
{
m_queue.reserve(size);
m_mux = xSemaphoreCreateMutex();
m_queue_num_elems_sem = xSemaphoreCreateCounting(size, 0);
}
template <typename T> os_queue<T>::~os_queue()
{
vSemaphoreDelete(m_mux);
vSemaphoreDelete(m_queue_num_elems_sem);
}
并且方法send
和receive
以这样的方式实现:
template <typename T> template <typename U> bool os_queue<T>::send(U &&u)
{
static_assert(is_equiv<T, U>::value, "The argument must be of the same type as a queue element");
bool is_full = false;
{
os_lockguard guard(m_mux);
if (m_queue.size() == m_size)
is_full = true;
else
m_queue.emplace_back(std::forward<U>(u));
}
if (!is_full)
assert(xSemaphoreGive(m_queue_num_elems_sem) == pdTRUE);
return !is_full;
}
template <typename T> T os_queue<T>::receive(TickType_t timeout)
{
if (xSemaphoreTake(m_queue_num_elems_sem, timeout) == pdFALSE)
throw std::runtime_error("Timeout when receiving element from the queue");
else
{
os_lockguard guard(m_mux);
auto res = std::move(m_queue[m_queue.size() - 1]);
m_queue.pop_back();
return res;
}
}
os_lockguard
在构造时使用互斥体/信号量xSemaphoreTake
,在销毁时调用它xSemaphoreGive
来释放它。
计数信号量用于控制队列中元素的数量。
我想测试该解决方案并使用Linux 的 FreeRTOS 端口。
要将任何函数作为任务代码传递,我创建了这样一个类:
class os_task
{
public:
template <typename TaskFuncType>
os_task(TaskFuncType &&task_code, std::string name, unsigned short stack_size, UBaseType_t priority)
: task_code(std::forward<TaskFuncType>(task_code))
{
if (xTaskCreate(
[](void *p) {
auto os_task_p = static_cast<os_task *>(p);
os_task_p->task_code();
os_wait_indefinitely();
},
name.c_str(),
stack_size,
this,
priority,
&task_handle) != pdPASS)
throw std::runtime_error("Could not create a task");
}
~os_task() { vTaskDelete(task_handle); }
private:
TaskHandle_t task_handle;
std::function<void(void)> task_code;
};
使用这个类,我想在一个读者和一个作者的情况下测试一个简单的案例:
static void test_single_writer_single_reader()
{
constexpr unsigned size = 16;
os_queue<int> q(size);
bool result_reader = false, result_writer = false;
auto curr_task_handle = xTaskGetCurrentTaskHandle();
os_task(std::bind(reader, std::ref(q), std::ref(result_reader), curr_task_handle), "reader", 128, 1);
os_task(std::bind(writer, std::ref(q), std::ref(result_writer), curr_task_handle), "writer", 128, 1);
try
{
wait_for_notifs(2);
}
catch (std::exception &e)
{
TEST_ASSERT_MESSAGE(false == true, e.what());
}
TEST_ASSERT(result_reader == true);
TEST_ASSERT(result_writer == true);
}
此测试从 FreeRTOS 任务中调用。
读取器和写入器将队列作为调用send
和receive
方法的引用,并使用布尔变量将操作结果保存在某处:
static void reader(os_queue<int> &queue, bool &result, TaskHandle_t task_to_notify_at_end)
{
int r;
result = true;
try
{
r = queue.receive(max_wait_time_ms);
}
catch (std::exception &e)
{
result = false;
}
xTaskNotifyGive(task_to_notify_at_end);
}
static void writer(os_queue<int> &queue, bool &result, TaskHandle_t task_to_notify_at_end)
{
int v = 0;
result = queue.send(v);
xTaskNotifyGive(task_to_notify_at_end);
}
finallywait_for_notifs
用于将任务与读取器和写入器同步:
static void wait_for_notifs(BaseType_t notifs_num)
{
while (notifs_num)
{
auto notifs = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
if (notifs)
notifs_num -= notifs;
else
throw std::runtime_error("Timeout when waiting for notifications");
}
}
主要思想是我想检查应用程序是否能够很好地处理比赛。
问题是,当阅读器首先执行并挂起时xSemaphoreTask
,然后作者写入队列以增加信号量的计数器,阅读器将不会恢复。
它不应该工作吗?存在超时以xSemaphoreTake
很好地处理这种情况:当信号量被占用时(其计数等于 0),函数等待并希望其他任务可以提供(增加计数器)信号量。
它可能与端口实现或配置有关吗?
我在 9.0.0 版中使用启用了抢占和时间片的 FreeRTOS。
解决方案
问题在这里:
os_task(std::bind(reader, std::ref(q), std::ref(result_reader), curr_task_handle), "reader", 128, 1);
os_task(std::bind(writer, std::ref(q), std::ref(result_writer), curr_task_handle), "writer", 128, 1);
创建的对象在构造完成后立即被删除。应该
os_task r(std::bind(reader, std::ref(q), std::ref(result_reader), curr_task_handle), "reader", 128, 1);
os_task w(std::bind(writer, std::ref(q), std::ref(result_writer), curr_task_handle), "writer", 128, 1);
推荐阅读
- javascript - 是否可以在 WebGL/OpenGL 中获得低于特定像素值的像素数?
- python - Python 线程中的错误是如何处理的?
- mysql - MySQL NOT IN 未返回预期结果
- html - 两个具有相同 css 的菜单,但只有一个在触摸设备上工作
- firebase - 无法捕获任务“:app:preDebugBuild”的输入文件的指纹
- css - 如何为 json 文件中包含的内容添加字体颜色?
- chart.js - Angular 7 中的折线图不起作用 - 数据类型是否正确?
- python - 在python中替换json文件中的列表
- javascript - 用于对 Mozilla 文档中定义的数字进行排序的 Javascript“Sort()”函数
- python - 在 .txt 中循环数据时出现规范化错误