ipc.c 109.1 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);
H
Henson 已提交
487

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

D
dzzxzz@gmail.com 已提交
490 491
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
492

D
dzzxzz@gmail.com 已提交
493
    RT_DEBUG_LOG(RT_DEBUG_IPC, ("thread %s take sem:%s, which value is: %d\n",
494 495 496
                                rt_thread_self()->name,
                                ((struct rt_object *)sem)->name,
                                sem->value));
497

D
dzzxzz@gmail.com 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    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
        {
517 518 519
            /* current context checking */
            RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);

D
dzzxzz@gmail.com 已提交
520 521 522 523 524 525 526 527
            /* 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",
528
                                        thread->name));
529

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

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

D
dzzxzz@gmail.com 已提交
541 542 543 544 545 546
                /* 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));
            }
547

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

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

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

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

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

C
chenjh 已提交
567

568
/**
C
chenjh 已提交
569
 * @brief    This function will try to take a semaphore, if the semaphore is unavailable, the thread returns immediately.
570
 *
C
chenjh 已提交
571 572 573
 * @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).
574
 *
C
chenjh 已提交
575 576 577 578 579 580
 * @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.
581 582 583
 */
rt_err_t rt_sem_trytake(rt_sem_t sem)
{
mysterywolf's avatar
mysterywolf 已提交
584
    return rt_sem_take(sem, RT_WAITING_NO);
585
}
586
RTM_EXPORT(rt_sem_trytake);
587

C
chenjh 已提交
588

589
/**
C
chenjh 已提交
590 591 592 593 594
 * @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.
595
 *
C
chenjh 已提交
596
 * @param    sem is a pointer to a semaphore object.
597
 *
C
chenjh 已提交
598 599
 * @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.
600 601 602
 */
rt_err_t rt_sem_release(rt_sem_t sem)
{
D
dzzxzz@gmail.com 已提交
603 604
    register rt_base_t temp;
    register rt_bool_t need_schedule;
605

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

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

D
dzzxzz@gmail.com 已提交
612
    need_schedule = RT_FALSE;
613

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

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

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

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

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

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

C
chenjh 已提交
652

653
/**
C
chenjh 已提交
654 655 656 657 658 659 660
 * @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.
661
 *
C
chenjh 已提交
662
 * @param    arg is the argument of the function to execute the command.
663
 *
C
chenjh 已提交
664 665
 * @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.
666
 */
B
bernard 已提交
667
rt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg)
668
{
D
dzzxzz@gmail.com 已提交
669
    rt_ubase_t level;
670 671

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

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

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

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

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

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

D
dzzxzz@gmail.com 已提交
693
        rt_schedule();
694

D
dzzxzz@gmail.com 已提交
695 696
        return RT_EOK;
    }
697

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

/**@}*/
703
#endif /* RT_USING_SEMAPHORE */
704 705 706

#ifdef RT_USING_MUTEX
/**
C
chenjh 已提交
707 708 709 710 711 712 713 714 715 716 717 718
 * @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.
719
 *
C
chenjh 已提交
720
 * @see      rt_mutex_create()
721
 *
C
chenjh 已提交
722 723 724 725 726 727 728
 * @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 已提交
729
 *           NOTE: This parameter has been obsoleted. It can be RT_IPC_FLAG_PRIO, RT_IPC_FLAG_FIFO or RT_NULL.
C
chenjh 已提交
730 731 732 733 734
 *
 * @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.
735
 */
D
dzzxzz 已提交
736
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
737
{
mysterywolf's avatar
mysterywolf 已提交
738 739
    /* flag parameter has been obsoleted */
    RT_UNUSED(flag);
740

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

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

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

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

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

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

C
chenjh 已提交
762

763
/**
C
chenjh 已提交
764
 * @brief    This function will detach a static mutex object.
765
 *
C
chenjh 已提交
766 767 768
 * @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.
769
 *
C
chenjh 已提交
770
 * @see      rt_mutex_delete()
771
 *
C
chenjh 已提交
772 773 774 775 776 777 778 779
 * @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.
780
 */
D
dzzxzz 已提交
781
rt_err_t rt_mutex_detach(rt_mutex_t mutex)
782
{
783
    /* parameter check */
D
dzzxzz@gmail.com 已提交
784
    RT_ASSERT(mutex != RT_NULL);
785 786
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
    RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent));
787

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

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

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

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
800 801 802 803 804 805
 * @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()
806
 *
C
chenjh 已提交
807
 * @param    name is a pointer to the name that given to the mutex.
808
 *
C
chenjh 已提交
809 810
 * @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 已提交
811
 *           NOTE: This parameter has been obsoleted. It can be RT_IPC_FLAG_PRIO, RT_IPC_FLAG_FIFO or RT_NULL.
812
 *
C
chenjh 已提交
813 814 815
 * @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.
816
 */
D
dzzxzz 已提交
817
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
818
{
D
dzzxzz@gmail.com 已提交
819
    struct rt_mutex *mutex;
mysterywolf's avatar
mysterywolf 已提交
820 821 822

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

D
dzzxzz@gmail.com 已提交
824
    RT_DEBUG_NOT_IN_INTERRUPT;
825

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

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

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

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

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

C
chenjh 已提交
846

847
/**
C
chenjh 已提交
848 849 850 851 852 853 854
 * @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()
855
 *
C
chenjh 已提交
856
 * @param    mutex is a pointer to a mutex object to be deleted.
857
 *
C
chenjh 已提交
858 859
 * @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.
860
 *
C
chenjh 已提交
861 862 863
 * @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.
864
 */
D
dzzxzz 已提交
865
rt_err_t rt_mutex_delete(rt_mutex_t mutex)
866
{
867
    /* parameter check */
D
dzzxzz@gmail.com 已提交
868
    RT_ASSERT(mutex != RT_NULL);
869 870
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
    RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent) == RT_FALSE);
871

872 873
    RT_DEBUG_NOT_IN_INTERRUPT;

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

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

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

C
chenjh 已提交
885

886
/**
C
chenjh 已提交
887 888 889
 * @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 已提交
890 891
 * @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 已提交
892
 *           thread preparing to take the mutex.
J
jihongbin 已提交
893
 *           On the contrary, the rt_mutex_release() function will increase the count value of mutex->value by 1 each time.
C
chenjh 已提交
894 895 896 897
 *
 * @see      rt_mutex_trytake()
 *
 * @param    mutex is a pointer to a mutex object.
898
 *
C
chenjh 已提交
899 900 901 902
 * @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.
903
 *
C
chenjh 已提交
904 905 906 907
 * @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.
908
 */
