ipc.c 109.3 KB
Newer Older
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-14     Bernard      the first version
 * 2006-04-25     Bernard      implement semaphore
10
 * 2006-05-03     Bernard      add RT_IPC_DEBUG
11 12 13 14 15 16 17 18 19 20
 *                             modify the type of IPC waiting time to rt_int32_t
 * 2006-05-10     Bernard      fix the semaphore take bug and add IPC object
 * 2006-05-12     Bernard      implement mailbox and message queue
 * 2006-05-20     Bernard      implement mutex
 * 2006-05-23     Bernard      implement fast event
 * 2006-05-24     Bernard      implement event
 * 2006-06-03     Bernard      fix the thread timer init bug
 * 2006-06-05     Bernard      fix the mutex release bug
 * 2006-06-07     Bernard      fix the message queue send bug
 * 2006-08-04     Bernard      add hook support
B
bernard.xiong 已提交
21
 * 2009-05-21     Yi.qiu       fix the sem release bug
22 23
 * 2009-07-18     Bernard      fix the event clear bug
 * 2009-09-09     Bernard      remove fast event and fix ipc release bug
24
 * 2009-10-10     Bernard      change semaphore and mutex value to unsigned value
B
bernard.xiong 已提交
25
 * 2009-10-25     Bernard      change the mb/mq receive timeout to 0 if the
26
 *                             re-calculated delta tick is a negative number.
B
bernard.xiong 已提交
27
 * 2009-12-16     Bernard      fix the rt_ipc_object_suspend issue when IPC flag
28
 *                             is RT_IPC_FLAG_PRIO
29
 * 2010-01-20     mbbill       remove rt_ipc_object_decrease function.
30
 * 2010-04-20     Bernard      move memcpy outside interrupt disable in mq
qiuyiuestc's avatar
qiuyiuestc 已提交
31
 * 2010-10-26     yi.qiu       add module support in rt_mp_delete and rt_mq_delete
32
 * 2010-11-10     Bernard      add IPC reset command implementation.
33
 * 2011-12-18     Bernard      add more parameter checking in message queue
34
 * 2013-09-14     Grissiom     add an option check in rt_event_recv
B
Bernard Xiong 已提交
35
 * 2018-10-02     Bernard      add 64bit support for mailbox
36
 * 2019-09-16     tyx          add send wait support for message queue
mysterywolf's avatar
mysterywolf 已提交
37
 * 2020-07-29     Meco Man     fix thread->event_set/event_info when received an
mysterywolf's avatar
mysterywolf 已提交
38
 *                             event without pending
39
 * 2020-10-11     Meco Man     add value overflow-check code
mysterywolf's avatar
mysterywolf 已提交
40 41
 * 2021-01-03     Meco Man     implement rt_mb_urgent()
 * 2021-05-30     Meco Man     implement rt_mutex_trytake()
42
 * 2022-01-07     Gabriel      Moving __on_rt_xxxxx_hook to ipc.c
43
 * 2022-01-24     THEWON       let rt_mutex_take return thread->error when using signal
44 45 46 47 48
 */

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

49 50 51 52 53 54 55 56 57 58 59
#ifndef __on_rt_object_trytake_hook
    #define __on_rt_object_trytake_hook(parent)     __ON_HOOK_ARGS(rt_object_trytake_hook, (parent))
#endif
#ifndef __on_rt_object_take_hook
    #define __on_rt_object_take_hook(parent)        __ON_HOOK_ARGS(rt_object_take_hook, (parent))
#endif
#ifndef __on_rt_object_put_hook
    #define __on_rt_object_put_hook(parent)         __ON_HOOK_ARGS(rt_object_put_hook, (parent))
#endif

#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
D
dzzxzz 已提交
60 61 62
extern void (*rt_object_trytake_hook)(struct rt_object *object);
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
63
#endif /* RT_USING_HOOK */
64 65 66 67 68

/**
 * @addtogroup IPC
 */

D
dogandog 已提交
69
/**@{*/
70 71

/**
C
chenjh 已提交
72
 * @brief    This function will initialize an IPC object, such as semaphore, mutex, messagequeue and mailbox.
73
 *
C
chenjh 已提交
74
 * @note     Executing this function will complete an initialization of the suspend thread list of the ipc object.
75
 *
C
chenjh 已提交
76 77 78 79 80 81
 * @param    ipc is a pointer to the IPC object.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           When the return value is any other values, it means the initialization failed.
 *
 * @warning  This function can be called from all IPC initialization and creation.
82
 */
mysterywolf's avatar
mysterywolf 已提交
83
rt_inline rt_err_t _ipc_object_init(struct rt_ipc_object *ipc)
84
{
B
Bernard Xiong 已提交
85
    /* initialize ipc object */
D
dzzxzz@gmail.com 已提交
86
    rt_list_init(&(ipc->suspend_thread));
87

D
dzzxzz@gmail.com 已提交
88
    return RT_EOK;
89 90
}

C
chenjh 已提交
91

92
/**
C
chenjh 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
 * @brief    This function will suspend a thread to a IPC object list.
 *
 * @param    list is a pointer to a suspended thread list of the IPC object.
 *
 * @param    thread is a pointer to the thread object to be suspended.
 *
 * @param    flag is a flag for the thread object to be suspended. It determines how the thread is suspended.
 *           The flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
 *
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                         (also known as first-come-first-served (FCFS) scheduling strategy).
 *
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to use
 *               RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this semaphore will become non-real-time threads.
111
 *
C
chenjh 已提交
112 113
 * @return   Return the operation status. When the return value is RT_EOK, the function is successfully executed.
 *           When the return value is any other values, it means the initialization failed.
114
 *
C
chenjh 已提交
115 116 117 118 119
 * @warning  This function can ONLY be called in the thread context, you can use RT_DEBUG_IN_THREAD_CONTEXT to
 *           check the context.
 *           In addition, this function is generally called by the following functions:
 *           rt_sem_take(),  rt_mutex_take(),  rt_event_recv(),   rt_mb_send_wait(),
 *           rt_mb_recv(),   rt_mq_recv(),     rt_mq_send_wait()
120
 */
mysterywolf's avatar
mysterywolf 已提交
121 122 123
rt_inline rt_err_t _ipc_list_suspend(rt_list_t        *list,
                                     struct rt_thread *thread,
                                     rt_uint8_t        flag)
124
{
D
dzzxzz@gmail.com 已提交
125 126 127 128 129 130 131
    /* suspend thread */
    rt_thread_suspend(thread);

    switch (flag)
    {
    case RT_IPC_FLAG_FIFO:
        rt_list_insert_before(list, &(thread->tlist));
132
        break; /* RT_IPC_FLAG_FIFO */
D
dzzxzz@gmail.com 已提交
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 158 159

    case RT_IPC_FLAG_PRIO:
        {
            struct rt_list_node *n;
            struct rt_thread *sthread;

            /* find a suitable position */
            for (n = list->next; n != list; n = n->next)
            {
                sthread = rt_list_entry(n, struct rt_thread, tlist);

                /* find out */
                if (thread->current_priority < sthread->current_priority)
                {
                    /* insert this thread before the sthread */
                    rt_list_insert_before(&(sthread->tlist), &(thread->tlist));
                    break;
                }
            }

            /*
             * not found a suitable position,
             * append to the end of suspend_thread list
             */
            if (n == list)
                rt_list_insert_before(list, &(thread->tlist));
        }
160
        break;/* RT_IPC_FLAG_PRIO */
C
chenchaoqun 已提交
161 162

    default:
T
tangyuxin 已提交
163
        RT_ASSERT(0);
mysterywolf's avatar
mysterywolf 已提交
164
        break;
D
dzzxzz@gmail.com 已提交
165 166 167
    }

    return RT_EOK;
168 169
}

C
chenjh 已提交
170

171
/**
C
chenjh 已提交
172 173 174 175 176 177 178 179 180 181
 * @brief    This function will resume a thread.
 *
 * @note     This function will resume the first thread in the list of a IPC object.
 *           1. remove the thread from suspend queue of a IPC object.
 *           2. put the thread into system ready queue.
 *
 *           By contrast, the rt_ipc_list_resume_all() function will resume all suspended threads
 *           in the list of a IPC object.
 *
 * @param    list is a pointer to a suspended thread list of the IPC object.
182
 *
C
chenjh 已提交
183 184
 * @return   Return the operation status. When the return value is RT_EOK, the function is successfully executed.
 *           When the return value is any other values, it means this operation failed.
185
 *
C
chenjh 已提交
186 187 188
 * @warning  This function is generally called by the following functions:
 *           rt_sem_release(),    rt_mutex_release(),    rt_mb_send_wait(),    rt_mq_send_wait(),
 *           rt_mb_urgent(),      rt_mb_recv(),          rt_mq_urgent(),       rt_mq_recv(),
189
 */
mysterywolf's avatar
mysterywolf 已提交
190
rt_inline rt_err_t _ipc_list_resume(rt_list_t *list)
191
{
D
dzzxzz@gmail.com 已提交
192
    struct rt_thread *thread;
193

D
dzzxzz@gmail.com 已提交
194 195
    /* get thread entry */
    thread = rt_list_entry(list->next, struct rt_thread, tlist);
196

D
dzzxzz@gmail.com 已提交
197
    RT_DEBUG_LOG(RT_DEBUG_IPC, ("resume thread:%s\n", thread->name));
198

D
dzzxzz@gmail.com 已提交
199 200
    /* resume it */
    rt_thread_resume(thread);
201

D
dzzxzz@gmail.com 已提交
202
    return RT_EOK;
203 204
}

C
chenjh 已提交
205

206
/**
C
chenjh 已提交
207 208 209 210 211
 * @brief   This function will resume all suspended threads in the IPC object list,
 *          including the suspended list of IPC object, and private list of mailbox etc.
 *
 * @note    This function will resume all threads in the IPC object list.
 *          By contrast, the rt_ipc_list_resume() function will resume a suspended thread in the list of a IPC object.
212
 *
C
chenjh 已提交
213 214 215 216
 * @param   list is a pointer to a suspended thread list of the IPC object.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the function is successfully executed.
 *           When the return value is any other values, it means this operation failed.
217 218
 *
 */
mysterywolf's avatar
mysterywolf 已提交
219
rt_inline rt_err_t _ipc_list_resume_all(rt_list_t *list)
220
{
D
dzzxzz@gmail.com 已提交
221 222 223
    struct rt_thread *thread;
    register rt_ubase_t temp;

B
Bernard Xiong 已提交
224
    /* wakeup all suspended threads */
D
dzzxzz@gmail.com 已提交
225 226 227 228 229
    while (!rt_list_isempty(list))
    {
        /* disable interrupt */
        temp = rt_hw_interrupt_disable();

B
Bernard Xiong 已提交
230
        /* get next suspended thread */
D
dzzxzz@gmail.com 已提交
231 232 233 234 235 236 237
        thread = rt_list_entry(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
B
Bernard Xiong 已提交
238
         * suspended list
D
dzzxzz@gmail.com 已提交
239 240 241 242 243 244 245 246
         */
        rt_thread_resume(thread);

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
    }

    return RT_EOK;
247 248
}

C
chenjh 已提交
249 250
/**@}*/

251 252
#ifdef RT_USING_SEMAPHORE
/**
C
chenjh 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
 * @addtogroup semaphore
 */

/**@{*/
/**
 * @brief    This function will initialize a static semaphore object.
 *
 * @note     For the static semaphore object, its memory space is allocated by the compiler during compiling,
 *           and shall placed on the read-write data segment or on the uninitialized data segment.
 *           By contrast, the rt_sem_create() function will allocate memory space automatically and initialize
 *           the semaphore.
 *
 * @see      rt_sem_create()
 *
 * @param    sem is a pointer to the semaphore to initialize. It is assumed that storage for the semaphore will be
 *           allocated in your application.
269
 *
C
chenjh 已提交
270
 * @param    name is a pointer to the name you would like to give the semaphore.
271
 *
C
chenjh 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
 * @param    value is the initial value for the semaphore.
 *           If used to share resources, you should initialize the value as the number of available resources.
 *           If used to signal the occurrence of an event, you should initialize the value as 0.
 *
 * @param    flag is the semaphore flag, which determines the queuing way of how multiple threads wait
 *           when the semaphore is not available.
 *           The semaphore flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
 *
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                         (also known as first-come-first-served (FCFS) scheduling strategy).
 *
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
 *               use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this semaphore will become non-real-time threads.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it represents the initialization failed.
 *
 * @warning  This function can ONLY be called from threads.
294
 */
D
dzzxzz@gmail.com 已提交
295 296 297 298
rt_err_t rt_sem_init(rt_sem_t    sem,
                     const char *name,
                     rt_uint32_t value,
                     rt_uint8_t  flag)
299
{
D
dzzxzz@gmail.com 已提交
300
    RT_ASSERT(sem != RT_NULL);
301
    RT_ASSERT(value < 0x10000U);
302
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
303

B
Bernard Xiong 已提交
304
    /* initialize object */
D
dzzxzz@gmail.com 已提交
305
    rt_object_init(&(sem->parent.parent), RT_Object_Class_Semaphore, name);
306

B
Bernard Xiong 已提交
307
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
308
    _ipc_object_init(&(sem->parent));
309

B
Bernard Xiong 已提交
310
    /* set initial value */
311
    sem->value = (rt_uint16_t)value;
312

D
dzzxzz@gmail.com 已提交
313 314
    /* set parent */
    sem->parent.parent.flag = flag;
315

D
dzzxzz@gmail.com 已提交
316
    return RT_EOK;
317
}
318
RTM_EXPORT(rt_sem_init);
319

C
chenjh 已提交
320

321
/**
C
chenjh 已提交
322 323 324 325 326
 * @brief    This function will detach a static semaphore object.
 *
 * @note     This function is used to detach a static semaphore object which is initialized by rt_sem_init() function.
 *           By contrast, the rt_sem_delete() function will delete a semaphore object.
 *           When the semaphore is successfully detached, it will resume all suspended threads in the semaphore list.
327
 *
C
chenjh 已提交
328
 * @see      rt_sem_delete()
329
 *
C
chenjh 已提交
330
 * @param    sem is a pointer to a semaphore object to be detached.
331
 *
C
chenjh 已提交
332 333 334 335 336 337
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it means that the semaphore detach failed.
 *
 * @warning  This function can ONLY detach a static semaphore initialized by the rt_sem_init() function.
 *           If the semaphore is created by the rt_sem_create() function, you MUST NOT USE this function to detach it,
 *           ONLY USE the rt_sem_delete() function to complete the deletion.
338
 */
D
dzzxzz 已提交
339
rt_err_t rt_sem_detach(rt_sem_t sem)
340
{
341
    /* parameter check */
D
dzzxzz@gmail.com 已提交
342
    RT_ASSERT(sem != RT_NULL);
343 344
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
    RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent));
345

B
Bernard Xiong 已提交
346
    /* wakeup all suspended threads */
mysterywolf's avatar
mysterywolf 已提交
347
    _ipc_list_resume_all(&(sem->parent.suspend_thread));
348

D
dzzxzz@gmail.com 已提交
349 350
    /* detach semaphore object */
    rt_object_detach(&(sem->parent.parent));
351

D
dzzxzz@gmail.com 已提交
352
    return RT_EOK;
