首页 > 解决方案 > 调用 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);
}

并且方法sendreceive以这样的方式实现:

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 任务中调用。

读取器和写入器将队列作为调用sendreceive方法的引用,并使用布尔变量将操作结果保存在某处:

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。

标签: c++c++14semaphorefreertos

解决方案


问题在这里:

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);

推荐阅读