D
dzzxzz 已提交
909
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
910
{
D
dzzxzz@gmail.com 已提交
911 912
    register rt_base_t temp;
    struct rt_thread *thread;
913

B
bernard 已提交
914
    /* this function must not be used in interrupt even if time = 0 */
915
    /* current context checking */
H
Henson 已提交
916
    RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
917

918
    /* parameter check */
919
    RT_ASSERT(mutex != RT_NULL);
920
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
921 922 923

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

D
dzzxzz@gmail.com 已提交
925 926
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
927

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

D
dzzxzz@gmail.com 已提交
930
    RT_DEBUG_LOG(RT_DEBUG_IPC,
931 932
                 ("mutex_take: current thread %s, mutex value: %d, hold: %d\n",
                  thread->name, mutex->value, mutex->hold));
933

D
dzzxzz@gmail.com 已提交
934 935 936 937 938
    /* reset thread error */
    thread->error = RT_EOK;

    if (mutex->owner == thread)
    {
mysterywolf's avatar
mysterywolf 已提交
939
        if(mutex->hold < RT_MUTEX_HOLD_MAX)
940 941 942 943 944 945 946 947 948
        {
            /* it's the same thread */
            mutex->hold ++;
        }
        else
        {
            rt_hw_interrupt_enable(temp); /* enable interrupt */
            return -RT_EFULL; /* value overflowed */
        }
D
dzzxzz@gmail.com 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962
    }
    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 已提交
963
            if(mutex->hold < RT_MUTEX_HOLD_MAX)
964 965 966 967 968 969 970 971
            {
                mutex->hold ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
D
dzzxzz@gmail.com 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
        }
        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",
990
                                            thread->name));
991

D
dzzxzz@gmail.com 已提交
992 993 994 995 996 997 998 999 1000 1001
                /* 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 已提交
1002
                _ipc_list_suspend(&(mutex->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
1003 1004 1005 1006 1007 1008 1009
                                    thread,
                                    mutex->parent.parent.flag);

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

D
dzzxzz@gmail.com 已提交
1013 1014 1015 1016 1017 1018 1019 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
                    /* 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;
1047
}
1048
RTM_EXPORT(rt_mutex_take);
1049

C
chenjh 已提交
1050

mysterywolf's avatar
mysterywolf 已提交
1051
/**
C
chenjh 已提交
1052 1053 1054 1055 1056 1057 1058 1059
 * @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 已提交
1060
 *
C
chenjh 已提交
1061
 * @param    mutex is a pointer to a mutex object.
mysterywolf's avatar
mysterywolf 已提交
1062
 *
C
chenjh 已提交
1063 1064
 * @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 已提交
1065 1066 1067 1068 1069 1070 1071
 */
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 已提交
1072

1073
/**
C
chenjh 已提交
1074 1075 1076 1077 1078
 * @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.
1079
 *
C
chenjh 已提交
1080
 * @param    mutex is a pointer to a mutex object.
1081
 *
C
chenjh 已提交
1082 1083
 * @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.
1084 1085 1086
 */
rt_err_t rt_mutex_release(rt_mutex_t mutex)
{
D
dzzxzz@gmail.com 已提交
1087 1088 1089
    register rt_base_t temp;
    struct rt_thread *thread;
    rt_bool_t need_schedule;
1090

1091 1092 1093 1094
    /* parameter check */
    RT_ASSERT(mutex != RT_NULL);
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);

D
dzzxzz@gmail.com 已提交
1095
    need_schedule = RT_FALSE;
1096

1097 1098
    /* only thread could release mutex because we need test the ownership */
    RT_DEBUG_IN_THREAD_CONTEXT;
1099

B
bernard 已提交
1100 1101 1102
    /* get current thread */
    thread = rt_thread_self();

D
dzzxzz@gmail.com 已提交
1103 1104
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
1105

D
dzzxzz@gmail.com 已提交
1106
    RT_DEBUG_LOG(RT_DEBUG_IPC,
1107 1108
                 ("mutex_release:current thread %s, mutex value: %d, hold: %d\n",
                  thread->name, mutex->value, mutex->hold));
1109

D
dzzxzz@gmail.com 已提交
1110 1111 1112 1113 1114 1115 1116 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
    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",
1145
                                        thread->name));
1146

D
dzzxzz@gmail.com 已提交
1147 1148 1149
            /* set new owner and priority */
            mutex->owner             = thread;
            mutex->original_priority = thread->current_priority;
St0pH@CK's avatar
St0pH@CK 已提交
1150

mysterywolf's avatar
mysterywolf 已提交
1151
            if(mutex->hold < RT_MUTEX_HOLD_MAX)
1152 1153 1154 1155 1156 1157 1158 1159
            {
                mutex->hold ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
1160

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

D
dzzxzz@gmail.com 已提交
1164 1165 1166 1167
            need_schedule = RT_TRUE;
        }
        else
        {
mysterywolf's avatar
mysterywolf 已提交
1168
            if(mutex->value < RT_MUTEX_VALUE_MAX)
1169 1170 1171 1172 1173 1174 1175 1176 1177
            {
                /* increase value */
                mutex->value ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
mysterywolf's avatar
mysterywolf 已提交
1178

D
dzzxzz@gmail.com 已提交
1179 1180 1181 1182 1183
            /* clear owner */
            mutex->owner             = RT_NULL;
            mutex->original_priority = 0xff;
        }
    }
1184

D
dzzxzz@gmail.com 已提交
1185 1186
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
1187

D
dzzxzz@gmail.com 已提交
1188 1189 1190
    /* perform a schedule */
    if (need_schedule == RT_TRUE)
        rt_schedule();
B
bernard.xiong 已提交
1191

D
dzzxzz@gmail.com 已提交
1192
    return RT_EOK;
1193
}
1194
RTM_EXPORT(rt_mutex_release);
1195

C
chenjh 已提交
1196

1197
/**
C
chenjh 已提交
1198 1199 1200
 * @brief    This function will set some extra attributions of a mutex object.
 *
 * @note     Currently this function does not implement the control function.
1201
 *
C
chenjh 已提交
1202
 * @param    mutex is a pointer to a mutex object.
1203
 *
C
chenjh 已提交
1204 1205 1206 1207 1208 1209
 * @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.
1210
 */
B
bernard 已提交
1211
rt_err_t rt_mutex_control(rt_mutex_t mutex, int cmd, void *arg)
1212
{
1213 1214 1215 1216
    /* parameter check */
    RT_ASSERT(mutex != RT_NULL);
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);

D
dzzxzz@gmail.com 已提交
1217
    return -RT_ERROR;
1218
}
1219
RTM_EXPORT(rt_mutex_control);
C
chenjh 已提交
1220 1221

/**@}*/
1222
#endif /* RT_USING_MUTEX */
1223 1224 1225