353
}
354
RTM_EXPORT(rt_sem_detach);
355 356 357

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
358 359 360 361 362 363 364 365 366 367 368 369
 * @brief    Creating a semaphore object.
 *
 * @note     For the semaphore object, its memory space is allocated automatically.
 *           By contrast, the rt_sem_init() function will initialize a static semaphore object.
 *
 * @see      rt_sem_init()
 *
 * @param    name is a pointer to the name you would like to give the semaphore.
 *
 * @param    value is the initial value for the semaphore.
 *           If used to share resources, you should initialize the value as the number of available resources.
 *           If used to signal the occurrence of an event, you should initialize the value as 0.
370
 *
C
chenjh 已提交
371 372 373
 * @param    flag is the semaphore flag, which determines the queuing way of how multiple threads wait
 *           when the semaphore is not available.
 *           The semaphore flag can be ONE of the following values:
374
 *
C
chenjh 已提交
375
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
376
 *
C
chenjh 已提交
377 378 379 380 381 382 383 384 385 386 387
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                         (also known as first-come-first-served (FCFS) scheduling strategy).
 *
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
 *               use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this semaphore will become non-real-time threads.
 *
 * @return   Return a pointer to the semaphore object. When the return value is RT_NULL, it means the creation failed.
 *
 * @warning  This function can NOT be called in interrupt context. You can use macor RT_DEBUG_NOT_IN_INTERRUPT to check it.
388
 */
D
dzzxzz 已提交
389
rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)
390
{
D
dzzxzz@gmail.com 已提交
391
    rt_sem_t sem;
392

393
    RT_ASSERT(value < 0x10000U);
394
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
395

396 397
    RT_DEBUG_NOT_IN_INTERRUPT;

D
dzzxzz@gmail.com 已提交
398 399 400 401
    /* allocate object */
    sem = (rt_sem_t)rt_object_allocate(RT_Object_Class_Semaphore, name);
    if (sem == RT_NULL)
        return sem;
402

B
Bernard Xiong 已提交
403
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
404
    _ipc_object_init(&(sem->parent));
405

B
Bernard Xiong 已提交
406
    /* set initial value */
D
dzzxzz@gmail.com 已提交
407
    sem->value = value;
408

D
dzzxzz@gmail.com 已提交
409 410
    /* set parent */
    sem->parent.parent.flag = flag;
411

D
dzzxzz@gmail.com 已提交
412
    return sem;
413
}
414
RTM_EXPORT(rt_sem_create);
415

C
chenjh 已提交
416

417
/**
C
chenjh 已提交
418
 * @brief    This function will delete a semaphore object and release the memory space.
419
 *
C
chenjh 已提交
420 421 422
 * @note     This function is used to delete a semaphore object which is created by the rt_sem_create() function.
 *           By contrast, the rt_sem_detach() function will detach a static semaphore object.
 *           When the semaphore is successfully deleted, it will resume all suspended threads in the semaphore list.
423
 *
C
chenjh 已提交
424
 * @see      rt_sem_detach()
425
 *
C
chenjh 已提交
426 427 428 429 430 431 432 433
 * @param    sem is a pointer to a semaphore object to be deleted.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the semaphore detach failed.
 *
 * @warning  This function can ONLY delete a semaphore initialized by the rt_sem_create() function.
 *           If the semaphore is initialized by the rt_sem_init() function, you MUST NOT USE this function to delete it,
 *           ONLY USE the rt_sem_detach() function to complete the detachment.
434
 */
D
dzzxzz 已提交
435
rt_err_t rt_sem_delete(rt_sem_t sem)
436
{
437
    /* parameter check */
D
dzzxzz@gmail.com 已提交
438
    RT_ASSERT(sem != RT_NULL);
439 440
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
    RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent) == RT_FALSE);
441

442 443
    RT_DEBUG_NOT_IN_INTERRUPT;

B
Bernard Xiong 已提交
444
    /* wakeup all suspended threads */
mysterywolf's avatar
mysterywolf 已提交
445
    _ipc_list_resume_all(&(sem->parent.suspend_thread));
446

D
dzzxzz@gmail.com 已提交
447 448
    /* delete semaphore object */
    rt_object_delete(&(sem->parent.parent));
449

D
dzzxzz@gmail.com 已提交
450
    return RT_EOK;
451
}
452
RTM_EXPORT(rt_sem_delete);
453
#endif /* RT_USING_HEAP */
454

C
chenjh 已提交
455

456
/**
C
chenjh 已提交
457 458 459 460 461 462 463
 * @brief    This function will take a semaphore, if the semaphore is unavailable, the thread shall wait for
 *           the semaphore up to a specified time.
 *
 * @note     When this function is called, the count value of the sem->value will decrease 1 until it is equal to 0.
 *           When the sem->value is 0, it means that the semaphore is unavailable. At this time, it will suspend the
 *           thread preparing to take the semaphore.
 *           On the contrary, the rt_sem_release() function will increase the count value of sem->value by 1 each time.
464
 *
C
chenjh 已提交
465
 * @see      rt_sem_trytake()
466
 *
C
chenjh 已提交
467 468 469 470 471 472 473 474 475 476 477
 * @param    sem is a pointer to a semaphore object.
 *
 * @param    time is a timeout period (unit: an OS tick). If the semaphore is unavailable, the thread will wait for
 *           the semaphore up to the amount of time specified by the argument.
 *           NOTE: Generally, we use the macro RT_WAITING_FOREVER to set this parameter, which means that when the
 *           semaphore is unavailable, the thread will be waitting forever.
 *
 * @return   Return the operation status. ONLY When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the semaphore take failed.
 *
 * @warning  This function can ONLY be called in the thread context. It MUST NOT BE called in interrupt context.
478
 */
D
dzzxzz 已提交
479
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
480
{
D
dzzxzz@gmail.com 已提交
481 482
    register rt_base_t temp;
    struct rt_thread *thread;
483

484
    /* parameter check */
D
dzzxzz@gmail.com 已提交
485
    RT_ASSERT(sem != RT_NULL);
486
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
487 488 489 490 491
    /* current context checking */
    if (time != 0)
    {
        RT_DEBUG_SCHEDULER_AVAILABLE;
    }
492

D
dzzxzz@gmail.com 已提交
493
    RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));
494

D
dzzxzz@gmail.com 已提交
495 496
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
497

D
dzzxzz@gmail.com 已提交
498
    RT_DEBUG_LOG(RT_DEBUG_IPC, ("thread %s take sem:%s, which value is: %d\n",
499 500 501
                                rt_thread_self()->name,
                                ((struct rt_object *)sem)->name,
                                sem->value));
502

D
dzzxzz@gmail.com 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    if (sem->value > 0)
    {
        /* semaphore is available */
        sem->value --;

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
    }
    else
    {
        /* no waiting, return with timeout */
        if (time == 0)
        {
            rt_hw_interrupt_enable(temp);

            return -RT_ETIMEOUT;
        }
        else
        {
            /* semaphore is unavailable, push to suspend list */
            /* get current thread */
            thread = rt_thread_self();

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

            RT_DEBUG_LOG(RT_DEBUG_IPC, ("sem take: suspend thread - %s\n",
530
                                        thread->name));
531

D
dzzxzz@gmail.com 已提交
532
            /* suspend thread */
mysterywolf's avatar
mysterywolf 已提交
533
            _ipc_list_suspend(&(sem->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
534 535
                                thread,
                                sem->parent.parent.flag);
536

D
dzzxzz@gmail.com 已提交
537 538 539 540
            /* has waiting time, start thread timer */
            if (time > 0)
            {
                RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",
541
                                            thread->name));
542

D
dzzxzz@gmail.com 已提交
543 544 545 546 547 548
                /* reset the timeout of thread timer and start it */
                rt_timer_control(&(thread->thread_timer),
                                 RT_TIMER_CTRL_SET_TIME,
                                 &time);
                rt_timer_start(&(thread->thread_timer));
            }
549

D
dzzxzz@gmail.com 已提交
550 551
            /* enable interrupt */
            rt_hw_interrupt_enable(temp);
552

D
dzzxzz@gmail.com 已提交
553 554
            /* do schedule */
            rt_schedule();
555

D
dzzxzz@gmail.com 已提交
556 557 558 559 560 561
            if (thread->error != RT_EOK)
            {
                return thread->error;
            }
        }
    }
562

D
dzzxzz@gmail.com 已提交
563
    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(sem->parent.parent)));
564

D
dzzxzz@gmail.com 已提交
565
    return RT_EOK;
566
}
567
RTM_EXPORT(rt_sem_take);
568

C
chenjh 已提交
569

570
/**
C
chenjh 已提交
571
 * @brief    This function will try to take a semaphore, if the semaphore is unavailable, the thread returns immediately.
572
 *
C
chenjh 已提交
573 574 575
 * @note     This function is very similar to the rt_sem_take() function, when the semaphore is not available,
 *           the rt_sem_trytake() function will return immediately without waiting for a timeout.
 *           In other words, rt_sem_trytake(sem) has the same effect as rt_sem_take(sem, 0).
576
 *
C
chenjh 已提交
577 578 579 580 581 582
 * @see      rt_sem_take()
 *
 * @param    sem is a pointer to a semaphore object.
 *
 * @return   Return the operation status. ONLY When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the semaphore take failed.
583 584 585
 */
rt_err_t rt_sem_trytake(rt_sem_t sem)
{
mysterywolf's avatar
mysterywolf 已提交
586
    return rt_sem_take(sem, RT_WAITING_NO);
587
}
588
RTM_EXPORT(rt_sem_trytake);
589

C
chenjh 已提交
590

591
/**
C
chenjh 已提交
592 593 594 595 596
 * @brief    This function will release a semaphore. If there is thread suspended on the semaphore, it will get resumed.
 *
 * @note     If there are threads suspended on this semaphore, the first thread in the list of this semaphore object
 *           will be resumed, and a thread scheduling (rt_schedule) will be executed.
 *           If no threads are suspended on this semaphore, the count value sem->value of this semaphore will increase by 1.
597
 *
C
chenjh 已提交
598
 * @param    sem is a pointer to a semaphore object.
599
 *
C
chenjh 已提交
600 601
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the semaphore release failed.
602 603 604
 */
rt_err_t rt_sem_release(rt_sem_t sem)
{
D
dzzxzz@gmail.com 已提交
605 606
    register rt_base_t temp;
    register rt_bool_t need_schedule;
607

608 609 610 611
    /* parameter check */
    RT_ASSERT(sem != RT_NULL);
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);

D
dzzxzz@gmail.com 已提交
612
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(sem->parent.parent)));
613

D
dzzxzz@gmail.com 已提交
614
    need_schedule = RT_FALSE;
615

D
dzzxzz@gmail.com 已提交
616 617
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
618

D
dzzxzz@gmail.com 已提交
619
    RT_DEBUG_LOG(RT_DEBUG_IPC, ("thread %s releases sem:%s, which value is: %d\n",
620 621 622
                                rt_thread_self()->name,
                                ((struct rt_object *)sem)->name,
                                sem->value));
623

D
dzzxzz@gmail.com 已提交
624 625 626
    if (!rt_list_isempty(&sem->parent.suspend_thread))
    {
        /* resume the suspended thread */
mysterywolf's avatar
mysterywolf 已提交
627
        _ipc_list_resume(&(sem->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
628 629 630
        need_schedule = RT_TRUE;
    }
    else
mysterywolf's avatar
mysterywolf 已提交
631
    {
mysterywolf's avatar
mysterywolf 已提交
632
        if(sem->value < RT_SEM_VALUE_MAX)
mysterywolf's avatar
mysterywolf 已提交
633 634 635 636 637 638 639 640 641
        {
            sem->value ++; /* increase value */
        }
        else
        {
            rt_hw_interrupt_enable(temp); /* enable interrupt */
            return -RT_EFULL; /* value overflowed */
        }
    }
642

D
dzzxzz@gmail.com 已提交
643 644
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
645

D
dzzxzz@gmail.com 已提交
646 647 648
    /* resume a thread, re-schedule */
    if (need_schedule == RT_TRUE)
        rt_schedule();
649

D
dzzxzz@gmail.com 已提交
650
    return RT_EOK;
651
}
652
RTM_EXPORT(rt_sem_release);
653

C
chenjh 已提交
654

655
/**
C
chenjh 已提交
656 657 658 659 660 661 662
 * @brief    This function will set some extra attributions of a semaphore object.
 *
 * @note     Currently this function only supports the RT_IPC_CMD_RESET command to reset the semaphore.
 *
 * @param    sem is a pointer to a semaphore object.
 *
 * @param    cmd is a command word used to configure some attributions of the semaphore.
663
 *
C
chenjh 已提交
664
 * @param    arg is the argument of the function to execute the command.
665
 *
C
chenjh 已提交
666 667
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that this function failed to execute.
668
 */
B
bernard 已提交
669
rt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg)
670
{
D
dzzxzz@gmail.com 已提交
671
    rt_ubase_t level;
672 673

    /* parameter check */
D
dzzxzz@gmail.com 已提交
674
    RT_ASSERT(sem != RT_NULL);
675
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
676

D
dzzxzz@gmail.com 已提交
677 678
    if (cmd == RT_IPC_CMD_RESET)
    {
B
Bernard Xiong 已提交
679
        rt_ubase_t value;
680

D
dzzxzz@gmail.com 已提交
681
        /* get value */
B
Bernard Xiong 已提交
682
        value = (rt_ubase_t)arg;
D
dzzxzz@gmail.com 已提交
683 684
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
685

D
dzzxzz@gmail.com 已提交
686
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
687
        _ipc_list_resume_all(&sem->parent.suspend_thread);
688

D
dzzxzz@gmail.com 已提交
689 690
        /* set new value */
        sem->value = (rt_uint16_t)value;
691

D
dzzxzz@gmail.com 已提交
692 693
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
694

D
dzzxzz@gmail.com 已提交
695
        rt_schedule();
696

D
dzzxzz@gmail.com 已提交
697 698
        return RT_EOK;
    }
699

D
dzzxzz@gmail.com 已提交
700
    return -RT_ERROR;
701
}
702
RTM_EXPORT(rt_sem_control);
C
chenjh 已提交
703 704

/**@}*/
705
#endif /* RT_USING_SEMAPHORE */
706 707 708

#ifdef RT_USING_MUTEX
/**
C
chenjh 已提交
709 710 711 712 713 714 715 716 717 718 719 720
 * @addtogroup mutex
 */

/**@{*/

/**
 * @brief    Initialize a static mutex object.
 *
 * @note     For the static mutex object, its memory space is allocated by the compiler during compiling,
 *           and shall placed on the read-write data segment or on the uninitialized data segment.
 *           By contrast, the rt_mutex_create() function will automatically allocate memory space
 *           and initialize the mutex.
721
 *
C
chenjh 已提交
722
 * @see      rt_mutex_create()
723
 *
C
chenjh 已提交
724 725 726 727 728 729 730
 * @param    mutex is a pointer to the mutex to initialize. It is assumed that storage for the mutex will be
 *           allocated in your application.
 *
 * @param    name is a pointer to the name that given to the mutex.
 *
 * @param    flag is the mutex flag, which determines the queuing way of how multiple threads wait
 *           when the mutex is not available.
mysterywolf's avatar
mysterywolf 已提交
731
 *           NOTE: This parameter has been obsoleted. It can be RT_IPC_FLAG_PRIO, RT_IPC_FLAG_FIFO or RT_NULL.
C
chenjh 已提交
732 733 734 735 736
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it represents the initialization failed.
 *
 * @warning  This function can ONLY be called from threads.
737
 */
