waitqueue.c 4.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7
 *
 * Change Logs:
 * Date           Author       Notes
8
 * 2018/06/26     Bernard      Fix the wait queue issue when wakeup a soon
9 10 11
 *                             to blocked thread.
 */

B
bernard 已提交
12 13 14 15 16 17
#include <stdint.h>

#include <rthw.h>
#include <rtdevice.h>
#include <rtservice.h>

O
ousugo 已提交
18 19
/**
 * @brief    This function will insert a node to the wait queue.
O
ousugo 已提交
20
 *
O
ousugo 已提交
21
 * @param    queue is a pointer to the wait queue.
O
ousugo 已提交
22
 *
O
ousugo 已提交
23 24
 * @param    node is a pointer to the node to be inserted.
 */
B
bernard 已提交
25 26 27 28 29
void rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node)
{
    rt_base_t level;

    level = rt_hw_interrupt_disable();
30
    rt_list_insert_before(&(queue->waiting_list), &(node->list));
B
bernard 已提交
31 32 33
    rt_hw_interrupt_enable(level);
}

O
ousugo 已提交
34 35
/**
 * @brief    This function will remove a node from the wait queue.
O
ousugo 已提交
36
 *
O
ousugo 已提交
37
 * @param    queue is a pointer to the wait queue.
O
ousugo 已提交
38
 *
O
ousugo 已提交
39 40
 * @param    node is a pointer to the node to be removed.
 */
B
bernard 已提交
41 42 43 44 45 46 47 48 49
void rt_wqueue_remove(struct rt_wqueue_node *node)
{
    rt_base_t level;

    level = rt_hw_interrupt_disable();
    rt_list_remove(&(node->list));
    rt_hw_interrupt_enable(level);
}

O
ousugo 已提交
50
/**
O
ousugo 已提交
51 52
 * @brief    This function is the default wakeup function, but it doesn't do anything in actual.
 *           It always return 0, user should define their own wakeup function.
O
ousugo 已提交
53
 *
O
ousugo 已提交
54
 * @param    queue is a pointer to the wait queue.
O
ousugo 已提交
55
 *
O
ousugo 已提交
56
 * @param    key is the wakeup condition.
O
ousugo 已提交
57
 *
O
ousugo 已提交
58 59
 * @return   always return 0.
 */
B
bernard 已提交
60 61 62 63 64
int __wqueue_default_wake(struct rt_wqueue_node *wait, void *key)
{
    return 0;
}

O
ousugo 已提交
65 66
/**
 * @brief    This function will wake up a pending thread on the specified waiting queue that meets the conditions.
O
ousugo 已提交
67
 *
O
ousugo 已提交
68
 * @param    queue is a pointer to the wait queue.
O
ousugo 已提交
69
 *
O
ousugo 已提交
70 71 72 73
 * @param    key is the wakeup conditions, but it is not effective now, because
 *           default wakeup function always return 0.
 *           If user wants to use it, user should define their own wakeup function.
 */
B
bernard 已提交
74 75 76 77 78
void rt_wqueue_wakeup(rt_wqueue_t *queue, void *key)
{
    rt_base_t level;
    register int need_schedule = 0;

79
    rt_list_t *queue_list;
B
bernard 已提交
80 81 82
    struct rt_list_node *node;
    struct rt_wqueue_node *entry;

83
    queue_list = &(queue->waiting_list);
B
bernard 已提交
84 85

    level = rt_hw_interrupt_disable();
86 87
    /* set wakeup flag in the queue */
    queue->flag = RT_WQ_FLAG_WAKEUP;
88 89

    if (!(rt_list_isempty(queue_list)))
B
bernard 已提交
90
    {
91 92 93 94 95 96 97 98 99 100 101 102
        for (node = queue_list->next; node != queue_list; node = node->next)
        {
            entry = rt_list_entry(node, struct rt_wqueue_node, list);
            if (entry->wakeup(entry, key) == 0)
            {
                rt_thread_resume(entry->polling_thread);
                need_schedule = 1;

                rt_wqueue_remove(entry);
                break;
            }
        }
B
bernard 已提交
103 104 105 106 107 108 109
    }
    rt_hw_interrupt_enable(level);

    if (need_schedule)
        rt_schedule();
}

O
ousugo 已提交
110 111 112
/**
 * @brief    This function will join a thread to the specified waiting queue, the thread will holds a wait or
 *           timeout return on the specified wait queue.
O
ousugo 已提交
113
 *
O
ousugo 已提交
114
 * @param    queue is a pointer to the wait queue.
O
ousugo 已提交
115
 *
O
ousugo 已提交
116
 * @param    condition is parameters compatible with POSIX standard interface (currently meaningless, just pass in 0).
O
ousugo 已提交
117
 *
O
ousugo 已提交
118
 * @param    msec is the timeout value, unit is millisecond.
O
ousugo 已提交
119
 *
O
ousugo 已提交
120 121
 * @return   Return 0 if the thread is woken up.
 */
B
bernard 已提交
122 123 124
int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec)
{
    int tick;
125
    rt_thread_t tid = rt_thread_self();
B
bernard 已提交
126 127
    rt_timer_t  tmr = &(tid->thread_timer);
    struct rt_wqueue_node __wait;
128 129 130 131
    rt_base_t level;

    /* current context checking */
    RT_DEBUG_NOT_IN_INTERRUPT;
B
bernard 已提交
132 133 134 135 136 137 138 139 140 141 142

    tick = rt_tick_from_millisecond(msec);

    if ((condition) || (tick == 0))
        return 0;

    __wait.polling_thread = rt_thread_self();
    __wait.key = 0;
    __wait.wakeup = __wqueue_default_wake;
    rt_list_init(&__wait.list);

143
    level = rt_hw_interrupt_disable();
144 145 146 147 148
    if (queue->flag == RT_WQ_FLAG_WAKEUP)
    {
        /* already wakeup */
        goto __exit_wakeup;
    }
149

B
bernard 已提交
150 151 152 153 154 155 156 157 158 159 160 161
    rt_wqueue_add(queue, &__wait);
    rt_thread_suspend(tid);

    /* start timer */
    if (tick != RT_WAITING_FOREVER)
    {
        rt_timer_control(tmr,
                         RT_TIMER_CTRL_SET_TIME,
                         &tick);

        rt_timer_start(tmr);
    }
162
    rt_hw_interrupt_enable(level);
B
bernard 已提交
163 164 165

    rt_schedule();

166 167 168
    level = rt_hw_interrupt_disable();

__exit_wakeup:
169
    queue->flag = RT_WQ_FLAG_CLEAN;
170 171
    rt_hw_interrupt_enable(level);

B
bernard 已提交
172 173 174 175
    rt_wqueue_remove(&__wait);

    return 0;
}