#ifdef RT_USING_EVENT
/**
C
chenjh 已提交
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
 * @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.
1251
 *
C
chenjh 已提交
1252 1253
 *               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).
1254
 *
C
chenjh 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263
 *               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.
1264
 */
D
dzzxzz 已提交
1265
rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
1266
{
1267
    /* parameter check */
D
dzzxzz@gmail.com 已提交
1268
    RT_ASSERT(event != RT_NULL);
1269
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1270

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

D
dzzxzz@gmail.com 已提交
1274 1275
    /* set parent flag */
    event->parent.parent.flag = flag;
1276

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

B
Bernard Xiong 已提交
1280
    /* initialize event */
D
dzzxzz@gmail.com 已提交
1281
    event->set = 0;
1282

D
dzzxzz@gmail.com 已提交
1283
    return RT_EOK;
1284
}
1285
RTM_EXPORT(rt_event_init);
1286

C
chenjh 已提交
1287

1288
/**
C
chenjh 已提交
1289 1290 1291 1292 1293
 * @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.
1294
 *
C
chenjh 已提交
1295
 * @see      rt_event_delete()
1296
 *
C
chenjh 已提交
1297 1298 1299 1300 1301 1302 1303 1304
 * @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.
1305 1306 1307
 */
rt_err_t rt_event_detach(rt_event_t event)
{
D
dzzxzz@gmail.com 已提交
1308 1309
    /* parameter check */
    RT_ASSERT(event != RT_NULL);
1310 1311
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
    RT_ASSERT(rt_object_is_systemobject(&event->parent.parent));
1312

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

D
dzzxzz@gmail.com 已提交
1316 1317
    /* detach event object */
    rt_object_detach(&(event->parent.parent));
1318

D
dzzxzz@gmail.com 已提交
1319
    return RT_EOK;
1320
}
1321
RTM_EXPORT(rt_event_detach);
1322 1323 1324

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
1325 1326 1327 1328 1329 1330
 * @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()
1331
 *
C
chenjh 已提交
1332
 * @param    name is a pointer to the name that given to the event.
1333
 *
C
chenjh 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
 * @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.
1351
 */
D
dzzxzz 已提交
1352
rt_event_t rt_event_create(const char *name, rt_uint8_t flag)
1353
{
D
dzzxzz@gmail.com 已提交
1354
    rt_event_t event;
1355

1356
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1357 1358

    RT_DEBUG_NOT_IN_INTERRUPT;
H
HelloByeAll 已提交
1359

D
dzzxzz@gmail.com 已提交
1360 1361 1362 1363
    /* allocate object */
    event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);
    if (event == RT_NULL)
        return event;
1364

D
dzzxzz@gmail.com 已提交
1365 1366
    /* set parent */
    event->parent.parent.flag = flag;
1367

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

B
Bernard Xiong 已提交
1371
    /* initialize event */
D
dzzxzz@gmail.com 已提交
1372
    event->set = 0;
1373

D
dzzxzz@gmail.com 已提交
1374
    return event;
1375
}
1376
RTM_EXPORT(rt_event_create);
1377

C
chenjh 已提交
1378

1379
/**
C
chenjh 已提交
1380 1381 1382 1383 1384 1385 1386
 * @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()
1387
 *
C
chenjh 已提交
1388
 * @param    event is a pointer to an event object to be deleted.
1389
 *
C
chenjh 已提交
1390 1391 1392 1393 1394 1395
 * @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.
1396
 */
D
dzzxzz 已提交
1397
rt_err_t rt_event_delete(rt_event_t event)
1398
{
D
dzzxzz@gmail.com 已提交
1399 1400
    /* parameter check */
    RT_ASSERT(event != RT_NULL);
1401 1402
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
    RT_ASSERT(rt_object_is_systemobject(&event->parent.parent) == RT_FALSE);
1403

D
dzzxzz@gmail.com 已提交
1404
    RT_DEBUG_NOT_IN_INTERRUPT;
1405

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

D
dzzxzz@gmail.com 已提交
1409 1410
    /* delete event object */
    rt_object_delete(&(event->parent.parent));
1411

D
dzzxzz@gmail.com 已提交
1412
    return RT_EOK;
1413
}
1414
RTM_EXPORT(rt_event_delete);
1415
#endif /* RT_USING_HEAP */
1416

C
chenjh 已提交
1417

1418
/**
C
chenjh 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427
 * @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.
1428
 *
C
chenjh 已提交
1429 1430
 * @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.
1431
 *
C
chenjh 已提交
1432 1433
 * @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.
1434 1435 1436
 */
rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
{
D
dzzxzz@gmail.com 已提交
1437 1438 1439 1440 1441 1442 1443 1444
    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);
1445 1446
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);

D
dzzxzz@gmail.com 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
    if (set == 0)
        return -RT_ERROR;

    need_schedule = RT_FALSE;

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

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

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

D
dzzxzz@gmail.com 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    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 已提交
1482
                    /* save the received event set */
D
dzzxzz@gmail.com 已提交
1483 1484 1485 1486 1487 1488
                    thread->event_set = thread->event_set & event->set;

                    /* received an OR event */
                    status = RT_EOK;
                }
            }
1489 1490 1491 1492 1493 1494 1495
            else
            {
                /* enable interrupt */
                rt_hw_interrupt_enable(level);

                return -RT_EINVAL;
            }
D
dzzxzz@gmail.com 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523

            /* 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;
1524
}
1525
RTM_EXPORT(rt_event_send);
1526

C
chenjh 已提交
1527

1528
/**
C
chenjh 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
 * @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.
1540
 *
C
chenjh 已提交
1541 1542 1543
 * @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):
1544
 *
C
chenjh 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
 *
 *               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.
1559
 */
D
dzzxzz@gmail.com 已提交
1560 1561 1562 1563 1564
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)
1565
{
D
dzzxzz@gmail.com 已提交
1566 1567 1568 1569 1570 1571
    struct rt_thread *thread;
    register rt_ubase_t level;
    register rt_base_t status;

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

1574
    /* current context checking */
H
Henson 已提交
1575
    RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
1576

D
dzzxzz@gmail.com 已提交
1577 1578 1579
    if (set == 0)
        return -RT_ERROR;

B
Bernard Xiong 已提交
1580
    /* initialize status */
D
dzzxzz@gmail.com 已提交
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
    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;
    }
1603 1604 1605 1606 1607
    else
    {
        /* either RT_EVENT_FLAG_AND or RT_EVENT_FLAG_OR should be set */
        RT_ASSERT(0);
    }