D
dzzxzz 已提交
738
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
739
{
mysterywolf's avatar
mysterywolf 已提交
740 741
    /* flag parameter has been obsoleted */
    RT_UNUSED(flag);
742

743
    /* parameter check */
D
dzzxzz@gmail.com 已提交
744
    RT_ASSERT(mutex != RT_NULL);
745

B
Bernard Xiong 已提交
746
    /* initialize object */
D
dzzxzz@gmail.com 已提交
747
    rt_object_init(&(mutex->parent.parent), RT_Object_Class_Mutex, name);
748

B
Bernard Xiong 已提交
749
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
750
    _ipc_object_init(&(mutex->parent));
751

D
dzzxzz@gmail.com 已提交
752 753 754 755
    mutex->value = 1;
    mutex->owner = RT_NULL;
    mutex->original_priority = 0xFF;
    mutex->hold  = 0;
756

mysterywolf's avatar
mysterywolf 已提交
757
    /* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */
758
    mutex->parent.parent.flag = RT_IPC_FLAG_PRIO;
759

D
dzzxzz@gmail.com 已提交
760
    return RT_EOK;
761
}
762
RTM_EXPORT(rt_mutex_init);
763

C
chenjh 已提交
764

765
/**
C
chenjh 已提交
766
 * @brief    This function will detach a static mutex object.
767
 *
C
chenjh 已提交
768 769 770
 * @note     This function is used to detach a static mutex object which is initialized by rt_mutex_init() function.
 *           By contrast, the rt_mutex_delete() function will delete a mutex object.
 *           When the mutex is successfully detached, it will resume all suspended threads in the mutex list.
771
 *
C
chenjh 已提交
772
 * @see      rt_mutex_delete()
773
 *
C
chenjh 已提交
774 775 776 777 778 779 780 781
 * @param    mutex is a pointer to a mutex object to be detached.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it means that the mutex detach failed.
 *
 * @warning  This function can ONLY detach a static mutex initialized by the rt_mutex_init() function.
 *           If the mutex is created by the rt_mutex_create() function, you MUST NOT USE this function to detach it,
 *           ONLY USE the rt_mutex_delete() function to complete the deletion.
782
 */
D
dzzxzz 已提交
783
rt_err_t rt_mutex_detach(rt_mutex_t mutex)
784
{
785
    /* parameter check */
D
dzzxzz@gmail.com 已提交
786
    RT_ASSERT(mutex != RT_NULL);
787 788
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
    RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent));
789

B
Bernard Xiong 已提交
790
    /* wakeup all suspended threads */
mysterywolf's avatar
mysterywolf 已提交
791
    _ipc_list_resume_all(&(mutex->parent.suspend_thread));
792

793
    /* detach mutex object */
D
dzzxzz@gmail.com 已提交
794
    rt_object_detach(&(mutex->parent.parent));
795

D
dzzxzz@gmail.com 已提交
796
    return RT_EOK;
797
}
798
RTM_EXPORT(rt_mutex_detach);
799 800 801

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
802 803 804 805 806 807
 * @brief    This function will create a mutex object.
 *
 * @note     For the mutex object, its memory space is automatically allocated.
 *           By contrast, the rt_mutex_init() function will initialize a static mutex object.
 *
 * @see      rt_mutex_init()
808
 *
C
chenjh 已提交
809
 * @param    name is a pointer to the name that given to the mutex.
810
 *
C
chenjh 已提交
811 812
 * @param    flag is the mutex flag, which determines the queuing way of how multiple threads wait
 *           when the mutex is not available.
mysterywolf's avatar
mysterywolf 已提交
813
 *           NOTE: This parameter has been obsoleted. It can be RT_IPC_FLAG_PRIO, RT_IPC_FLAG_FIFO or RT_NULL.
814
 *
C
chenjh 已提交
815 816 817
 * @return   Return a pointer to the mutex object. When the return value is RT_NULL, it means the creation failed.
 *
 * @warning  This function can ONLY be called from threads.
818
 */
D
dzzxzz 已提交
819
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
820
{
D
dzzxzz@gmail.com 已提交
821
    struct rt_mutex *mutex;
mysterywolf's avatar
mysterywolf 已提交
822 823 824

    /* flag parameter has been obsoleted */
    RT_UNUSED(flag);
825

D
dzzxzz@gmail.com 已提交
826
    RT_DEBUG_NOT_IN_INTERRUPT;
827

D
dzzxzz@gmail.com 已提交
828 829 830 831
    /* allocate object */
    mutex = (rt_mutex_t)rt_object_allocate(RT_Object_Class_Mutex, name);
    if (mutex == RT_NULL)
        return mutex;
832

B
Bernard Xiong 已提交
833
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
834
    _ipc_object_init(&(mutex->parent));
835

D
dzzxzz@gmail.com 已提交
836 837 838 839
    mutex->value              = 1;
    mutex->owner              = RT_NULL;
    mutex->original_priority  = 0xFF;
    mutex->hold               = 0;
840

mysterywolf's avatar
mysterywolf 已提交
841
    /* flag can only be RT_IPC_FLAG_PRIO. RT_IPC_FLAG_FIFO cannot solve the unbounded priority inversion problem */
842
    mutex->parent.parent.flag = RT_IPC_FLAG_PRIO;
843

D
dzzxzz@gmail.com 已提交
844
    return mutex;
845
}
846
RTM_EXPORT(rt_mutex_create);
847

C
chenjh 已提交
848

849
/**
C
chenjh 已提交
850 851 852 853 854 855 856
 * @brief    This function will delete a mutex object and release this memory space.
 *
 * @note     This function is used to delete a mutex object which is created by the rt_mutex_create() function.
 *           By contrast, the rt_mutex_detach() function will detach a static mutex object.
 *           When the mutex is successfully deleted, it will resume all suspended threads in the mutex list.
 *
 * @see      rt_mutex_detach()
857
 *
C
chenjh 已提交
858
 * @param    mutex is a pointer to a mutex object to be deleted.
859
 *
C
chenjh 已提交
860 861
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mutex detach failed.
862
 *
C
chenjh 已提交
863 864 865
 * @warning  This function can ONLY delete a mutex initialized by the rt_mutex_create() function.
 *           If the mutex is initialized by the rt_mutex_init() function, you MUST NOT USE this function to delete it,
 *           ONLY USE the rt_mutex_detach() function to complete the detachment.
866
 */
D
dzzxzz 已提交
867
rt_err_t rt_mutex_delete(rt_mutex_t mutex)
868
{
869
    /* parameter check */
D
dzzxzz@gmail.com 已提交
870
    RT_ASSERT(mutex != RT_NULL);
871 872
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
    RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent) == RT_FALSE);
873

874 875
    RT_DEBUG_NOT_IN_INTERRUPT;

B
Bernard Xiong 已提交
876
    /* wakeup all suspended threads */
mysterywolf's avatar
mysterywolf 已提交
877
    _ipc_list_resume_all(&(mutex->parent.suspend_thread));
878

I
iglencao 已提交
879
    /* delete mutex object */
D
dzzxzz@gmail.com 已提交
880
    rt_object_delete(&(mutex->parent.parent));
881

D
dzzxzz@gmail.com 已提交
882
    return RT_EOK;
883
}
884
RTM_EXPORT(rt_mutex_delete);
885
#endif /* RT_USING_HEAP */
886

C
chenjh 已提交
887

888
/**
C
chenjh 已提交
889 890 891
 * @brief    This function will take a mutex, if the mutex is unavailable, the thread shall wait for
 *           the mutex up to a specified time.
 *
J
jihongbin 已提交
892 893
 * @note     When this function is called, the count value of the mutex->value will decrease 1 until it is equal to 0.
 *           When the mutex->value is 0, it means that the mutex is unavailable. At this time, it will suspend the
C
chenjh 已提交
894
 *           thread preparing to take the mutex.
J
jihongbin 已提交
895
 *           On the contrary, the rt_mutex_release() function will increase the count value of mutex->value by 1 each time.
C
chenjh 已提交
896 897 898 899
 *
 * @see      rt_mutex_trytake()
 *
 * @param    mutex is a pointer to a mutex object.
900
 *
C
chenjh 已提交
901 902 903 904
 * @param    time is a timeout period (unit: an OS tick). If the mutex is unavailable, the thread will wait for
 *           the mutex up to the amount of time specified by the argument.
 *           NOTE: Generally, we set this parameter to RT_WAITING_FOREVER, which means that when the mutex is unavailable,
 *           the thread will be waitting forever.
905
 *
C
chenjh 已提交
906 907 908 909
 * @return   Return the operation status. ONLY When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mutex take failed.
 *
 * @warning  This function can ONLY be called in the thread context. It MUST NOT BE called in interrupt context.
910
 */
D
dzzxzz 已提交
911
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
912
{
D
dzzxzz@gmail.com 已提交
913 914
    register rt_base_t temp;
    struct rt_thread *thread;
915

B
bernard 已提交
916 917 918
    /* this function must not be used in interrupt even if time = 0 */
    RT_DEBUG_IN_THREAD_CONTEXT;

919 920 921 922 923 924
    /* current context checking */
    if (time != 0)
    {
        RT_DEBUG_SCHEDULER_AVAILABLE;
    }

925
    /* parameter check */
926
    RT_ASSERT(mutex != RT_NULL);
927
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
928 929 930

    /* get current thread */
    thread = rt_thread_self();
931

D
dzzxzz@gmail.com 已提交
932 933
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
934

D
dzzxzz@gmail.com 已提交
935
    RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mutex->parent.parent)));
936

D
dzzxzz@gmail.com 已提交
937
    RT_DEBUG_LOG(RT_DEBUG_IPC,
938 939
                 ("mutex_take: current thread %s, mutex value: %d, hold: %d\n",
                  thread->name, mutex->value, mutex->hold));
940

D
dzzxzz@gmail.com 已提交
941 942 943 944 945
    /* reset thread error */
    thread->error = RT_EOK;

    if (mutex->owner == thread)
    {
mysterywolf's avatar
mysterywolf 已提交
946
        if(mutex->hold < RT_MUTEX_HOLD_MAX)
947 948 949 950 951 952 953 954 955
        {
            /* it's the same thread */
            mutex->hold ++;
        }
        else
        {
            rt_hw_interrupt_enable(temp); /* enable interrupt */
            return -RT_EFULL; /* value overflowed */
        }
D
dzzxzz@gmail.com 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969
    }
    else
    {
        /* The value of mutex is 1 in initial status. Therefore, if the
         * value is great than 0, it indicates the mutex is avaible.
         */
        if (mutex->value > 0)
        {
            /* mutex is available */
            mutex->value --;

            /* set mutex owner and original priority */
            mutex->owner             = thread;
            mutex->original_priority = thread->current_priority;
mysterywolf's avatar
mysterywolf 已提交
970
            if(mutex->hold < RT_MUTEX_HOLD_MAX)
971 972 973 974 975 976 977 978
            {
                mutex->hold ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
D
dzzxzz@gmail.com 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
        }
        else
        {
            /* no waiting, return with timeout */
            if (time == 0)
            {
                /* set error as timeout */
                thread->error = -RT_ETIMEOUT;

                /* enable interrupt */
                rt_hw_interrupt_enable(temp);

                return -RT_ETIMEOUT;
            }
            else
            {
                /* mutex is unavailable, push to suspend list */
                RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_take: suspend thread: %s\n",
997
                                            thread->name));
998

D
dzzxzz@gmail.com 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
                /* change the owner thread priority of mutex */
                if (thread->current_priority < mutex->owner->current_priority)
                {
                    /* change the owner thread priority */
                    rt_thread_control(mutex->owner,
                                      RT_THREAD_CTRL_CHANGE_PRIORITY,
                                      &thread->current_priority);
                }

                /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
1009
                _ipc_list_suspend(&(mutex->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
1010 1011 1012 1013 1014 1015 1016
                                    thread,
                                    mutex->parent.parent.flag);

                /* has waiting time, start thread timer */
                if (time > 0)
                {
                    RT_DEBUG_LOG(RT_DEBUG_IPC,
1017 1018
                                 ("mutex_take: start the timer of thread:%s\n",
                                  thread->name));
1019

D
dzzxzz@gmail.com 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
                    /* reset the timeout of thread timer and start it */
                    rt_timer_control(&(thread->thread_timer),
                                     RT_TIMER_CTRL_SET_TIME,
                                     &time);
                    rt_timer_start(&(thread->thread_timer));
                }

                /* enable interrupt */
                rt_hw_interrupt_enable(temp);

                /* do schedule */
                rt_schedule();

                if (thread->error != RT_EOK)
                {
                    /* return error */
                    return thread->error;
                }
                else
                {
                    /* the mutex is taken successfully. */
                    /* disable interrupt */
                    temp = rt_hw_interrupt_disable();
                }
            }
        }
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mutex->parent.parent)));

    return RT_EOK;
1054
}
1055
RTM_EXPORT(rt_mutex_take);
1056

C
chenjh 已提交
1057

mysterywolf's avatar
mysterywolf 已提交
1058
/**
C
chenjh 已提交
1059 1060 1061 1062 1063 1064 1065 1066
 * @brief    This function will try to take a mutex, if the mutex is unavailable, the thread returns immediately.
 *
 * @note     This function is very similar to the rt_mutex_take() function, when the mutex is not available,
 *           except that rt_mutex_trytake() will return immediately without waiting for a timeout
 *           when the mutex is not available.
 *           In other words, rt_mutex_trytake(mutex) has the same effect as rt_mutex_take(mutex, 0).
 *
 * @see      rt_mutex_take()
mysterywolf's avatar
mysterywolf 已提交
1067
 *
C
chenjh 已提交
1068
 * @param    mutex is a pointer to a mutex object.
mysterywolf's avatar
mysterywolf 已提交
1069
 *
C
chenjh 已提交
1070 1071
 * @return   Return the operation status. ONLY When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mutex take failed.
mysterywolf's avatar
mysterywolf 已提交
1072 1073 1074 1075 1076 1077 1078
 */
rt_err_t rt_mutex_trytake(rt_mutex_t mutex)
{
    return rt_mutex_take(mutex, RT_WAITING_NO);
}
RTM_EXPORT(rt_mutex_trytake);

C
chenjh 已提交
1079

1080
/**
C
chenjh 已提交
1081 1082 1083 1084 1085
 * @brief    This function will release a mutex. If there is thread suspended on the mutex, the thread will be resumed.
 *
 * @note     If there are threads suspended on this mutex, the first thread in the list of this mutex object
 *           will be resumed, and a thread scheduling (rt_schedule) will be executed.
 *           If no threads are suspended on this mutex, the count value mutex->value of this mutex will increase by 1.
1086
 *
C
chenjh 已提交
1087
 * @param    mutex is a pointer to a mutex object.
1088
 *
C
chenjh 已提交
1089 1090
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mutex release failed.
1091 1092 1093
 */
