dataqueue.c 10.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-09-30     Bernard      first version.
9
 * 2016-10-31     armink       fix some resume push and pop thread bugs
10 11 12 13 14 15
 */

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

16 17
#define DATAQUEUE_MAGIC  0xbead0e0e

18 19 20 21 22 23 24 25 26 27 28 29 30
struct rt_data_item
{
    const void *data_ptr;
    rt_size_t data_size;
};

rt_err_t
rt_data_queue_init(struct rt_data_queue *queue,
                   rt_uint16_t size,
                   rt_uint16_t lwm,
                   void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event))
{
    RT_ASSERT(queue != RT_NULL);
Q
qiyongzhong0 已提交
31
    RT_ASSERT(size > 0);
32 33 34

    queue->evt_notify = evt_notify;

35
    queue->magic = DATAQUEUE_MAGIC;
36 37 38 39 40
    queue->size = size;
    queue->lwm = lwm;

    queue->get_index = 0;
    queue->put_index = 0;
Q
qiyongzhong0 已提交
41 42
    queue->is_empty = 1;
    queue->is_full = 0;
43 44 45

    rt_list_init(&(queue->suspended_push_list));
    rt_list_init(&(queue->suspended_pop_list));
Q
qiyongzhong0 已提交
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    queue->queue = (struct rt_data_item *)rt_malloc(sizeof(struct rt_data_item) * size);
    if (queue->queue == RT_NULL)
    {
        return -RT_ENOMEM;
    }

    return RT_EOK;
}
RTM_EXPORT(rt_data_queue_init);

rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
                            const void *data_ptr,
                            rt_size_t data_size,
                            rt_int32_t timeout)
{
    rt_ubase_t  level;
    rt_thread_t thread;
    rt_err_t    result;
65

66
    RT_ASSERT(queue != RT_NULL);
Q
qiyongzhong0 已提交
67
    RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
68 69 70 71 72

    result = RT_EOK;
    thread = rt_thread_self();

    level = rt_hw_interrupt_disable();
Q
qiyongzhong0 已提交
73
    while (queue->is_full)
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    {
        /* queue is full */
        if (timeout == 0)
        {
            result = -RT_ETIMEOUT;

            goto __exit;
        }

        /* current context checking */
        RT_DEBUG_NOT_IN_INTERRUPT;

        /* reset thread error number */
        thread->error = RT_EOK;
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        /* suspend thread on the push list */
        rt_thread_suspend(thread);
        rt_list_insert_before(&(queue->suspended_push_list), &(thread->tlist));
        /* start timer */
        if (timeout > 0)
        {
            /* reset the timeout of thread timer and start it */
            rt_timer_control(&(thread->thread_timer),
                             RT_TIMER_CTRL_SET_TIME,
                             &timeout);
            rt_timer_start(&(thread->thread_timer));
        }

        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        /* do schedule */
        rt_schedule();

        /* thread is waked up */
        result = thread->error;
        level = rt_hw_interrupt_disable();
111
        if (result != RT_EOK) goto __exit;
112 113
    }

Q
qiyongzhong0 已提交
114 115
    queue->queue[queue->put_index].data_ptr  = data_ptr;
    queue->queue[queue->put_index].data_size = data_size;
116
    queue->put_index += 1;
Q
qiyongzhong0 已提交
117 118 119 120 121 122 123 124 125
    if (queue->put_index == queue->size)
    {
        queue->put_index = 0;
    }
    queue->is_empty = 0;
    if (queue->put_index == queue->get_index)
    {
        queue->is_full = 1;
    }
126

127
    /* there is at least one thread in suspended list */
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    if (!rt_list_isempty(&(queue->suspended_pop_list)))
    {
        /* get thread entry */
        thread = rt_list_entry(queue->suspended_pop_list.next,
                               struct rt_thread,
                               tlist);

        /* resume it */
        rt_thread_resume(thread);
        rt_hw_interrupt_enable(level);

        /* perform a schedule */
        rt_schedule();

        return result;
    }

__exit:
    rt_hw_interrupt_enable(level);
    if ((result == RT_EOK) && queue->evt_notify != RT_NULL)
    {
        queue->evt_notify(queue, RT_DATAQUEUE_EVENT_PUSH);
    }

    return result;
}
RTM_EXPORT(rt_data_queue_push);

rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
                           const void** data_ptr,
158
                           rt_size_t *size,
159 160 161 162 163
                           rt_int32_t timeout)
{
    rt_ubase_t  level;
    rt_thread_t thread;
    rt_err_t    result;
164

165
    RT_ASSERT(queue != RT_NULL);
Q
qiyongzhong0 已提交
166
    RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
167 168 169 170 171 172 173
    RT_ASSERT(data_ptr != RT_NULL);
    RT_ASSERT(size != RT_NULL);

    result = RT_EOK;
    thread = rt_thread_self();

    level = rt_hw_interrupt_disable();
Q
qiyongzhong0 已提交
174
    while (queue->is_empty)
175 176 177 178 179 180 181 182 183 184 185 186 187
    {
        /* queue is empty */
        if (timeout == 0)
        {
            result = -RT_ETIMEOUT;
            goto __exit;
        }

        /* current context checking */
        RT_DEBUG_NOT_IN_INTERRUPT;

        /* reset thread error number */
        thread->error = RT_EOK;
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        /* suspend thread on the pop list */
        rt_thread_suspend(thread);
        rt_list_insert_before(&(queue->suspended_pop_list), &(thread->tlist));
        /* start timer */
        if (timeout > 0)
        {
            /* reset the timeout of thread timer and start it */
            rt_timer_control(&(thread->thread_timer),
                             RT_TIMER_CTRL_SET_TIME,
                             &timeout);
            rt_timer_start(&(thread->thread_timer));
        }

        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        /* do schedule */
        rt_schedule();

        /* thread is waked up */
        result = thread->error;
        level  = rt_hw_interrupt_disable();
        if (result != RT_EOK)
            goto __exit;
    }

Q
qiyongzhong0 已提交
215 216
    *data_ptr = queue->queue[queue->get_index].data_ptr;
    *size     = queue->queue[queue->get_index].data_size;
217
    queue->get_index += 1;
Q
qiyongzhong0 已提交
218 219 220 221 222 223 224 225 226
    if (queue->get_index == queue->size)
    {
        queue->get_index = 0;
    }
    queue->is_full = 0;
    if (queue->put_index == queue->get_index)
    {
        queue->is_empty = 1;
    }
227

Q
qiyongzhong0 已提交
228
    if (rt_data_queue_len(queue) <= queue->lwm)
229
    {
230
        /* there is at least one thread in suspended list */
231 232 233 234 235 236 237 238 239 240 241 242 243 244
        if (!rt_list_isempty(&(queue->suspended_push_list)))
        {
            /* get thread entry */
            thread = rt_list_entry(queue->suspended_push_list.next,
                                   struct rt_thread,
                                   tlist);

            /* resume it */
            rt_thread_resume(thread);
            rt_hw_interrupt_enable(level);

            /* perform a schedule */
            rt_schedule();
        }
245 246 247 248
        else
        {
            rt_hw_interrupt_enable(level);
        }
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

        if (queue->evt_notify != RT_NULL)
            queue->evt_notify(queue, RT_DATAQUEUE_EVENT_LWM);

        return result;
    }

__exit:
    rt_hw_interrupt_enable(level);
    if ((result == RT_EOK) && (queue->evt_notify != RT_NULL))
    {
        queue->evt_notify(queue, RT_DATAQUEUE_EVENT_POP);
    }

    return result;
}
RTM_EXPORT(rt_data_queue_pop);

267
rt_err_t rt_data_queue_peek(struct rt_data_queue *queue,
268 269 270 271
                            const void** data_ptr,
                            rt_size_t *size)
{
    rt_ubase_t  level;
272

273
    RT_ASSERT(queue != RT_NULL);
Q
qiyongzhong0 已提交
274
    RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
275

276
    if (queue->is_empty)
277 278 279 280
    {
        return -RT_EEMPTY;
    }

Q
qiyongzhong0 已提交
281 282 283 284
    level = rt_hw_interrupt_disable();

    *data_ptr = queue->queue[queue->get_index].data_ptr;
    *size     = queue->queue[queue->get_index].data_size;
285 286 287 288 289

    rt_hw_interrupt_enable(level);

    return RT_EOK;
}
290
RTM_EXPORT(rt_data_queue_peek);
291 292 293