D
dzzxzz@gmail.com 已提交
1608 1609 1610 1611

    if (status == RT_EOK)
    {
        /* set received event */
1612
        if (recved)
P
pathletboy 已提交
1613
            *recved = (event->set & set);
mysterywolf's avatar
mysterywolf 已提交
1614 1615

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

D
dzzxzz@gmail.com 已提交
1619 1620 1621 1622 1623 1624 1625 1626
        /* received event */
        if (option & RT_EVENT_FLAG_CLEAR)
            event->set &= ~set;
    }
    else if (timeout == 0)
    {
        /* no waiting */
        thread->error = -RT_ETIMEOUT;
1627

G
greed-island 已提交
1628 1629
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
1630

G
greed-island 已提交
1631
        return -RT_ETIMEOUT;
D
dzzxzz@gmail.com 已提交
1632 1633 1634 1635 1636 1637 1638 1639
    }
    else
    {
        /* fill thread event info */
        thread->event_set  = set;
        thread->event_info = option;

        /* put thread to suspended thread list */
mysterywolf's avatar
mysterywolf 已提交
1640
        _ipc_list_suspend(&(event->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
                            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 已提交
1670 1671
        if (recved)
            *recved = thread->event_set;
D
dzzxzz@gmail.com 已提交
1672 1673 1674 1675 1676 1677 1678 1679
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

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

    return thread->error;
1680
}
1681
RTM_EXPORT(rt_event_recv);
1682

C
chenjh 已提交
1683

1684
/**
C
chenjh 已提交
1685 1686 1687 1688 1689
 * @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.
1690
 *
C
chenjh 已提交
1691
 * @param    cmd is a command word used to configure some attributions of the event.
1692
 *
C
chenjh 已提交
1693 1694 1695 1696
 * @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.
1697
 */
B
bernard 已提交
1698
rt_err_t rt_event_control(rt_event_t event, int cmd, void *arg)
1699
{
D
dzzxzz@gmail.com 已提交
1700
    rt_ubase_t level;
1701

1702 1703 1704
    /* parameter check */
    RT_ASSERT(event != RT_NULL);
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
B
Bernard Xiong 已提交
1705

D
dzzxzz@gmail.com 已提交
1706 1707 1708 1709
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
1710

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

B
Bernard Xiong 已提交
1714
        /* initialize event set */
D
dzzxzz@gmail.com 已提交
1715
        event->set = 0;
1716

D
dzzxzz@gmail.com 已提交
1717 1718
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
1719

D
dzzxzz@gmail.com 已提交
1720
        rt_schedule();
1721

D
dzzxzz@gmail.com 已提交
1722 1723
        return RT_EOK;
    }
1724

D
dzzxzz@gmail.com 已提交
1725
    return -RT_ERROR;
1726
}
B
Bernard Xiong 已提交
1727
RTM_EXPORT(rt_event_control);
C
chenjh 已提交
1728 1729

/**@}*/
1730
#endif /* RT_USING_EVENT */
1731 1732 1733

#ifdef RT_USING_MAILBOX
/**
C
chenjh 已提交
1734 1735 1736 1737 1738 1739 1740
 * @addtogroup mailbox
 */

/**@{*/

/**
 * @brief    Initialize a static mailbox object.
1741
 *
C
chenjh 已提交
1742 1743 1744
 * @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.
1745
 *
C
chenjh 已提交
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
 * @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.
1774
 */
D
dzzxzz@gmail.com 已提交
1775 1776 1777 1778 1779
rt_err_t rt_mb_init(rt_mailbox_t mb,
                    const char  *name,
                    void        *msgpool,
                    rt_size_t    size,
                    rt_uint8_t   flag)
1780
{
D
dzzxzz@gmail.com 已提交
1781
    RT_ASSERT(mb != RT_NULL);
1782
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1783

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

D
dzzxzz@gmail.com 已提交
1787 1788
    /* set parent flag */
    mb->parent.parent.flag = flag;
1789

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

B
Bernard Xiong 已提交
1793
    /* initialize mailbox */
1794
    mb->msg_pool   = (rt_ubase_t *)msgpool;
D
dzzxzz@gmail.com 已提交
1795 1796 1797 1798
    mb->size       = size;
    mb->entry      = 0;
    mb->in_offset  = 0;
    mb->out_offset = 0;
1799

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

D
dzzxzz@gmail.com 已提交
1803
    return RT_EOK;
1804
}
1805
RTM_EXPORT(rt_mb_init);
1806

C
chenjh 已提交
1807

1808
/**
C
chenjh 已提交
1809 1810 1811 1812 1813
 * @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.
1814
 *
C
chenjh 已提交
1815
 * @see      rt_mb_delete()
1816
 *
C
chenjh 已提交
1817 1818 1819 1820 1821 1822 1823 1824
 * @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.
1825 1826 1827
 */
rt_err_t rt_mb_detach(rt_mailbox_t mb)
{
D
dzzxzz@gmail.com 已提交
1828 1829
    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
1830 1831
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
    RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent));
1832

D
dzzxzz@gmail.com 已提交
1833
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1834
    _ipc_list_resume_all(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
1835
    /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1836
    _ipc_list_resume_all(&(mb->suspend_sender_thread));
1837

D
dzzxzz@gmail.com 已提交
1838 1839
    /* detach mailbox object */
    rt_object_detach(&(mb->parent.parent));
1840

D
dzzxzz@gmail.com 已提交
1841
    return RT_EOK;
1842
}
1843
RTM_EXPORT(rt_mb_detach);
1844 1845 1846

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
1847 1848 1849 1850 1851 1852
 * @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()
1853
 *
C
chenjh 已提交
1854
 * @param  name is a pointer that given to the mailbox.
1855
 *
C
chenjh 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
 * @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.
1876
 */
D
dzzxzz 已提交
1877
rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
1878
{
D
dzzxzz@gmail.com 已提交
1879
    rt_mailbox_t mb;
1880

1881
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1882

1883 1884
    RT_DEBUG_NOT_IN_INTERRUPT;

D
dzzxzz@gmail.com 已提交
1885 1886 1887 1888
    /* allocate object */
    mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
    if (mb == RT_NULL)
        return mb;
1889

D
dzzxzz@gmail.com 已提交
1890 1891
    /* set parent */
    mb->parent.parent.flag = flag;
1892

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

B
Bernard Xiong 已提交
1896
    /* initialize mailbox */
D
dzzxzz@gmail.com 已提交
1897
    mb->size     = size;
1898
    mb->msg_pool = (rt_ubase_t *)RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t));
D
dzzxzz@gmail.com 已提交
1899 1900 1901 1902
    if (mb->msg_pool == RT_NULL)
    {
        /* delete mailbox object */
        rt_object_delete(&(mb->parent.parent));
1903

D
dzzxzz@gmail.com 已提交
1904 1905 1906 1907 1908
        return RT_NULL;
    }
    mb->entry      = 0;
    mb->in_offset  = 0;
    mb->out_offset = 0;