rt_err_t rt_mutex_release(rt_mutex_t mutex)
{
D
dzzxzz@gmail.com 已提交
1094 1095 1096
    register rt_base_t temp;
    struct rt_thread *thread;
    rt_bool_t need_schedule;
1097

1098 1099 1100 1101
    /* parameter check */
    RT_ASSERT(mutex != RT_NULL);
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);

D
dzzxzz@gmail.com 已提交
1102
    need_schedule = RT_FALSE;
1103

1104 1105
    /* only thread could release mutex because we need test the ownership */
    RT_DEBUG_IN_THREAD_CONTEXT;
1106

B
bernard 已提交
1107 1108 1109
    /* get current thread */
    thread = rt_thread_self();

D
dzzxzz@gmail.com 已提交
1110 1111
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
1112

D
dzzxzz@gmail.com 已提交
1113
    RT_DEBUG_LOG(RT_DEBUG_IPC,
1114 1115
                 ("mutex_release:current thread %s, mutex value: %d, hold: %d\n",
                  thread->name, mutex->value, mutex->hold));
1116

D
dzzxzz@gmail.com 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mutex->parent.parent)));

    /* mutex only can be released by owner */
    if (thread != mutex->owner)
    {
        thread->error = -RT_ERROR;

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        return -RT_ERROR;
    }

    /* decrease hold */
    mutex->hold --;
    /* if no hold */
    if (mutex->hold == 0)
    {
        /* change the owner thread to original priority */
        if (mutex->original_priority != mutex->owner->current_priority)
        {
            rt_thread_control(mutex->owner,
                              RT_THREAD_CTRL_CHANGE_PRIORITY,
                              &(mutex->original_priority));
        }

        /* wakeup suspended thread */
        if (!rt_list_isempty(&mutex->parent.suspend_thread))
        {
            /* get suspended thread */
            thread = rt_list_entry(mutex->parent.suspend_thread.next,
                                   struct rt_thread,
                                   tlist);

            RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_release: resume thread: %s\n",
1152
                                        thread->name));
1153

D
dzzxzz@gmail.com 已提交
1154 1155 1156
            /* set new owner and priority */
            mutex->owner             = thread;
            mutex->original_priority = thread->current_priority;
St0pH@CK's avatar
St0pH@CK 已提交
1157

mysterywolf's avatar
mysterywolf 已提交
1158
            if(mutex->hold < RT_MUTEX_HOLD_MAX)
1159 1160 1161 1162 1163 1164 1165 1166
            {
                mutex->hold ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
1167

D
dzzxzz@gmail.com 已提交
1168
            /* resume thread */
mysterywolf's avatar
mysterywolf 已提交
1169
            _ipc_list_resume(&(mutex->parent.suspend_thread));
1170

D
dzzxzz@gmail.com 已提交
1171 1172 1173 1174
            need_schedule = RT_TRUE;
        }
        else
        {
mysterywolf's avatar
mysterywolf 已提交
1175
            if(mutex->value < RT_MUTEX_VALUE_MAX)
1176 1177 1178 1179 1180 1181 1182 1183 1184
            {
                /* increase value */
                mutex->value ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
mysterywolf's avatar
mysterywolf 已提交
1185

D
dzzxzz@gmail.com 已提交
1186 1187 1188 1189 1190
            /* clear owner */
            mutex->owner             = RT_NULL;
            mutex->original_priority = 0xff;
        }
    }
1191

D
dzzxzz@gmail.com 已提交
1192 1193
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
1194

D
dzzxzz@gmail.com 已提交
1195 1196 1197
    /* perform a schedule */
    if (need_schedule == RT_TRUE)
        rt_schedule();
B
bernard.xiong 已提交
1198

D
dzzxzz@gmail.com 已提交
1199
    return RT_EOK;
1200
}
1201
RTM_EXPORT(rt_mutex_release);
1202

C
chenjh 已提交
1203

1204
/**
C
chenjh 已提交
1205 1206 1207
 * @brief    This function will set some extra attributions of a mutex object.
 *
 * @note     Currently this function does not implement the control function.
1208
 *
C
chenjh 已提交
1209
 * @param    mutex is a pointer to a mutex object.
1210
 *
C
chenjh 已提交
1211 1212 1213 1214 1215 1216
 * @param    cmd is a command word used to configure some attributions of the mutex.
 *
 * @param    arg is the argument of the function to execute the command.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that this function failed to execute.
1217
 */
B
bernard 已提交
1218
rt_err_t rt_mutex_control(rt_mutex_t mutex, int cmd, void *arg)
1219
{
1220 1221 1222 1223
    /* parameter check */
    RT_ASSERT(mutex != RT_NULL);
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);

D
dzzxzz@gmail.com 已提交
1224
    return -RT_ERROR;
1225
}
1226
RTM_EXPORT(rt_mutex_control);
C
chenjh 已提交
1227 1228

/**@}*/
1229
#endif /* RT_USING_MUTEX */
1230 1231 1232

#ifdef RT_USING_EVENT
/**
C
chenjh 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
 * @addtogroup event
 */

/**@{*/

/**
 * @brief    The function will initialize a static event object.
 *
 * @note     For the static event object, its memory space is allocated by the compiler during compiling,
 *           and shall placed on the read-write data segment or on the uninitialized data segment.
 *           By contrast, the rt_event_create() function will allocate memory space automatically
 *           and initialize the event.
 *
 * @see      rt_event_create()
 *
 * @param    event is a pointer to the event to initialize. It is assumed that storage for the event
 *           will be allocated in your application.
 *
 * @param    name is a pointer to the name that given to the event.
 *
 * @param    flag is the event flag, which determines the queuing way of how multiple threads wait
 *           when the event is not available.
 *           The event flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
1258
 *
C
chenjh 已提交
1259 1260
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                         (also known as first-come-first-served (FCFS) scheduling strategy).
1261
 *
C
chenjh 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
 *               use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this event will become non-real-time threads.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it represents the initialization failed.
 *
 * @warning  This function can ONLY be called from threads.
1271
 */
D
dzzxzz 已提交
1272
rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
1273
{
1274
    /* parameter check */
D
dzzxzz@gmail.com 已提交
1275
    RT_ASSERT(event != RT_NULL);
1276
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1277

B
Bernard Xiong 已提交
1278
    /* initialize object */
D
dzzxzz@gmail.com 已提交
1279
    rt_object_init(&(event->parent.parent), RT_Object_Class_Event, name);
1280

D
dzzxzz@gmail.com 已提交
1281 1282
    /* set parent flag */
    event->parent.parent.flag = flag;
1283

B
Bernard Xiong 已提交
1284
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
1285
    _ipc_object_init(&(event->parent));
1286

B
Bernard Xiong 已提交
1287
    /* initialize event */
D
dzzxzz@gmail.com 已提交
1288
    event->set = 0;
1289

D
dzzxzz@gmail.com 已提交
1290
    return RT_EOK;
1291
}
1292
RTM_EXPORT(rt_event_init);
1293

C
chenjh 已提交
1294

1295
/**
C
chenjh 已提交
1296 1297 1298 1299 1300
 * @brief    This function will detach a static event object.
 *
 * @note     This function is used to detach a static event object which is initialized by rt_event_init() function.
 *           By contrast, the rt_event_delete() function will delete an event object.
 *           When the event is successfully detached, it will resume all suspended threads in the event list.
1301
 *
C
chenjh 已提交
1302
 * @see      rt_event_delete()
1303
 *
C
chenjh 已提交
1304 1305 1306 1307 1308 1309 1310 1311
 * @param    event is a pointer to an event object to be detached.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it means that the event detach failed.
 *
 * @warning  This function can ONLY detach a static event initialized by the rt_event_init() function.
 *           If the event is created by the rt_event_create() function, you MUST NOT USE this function to detach it,
 *           ONLY USE the rt_event_delete() function to complete the deletion.
1312 1313 1314
 */
rt_err_t rt_event_detach(rt_event_t event)
{
D
dzzxzz@gmail.com 已提交
1315 1316
    /* parameter check */
    RT_ASSERT(event != RT_NULL);
1317 1318
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
    RT_ASSERT(rt_object_is_systemobject(&event->parent.parent));
1319

D
dzzxzz@gmail.com 已提交
1320
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1321
    _ipc_list_resume_all(&(event->parent.suspend_thread));
1322

D
dzzxzz@gmail.com 已提交
1323 1324
    /* detach event object */
    rt_object_detach(&(event->parent.parent));
1325

D
dzzxzz@gmail.com 已提交
1326
    return RT_EOK;
1327
}
1328
RTM_EXPORT(rt_event_detach);
1329 1330 1331

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
1332 1333 1334 1335 1336 1337
 * @brief    Creating an event object.
 *
 * @note     For the event object, its memory space is allocated automatically.
 *           By contrast, the rt_event_init() function will initialize a static event object.
 *
 * @see      rt_event_init()
1338
 *
C
chenjh 已提交
1339
 * @param    name is a pointer to the name that given to the event.
1340
 *
C
chenjh 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
 * @param    flag is the event flag, which determines the queuing way of how multiple threads wait when the event
 *           is not available.
 *           The event flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
 *
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                         (also known as first-come-first-served (FCFS) scheduling strategy).
 *
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
 *               use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this event will become non-real-time threads.
 *
 * @return   Return a pointer to the event object. When the return value is RT_NULL, it means the creation failed.
 *
 * @warning  This function can ONLY be called from threads.
1358
 */
D
dzzxzz 已提交
1359
rt_event_t rt_event_create(const char *name, rt_uint8_t flag)
1360
{
D
dzzxzz@gmail.com 已提交
1361
    rt_event_t event;
1362

1363
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1364 1365

    RT_DEBUG_NOT_IN_INTERRUPT;
H
HelloByeAll 已提交
1366

D
dzzxzz@gmail.com 已提交
1367 1368 1369 1370
    /* allocate object */
    event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);
    if (event == RT_NULL)
        return event;
1371

D
dzzxzz@gmail.com 已提交
1372 1373
    /* set parent */
    event->parent.parent.flag = flag;
1374

B
Bernard Xiong 已提交
1375
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
1376
    _ipc_object_init(&(event->parent));
1377

B
Bernard Xiong 已提交
1378
    /* initialize event */
D
dzzxzz@gmail.com 已提交
1379
    event->set = 0;
1380

D
dzzxzz@gmail.com 已提交
1381
    return event;
1382
}
1383
RTM_EXPORT(rt_event_create);
1384

C
chenjh 已提交
1385

1386
/**
C
chenjh 已提交
1387 1388 1389 1390 1391 1392 1393
 * @brief    This function will delete an event object and release the memory space.
 *
 * @note     This function is used to delete an event object which is created by the rt_event_create() function.
 *           By contrast, the rt_event_detach() function will detach a static event object.
 *           When the event is successfully deleted, it will resume all suspended threads in the event list.
 *
 * @see      rt_event_detach()
1394
 *
C
chenjh 已提交
1395
 * @param    event is a pointer to an event object to be deleted.
1396
 *
C
chenjh 已提交
1397 1398 1399 1400 1401 1402
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the event detach failed.
 *
 * @warning  This function can ONLY delete an event initialized by the rt_event_create() function.
 *           If the event is initialized by the rt_event_init() function, you MUST NOT USE this function to delete it,
 *           ONLY USE the rt_event_detach() function to complete the detachment.
1403
 */
D
dzzxzz 已提交
1404
rt_err_t rt_event_delete(rt_event_t event)
1405
{
D
dzzxzz@gmail.com 已提交
1406 1407
    /* parameter check */
    RT_ASSERT(event != RT_NULL);
1408 1409
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
    RT_ASSERT(rt_object_is_systemobject(&event->parent.parent) == RT_FALSE);
1410

D
dzzxzz@gmail.com 已提交
1411
    RT_DEBUG_NOT_IN_INTERRUPT;
1412

D
dzzxzz@gmail.com 已提交
1413
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1414
    _ipc_list_resume_all(&(event->parent.suspend_thread));
1415

D
dzzxzz@gmail.com 已提交
1416 1417
    /* delete event object */
    rt_object_delete(&(event->parent.parent));
1418

D
dzzxzz@gmail.com 已提交
1419
    return RT_EOK;
1420
}
1421
RTM_EXPORT(rt_event_delete);
1422
#endif /* RT_USING_HEAP */
1423

C
chenjh 已提交
1424

1425
/**
C
chenjh 已提交
1426 1427 1428 1429 1430 1431 1432 1433 1434
 * @brief    This function will send an event to the event object.
 *           If there is a thread suspended on the event, the thread will be resumed.
 *
 * @note     When using this function, you need to use the parameter (set) to specify the event flag of the event object,
 *           then the function will traverse the list of suspended threads waiting on the event object.
 *           If there is a thread suspended on the event, and the thread's event_info and the event flag of
 *           the current event object matches, the thread will be resumed.
 *
 * @param    event is a pointer to the event object to be sent.
1435
 *
C
chenjh 已提交
1436 1437
 * @param    set is a flag that you will set for this event's flag.
 *           You can set an event flag, or you can set multiple flags through OR logic operation.
1438
 *
C
chenjh 已提交
1439 1440
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the event detach failed.
1441 1442 1443
 */
rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
{
D
dzzxzz@gmail.com 已提交
1444 1445 1446 1447 1448 1449 1450 1451
    struct rt_list_node *n;
    struct rt_thread *thread;
    register rt_ubase_t level;
    register rt_base_t status;
    rt_bool_t need_schedule;

    /* parameter check */
    RT_ASSERT(event != RT_NULL);
1452 1453
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);

D
dzzxzz@gmail.com 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    if (set == 0)
        return -RT_ERROR;

    need_schedule = RT_FALSE;

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    /* set event */
    event->set |= set;

1465
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(event->parent.parent)));
B
Bernard Xiong 已提交
1466

D
dzzxzz@gmail.com 已提交
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
    if (!rt_list_isempty(&event->parent.suspend_thread))
    {
        /* search thread list to resume thread */
        n = event->parent.suspend_thread.next;
        while (n != &(event->parent.suspend_thread))
        {
            /* get thread */
            thread = rt_list_entry(n, struct rt_thread, tlist);

            status = -RT_ERROR;
            if (thread->event_info & RT_EVENT_FLAG_AND)
            {
                if ((thread->event_set & event->set) == thread->event_set)
                {
                    /* received an AND event */
                    status = RT_EOK;
                }
            }
            else if (thread->event_info & RT_EVENT_FLAG_OR)
            {
                if (thread->event_set & event->set)
                {
B
Bernard Xiong 已提交
1489
                    /* save the received event set */
D
dzzxzz@gmail.com 已提交
1490 1491 1492 1493 1494 1495
                    thread->event_set = thread->event_set & event->set;

                    /* received an OR event */
                    status = RT_EOK;
                }
            }
1496 1497 1498 1499 1500 1501 1502
            else
            {
                /* enable interrupt */
                rt_hw_interrupt_enable(level);

                return -RT_EINVAL;
            }
D
dzzxzz@gmail.com 已提交
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530

            /* move node to the next */
            n = n->next;

            /* condition is satisfied, resume thread */
            if (status == RT_EOK)
            {
                /* clear event */
                if (thread->event_info & RT_EVENT_FLAG_CLEAR)
                    event->set &= ~thread->event_set;

                /* resume thread, and thread list breaks out */
                rt_thread_resume(thread);

                /* need do a scheduling */
                need_schedule = RT_TRUE;
            }
        }
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    /* do a schedule */
    if (need_schedule == RT_TRUE)
        rt_schedule();

    return RT_EOK;
1531
}
1532
RTM_EXPORT(rt_event_send);
1533