void rt_data_queue_reset(struct rt_data_queue *queue)
{
Q
qiyongzhong0 已提交
294
    rt_ubase_t  level;
295
    struct rt_thread *thread;
296

Q
qiyongzhong0 已提交
297
    RT_ASSERT(queue != RT_NULL);
298
    RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
Q
qiyongzhong0 已提交
299 300 301 302 303 304 305

    level = rt_hw_interrupt_disable();

    queue->get_index = 0;
    queue->put_index = 0;
    queue->is_empty = 1;
    queue->is_full = 0;
306

Q
qiyongzhong0 已提交
307
    rt_hw_interrupt_enable(level);
308

309 310 311 312 313 314 315
    rt_enter_critical();
    /* wakeup all suspend threads */

    /* resume on pop list */
    while (!rt_list_isempty(&(queue->suspended_pop_list)))
    {
        /* disable interrupt */
Q
qiyongzhong0 已提交
316
        level = rt_hw_interrupt_disable();
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

        /* get next suspend thread */
        thread = rt_list_entry(queue->suspended_pop_list.next,
                               struct rt_thread,
                               tlist);
        /* set error code to RT_ERROR */
        thread->error = -RT_ERROR;

        /*
         * resume thread
         * In rt_thread_resume function, it will remove current thread from
         * suspend list
         */
        rt_thread_resume(thread);

        /* enable interrupt */
Q
qiyongzhong0 已提交
333
        rt_hw_interrupt_enable(level);
334 335 336 337 338 339
    }

    /* resume on push list */
    while (!rt_list_isempty(&(queue->suspended_push_list)))
    {
        /* disable interrupt */
Q
qiyongzhong0 已提交
340
        level = rt_hw_interrupt_disable();
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

        /* get next suspend thread */
        thread = rt_list_entry(queue->suspended_push_list.next,
                               struct rt_thread,
                               tlist);
        /* set error code to RT_ERROR */
        thread->error = -RT_ERROR;

        /*
         * resume thread
         * In rt_thread_resume function, it will remove current thread from
         * suspend list
         */
        rt_thread_resume(thread);

        /* enable interrupt */
Q
qiyongzhong0 已提交
357
        rt_hw_interrupt_enable(level);
358 359 360 361 362 363
    }
    rt_exit_critical();

    rt_schedule();
}
RTM_EXPORT(rt_data_queue_reset);
364 365 366 367 368 369

rt_err_t rt_data_queue_deinit(struct rt_data_queue *queue)
{
    rt_ubase_t level;

    RT_ASSERT(queue != RT_NULL);
Q
qiyongzhong0 已提交
370
    RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
371 372 373 374

    /* wakeup all suspend threads */
    rt_data_queue_reset(queue);

Q
qiyongzhong0 已提交
375
    level = rt_hw_interrupt_disable();
376
    queue->magic = 0;
377
    rt_hw_interrupt_enable(level);
378

Q
qiyongzhong0 已提交
379
    rt_free(queue->queue);
380 381 382 383

    return RT_EOK;
}
RTM_EXPORT(rt_data_queue_deinit);
Q
qiyongzhong0 已提交
384 385 386 387 388

rt_uint16_t rt_data_queue_len(struct rt_data_queue *queue)
{
    rt_ubase_t level;
    rt_int16_t len;
389

Q
qiyongzhong0 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    RT_ASSERT(queue != RT_NULL);
    RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);

    if (queue->is_empty)
    {
        return 0;
    }

    level = rt_hw_interrupt_disable();

    if (queue->put_index > queue->get_index)
    {
        len = queue->put_index - queue->get_index;
    }
    else
    {
        len = queue->size + queue->put_index - queue->get_index;
    }
408

Q
qiyongzhong0 已提交
409 410 411 412 413 414
    rt_hw_interrupt_enable(level);

    return len;
}
RTM_EXPORT(rt_data_queue_len);