1909

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

D
dzzxzz@gmail.com 已提交
1913
    return mb;
1914
}
1915
RTM_EXPORT(rt_mb_create);
1916

C
chenjh 已提交
1917

1918
/**
C
chenjh 已提交
1919 1920 1921 1922 1923
 * @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.
1924
 *
C
chenjh 已提交
1925
 * @see      rt_mb_detach()
1926
 *
C
chenjh 已提交
1927 1928 1929 1930 1931 1932 1933 1934
 * @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.
1935
 */
D
dzzxzz 已提交
1936
rt_err_t rt_mb_delete(rt_mailbox_t mb)
M
mbbill@gmail.com 已提交
1937
{
D
dzzxzz@gmail.com 已提交
1938 1939
    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
1940 1941
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
    RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent) == RT_FALSE);
1942

1943 1944
    RT_DEBUG_NOT_IN_INTERRUPT;

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

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

1951 1952
    /* free mailbox pool */
    RT_KERNEL_FREE(mb->msg_pool);
1953

D
dzzxzz@gmail.com 已提交
1954 1955
    /* delete mailbox object */
    rt_object_delete(&(mb->parent.parent));
1956

D
dzzxzz@gmail.com 已提交
1957
    return RT_EOK;
1958
}
1959
RTM_EXPORT(rt_mb_delete);
1960
#endif /* RT_USING_HEAP */
1961

C
chenjh 已提交
1962

1963
/**
C
chenjh 已提交
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
 * @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).
1980
 *
C
chenjh 已提交
1981 1982
 * @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.
1983
 *
C
chenjh 已提交
1984
 * @warning  This function can be called in interrupt context and thread context.
1985
 */
D
dzzxzz@gmail.com 已提交
1986
rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
B
Bernard Xiong 已提交
1987
                         rt_ubase_t   value,
D
dzzxzz@gmail.com 已提交
1988
                         rt_int32_t   timeout)