C
chenjh 已提交
1534

1535
/**
C
chenjh 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
 * @brief  This function will receive an event from event object. if the event is unavailable, the thread shall wait for
 *         the event up to a specified time.
 *
 * @note   If there are threads suspended on this semaphore, the first thread in the list of this semaphore object
 *         will be resumed, and a thread scheduling (rt_schedule) will be executed.
 *         If no threads are suspended on this semaphore, the count value sem->value of this semaphore will increase by 1.
 *
 * @param    event is a pointer to the event object to be received.
 *
 * @param    set is a flag that you will set for this event's flag.
 *           You can set an event flag, or you can set multiple flags through OR logic operation.
1547
 *
C
chenjh 已提交
1548 1549 1550
 * @param    option is the option of this receiving event, it indicates how the receiving event is operated.
 *           The option can be one or more of the following values, When selecting multiple values,use logical OR to operate.
 *           (NOTE: RT_EVENT_FLAG_OR and RT_EVENT_FLAG_AND can only select one):
1551
 *
C
chenjh 已提交
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
 *
 *               RT_EVENT_FLAG_OR           The thread select to use logical OR to receive the event.
 *
 *               RT_EVENT_FLAG_AND          The thread select to use logical OR to receive the event.
 *
 *               RT_EVENT_FLAG_CLEAR        When the thread receives the corresponding event, the function
 *                                          determines whether to clear the event flag.
 *
 * @param    timeout is a timeout period (unit: an OS tick).
 *
 * @param    recved is a pointer to the received event. If you don't care about this value, you can use RT_NULL to set.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the semaphore release failed.
1566
 */
D
dzzxzz@gmail.com 已提交
1567 1568 1569 1570 1571
rt_err_t rt_event_recv(rt_event_t   event,
                       rt_uint32_t  set,
                       rt_uint8_t   option,
                       rt_int32_t   timeout,
                       rt_uint32_t *recved)
1572
{
D
dzzxzz@gmail.com 已提交
1573 1574 1575 1576 1577 1578
    struct rt_thread *thread;
    register rt_ubase_t level;
    register rt_base_t status;

    /* parameter check */
    RT_ASSERT(event != RT_NULL);
1579 1580
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);

1581 1582 1583 1584 1585 1586 1587 1588
    /* current context checking */
    RT_DEBUG_IN_THREAD_CONTEXT;

    if (timeout != 0)
    {
        RT_DEBUG_SCHEDULER_AVAILABLE;
    }

D
dzzxzz@gmail.com 已提交
1589 1590 1591
    if (set == 0)
        return -RT_ERROR;

B
Bernard Xiong 已提交
1592
    /* initialize status */
D
dzzxzz@gmail.com 已提交
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
    status = -RT_ERROR;
    /* get current thread */
    thread = rt_thread_self();
    /* reset thread error */
    thread->error = RT_EOK;

    RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(event->parent.parent)));

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    /* check event set */
    if (option & RT_EVENT_FLAG_AND)
    {
        if ((event->set & set) == set)
            status = RT_EOK;
    }
    else if (option & RT_EVENT_FLAG_OR)
    {
        if (event->set & set)
            status = RT_EOK;
    }
1615 1616 1617 1618 1619
    else
    {
        /* either RT_EVENT_FLAG_AND or RT_EVENT_FLAG_OR should be set */
        RT_ASSERT(0);
    }
D
dzzxzz@gmail.com 已提交
1620 1621 1622 1623

    if (status == RT_EOK)
    {
        /* set received event */
1624
        if (recved)
P
pathletboy 已提交
1625
            *recved = (event->set & set);
mysterywolf's avatar
mysterywolf 已提交
1626 1627

        /* fill thread event info */
1628 1629
        thread->event_set = (event->set & set);
        thread->event_info = option;
mysterywolf's avatar
mysterywolf 已提交
1630

D
dzzxzz@gmail.com 已提交
1631 1632 1633 1634 1635 1636 1637 1638
        /* received event */
        if (option & RT_EVENT_FLAG_CLEAR)
            event->set &= ~set;
    }
    else if (timeout == 0)
    {
        /* no waiting */
        thread->error = -RT_ETIMEOUT;
1639

G
greed-island 已提交
1640 1641
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
1642

G
greed-island 已提交
1643
        return -RT_ETIMEOUT;
D
dzzxzz@gmail.com 已提交
1644 1645 1646 1647 1648 1649 1650 1651
    }
    else
    {
        /* fill thread event info */
        thread->event_set  = set;
        thread->event_info = option;

        /* put thread to suspended thread list */
mysterywolf's avatar
mysterywolf 已提交
1652
        _ipc_list_suspend(&(event->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
                            thread,
                            event->parent.parent.flag);

        /* if there is a waiting timeout, active thread 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 a schedule */
        rt_schedule();

        if (thread->error != RT_EOK)
        {
            /* return error */
            return thread->error;
        }

        /* received an event, disable interrupt to protect */
        level = rt_hw_interrupt_disable();

        /* set received event */
P
pathletboy 已提交
1682 1683
        if (recved)
            *recved = thread->event_set;
D
dzzxzz@gmail.com 已提交
1684 1685 1686 1687 1688 1689 1690 1691
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(event->parent.parent)));

    return thread->error;
1692
}
1693
RTM_EXPORT(rt_event_recv);
1694

C
chenjh 已提交
1695

1696
/**
C
chenjh 已提交
1697 1698 1699 1700 1701
 * @brief    This function will set some extra attributions of an event object.
 *
 * @note     Currently this function only supports the RT_IPC_CMD_RESET command to reset the event.
 *
 * @param    event is a pointer to an event object.
1702
 *
C
chenjh 已提交
1703
 * @param    cmd is a command word used to configure some attributions of the event.
1704
 *
C
chenjh 已提交
1705 1706 1707 1708
 * @param    arg is the argument of the function to execute the command.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that this function failed to execute.
1709
 */
B
bernard 已提交
1710
rt_err_t rt_event_control(rt_event_t event, int cmd, void *arg)
1711
{
D
dzzxzz@gmail.com 已提交
1712
    rt_ubase_t level;
1713

1714 1715 1716
    /* parameter check */
    RT_ASSERT(event != RT_NULL);
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
B
Bernard Xiong 已提交
1717

D
dzzxzz@gmail.com 已提交
1718 1719 1720 1721
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
1722

D
dzzxzz@gmail.com 已提交
1723
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
1724
        _ipc_list_resume_all(&event->parent.suspend_thread);
1725

B
Bernard Xiong 已提交
1726
        /* initialize event set */
D
dzzxzz@gmail.com 已提交
1727
        event->set = 0;
1728

D
dzzxzz@gmail.com 已提交
1729 1730
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
1731

D
dzzxzz@gmail.com 已提交
1732
        rt_schedule();
1733

D
dzzxzz@gmail.com 已提交
1734 1735
        return RT_EOK;
    }
1736

D
dzzxzz@gmail.com 已提交
1737
    return -RT_ERROR;
1738
}
B
Bernard Xiong 已提交
1739
RTM_EXPORT(rt_event_control);
C
chenjh 已提交
1740 1741

/**@}*/
1742
#endif /* RT_USING_EVENT */
1743 1744 1745

#ifdef RT_USING_MAILBOX
/**
C
chenjh 已提交
1746 1747 1748 1749 1750 1751 1752
 * @addtogroup mailbox
 */

/**@{*/

/**
 * @brief    Initialize a static mailbox object.
1753
 *
C
chenjh 已提交
1754 1755 1756
 * @note     For the static mailbox object, its memory space is allocated by the compiler during compiling,
 *           and shall placed on the read-write data segment or on the uninitialized data segment.
 *           By contrast, the rt_mb_create() function will allocate memory space automatically and initialize the mailbox.
1757
 *
C
chenjh 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
 * @see      rt_mb_create()
 *
 * @param    mb is a pointer to the mailbox to initialize.
 *           It is assumed that storage for the mailbox will be allocated in your application.
 *
 * @param    name is a pointer to the name that given to the mailbox.
 *
 * @param    size is the maximum number of mails in the mailbox.
 *           For example, when the mailbox buffer capacity is N, size is N/4.
 *
 * @param    flag is the mailbox flag, which determines the queuing way of how multiple threads wait
 *           when the mailbox is not available.
 *           The mailbox flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
 *
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                       (also known as first-come-first-served (FCFS) scheduling strategy).
 *
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
 *               use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this mailbox will become non-real-time threads.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it represents the initialization failed.
 *
 * @warning  This function can ONLY be called from threads.
1786
 */
D
dzzxzz@gmail.com 已提交
1787 1788 1789 1790 1791
rt_err_t rt_mb_init(rt_mailbox_t mb,
                    const char  *name,
                    void        *msgpool,
                    rt_size_t    size,
                    rt_uint8_t   flag)
1792
{
D
dzzxzz@gmail.com 已提交
1793
    RT_ASSERT(mb != RT_NULL);
1794
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1795

B
Bernard Xiong 已提交
1796
    /* initialize object */
D
dzzxzz@gmail.com 已提交
1797
    rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);
1798

D
dzzxzz@gmail.com 已提交
1799 1800
    /* set parent flag */
    mb->parent.parent.flag = flag;
1801

B
Bernard Xiong 已提交
1802
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
1803
    _ipc_object_init(&(mb->parent));
1804

B
Bernard Xiong 已提交
1805
    /* initialize mailbox */
1806
    mb->msg_pool   = (rt_ubase_t *)msgpool;
D
dzzxzz@gmail.com 已提交
1807 1808 1809 1810
    mb->size       = size;
    mb->entry      = 0;
    mb->in_offset  = 0;
    mb->out_offset = 0;
1811

B
Bernard Xiong 已提交
1812
    /* initialize an additional list of sender suspend thread */
D
dzzxzz@gmail.com 已提交
1813
    rt_list_init(&(mb->suspend_sender_thread));
1814

D
dzzxzz@gmail.com 已提交
1815
    return RT_EOK;
1816
}
1817
RTM_EXPORT(rt_mb_init);
1818

C
chenjh 已提交
1819

1820
/**
C
chenjh 已提交
1821 1822 1823 1824 1825
 * @brief    This function will detach a static mailbox object.
 *
 * @note     This function is used to detach a static mailbox object which is initialized by rt_mb_init() function.
 *           By contrast, the rt_mb_delete() function will delete a mailbox object.
 *           When the mailbox is successfully detached, it will resume all suspended threads in the mailbox list.
1826
 *
C
chenjh 已提交
1827
 * @see      rt_mb_delete()
1828
 *
C
chenjh 已提交
1829 1830 1831 1832 1833 1834 1835 1836
 * @param    mb is a pointer to a mailbox object to be detached.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it means that the mailbox detach failed.
 *
 * @warning  This function can ONLY detach a static mailbox initialized by the rt_mb_init() function.
 *           If the mailbox is created by the rt_mb_create() function, you MUST NOT USE this function to detach it,
 *           ONLY USE the rt_mb_delete() function to complete the deletion.
1837 1838 1839
 */
rt_err_t rt_mb_detach(rt_mailbox_t mb)
{
D
dzzxzz@gmail.com 已提交
1840 1841
    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
1842 1843
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
    RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent));
1844

D
dzzxzz@gmail.com 已提交
1845
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1846
    _ipc_list_resume_all(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
1847
    /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1848
    _ipc_list_resume_all(&(mb->suspend_sender_thread));
1849

D
dzzxzz@gmail.com 已提交
1850 1851
    /* detach mailbox object */
    rt_object_detach(&(mb->parent.parent));
1852

D
dzzxzz@gmail.com 已提交
1853
    return RT_EOK;
1854
}
1855
RTM_EXPORT(rt_mb_detach);
1856 1857 1858

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
1859 1860 1861 1862 1863 1864
 * @brief  Creating a mailbox object.
 *
 * @note   For the mailbox object, its memory space is allocated automatically.
 *         By contrast, the rt_mb_init() function will initialize a static mailbox object.
 *
 * @see    rt_mb_init()
1865
 *
C
chenjh 已提交
1866
 * @param  name is a pointer that given to the mailbox.
1867
 *
C
chenjh 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
 * @param    size is the maximum number of mails in the mailbox.
 *           For example, when mailbox buffer capacity is N, size is N/4.
 *
 * @param    flag is the mailbox flag, which determines the queuing way of how multiple threads wait
 *           when the mailbox is not available.
 *           The mailbox flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
 *
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                         (also known as first-come-first-served (FCFS) scheduling strategy).
 *
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
 *               use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this mailbox will become non-real-time threads.
 *
 * @return   Return a pointer to the mailbox object. When the return value is RT_NULL, it means the creation failed.
 *
 * @warning  This function can ONLY be called from threads.
1888
 */
D
dzzxzz 已提交
1889
rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
1890
{
D
dzzxzz@gmail.com 已提交
1891
    rt_mailbox_t mb;
1892

1893
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1894

1895 1896
    RT_DEBUG_NOT_IN_INTERRUPT;

D
dzzxzz@gmail.com 已提交
1897 1898 1899 1900
    /* allocate object */
    mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
    if (mb == RT_NULL)
        return mb;
1901

D
dzzxzz@gmail.com 已提交
1902 1903
    /* set parent */
    mb->parent.parent.flag = flag;
1904

B
Bernard Xiong 已提交
1905
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
1906
    _ipc_object_init(&(mb->parent));
1907

B
Bernard Xiong 已提交
1908
    /* initialize mailbox */
D
dzzxzz@gmail.com 已提交
1909
    mb->size     = size;
1910
    mb->msg_pool = (rt_ubase_t *)RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t));
D
dzzxzz@gmail.com 已提交
1911 1912 1913 1914
    if (mb->msg_pool == RT_NULL)
    {
        /* delete mailbox object */
        rt_object_delete(&(mb->parent.parent));
1915

D
dzzxzz@gmail.com 已提交
1916 1917 1918 1919 1920
        return RT_NULL;
    }
    mb->entry      = 0;
    mb->in_offset  = 0;
    mb->out_offset = 0;
1921

B
Bernard Xiong 已提交
1922
    /* initialize an additional list of sender suspend thread */
D
dzzxzz@gmail.com 已提交
1923
    rt_list_init(&(mb->suspend_sender_thread));
1924

D
dzzxzz@gmail.com 已提交
1925
    return mb;
1926
}
1927
RTM_EXPORT(rt_mb_create);
1928

C
chenjh 已提交
1929

1930
/**
C
chenjh 已提交
1931 1932 1933 1934 1935
 * @brief    This function will delete a mailbox object and release the memory space.
 *
 * @note     This function is used to delete a mailbox object which is created by the rt_mb_create() function.
 *           By contrast, the rt_mb_detach() function will detach a static mailbox object.
 *           When the mailbox is successfully deleted, it will resume all suspended threads in the mailbox list.
1936
 *
C
chenjh 已提交
1937
 * @see      rt_mb_detach()
1938
 *
C
chenjh 已提交
1939 1940 1941 1942 1943 1944 1945 1946
 * @param    mb is a pointer to a mailbox object to be deleted.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mailbox detach failed.
 *
 * @warning  This function can only delete mailbox created by the rt_mb_create() function.
 *           If the mailbox is initialized by the rt_mb_init() function, you MUST NOT USE this function to delete it,
 *           ONLY USE the rt_mb_detach() function to complete the detachment.
1947
 */
