xSemaphoreGive被调用后,xSemaphoreTake将不会继续执行任务

时间:2018-09-01 20:31:46

标签: c++ c++14 semaphore freertos

我想在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释放它。

计数信号量用于控制队列中元素的数量。

我想测试该解决方案并使用FreeRTOS port for Linux

为了将任何功能作为任务代码传递,我创建了这样的类:

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

最后,wait_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上挂起时,写入器将写入队列,从而增加sempahore的计数器,读取器将无法恢复。

这行不通吗?存在xSemaphoreTake中的超时可以很好地处理这种情况:当获取信号量(其计数等于0)时,该函数等待,并希望其他任务可以给该信号量(增加计数器)。

与端口的实现或配置有关吗?

我在9.0.0版中使用FreeRTOS,并且启用了抢占和时间片。

1 个答案:

答案 0 :(得分:2)

问题在这里:

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