1989
{
D
dzzxzz@gmail.com 已提交
1990 1991 1992 1993 1994 1995
    struct rt_thread *thread;
    register rt_ubase_t temp;
    rt_uint32_t tick_delta;

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

1998
    /* current context checking */
H
Henson 已提交
1999
    RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2000

D
dzzxzz@gmail.com 已提交
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
    /* 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 已提交
2034
        _ipc_list_suspend(&(mb->suspend_sender_thread),
D
dzzxzz@gmail.com 已提交
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
                            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",
2045
                                        thread->name));
2046

D
dzzxzz@gmail.com 已提交
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
            /* 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 已提交
2086

mysterywolf's avatar
mysterywolf 已提交
2087
    if(mb->entry < RT_MB_ENTRY_MAX)
2088 2089 2090 2091 2092 2093 2094 2095 2096
    {
        /* increase message entry */
        mb->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
mysterywolf's avatar
mysterywolf 已提交
2097

D
dzzxzz@gmail.com 已提交
2098 2099 2100
    /* resume suspended thread */
    if (!rt_list_isempty(&mb->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2101
        _ipc_list_resume(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
2115
}
2116
RTM_EXPORT(rt_mb_send_wait);
2117

C
chenjh 已提交
2118

2119
/**
C
chenjh 已提交
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
 * @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.
2130
 *
C
chenjh 已提交
2131
 * @param    value is a value to the content of the mail you want to send.
2132
 *
C
chenjh 已提交
2133 2134
 * @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.
2135
 */
B
Bernard Xiong 已提交
2136
rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value)
2137
{
D
dzzxzz@gmail.com 已提交
2138
    return rt_mb_send_wait(mb, value, 0);
2139
}
2140
RTM_EXPORT(rt_mb_send);
2141

C
chenjh 已提交
2142

2143
/**
C
chenjh 已提交
2144 2145 2146 2147 2148 2149 2150 2151 2152
 * @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.
2153
 *
C
chenjh 已提交
2154
 * @param    value is the content of the mail you want to send.
2155
 *
C
chenjh 已提交
2156 2157
 * @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.
2158
 */
mysterywolf's avatar
update  
mysterywolf 已提交
2159
rt_err_t rt_mb_urgent(rt_mailbox_t mb, rt_ubase_t value)
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
{
    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 已提交
2172 2173 2174 2175 2176 2177
    if (mb->entry == mb->size)
    {
        rt_hw_interrupt_enable(temp);
        return -RT_EFULL;
    }

2178
    /* rewind to the previous position */
mysterywolf's avatar
update  
mysterywolf 已提交
2179
    if (mb->out_offset > 0)
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
    {
        mb->out_offset --;
    }
    else
    {
        mb->out_offset = mb->size - 1;
    }

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

mysterywolf's avatar
update  
mysterywolf 已提交
2191 2192
    /* increase message entry */
    mb->entry ++;
2193 2194 2195 2196

    /* resume suspended thread */
    if (!rt_list_isempty(&mb->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2197
        _ipc_list_resume(&(mb->parent.suspend_thread));
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213

        /* 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 已提交
2214

2215
/**
C
chenjh 已提交
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
 * @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.
2227
 *
C
chenjh 已提交
2228
 * @param    timeout is a timeout period (unit: an OS tick).
2229
 *
C
chenjh 已提交
2230 2231
 * @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.
2232
 */
B
Bernard Xiong 已提交
2233
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
2234
{
D
dzzxzz@gmail.com 已提交
2235 2236 2237 2238 2239 2240
    struct rt_thread *thread;
    register rt_ubase_t temp;
    rt_uint32_t tick_delta;

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

2243
    /* current context checking */
H
Henson 已提交
2244
    RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2245

D
dzzxzz@gmail.com 已提交
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
    /* 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 已提交
2277

D
dzzxzz@gmail.com 已提交
2278 2279 2280 2281
            return -RT_ETIMEOUT;
        }

        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2282
        _ipc_list_suspend(&(mb->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
                            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",
2293
                                        thread->name));
2294

D
dzzxzz@gmail.com 已提交
2295 2296 2297 2298 2299 2300
            /* 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));
        }
2301

D
dzzxzz@gmail.com 已提交
2302 2303
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2304

D
dzzxzz@gmail.com 已提交
2305 2306
        /* re-schedule */
        rt_schedule();
2307

D
dzzxzz@gmail.com 已提交
2308 2309 2310 2311 2312 2313
        /* resume from suspend state */
        if (thread->error != RT_EOK)
        {
            /* return error */
            return thread->error;
        }
2314

D
dzzxzz@gmail.com 已提交
2315 2316
        /* disable interrupt */
        temp = rt_hw_interrupt_disable();
2317

D
dzzxzz@gmail.com 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326
        /* 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;
        }
    }
2327

D
dzzxzz@gmail.com 已提交
2328 2329
    /* fill ptr */
    *value = mb->msg_pool[mb->out_offset];
2330

D
dzzxzz@gmail.com 已提交
2331 2332 2333 2334
    /* increase output offset */
    ++ mb->out_offset;
    if (mb->out_offset >= mb->size)
        mb->out_offset = 0;
2335

D
dzzxzz@gmail.com 已提交
2336
    /* decrease message entry */
2337 2338 2339 2340
    if(mb->entry > 0)
    {
        mb->entry --;
    }
2341

D
dzzxzz@gmail.com 已提交
2342 2343 2344
    /* resume suspended thread */
    if (!rt_list_isempty(&(mb->suspend_sender_thread)))
    {
mysterywolf's avatar
mysterywolf 已提交
2345
        _ipc_list_resume(&(mb->suspend_sender_thread));
2346

D
dzzxzz@gmail.com 已提交
2347 2348
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2349

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

D
dzzxzz@gmail.com 已提交
2352
        rt_schedule();
2353

D
dzzxzz@gmail.com 已提交
2354 2355
        return RT_EOK;
    }
2356

D
dzzxzz@gmail.com 已提交
2357 2358
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
2359

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

D
dzzxzz@gmail.com 已提交
2362
    return RT_EOK;
2363
}
2364
RTM_EXPORT(rt_mb_recv);
2365

C
chenjh 已提交
2366

2367
/**
C
chenjh 已提交
2368 2369 2370 2371 2372 2373 2374
 * @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.
2375
 *
C
chenjh 已提交
2376
 * @param    arg is the argument of the function to execute the command.
2377
 *
C
chenjh 已提交
2378 2379
 * @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.
2380
 */
B
bernard 已提交
2381
rt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg)
2382
{
D
dzzxzz@gmail.com 已提交
2383
    rt_ubase_t level;
2384 2385

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

D
dzzxzz@gmail.com 已提交
2389 2390 2391 2392
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
2393

D
dzzxzz@gmail.com 已提交
2394
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
2395
        _ipc_list_resume_all(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2396
        /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2397
        _ipc_list_resume_all(&(mb->suspend_sender_thread));
2398

D
dzzxzz@gmail.com 已提交
2399 2400 2401 2402
        /* re-init mailbox */
        mb->entry      = 0;
        mb->in_offset  = 0;
        mb->out_offset = 0;
2403

D
dzzxzz@gmail.com 已提交
2404 2405
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
2406

D
dzzxzz@gmail.com 已提交
2407
        rt_schedule();
2408

D
dzzxzz@gmail.com 已提交
2409 2410
        return RT_EOK;
    }
2411

D
dzzxzz@gmail.com 已提交
2412
    return -RT_ERROR;
2413
}
B
Bernard Xiong 已提交
2414
RTM_EXPORT(rt_mb_control);
C
chenjh 已提交
2415 2416

/**@}*/
2417
#endif /* RT_USING_MAILBOX */
2418 2419

#ifdef RT_USING_MESSAGEQUEUE
C
chenjh 已提交
2420 2421 2422 2423 2424 2425
/**
 * @addtogroup messagequeue
 */

/**@{*/

2426 2427
struct rt_mq_message
{
D
dzzxzz@gmail.com 已提交
2428
    struct rt_mq_message *next;
2429 2430
};

C
chenjh 已提交
2431

2432
/**
C
chenjh 已提交
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
 * @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.
2460
 *
C
chenjh 已提交
2461 2462
 *               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).
2463
 *
C
chenjh 已提交
2464 2465 2466 2467 2468 2469 2470 2471 2472
 *               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.
2473
 */
D
dzzxzz@gmail.com 已提交
2474 2475 2476 2477 2478 2479
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)
2480
{
D
dzzxzz@gmail.com 已提交
2481 2482
    struct rt_mq_message *head;
    register rt_base_t temp;
2483

D
dzzxzz@gmail.com 已提交
2484 2485
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2486
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
2487

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

D
dzzxzz@gmail.com 已提交
2491 2492
    /* set parent flag */
    mq->parent.parent.flag = flag;
2493

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

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

D
dzzxzz@gmail.com 已提交
2500 2501 2502
    /* 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));
2503

B
Bernard Xiong 已提交
2504
    /* initialize message list */
D
dzzxzz@gmail.com 已提交
2505 2506
    mq->msg_queue_head = RT_NULL;
    mq->msg_queue_tail = RT_NULL;
2507

B
Bernard Xiong 已提交
2508
    /* initialize message empty list */
D
dzzxzz@gmail.com 已提交
2509 2510 2511 2512
    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 +
2513
                                        temp * (mq->msg_size + sizeof(struct rt_mq_message)));
2514
        head->next = (struct rt_mq_message *)mq->msg_queue_free;
D
dzzxzz@gmail.com 已提交
2515 2516
        mq->msg_queue_free = head;
    }
2517

D
dzzxzz@gmail.com 已提交
2518 2519
    /* the initial entry is zero */
    mq->entry = 0;
2520

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

D
dzzxzz@gmail.com 已提交
2524
    return RT_EOK;
2525
}
2526
RTM_EXPORT(rt_mq_init);
2527

C
chenjh 已提交
2528

2529
/**
C
chenjh 已提交
2530 2531 2532 2533 2534
 * @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.
2535
 *
C
chenjh 已提交
2536
 * @see      rt_mq_delete()
2537
 *
C
chenjh 已提交
2538 2539 2540 2541 2542 2543 2544 2545
 * @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.
2546 2547 2548
 */
rt_err_t rt_mq_detach(rt_mq_t mq)
{
D
dzzxzz@gmail.com 已提交
2549 2550
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2551 2552
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
    RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent));
2553

D
dzzxzz@gmail.com 已提交
2554
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2555
    _ipc_list_resume_all(&mq->parent.suspend_thread);
2556
    /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2557
    _ipc_list_resume_all(&(mq->suspend_sender_thread));
2558

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

D
dzzxzz@gmail.com 已提交
2562
    return RT_EOK;
2563
}
2564
RTM_EXPORT(rt_mq_detach);
2565 2566 2567

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
2568 2569 2570 2571 2572 2573
 * @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()
2574
 *
C
chenjh 已提交
2575
 * @param    name is a pointer that given to the messagequeue.
2576
 *
C
chenjh 已提交
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
 * @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.
2598
 */
D
dzzxzz@gmail.com 已提交
2599 2600 2601 2602
rt_mq_t rt_mq_create(const char *name,
                     rt_size_t   msg_size,
                     rt_size_t   max_msgs,
                     rt_uint8_t  flag)