D
dzzxzz 已提交
1948
rt_err_t rt_mb_delete(rt_mailbox_t mb)
M
mbbill@gmail.com 已提交
1949
{
D
dzzxzz@gmail.com 已提交
1950 1951
    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
1952 1953
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
    RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent) == RT_FALSE);
1954

1955 1956
    RT_DEBUG_NOT_IN_INTERRUPT;

D
dzzxzz@gmail.com 已提交
1957
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1958
    _ipc_list_resume_all(&(mb->parent.suspend_thread));
1959

D
dzzxzz@gmail.com 已提交
1960
    /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1961
    _ipc_list_resume_all(&(mb->suspend_sender_thread));
1962

1963 1964
    /* free mailbox pool */
    RT_KERNEL_FREE(mb->msg_pool);
1965

D
dzzxzz@gmail.com 已提交
1966 1967
    /* delete mailbox object */
    rt_object_delete(&(mb->parent.parent));
1968

D
dzzxzz@gmail.com 已提交
1969
    return RT_EOK;
1970
}
1971
RTM_EXPORT(rt_mb_delete);
1972
#endif /* RT_USING_HEAP */
1973

C
chenjh 已提交
1974

1975
/**
C
chenjh 已提交
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
 * @brief    This function will send an mail to the mailbox object. If there is a thread suspended on the mailbox,
 *           the thread will be resumed.
 *
 * @note     When using this function to send a mail, if the mailbox if fully used, the current thread will
 *           wait for a timeout. If the set timeout time is reached and there is still no space available,
 *           the sending thread will be resumed and an error code will be returned.
 *           By contrast, the rt_mb_send() function will return an error code immediately without waiting time
 *           when the mailbox if fully used.
 *
 * @see      rt_mb_send()
 *
 * @param    mb is a pointer to the mailbox object to be sent.
 *
 * @param    value is a value to the content of the mail you want to send.
 *
 * @param    timeout is a timeout period (unit: an OS tick).
1992
 *
C
chenjh 已提交
1993 1994
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mailbox detach failed.
1995
 *
C
chenjh 已提交
1996
 * @warning  This function can be called in interrupt context and thread context.
1997
 */
D
dzzxzz@gmail.com 已提交
1998
rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
B
Bernard Xiong 已提交
1999
                         rt_ubase_t   value,
D
dzzxzz@gmail.com 已提交
2000
                         rt_int32_t   timeout)
2001
{
D
dzzxzz@gmail.com 已提交
2002 2003 2004 2005 2006 2007
    struct rt_thread *thread;
    register rt_ubase_t temp;
    rt_uint32_t tick_delta;

    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
2008
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
D
dzzxzz@gmail.com 已提交
2009

2010 2011 2012 2013 2014 2015
    /* current context checking */
    if (timeout != 0)
    {
        RT_DEBUG_SCHEDULER_AVAILABLE;
    }

D
dzzxzz@gmail.com 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();

    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));

    /* disable interrupt */
    temp = rt_hw_interrupt_disable();

    /* for non-blocking call */
    if (mb->entry == mb->size && timeout == 0)
    {
        rt_hw_interrupt_enable(temp);
        return -RT_EFULL;
    }

    /* mailbox is full */
    while (mb->entry == mb->size)
    {
        /* reset error number in thread */
        thread->error = RT_EOK;

        /* no waiting, return timeout */
        if (timeout == 0)
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(temp);

            return -RT_EFULL;
        }

        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2049
        _ipc_list_suspend(&(mb->suspend_sender_thread),
D
dzzxzz@gmail.com 已提交
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
                            thread,
                            mb->parent.parent.flag);

        /* has waiting time, start thread timer */
        if (timeout > 0)
        {
            /* get the start tick of timer */
            tick_delta = rt_tick_get();

            RT_DEBUG_LOG(RT_DEBUG_IPC, ("mb_send_wait: start timer of thread:%s\n",
2060
                                        thread->name));
2061

D
dzzxzz@gmail.com 已提交
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
            /* 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(temp);

        /* re-schedule */
        rt_schedule();

        /* resume from suspend state */
        if (thread->error != RT_EOK)
        {
            /* return error */
            return thread->error;
        }

        /* disable interrupt */
        temp = rt_hw_interrupt_disable();

        /* if it's not waiting forever and then re-calculate timeout tick */
        if (timeout > 0)
        {
            tick_delta = rt_tick_get() - tick_delta;
            timeout -= tick_delta;
            if (timeout < 0)
                timeout = 0;
        }
    }

    /* set ptr */
    mb->msg_pool[mb->in_offset] = value;
    /* increase input offset */
    ++ mb->in_offset;
    if (mb->in_offset >= mb->size)
        mb->in_offset = 0;
mysterywolf's avatar
mysterywolf 已提交
2101

mysterywolf's avatar
mysterywolf 已提交
2102
    if(mb->entry < RT_MB_ENTRY_MAX)
2103 2104 2105 2106 2107 2108 2109 2110 2111
    {
        /* increase message entry */
        mb->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
mysterywolf's avatar
mysterywolf 已提交
2112

D
dzzxzz@gmail.com 已提交
2113 2114 2115
    /* resume suspended thread */
    if (!rt_list_isempty(&mb->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2116
        _ipc_list_resume(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
2130
}
2131
RTM_EXPORT(rt_mb_send_wait);
2132

C
chenjh 已提交
2133

2134
/**
C
chenjh 已提交
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
 * @brief    This function will send an mail to the mailbox object. If there is a thread suspended on the mailbox,
 *           the thread will be resumed.
 *
 * @note     When using this function to send a mail, if the mailbox is fully used, this function will return an error
 *           code immediately without waiting time.
 *           By contrast, the rt_mb_send_wait() function is set a timeout to wait for the mail to be sent.
 *
 * @see      rt_mb_send_wait()
 *
 * @param    mb is a pointer to the mailbox object to be sent.
2145
 *
C
chenjh 已提交
2146
 * @param    value is a value to the content of the mail you want to send.
2147
 *
C
chenjh 已提交
2148 2149
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mailbox detach failed.
2150
 */
B
Bernard Xiong 已提交
2151
rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value)
2152
{
D
dzzxzz@gmail.com 已提交
2153
    return rt_mb_send_wait(mb, value, 0);
2154
}
2155
RTM_EXPORT(rt_mb_send);
2156

C
chenjh 已提交
2157

2158
/**
C
chenjh 已提交
2159 2160 2161 2162 2163 2164 2165 2166 2167
 * @brief    This function will send an urgent mail to the mailbox object.
 *
 * @note     This function is almost the same as the rt_mb_send() function. The only difference is that
 *           when sending an urgent mail, the mail will be placed at the head of the mail queue so that
 *           the recipient can receive the urgent mail first.
 *
 * @see      rt_mb_send()
 *
 * @param    mb is a pointer to the mailbox object to be sent.
2168
 *
C
chenjh 已提交
2169
 * @param    value is the content of the mail you want to send.
2170
 *
C
chenjh 已提交
2171 2172
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mailbox detach failed.
2173
 */
mysterywolf's avatar
update  
mysterywolf 已提交
2174
rt_err_t rt_mb_urgent(rt_mailbox_t mb, rt_ubase_t value)
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
{
    register rt_ubase_t temp;

    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);

    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mb->parent.parent)));

    /* disable interrupt */
    temp = rt_hw_interrupt_disable();

mysterywolf's avatar
update  
mysterywolf 已提交
2187 2188 2189 2190 2191 2192
    if (mb->entry == mb->size)
    {
        rt_hw_interrupt_enable(temp);
        return -RT_EFULL;
    }

2193
    /* rewind to the previous position */
mysterywolf's avatar
update  
mysterywolf 已提交
2194
    if (mb->out_offset > 0)
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
    {
        mb->out_offset --;
    }
    else
    {
        mb->out_offset = mb->size - 1;
    }

    /* set ptr */
    mb->msg_pool[mb->out_offset] = value;

mysterywolf's avatar
update  
mysterywolf 已提交
2206 2207
    /* increase message entry */
    mb->entry ++;
2208 2209 2210 2211

    /* resume suspended thread */
    if (!rt_list_isempty(&mb->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2212
        _ipc_list_resume(&(mb->parent.suspend_thread));
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
}
RTM_EXPORT(rt_mb_urgent);

C
chenjh 已提交
2229

2230
/**
C
chenjh 已提交
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
 * @brief    This function will receive a mail from mailbox object, if there is no mail in mailbox object,
 *           the thread shall wait for a specified time.
 *
 * @note     Only when there is mail in the mailbox, the receiving thread can get the mail immediately and
 *           return RT_EOK, otherwise the receiving thread will be suspended until the set timeout. If the mail
 *           is still not received within the specified time, it will return-RT_ETIMEOUT.
 *
 * @param    mb is a pointer to the mailbox object to be received.
 *
 * @param    value is a flag that you will set for this mailbox's flag.
 *           You can set an mailbox flag, or you can set multiple flags through OR logic operations.
2242
 *
C
chenjh 已提交
2243
 * @param    timeout is a timeout period (unit: an OS tick).
2244
 *
C
chenjh 已提交
2245 2246
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mailbox release failed.
2247
 */
B
Bernard Xiong 已提交
2248
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
2249
{
D
dzzxzz@gmail.com 已提交
2250 2251 2252 2253 2254 2255
    struct rt_thread *thread;
    register rt_ubase_t temp;
    rt_uint32_t tick_delta;

    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
2256
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
D
dzzxzz@gmail.com 已提交
2257

2258 2259 2260 2261 2262 2263
    /* current context checking */
    if (timeout != 0)
    {
        RT_DEBUG_SCHEDULER_AVAILABLE;
    }

D
dzzxzz@gmail.com 已提交
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();

    RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mb->parent.parent)));

    /* disable interrupt */
    temp = rt_hw_interrupt_disable();

    /* for non-blocking call */
    if (mb->entry == 0 && timeout == 0)
    {
        rt_hw_interrupt_enable(temp);

        return -RT_ETIMEOUT;
    }

    /* mailbox is empty */
    while (mb->entry == 0)
    {
        /* reset error number in thread */
        thread->error = RT_EOK;

        /* no waiting, return timeout */
        if (timeout == 0)
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(temp);

            thread->error = -RT_ETIMEOUT;
B
Bernard Xiong 已提交
2295

D
dzzxzz@gmail.com 已提交
2296 2297 2298 2299
            return -RT_ETIMEOUT;
        }

        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2300
        _ipc_list_suspend(&(mb->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
                            thread,
                            mb->parent.parent.flag);

        /* has waiting time, start thread timer */
        if (timeout > 0)
        {
            /* get the start tick of timer */
            tick_delta = rt_tick_get();

            RT_DEBUG_LOG(RT_DEBUG_IPC, ("mb_recv: start timer of thread:%s\n",
2311
                                        thread->name));
2312

D
dzzxzz@gmail.com 已提交
2313 2314 2315 2316 2317 2318
            /* 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));
        }
2319

D
dzzxzz@gmail.com 已提交
2320 2321
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2322

D
dzzxzz@gmail.com 已提交
2323 2324
        /* re-schedule */
        rt_schedule();
2325

D
dzzxzz@gmail.com 已提交
2326 2327 2328 2329 2330 2331
        /* resume from suspend state */
        if (thread->error != RT_EOK)
        {
            /* return error */
            return thread->error;
        }
2332

D
dzzxzz@gmail.com 已提交
2333 2334
        /* disable interrupt */
        temp = rt_hw_interrupt_disable();
2335

D
dzzxzz@gmail.com 已提交
2336 2337 2338 2339 2340 2341 2342 2343 2344
        /* if it's not waiting forever and then re-calculate timeout tick */
        if (timeout > 0)
        {
            tick_delta = rt_tick_get() - tick_delta;
            timeout -= tick_delta;
            if (timeout < 0)
                timeout = 0;
        }
    }
2345

D
dzzxzz@gmail.com 已提交
2346 2347
    /* fill ptr */
    *value = mb->msg_pool[mb->out_offset];
2348

D
dzzxzz@gmail.com 已提交
2349 2350 2351 2352
    /* increase output offset */
    ++ mb->out_offset;
    if (mb->out_offset >= mb->size)
        mb->out_offset = 0;
2353

D
dzzxzz@gmail.com 已提交
2354
    /* decrease message entry */
2355 2356 2357 2358
    if(mb->entry > 0)
    {
        mb->entry --;
    }
2359

D
dzzxzz@gmail.com 已提交
2360 2361 2362
    /* resume suspended thread */
    if (!rt_list_isempty(&(mb->suspend_sender_thread)))
    {
mysterywolf's avatar
mysterywolf 已提交
2363
        _ipc_list_resume(&(mb->suspend_sender_thread));
2364

D
dzzxzz@gmail.com 已提交
2365 2366
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2367

D
dzzxzz@gmail.com 已提交
2368
        RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
2369

D
dzzxzz@gmail.com 已提交
2370
        rt_schedule();
2371

D
dzzxzz@gmail.com 已提交
2372 2373
        return RT_EOK;
    }
2374

D
dzzxzz@gmail.com 已提交
2375 2376
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
2377

D
dzzxzz@gmail.com 已提交
2378
    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mb->parent.parent)));
2379

D
dzzxzz@gmail.com 已提交
2380
    return RT_EOK;
2381
}
2382
RTM_EXPORT(rt_mb_recv);
2383

C
chenjh 已提交
2384

2385
/**
C
chenjh 已提交
2386 2387 2388 2389 2390 2391 2392
 * @brief    This function will set some extra attributions of a mailbox object.
 *
 * @note     Currently this function only supports the RT_IPC_CMD_RESET command to reset the mailbox.
 *
 * @param    mb is a pointer to a mailbox object.
 *
 * @param    cmd is a command used to configure some attributions of the mailbox.
2393
 *
C
chenjh 已提交
2394
 * @param    arg is the argument of the function to execute the command.
2395
 *
C
chenjh 已提交
2396 2397
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that this function failed to execute.
2398
 */
B
bernard 已提交
2399
rt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg)
2400
{
D
dzzxzz@gmail.com 已提交
2401
    rt_ubase_t level;
2402 2403

    /* parameter check */
D
dzzxzz@gmail.com 已提交
2404
    RT_ASSERT(mb != RT_NULL);
2405
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
2406

D
dzzxzz@gmail.com 已提交
2407 2408 2409 2410
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
2411

D
dzzxzz@gmail.com 已提交
2412
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
2413
        _ipc_list_resume_all(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2414
        /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2415
        _ipc_list_resume_all(&(mb->suspend_sender_thread));
2416

D
dzzxzz@gmail.com 已提交
2417 2418 2419 2420
        /* re-init mailbox */
        mb->entry      = 0;
        mb->in_offset  = 0;
        mb->out_offset = 0;
2421

D
dzzxzz@gmail.com 已提交
2422 2423
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
2424

D
dzzxzz@gmail.com 已提交
2425
        rt_schedule();
2426

D
dzzxzz@gmail.com 已提交
2427 2428
        return RT_EOK;
    }
2429

D
dzzxzz@gmail.com 已提交
2430
    return -RT_ERROR;
2431
}
B
Bernard Xiong 已提交
2432
RTM_EXPORT(rt_mb_control);
C
chenjh 已提交
2433 2434

/**@}*/
2435
#endif /* RT_USING_MAILBOX */
2436 2437

#ifdef RT_USING_MESSAGEQUEUE
C
chenjh 已提交
2438 2439 2440 2441 2442 2443
/**
 * @addtogroup messagequeue
 */

/**@{*/

2444 2445
struct rt_mq_message
{
D
dzzxzz@gmail.com 已提交
2446
    struct rt_mq_message *next;
2447 2448
};

C
chenjh 已提交
2449

2450
/**
C
chenjh 已提交
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
 * @brief    Initialize a static messagequeue object.
 *
 * @note     For the static messagequeue object, its memory space is allocated by the compiler during compiling,
 *           and shall placed on the read-write data segment or on the uninitialized data segment.
 *           By contrast, the rt_mq_create() function will allocate memory space automatically
 *           and initialize the messagequeue.
 *
 * @see      rt_mq_create()
 *
 * @param    mq is a pointer to the messagequeue to initialize. It is assumed that storage for
 *           the messagequeue will be allocated in your application.
 *
 * @param    name is a pointer to the name that given to the messagequeue.
 *
 * @param    msgpool is a pointer to the starting address of the memory space you allocated for
 *           the messagequeue in advance.
 *           In other words, msgpool is a pointer to the messagequeue buffer of the starting address.
 *
 * @param    msg_size is the maximum length of a message in the messagequeue (Unit: Byte).
 *
 * @param    pool_size is the size of the memory space allocated for the messagequeue in advance.
 *
 * @param    flag is the messagequeue flag, which determines the queuing way of how multiple threads wait
 *           when the messagequeue is not available.
 *           The messagequeue flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
2478
 *
C
chenjh 已提交
2479 2480
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                         (also known as first-come-first-served (FCFS) scheduling strategy).
2481
 *
C
chenjh 已提交
2482 2483 2484 2485 2486 2487 2488 2489 2490
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
 *               use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this messagequeue will become non-real-time threads.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it represents the initialization failed.
 *
 * @warning  This function can ONLY be called from threads.
2491
 */
D
dzzxzz@gmail.com 已提交
2492 2493 2494 2495 2496 2497
rt_err_t rt_mq_init(rt_mq_t     mq,
                    const char *name,
                    void       *msgpool,
                    rt_size_t   msg_size,
                    rt_size_t   pool_size,
                    rt_uint8_t  flag)
2498
{
D
dzzxzz@gmail.com 已提交
2499 2500
    struct rt_mq_message *head;
    register rt_base_t temp;
2501

D
dzzxzz@gmail.com 已提交
2502 2503
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2504
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
2505

B
Bernard Xiong 已提交
2506
    /* initialize object */
D
dzzxzz@gmail.com 已提交
2507
    rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);
2508

D
dzzxzz@gmail.com 已提交
2509 2510
    /* set parent flag */
    mq->parent.parent.flag = flag;
2511

B
Bernard Xiong 已提交
2512
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
2513
    _ipc_object_init(&(mq->parent));
2514

B
Bernard Xiong 已提交
2515
    /* set message pool */
D
dzzxzz@gmail.com 已提交
2516
    mq->msg_pool = msgpool;
2517

D
dzzxzz@gmail.com 已提交
2518 2519 2520
    /* get correct message size */
    mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
    mq->max_msgs = pool_size / (mq->msg_size + sizeof(struct rt_mq_message));
2521

B
Bernard Xiong 已提交
2522
    /* initialize message list */
D
dzzxzz@gmail.com 已提交
2523 2524
    mq->msg_queue_head = RT_NULL;
    mq->msg_queue_tail = RT_NULL;
2525

B
Bernard Xiong 已提交
2526
    /* initialize message empty list */
D
dzzxzz@gmail.com 已提交
2527 2528 2529 2530
    mq->msg_queue_free = RT_NULL;
    for (temp = 0; temp < mq->max_msgs; temp ++)
    {
        head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
2531
                                        temp * (mq->msg_size + sizeof(struct rt_mq_message)));
2532
        head->next = (struct rt_mq_message *)mq->msg_queue_free;
D
dzzxzz@gmail.com 已提交
2533 2534
        mq->msg_queue_free = head;
    }
2535

D
dzzxzz@gmail.com 已提交
2536 2537
    /* the initial entry is zero */
    mq->entry = 0;
2538

B
Bernard Xiong 已提交
2539
    /* initialize an additional list of sender suspend thread */
2540 2541
    rt_list_init(&(mq->suspend_sender_thread));

D
dzzxzz@gmail.com 已提交
2542
    return RT_EOK;
2543
}
2544
RTM_EXPORT(rt_mq_init);
2545

C
chenjh 已提交
2546

2547
/**
C
chenjh 已提交
2548 2549 2550 2551 2552
 * @brief    This function will detach a static messagequeue object.
 *
 * @note     This function is used to detach a static messagequeue object which is initialized by rt_mq_init() function.
 *           By contrast, the rt_mq_delete() function will delete a messagequeue object.
 *           When the messagequeue is successfully detached, it will resume all suspended threads in the messagequeue list.
2553
 *
C
chenjh 已提交
2554
 * @see      rt_mq_delete()
2555
 *
C
chenjh 已提交
2556 2557 2558 2559 2560 2561 2562 2563
 * @param    mq is a pointer to a messagequeue object to be detached.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the initialization is successful.
 *           If the return value is any other values, it means that the messagequeue detach failed.
 *
 * @warning  This function can ONLY detach a static messagequeue initialized by the rt_mq_init() function.
 *           If the messagequeue is created by the rt_mq_create() function, you MUST NOT USE this function to detach it,
 *           and ONLY USE the rt_mq_delete() function to complete the deletion.
2564 2565 2566
 */
rt_err_t rt_mq_detach(rt_mq_t mq)
{
D
dzzxzz@gmail.com 已提交
2567 2568
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2569 2570
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
    RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent));
2571

D
dzzxzz@gmail.com 已提交
2572
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2573
    _ipc_list_resume_all(&mq->parent.suspend_thread);
2574
    /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2575
    _ipc_list_resume_all(&(mq->suspend_sender_thread));
2576

D
dzzxzz@gmail.com 已提交
2577 2578
    /* detach message queue object */
    rt_object_detach(&(mq->parent.parent));
2579

D
dzzxzz@gmail.com 已提交
2580
    return RT_EOK;
2581
}
2582
RTM_EXPORT(rt_mq_detach);
2583 2584 2585

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
2586 2587 2588 2589 2590 2591
 * @brief    Creating a messagequeue object.
 *
 * @note     For the messagequeue object, its memory space is allocated automatically.
 *           By contrast, the rt_mq_init() function will initialize a static messagequeue object.
 *
 * @see      rt_mq_init()
2592
 *
C
chenjh 已提交
2593
 * @param    name is a pointer that given to the messagequeue.
2594
 *
C
chenjh 已提交
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
 * @param    msg_size is the maximum length of a message in the messagequeue (Unit: Byte).
 *
 * @param    max_msgs is the maximum number of messages in the messagequeue.
 *
 * @param    flag is the messagequeue flag, which determines the queuing way of how multiple threads wait
 *           when the messagequeue is not available.
 *           The messagequeue flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
 *
 *               RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
 *                                         (also known as first-come-first-served (FCFS) scheduling strategy).
 *
 *               NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
 *               use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
 *               the first-in-first-out principle, and you clearly understand that all threads involved in
 *               this messagequeue will become non-real-time threads.
 *
 * @return   Return a pointer to the messagequeue object. When the return value is RT_NULL, it means the creation failed.
 *
 * @warning  This function can NOT be called in interrupt context. You can use macor RT_DEBUG_NOT_IN_INTERRUPT to check it.
2616
 */
D
dzzxzz@gmail.com 已提交
2617 2618 2619 2620
rt_mq_t rt_mq_create(const char *name,
                     rt_size_t   msg_size,
                     rt_size_t   max_msgs,
                     rt_uint8_t  flag)
2621
{
D
dzzxzz@gmail.com 已提交
2622 2623 2624 2625
    struct rt_messagequeue *mq;
    struct rt_mq_message *head;
    register rt_base_t temp;

2626
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
2627 2628

    RT_DEBUG_NOT_IN_INTERRUPT;
H
HelloByeAll 已提交
2629

D
dzzxzz@gmail.com 已提交
2630 2631 2632 2633 2634 2635 2636 2637
    /* allocate object */
    mq = (rt_mq_t)rt_object_allocate(RT_Object_Class_MessageQueue, name);
    if (mq == RT_NULL)
        return mq;

    /* set parent */
    mq->parent.parent.flag = flag;

B
Bernard Xiong 已提交
2638
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
2639
    _ipc_object_init(&(mq->parent));
D
dzzxzz@gmail.com 已提交
2640

B
Bernard Xiong 已提交
2641
    /* initialize message queue */
D
dzzxzz@gmail.com 已提交
2642 2643 2644 2645 2646 2647

    /* get correct message size */
    mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
    mq->max_msgs = max_msgs;

    /* allocate message pool */
2648
    mq->msg_pool = RT_KERNEL_MALLOC((mq->msg_size + sizeof(struct rt_mq_message)) * mq->max_msgs);
D
dzzxzz@gmail.com 已提交
2649 2650
    if (mq->msg_pool == RT_NULL)
    {
2651
        rt_object_delete(&(mq->parent.parent));
D
dzzxzz@gmail.com 已提交
2652 2653 2654 2655

        return RT_NULL;
    }

B
Bernard Xiong 已提交
2656
    /* initialize message list */
D
dzzxzz@gmail.com 已提交
2657 2658 2659
    mq->msg_queue_head = RT_NULL;
    mq->msg_queue_tail = RT_NULL;

B
Bernard Xiong 已提交
2660
    /* initialize message empty list */
D
dzzxzz@gmail.com 已提交
2661 2662 2663 2664
    mq->msg_queue_free = RT_NULL;
    for (temp = 0; temp < mq->max_msgs; temp ++)
    {
        head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
2665
                                        temp * (mq->msg_size + sizeof(struct rt_mq_message)));
2666
        head->next = (struct rt_mq_message *)mq->msg_queue_free;
D
dzzxzz@gmail.com 已提交
2667 2668 2669 2670 2671 2672
        mq->msg_queue_free = head;
    }

    /* the initial entry is zero */
    mq->entry = 0;

B
Bernard Xiong 已提交
2673
    /* initialize an additional list of sender suspend thread */
2674 2675
    rt_list_init(&(mq->suspend_sender_thread));

D
dzzxzz@gmail.com 已提交
2676
    return mq;
2677
}
2678
RTM_EXPORT(rt_mq_create);
2679

C
chenjh 已提交
2680

2681
/**
C
chenjh 已提交
2682
 * @brief    This function will delete a messagequeue object and release the memory.
2683
 *
C
chenjh 已提交
2684 2685 2686
 * @note     This function is used to delete a messagequeue object which is created by the rt_mq_create() function.
 *           By contrast, the rt_mq_detach() function will detach a static messagequeue object.
 *           When the messagequeue is successfully deleted, it will resume all suspended threads in the messagequeue list.
2687
 *
C
chenjh 已提交
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
 * @see      rt_mq_detach()
 *
 * @param    mq is a pointer to a messagequeue object to be deleted.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the messagequeue detach failed.
 *
 * @warning  This function can ONLY delete a messagequeue initialized by the rt_mq_create() function.
 *           If the messagequeue is initialized by the rt_mq_init() function, you MUST NOT USE this function to delete it,
 *           ONLY USE the rt_mq_detach() function to complete the detachment.
 *           for example,the rt_mq_create() function, it cannot be called in interrupt context.
2699
 */
D
dzzxzz 已提交
2700
rt_err_t rt_mq_delete(rt_mq_t mq)
2701
{
D
dzzxzz@gmail.com 已提交
2702 2703
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2704 2705
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
    RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent) == RT_FALSE);
2706

2707 2708
    RT_DEBUG_NOT_IN_INTERRUPT;

D
dzzxzz@gmail.com 已提交
2709
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2710
    _ipc_list_resume_all(&(mq->parent.suspend_thread));
2711
    /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2712
    _ipc_list_resume_all(&(mq->suspend_sender_thread));
2713

2714 2715
    /* free message queue pool */
    RT_KERNEL_FREE(mq->msg_pool);
2716

D
dzzxzz@gmail.com 已提交
2717 2718
    /* delete message queue object */
    rt_object_delete(&(mq->parent.parent));
2719

D
dzzxzz@gmail.com 已提交
2720
    return RT_EOK;
2721
}
2722
RTM_EXPORT(rt_mq_delete);
2723
#endif /* RT_USING_HEAP */
2724

C
chenjh 已提交
2725

2726
/**
C
chenjh 已提交
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
 * @brief    This function will send a message to the messagequeue object. If
 *           there is a thread suspended on the messagequeue, the thread will be
 *           resumed.
 *
 * @note     When using this function to send a message, if the messagequeue is
 *           fully used, the current thread will wait for a timeout. If reaching
 *           the timeout and there is still no space available, the sending
 *           thread will be resumed and an error code will be returned. By
 *           contrast, the rt_mq_send() function will return an error code
 *           immediately without waiting when the messagequeue if fully used.
 *
 * @see      rt_mq_send()
 *
 * @param    mq is a pointer to the messagequeue object to be sent.
 *
 * @param    buffer is the content of the message.
2743
 *
C
chenjh 已提交
2744
 * @param    size is the length of the message(Unit: Byte).
2745
 *
C
chenjh 已提交
2746 2747 2748 2749 2750 2751 2752 2753
 * @param    timeout is a timeout period (unit: an OS tick).
 *
 * @return   Return the operation status. When the return value is RT_EOK, the
 *           operation is successful. If the return value is any other values,
 *           it means that the messagequeue detach failed.
 *
 * @warning  This function can be called in interrupt context and thread
 * context.
2754
 */
2755 2756 2757 2758
rt_err_t rt_mq_send_wait(rt_mq_t     mq,
                         const void *buffer,
                         rt_size_t   size,
                         rt_int32_t  timeout)
2759
{
D
dzzxzz@gmail.com 已提交
2760 2761
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
2762 2763
    rt_uint32_t tick_delta;
    struct rt_thread *thread;
D
dzzxzz@gmail.com 已提交
2764

2765
    /* parameter check */
D
dzzxzz@gmail.com 已提交
2766
    RT_ASSERT(mq != RT_NULL);
2767
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
2768 2769 2770
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);

2771 2772 2773 2774 2775 2776
    /* current context checking */
    if (timeout != 0)
    {
        RT_DEBUG_SCHEDULER_AVAILABLE;
    }

D
dzzxzz@gmail.com 已提交
2777 2778 2779 2780
    /* greater than one message size */
    if (size > mq->msg_size)
        return -RT_ERROR;