2603
{
D
dzzxzz@gmail.com 已提交
2604 2605 2606 2607
    struct rt_messagequeue *mq;
    struct rt_mq_message *head;
    register rt_base_t temp;

2608
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
2609 2610

    RT_DEBUG_NOT_IN_INTERRUPT;
H
HelloByeAll 已提交
2611

D
dzzxzz@gmail.com 已提交
2612 2613 2614 2615 2616 2617 2618 2619
    /* 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 已提交
2620
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
2621
    _ipc_object_init(&(mq->parent));
D
dzzxzz@gmail.com 已提交
2622

B
Bernard Xiong 已提交
2623
    /* initialize message queue */
D
dzzxzz@gmail.com 已提交
2624 2625 2626 2627 2628 2629

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

    /* allocate message pool */
2630
    mq->msg_pool = RT_KERNEL_MALLOC((mq->msg_size + sizeof(struct rt_mq_message)) * mq->max_msgs);
D
dzzxzz@gmail.com 已提交
2631 2632
    if (mq->msg_pool == RT_NULL)
    {
2633
        rt_object_delete(&(mq->parent.parent));
D
dzzxzz@gmail.com 已提交
2634 2635 2636 2637

        return RT_NULL;
    }

B
Bernard Xiong 已提交
2638
    /* initialize message list */
D
dzzxzz@gmail.com 已提交
2639 2640 2641
    mq->msg_queue_head = RT_NULL;
    mq->msg_queue_tail = RT_NULL;

B
Bernard Xiong 已提交
2642
    /* initialize message empty list */
D
dzzxzz@gmail.com 已提交
2643 2644 2645 2646
    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 +
2647
                                        temp * (mq->msg_size + sizeof(struct rt_mq_message)));
2648
        head->next = (struct rt_mq_message *)mq->msg_queue_free;
D
dzzxzz@gmail.com 已提交
2649 2650 2651 2652 2653 2654
        mq->msg_queue_free = head;
    }

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

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

D
dzzxzz@gmail.com 已提交
2658
    return mq;
2659
}
2660
RTM_EXPORT(rt_mq_create);
2661

C
chenjh 已提交
2662

2663
/**
C
chenjh 已提交
2664
 * @brief    This function will delete a messagequeue object and release the memory.
2665
 *
C
chenjh 已提交
2666 2667 2668
 * @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.
2669
 *
C
chenjh 已提交
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
 * @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.
2681
 */
D
dzzxzz 已提交
2682
rt_err_t rt_mq_delete(rt_mq_t mq)
2683
{
D
dzzxzz@gmail.com 已提交
2684 2685
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2686 2687
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
    RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent) == RT_FALSE);
2688

2689 2690
    RT_DEBUG_NOT_IN_INTERRUPT;

D
dzzxzz@gmail.com 已提交
2691
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2692
    _ipc_list_resume_all(&(mq->parent.suspend_thread));
2693
    /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2694
    _ipc_list_resume_all(&(mq->suspend_sender_thread));
2695

2696 2697
    /* free message queue pool */
    RT_KERNEL_FREE(mq->msg_pool);
2698

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

D
dzzxzz@gmail.com 已提交
2702
    return RT_EOK;
2703
}
2704
RTM_EXPORT(rt_mq_delete);
2705
#endif /* RT_USING_HEAP */
2706

C
chenjh 已提交
2707

2708
/**
C
chenjh 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
 * @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.
2725
 *
C
chenjh 已提交
2726
 * @param    size is the length of the message(Unit: Byte).
2727
 *
C
chenjh 已提交
2728 2729 2730 2731 2732 2733 2734 2735
 * @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.
2736
 */
2737 2738 2739 2740
rt_err_t rt_mq_send_wait(rt_mq_t     mq,
                         const void *buffer,
                         rt_size_t   size,
                         rt_int32_t  timeout)
2741
{
D
dzzxzz@gmail.com 已提交
2742 2743
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
2744 2745
    rt_uint32_t tick_delta;
    struct rt_thread *thread;
D
dzzxzz@gmail.com 已提交
2746

2747
    /* parameter check */
D
dzzxzz@gmail.com 已提交
2748
    RT_ASSERT(mq != RT_NULL);
2749
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
2750 2751 2752
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);

2753
    /* current context checking */
H
Henson 已提交
2754
    RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2755

D
dzzxzz@gmail.com 已提交
2756 2757 2758 2759
    /* greater than one message size */
    if (size > mq->msg_size)
        return -RT_ERROR;

2760 2761 2762 2763 2764
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();

D
dzzxzz@gmail.com 已提交
2765 2766 2767 2768 2769 2770
    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 */
2771
    msg = (struct rt_mq_message *)mq->msg_queue_free;
2772 2773
    /* for non-blocking call */
    if (msg == RT_NULL && timeout == 0)
D
dzzxzz@gmail.com 已提交
2774 2775 2776 2777 2778 2779
    {
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        return -RT_EFULL;
    }
2780 2781

    /* message queue is full */