2781 2782 2783 2784 2785
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();

D
dzzxzz@gmail.com 已提交
2786 2787 2788 2789 2790 2791
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));

    /* disable interrupt */
    temp = rt_hw_interrupt_disable();

    /* get a free list, there must be an empty item */
2792
    msg = (struct rt_mq_message *)mq->msg_queue_free;
2793 2794
    /* for non-blocking call */
    if (msg == RT_NULL && timeout == 0)
D
dzzxzz@gmail.com 已提交
2795 2796 2797 2798 2799 2800
    {
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        return -RT_EFULL;
    }
2801 2802

    /* message queue is full */
2803
    while ((msg = (struct rt_mq_message *)mq->msg_queue_free) == RT_NULL)
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
    {
        /* reset error number in thread */
        thread->error = RT_EOK;

        /* no waiting, return timeout */
        if (timeout == 0)
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(temp);

            return -RT_EFULL;
        }

        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2818
        _ipc_list_suspend(&(mq->suspend_sender_thread),
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863
                            thread,
                            mq->parent.parent.flag);

        /* has waiting time, start thread timer */
        if (timeout > 0)
        {
            /* get the start tick of timer */
            tick_delta = rt_tick_get();

            RT_DEBUG_LOG(RT_DEBUG_IPC, ("mq_send_wait: start timer of thread:%s\n",
                                        thread->name));

            /* 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(temp);

        /* re-schedule */
        rt_schedule();

        /* resume from suspend state */
        if (thread->error != RT_EOK)
        {
            /* return error */
            return thread->error;
        }

        /* disable interrupt */
        temp = rt_hw_interrupt_disable();

        /* if it's not waiting forever and then re-calculate timeout tick */
        if (timeout > 0)
        {
            tick_delta = rt_tick_get() - tick_delta;
            timeout -= tick_delta;
            if (timeout < 0)
                timeout = 0;
        }
    }

D
dzzxzz@gmail.com 已提交
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
    /* move free list pointer */
    mq->msg_queue_free = msg->next;

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    /* the msg is the new tailer of list, the next shall be NULL */
    msg->next = RT_NULL;
    /* copy buffer */
    rt_memcpy(msg + 1, buffer, size);

    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
    /* link msg to message queue */
    if (mq->msg_queue_tail != RT_NULL)
    {
        /* if the tail exists, */
        ((struct rt_mq_message *)mq->msg_queue_tail)->next = msg;
    }

    /* set new tail */
    mq->msg_queue_tail = msg;
    /* if the head is empty, set head */
    if (mq->msg_queue_head == RT_NULL)
        mq->msg_queue_head = msg;

mysterywolf's avatar
mysterywolf 已提交
2890
    if(mq->entry < RT_MQ_ENTRY_MAX)
2891 2892 2893 2894 2895 2896 2897 2898 2899
    {
        /* increase message entry */
        mq->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
D
dzzxzz@gmail.com 已提交
2900 2901 2902 2903

    /* resume suspended thread */
    if (!rt_list_isempty(&mq->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2904
        _ipc_list_resume(&(mq->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
2918
}
2919 2920
RTM_EXPORT(rt_mq_send_wait)

C
chenjh 已提交
2921

2922
/**
C
chenjh 已提交
2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
 * @brief    This function will send a message to the messagequeue object.
 *           If there is a thread suspended on the messagequeue, the thread will be resumed.
 *
 * @note     When using this function to send a message, if the messagequeue is fully used,
 *           the current thread will wait for a timeout.
 *           By contrast, when the messagequeue is fully used, the rt_mq_send_wait() function will
 *           return an error code immediately without waiting.
 *
 * @see      rt_mq_send_wait()
 *
 * @param    mq is a pointer to the messagequeue object to be sent.
 *
 * @param    buffer is the content of the message.
 *
 * @param    size is the length of the message(Unit: Byte).
2938
 *
C
chenjh 已提交
2939 2940
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the messagequeue detach failed.
2941
 *
C
chenjh 已提交
2942
 * @warning  This function can be called in interrupt context and thread context.
2943 2944 2945 2946 2947
 */
rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size)
{
    return rt_mq_send_wait(mq, buffer, size, 0);
}
2948
RTM_EXPORT(rt_mq_send);
2949

C
chenjh 已提交
2950

2951
/**
C
chenjh 已提交
2952 2953 2954 2955 2956 2957 2958 2959 2960
 * @brief    This function will send an urgent message to the messagequeue object.
 *
 * @note     This function is almost the same as the rt_mq_send() function. The only difference is that
 *           when sending an urgent message, the message is placed at the head of the messagequeue so that
 *           the recipient can receive the urgent message first.
 *
 * @see      rt_mq_send()
 *
 * @param    mq is a pointer to the messagequeue object to be sent.
2961
 *
C
chenjh 已提交
2962
 * @param    buffer is the content of the message.
2963
 *
C
chenjh 已提交
2964 2965 2966 2967
 * @param    size is the length of the message(Unit: Byte).
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mailbox detach failed.
2968
 */
2969
rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size)
2970
{
D
dzzxzz@gmail.com 已提交
2971 2972
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
2973

2974
    /* parameter check */
D
dzzxzz@gmail.com 已提交
2975
    RT_ASSERT(mq != RT_NULL);
2976
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
2977 2978
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);
2979

D
dzzxzz@gmail.com 已提交
2980 2981 2982
    /* greater than one message size */
    if (size > mq->msg_size)
        return -RT_ERROR;
2983

D
dzzxzz@gmail.com 已提交
2984
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(mq->parent.parent)));
2985

D
dzzxzz@gmail.com 已提交
2986 2987
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
2988

D
dzzxzz@gmail.com 已提交
2989 2990 2991 2992 2993 2994 2995
    /* get a free list, there must be an empty item */
    msg = (struct rt_mq_message *)mq->msg_queue_free;
    /* message queue is full */
    if (msg == RT_NULL)
    {
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2996

D
dzzxzz@gmail.com 已提交
2997 2998 2999 3000
        return -RT_EFULL;
    }
    /* move free list pointer */
    mq->msg_queue_free = msg->next;
3001

D
dzzxzz@gmail.com 已提交
3002 3003
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
3004

D
dzzxzz@gmail.com 已提交
3005 3006
    /* copy buffer */
    rt_memcpy(msg + 1, buffer, size);
3007

D
dzzxzz@gmail.com 已提交
3008 3009
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
3010

D
dzzxzz@gmail.com 已提交
3011
    /* link msg to the beginning of message queue */
3012
    msg->next = (struct rt_mq_message *)mq->msg_queue_head;
D
dzzxzz@gmail.com 已提交
3013
    mq->msg_queue_head = msg;
3014

D
dzzxzz@gmail.com 已提交
3015 3016 3017
    /* if there is no tail */
    if (mq->msg_queue_tail == RT_NULL)
        mq->msg_queue_tail = msg;
3018

mysterywolf's avatar
mysterywolf 已提交
3019
    if(mq->entry < RT_MQ_ENTRY_MAX)
3020 3021 3022 3023 3024 3025 3026 3027 3028
    {
        /* increase message entry */
        mq->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
mysterywolf's avatar
mysterywolf 已提交
3029

D
dzzxzz@gmail.com 已提交
3030 3031 3032
    /* resume suspended thread */
    if (!rt_list_isempty(&mq->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
3033
        _ipc_list_resume(&(mq->parent.suspend_thread));
3034

D
dzzxzz@gmail.com 已提交
3035 3036
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
3037

D
dzzxzz@gmail.com 已提交
3038
        rt_schedule();
D
dzzxzz 已提交
3039

D
dzzxzz@gmail.com 已提交
3040 3041
        return RT_EOK;
    }
3042

D
dzzxzz@gmail.com 已提交
3043 3044
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
3045

D
dzzxzz@gmail.com 已提交
3046
    return RT_EOK;
3047
}
3048
RTM_EXPORT(rt_mq_urgent);
3049

C
chenjh 已提交
3050

3051
/**
C
chenjh 已提交
3052 3053 3054 3055 3056 3057 3058 3059
 * @brief    This function will receive a message from message queue object,
 *           if there is no message in messagequeue object, the thread shall wait for a specified time.
 *
 * @note     Only when there is mail in the mailbox, the receiving thread can get the mail immediately and return RT_EOK,
 *           otherwise the receiving thread will be suspended until timeout.
 *           If the mail is not received within the specified time, it will return -RT_ETIMEOUT.
 *
 * @param    mq is a pointer to the messagequeue object to be received.
3060
 *
C
chenjh 已提交
3061
 * @param    buffer is the content of the message.
3062
 *
C
chenjh 已提交
3063 3064 3065 3066 3067 3068
 * @param    size is the length of the message(Unit: Byte).
 *
 * @param    timeout is a timeout period (unit: an OS tick).
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that the mailbox release failed.
3069
 */
D
dzzxzz@gmail.com 已提交
3070 3071 3072 3073
rt_err_t rt_mq_recv(rt_mq_t    mq,
                    void      *buffer,
                    rt_size_t  size,
                    rt_int32_t timeout)
3074
{
D
dzzxzz@gmail.com 已提交
3075 3076 3077 3078 3079
    struct rt_thread *thread;
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
    rt_uint32_t tick_delta;

3080
    /* parameter check */
D
dzzxzz@gmail.com 已提交
3081
    RT_ASSERT(mq != RT_NULL);
3082
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
3083 3084 3085
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);

3086 3087 3088 3089 3090 3091
    /* current context checking */
    if (timeout != 0)
    {
        RT_DEBUG_SCHEDULER_AVAILABLE;
    }

D
dzzxzz@gmail.com 已提交
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();
    RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));

    /* disable interrupt */
    temp = rt_hw_interrupt_disable();

    /* for non-blocking call */
    if (mq->entry == 0 && timeout == 0)
    {
        rt_hw_interrupt_enable(temp);

        return -RT_ETIMEOUT;
    }

    /* message queue is empty */
    while (mq->entry == 0)
    {
        /* reset error number in thread */
        thread->error = RT_EOK;

        /* no waiting, return timeout */
        if (timeout == 0)
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(temp);

            thread->error = -RT_ETIMEOUT;

            return -RT_ETIMEOUT;
        }

        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
3127
        _ipc_list_suspend(&(mq->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137
                            thread,
                            mq->parent.parent.flag);

        /* has waiting time, start thread timer */
        if (timeout > 0)
        {
            /* get the start tick of timer */
            tick_delta = rt_tick_get();

            RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",
3138
                                        thread->name));
3139

D
dzzxzz@gmail.com 已提交
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182
            /* 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(temp);

        /* re-schedule */
        rt_schedule();

        /* recv message */
        if (thread->error != RT_EOK)
        {
            /* return error */
            return thread->error;
        }

        /* disable interrupt */
        temp = rt_hw_interrupt_disable();

        /* if it's not waiting forever and then re-calculate timeout tick */
        if (timeout > 0)
        {
            tick_delta = rt_tick_get() - tick_delta;
            timeout -= tick_delta;
            if (timeout < 0)
                timeout = 0;
        }
    }

    /* get message from queue */
    msg = (struct rt_mq_message *)mq->msg_queue_head;

    /* move message queue head */
    mq->msg_queue_head = msg->next;
    /* reach queue tail, set to NULL */
    if (mq->msg_queue_tail == msg)
        mq->msg_queue_tail = RT_NULL;

    /* decrease message entry */
3183 3184 3185 3186
    if(mq->entry > 0)
    {
        mq->entry --;
    }
D
dzzxzz@gmail.com 已提交
3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    /* copy message */
    rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size);

    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
    /* put message to free list */
    msg->next = (struct rt_mq_message *)mq->msg_queue_free;
    mq->msg_queue_free = msg;
3199 3200 3201 3202

    /* resume suspended thread */
    if (!rt_list_isempty(&(mq->suspend_sender_thread)))
    {
mysterywolf's avatar
mysterywolf 已提交
3203
        _ipc_list_resume(&(mq->suspend_sender_thread));
3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));

        rt_schedule();

        return RT_EOK;
    }

D
dzzxzz@gmail.com 已提交
3215 3216 3217 3218 3219 3220
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(mq->parent.parent)));

    return RT_EOK;
3221
}
B
Bernard Xiong 已提交
3222
RTM_EXPORT(rt_mq_recv);
3223

C
chenjh 已提交
3224

3225
/**
C
chenjh 已提交
3226 3227 3228
 * @brief    This function will set some extra attributions of a messagequeue object.
 *
 * @note     Currently this function only supports the RT_IPC_CMD_RESET command to reset the messagequeue.
3229
 *
C
chenjh 已提交
3230
 * @param    mq is a pointer to a messagequeue object.
3231
 *
C
chenjh 已提交
3232 3233 3234 3235 3236 3237
 * @param    cmd is a command used to configure some attributions of the messagequeue.
 *
 * @param    arg is the argument of the function to execute the command.
 *
 * @return   Return the operation status. When the return value is RT_EOK, the operation is successful.
 *           If the return value is any other values, it means that this function failed to execute.
3238
 */
B
bernard 已提交
3239
rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg)
3240
{
D
dzzxzz@gmail.com 已提交
3241 3242
    rt_ubase_t level;
    struct rt_mq_message *msg;
3243

3244
    /* parameter check */
D
dzzxzz@gmail.com 已提交
3245
    RT_ASSERT(mq != RT_NULL);
3246
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
3247

D
dzzxzz@gmail.com 已提交
3248 3249 3250 3251
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
3252

D
dzzxzz@gmail.com 已提交
3253
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
3254
        _ipc_list_resume_all(&mq->parent.suspend_thread);
3255
        /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
3256
        _ipc_list_resume_all(&(mq->suspend_sender_thread));
3257

D
dzzxzz@gmail.com 已提交
3258 3259 3260 3261 3262
        /* release all message in the queue */
        while (mq->msg_queue_head != RT_NULL)
        {
            /* get message from queue */
            msg = (struct rt_mq_message *)mq->msg_queue_head;
3263

D
dzzxzz@gmail.com 已提交
3264 3265 3266 3267 3268
            /* move message queue head */
            mq->msg_queue_head = msg->next;
            /* reach queue tail, set to NULL */
            if (mq->msg_queue_tail == msg)
                mq->msg_queue_tail = RT_NULL;
3269

D
dzzxzz@gmail.com 已提交
3270 3271 3272 3273
            /* put message to free list */
            msg->next = (struct rt_mq_message *)mq->msg_queue_free;
            mq->msg_queue_free = msg;
        }
3274

D
dzzxzz@gmail.com 已提交
3275 3276
        /* clean entry */
        mq->entry = 0;
3277

D
dzzxzz@gmail.com 已提交
3278 3279
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
3280

D
dzzxzz@gmail.com 已提交
3281
        rt_schedule();
3282

D
dzzxzz@gmail.com 已提交
3283 3284
        return RT_EOK;
    }
3285

D
dzzxzz@gmail.com 已提交
3286
    return -RT_ERROR;
3287
}
D
dzzxzz@gmail.com 已提交
3288
RTM_EXPORT(rt_mq_control);
C
chenjh 已提交
3289 3290

/**@}*/
3291
#endif /* RT_USING_MESSAGEQUEUE */
D
dzzxzz 已提交
3292

D
dogandog 已提交
3293
/**@}*/