2782
    while ((msg = (struct rt_mq_message *)mq->msg_queue_free) == RT_NULL)
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
    {
        /* 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 已提交
2797
        _ipc_list_suspend(&(mq->suspend_sender_thread),
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842
                            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 已提交
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
    /* 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 已提交
2869
    if(mq->entry < RT_MQ_ENTRY_MAX)
2870 2871 2872 2873 2874 2875 2876 2877 2878
    {
        /* increase message entry */
        mq->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
D
dzzxzz@gmail.com 已提交
2879 2880 2881 2882

    /* resume suspended thread */
    if (!rt_list_isempty(&mq->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2883
        _ipc_list_resume(&(mq->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
2897
}
2898 2899
RTM_EXPORT(rt_mq_send_wait)

C
chenjh 已提交
2900

2901
/**
C
chenjh 已提交
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
 * @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).
2917
 *
C
chenjh 已提交
2918 2919
 * @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.
2920
 *
C
chenjh 已提交
2921
 * @warning  This function can be called in interrupt context and thread context.
2922 2923 2924 2925 2926
 */
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);
}
2927
RTM_EXPORT(rt_mq_send);
2928

C
chenjh 已提交
2929

2930
/**
C
chenjh 已提交
2931 2932 2933 2934 2935 2936 2937 2938 2939
 * @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.
2940
 *
C
chenjh 已提交
2941
 * @param    buffer is the content of the message.
2942
 *
C
chenjh 已提交
2943 2944 2945 2946
 * @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.
2947
 */
2948
rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size)
2949
{
D
dzzxzz@gmail.com 已提交
2950 2951
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
2952

2953
    /* parameter check */
D
dzzxzz@gmail.com 已提交
2954
    RT_ASSERT(mq != RT_NULL);
2955
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
2956 2957
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);
2958

D
dzzxzz@gmail.com 已提交
2959 2960 2961
    /* greater than one message size */
    if (size > mq->msg_size)
        return -RT_ERROR;
2962

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

D
dzzxzz@gmail.com 已提交
2965 2966
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
2967

D
dzzxzz@gmail.com 已提交
2968 2969 2970 2971 2972 2973 2974
    /* 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);
2975

D
dzzxzz@gmail.com 已提交
2976 2977 2978 2979
        return -RT_EFULL;
    }
    /* move free list pointer */
    mq->msg_queue_free = msg->next;
2980

D
dzzxzz@gmail.com 已提交
2981 2982
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
2983

D
dzzxzz@gmail.com 已提交
2984 2985
    /* copy buffer */
    rt_memcpy(msg + 1, buffer, size);
2986

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

D
dzzxzz@gmail.com 已提交
2990
    /* link msg to the beginning of message queue */
2991
    msg->next = (struct rt_mq_message *)mq->msg_queue_head;
D
dzzxzz@gmail.com 已提交
2992
    mq->msg_queue_head = msg;
2993

D
dzzxzz@gmail.com 已提交
2994 2995 2996
    /* if there is no tail */
    if (mq->msg_queue_tail == RT_NULL)
        mq->msg_queue_tail = msg;
2997

mysterywolf's avatar
mysterywolf 已提交
2998
    if(mq->entry < RT_MQ_ENTRY_MAX)
2999 3000 3001 3002 3003 3004 3005 3006 3007
    {
        /* increase message entry */
        mq->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
mysterywolf's avatar
mysterywolf 已提交
3008

D
dzzxzz@gmail.com 已提交
3009 3010 3011
    /* resume suspended thread */
    if (!rt_list_isempty(&mq->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
3012
        _ipc_list_resume(&(mq->parent.suspend_thread));
3013

D
dzzxzz@gmail.com 已提交
3014 3015
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
3016

D
dzzxzz@gmail.com 已提交
3017
        rt_schedule();
D
dzzxzz 已提交
3018

D
dzzxzz@gmail.com 已提交
3019 3020
        return RT_EOK;
    }
3021

D
dzzxzz@gmail.com 已提交
3022 3023
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
3024

D
dzzxzz@gmail.com 已提交
3025
    return RT_EOK;
3026
}
3027
RTM_EXPORT(rt_mq_urgent);
3028

C
chenjh 已提交
3029

3030
/**
C
chenjh 已提交
3031 3032 3033 3034 3035 3036 3037 3038
 * @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.
3039
 *
C
chenjh 已提交
3040
 * @param    buffer is the content of the message.
3041
 *
C
chenjh 已提交
3042 3043 3044 3045 3046 3047
 * @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.
3048
 */
D
dzzxzz@gmail.com 已提交
3049 3050 3051 3052
rt_err_t rt_mq_recv(rt_mq_t    mq,
                    void      *buffer,
                    rt_size_t  size,
                    rt_int32_t timeout)
3053
{
D
dzzxzz@gmail.com 已提交
3054 3055 3056 3057 3058
    struct rt_thread *thread;
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
    rt_uint32_t tick_delta;

3059
    /* parameter check */
D
dzzxzz@gmail.com 已提交
3060
    RT_ASSERT(mq != RT_NULL);
3061
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
3062 3063 3064
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);

3065
    /* current context checking */
H
Henson 已提交
3066
    RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
3067

D
dzzxzz@gmail.com 已提交
3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102
    /* 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 已提交
3103
        _ipc_list_suspend(&(mq->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
                            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",
3114
                                        thread->name));
3115

D
dzzxzz@gmail.com 已提交
3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158
            /* 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 */
3159 3160 3161 3162
    if(mq->entry > 0)
    {
        mq->entry --;
    }
D
dzzxzz@gmail.com 已提交
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174

    /* 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;
3175 3176 3177 3178

    /* resume suspended thread */
    if (!rt_list_isempty(&(mq->suspend_sender_thread)))
    {
mysterywolf's avatar
mysterywolf 已提交
3179
        _ipc_list_resume(&(mq->suspend_sender_thread));
3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190

        /* 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 已提交
3191 3192 3193 3194 3195 3196
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

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

    return RT_EOK;
3197
}
B
Bernard Xiong 已提交
3198
RTM_EXPORT(rt_mq_recv);
3199

C
chenjh 已提交
3200

3201
/**
C
chenjh 已提交
3202 3203 3204
 * @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.
3205
 *
C
chenjh 已提交
3206
 * @param    mq is a pointer to a messagequeue object.
3207
 *
C
chenjh 已提交
3208 3209 3210 3211 3212 3213
 * @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.
3214
 */
B
bernard 已提交
3215
rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg)
3216
{
D
dzzxzz@gmail.com 已提交
3217 3218
    rt_ubase_t level;
    struct rt_mq_message *msg;
3219

3220
    /* parameter check */
D
dzzxzz@gmail.com 已提交
3221
    RT_ASSERT(mq != RT_NULL);
3222
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
3223

D
dzzxzz@gmail.com 已提交
3224 3225 3226 3227
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
3228

D
dzzxzz@gmail.com 已提交
3229
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
3230
        _ipc_list_resume_all(&mq->parent.suspend_thread);
3231
        /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
3232
        _ipc_list_resume_all(&(mq->suspend_sender_thread));
3233

D
dzzxzz@gmail.com 已提交
3234 3235 3236 3237 3238
        /* 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;
3239

D
dzzxzz@gmail.com 已提交
3240 3241 3242 3243 3244
            /* 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;
3245

D
dzzxzz@gmail.com 已提交
3246 3247 3248 3249
            /* put message to free list */
            msg->next = (struct rt_mq_message *)mq->msg_queue_free;
            mq->msg_queue_free = msg;
        }
3250

D
dzzxzz@gmail.com 已提交
3251 3252
        /* clean entry */
        mq->entry = 0;
3253

D
dzzxzz@gmail.com 已提交
3254 3255
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
3256

D
dzzxzz@gmail.com 已提交
3257
        rt_schedule();
3258

D
dzzxzz@gmail.com 已提交
3259 3260
        return RT_EOK;
    }
3261

D
dzzxzz@gmail.com 已提交
3262
    return -RT_ERROR;
3263
}
D
dzzxzz@gmail.com 已提交
3264
RTM_EXPORT(rt_mq_control);
C
chenjh 已提交
3265 3266

/**@}*/
3267
#endif /* RT_USING_MESSAGEQUEUE */
D
dzzxzz 已提交
3268

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