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

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

#ifdef RT_USING_HOOK
D
dzzxzz 已提交
48 49 50
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);
51
#endif /* RT_USING_HOOK */
52 53 54 55 56

/**
 * @addtogroup IPC
 */

D
dogandog 已提交
57
/**@{*/
58 59

/**
C
chenjh 已提交
60
 * @brief    This function will initialize an IPC object, such as semaphore, mutex, messagequeue and mailbox.
61
 *
C
chenjh 已提交
62
 * @note     Executing this function will complete an initialization of the suspend thread list of the ipc object.
63
 *
C
chenjh 已提交
64 65 66 67 68 69
 * @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.
70
 */
mysterywolf's avatar
mysterywolf 已提交
71
rt_inline rt_err_t _ipc_object_init(struct rt_ipc_object *ipc)
72
{
B
Bernard Xiong 已提交
73
    /* initialize ipc object */
D
dzzxzz@gmail.com 已提交
74
    rt_list_init(&(ipc->suspend_thread));
75

D
dzzxzz@gmail.com 已提交
76
    return RT_EOK;
77 78
}

C
chenjh 已提交
79

80
/**
C
chenjh 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
 * @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.
99
 *
C
chenjh 已提交
100 101
 * @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.
102
 *
C
chenjh 已提交
103 104 105 106 107
 * @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()
108
 */
mysterywolf's avatar
mysterywolf 已提交
109 110 111
rt_inline rt_err_t _ipc_list_suspend(rt_list_t        *list,
                                     struct rt_thread *thread,
                                     rt_uint8_t        flag)
112
{
D
dzzxzz@gmail.com 已提交
113 114 115 116 117 118 119
    /* suspend thread */
    rt_thread_suspend(thread);

    switch (flag)
    {
    case RT_IPC_FLAG_FIFO:
        rt_list_insert_before(list, &(thread->tlist));
120
        break; /* RT_IPC_FLAG_FIFO */
D
dzzxzz@gmail.com 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    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));
        }
148
        break;/* RT_IPC_FLAG_PRIO */
C
chenchaoqun 已提交
149 150

    default:
T
tangyuxin 已提交
151
        RT_ASSERT(0);
mysterywolf's avatar
mysterywolf 已提交
152
        break;
D
dzzxzz@gmail.com 已提交
153 154 155
    }

    return RT_EOK;
156 157
}

C
chenjh 已提交
158

159
/**
C
chenjh 已提交
160 161 162 163 164 165 166 167 168 169
 * @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.
170
 *
C
chenjh 已提交
171 172
 * @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.
173
 *
C
chenjh 已提交
174 175 176
 * @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(),
177
 */
mysterywolf's avatar
mysterywolf 已提交
178
rt_inline rt_err_t _ipc_list_resume(rt_list_t *list)
179
{
D
dzzxzz@gmail.com 已提交
180
    struct rt_thread *thread;
181

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

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

D
dzzxzz@gmail.com 已提交
187 188
    /* resume it */
    rt_thread_resume(thread);
189

D
dzzxzz@gmail.com 已提交
190
    return RT_EOK;
191 192
}

C
chenjh 已提交
193

194
/**
C
chenjh 已提交
195 196 197 198 199
 * @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.
200
 *
C
chenjh 已提交
201 202 203 204
 * @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.
205 206
 *
 */
mysterywolf's avatar
mysterywolf 已提交
207
rt_inline rt_err_t _ipc_list_resume_all(rt_list_t *list)
208
{
D
dzzxzz@gmail.com 已提交
209 210 211
    struct rt_thread *thread;
    register rt_ubase_t temp;

B
Bernard Xiong 已提交
212
    /* wakeup all suspended threads */
D
dzzxzz@gmail.com 已提交
213 214 215 216 217
    while (!rt_list_isempty(list))
    {
        /* disable interrupt */
        temp = rt_hw_interrupt_disable();

B
Bernard Xiong 已提交
218
        /* get next suspended thread */
D
dzzxzz@gmail.com 已提交
219 220 221 222 223 224 225
        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 已提交
226
         * suspended list
D
dzzxzz@gmail.com 已提交
227 228 229 230 231 232 233 234
         */
        rt_thread_resume(thread);

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

    return RT_EOK;
235 236
}

C
chenjh 已提交
237 238
/**@}*/

239 240
#ifdef RT_USING_SEMAPHORE
/**
C
chenjh 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
 * @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.
257
 *
C
chenjh 已提交
258
 * @param    name is a pointer to the name you would like to give the semaphore.
259
 *
C
chenjh 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
 * @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.
282
 */
D
dzzxzz@gmail.com 已提交
283 284 285 286
rt_err_t rt_sem_init(rt_sem_t    sem,
                     const char *name,
                     rt_uint32_t value,
                     rt_uint8_t  flag)
287
{
D
dzzxzz@gmail.com 已提交
288
    RT_ASSERT(sem != RT_NULL);
289
    RT_ASSERT(value < 0x10000U);
290

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

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

B
Bernard Xiong 已提交
297
    /* set initial value */
298
    sem->value = (rt_uint16_t)value;
299

D
dzzxzz@gmail.com 已提交
300 301
    /* set parent */
    sem->parent.parent.flag = flag;
302

D
dzzxzz@gmail.com 已提交
303
    return RT_EOK;
304
}
305
RTM_EXPORT(rt_sem_init);
306

C
chenjh 已提交
307

308
/**
C
chenjh 已提交
309 310 311 312 313
 * @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.
314
 *
C
chenjh 已提交
315
 * @see      rt_sem_delete()
316
 *
C
chenjh 已提交
317
 * @param    sem is a pointer to a semaphore object to be detached.
318
 *
C
chenjh 已提交
319 320 321 322 323 324
 * @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.
325
 */
D
dzzxzz 已提交
326
rt_err_t rt_sem_detach(rt_sem_t sem)
327
{
328
    /* parameter check */
D
dzzxzz@gmail.com 已提交
329
    RT_ASSERT(sem != RT_NULL);
330 331
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
    RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent));
332

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

D
dzzxzz@gmail.com 已提交
336 337
    /* detach semaphore object */
    rt_object_detach(&(sem->parent.parent));
338

D
dzzxzz@gmail.com 已提交
339
    return RT_EOK;
340
}
341
RTM_EXPORT(rt_sem_detach);
342 343 344

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
345 346 347 348 349 350 351 352 353 354 355 356
 * @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.
357
 *
C
chenjh 已提交
358 359 360
 * @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:
361
 *
C
chenjh 已提交
362
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
363
 *
C
chenjh 已提交
364 365 366 367 368 369 370 371 372 373 374
 *               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.
375
 */
D
dzzxzz 已提交
376
rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)
377
{
D
dzzxzz@gmail.com 已提交
378
    rt_sem_t sem;
379

D
dzzxzz@gmail.com 已提交
380
    RT_DEBUG_NOT_IN_INTERRUPT;
381
    RT_ASSERT(value < 0x10000U);
382

D
dzzxzz@gmail.com 已提交
383 384 385 386
    /* allocate object */
    sem = (rt_sem_t)rt_object_allocate(RT_Object_Class_Semaphore, name);
    if (sem == RT_NULL)
        return sem;
387

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

B
Bernard Xiong 已提交
391
    /* set initial value */
D
dzzxzz@gmail.com 已提交
392
    sem->value = value;
393

D
dzzxzz@gmail.com 已提交
394 395
    /* set parent */
    sem->parent.parent.flag = flag;
396

D
dzzxzz@gmail.com 已提交
397
    return sem;
398
}
399
RTM_EXPORT(rt_sem_create);
400

C
chenjh 已提交
401

402
/**
C
chenjh 已提交
403
 * @brief    This function will delete a semaphore object and release the memory space.
404
 *
C
chenjh 已提交
405 406 407
 * @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.
408
 *
C
chenjh 已提交
409
 * @see      rt_sem_detach()
410
 *
C
chenjh 已提交
411 412 413 414 415 416 417 418
 * @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.
419
 */
D
dzzxzz 已提交
420
rt_err_t rt_sem_delete(rt_sem_t sem)
421
{
D
dzzxzz@gmail.com 已提交
422
    RT_DEBUG_NOT_IN_INTERRUPT;
423

424
    /* parameter check */
D
dzzxzz@gmail.com 已提交
425
    RT_ASSERT(sem != RT_NULL);
426 427
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
    RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent) == RT_FALSE);
428

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

D
dzzxzz@gmail.com 已提交
432 433
    /* delete semaphore object */
    rt_object_delete(&(sem->parent.parent));
434

D
dzzxzz@gmail.com 已提交
435
    return RT_EOK;
436
}
437
RTM_EXPORT(rt_sem_delete);
438
#endif /* RT_USING_HEAP */
439

C
chenjh 已提交
440

441
/**
C
chenjh 已提交
442 443 444 445 446 447 448
 * @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.
449
 *
C
chenjh 已提交
450
 * @see      rt_sem_trytake()
451
 *
C
chenjh 已提交
452 453 454 455 456 457 458 459 460 461 462
 * @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.
463
 */
D
dzzxzz 已提交
464
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
465
{
D
dzzxzz@gmail.com 已提交
466 467
    register rt_base_t temp;
    struct rt_thread *thread;
468

469
    /* parameter check */
D
dzzxzz@gmail.com 已提交
470
    RT_ASSERT(sem != RT_NULL);
471
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
472

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

D
dzzxzz@gmail.com 已提交
475 476
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
477

D
dzzxzz@gmail.com 已提交
478
    RT_DEBUG_LOG(RT_DEBUG_IPC, ("thread %s take sem:%s, which value is: %d\n",
479 480 481
                                rt_thread_self()->name,
                                ((struct rt_object *)sem)->name,
                                sem->value));
482

D
dzzxzz@gmail.com 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    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
        {
            /* current context checking */
G
Grissiom 已提交
503
            RT_DEBUG_IN_THREAD_CONTEXT;
D
dzzxzz@gmail.com 已提交
504 505 506 507 508 509 510 511 512

            /* 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",
513
                                        thread->name));
514

D
dzzxzz@gmail.com 已提交
515
            /* suspend thread */
mysterywolf's avatar
mysterywolf 已提交
516
            _ipc_list_suspend(&(sem->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
517 518
                                thread,
                                sem->parent.parent.flag);
519

D
dzzxzz@gmail.com 已提交
520 521 522 523
            /* has waiting time, start thread timer */
            if (time > 0)
            {
                RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n",
524
                                            thread->name));
525

D
dzzxzz@gmail.com 已提交
526 527 528 529 530 531
                /* 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));
            }
532

D
dzzxzz@gmail.com 已提交
533 534
            /* enable interrupt */
            rt_hw_interrupt_enable(temp);
535

D
dzzxzz@gmail.com 已提交
536 537
            /* do schedule */
            rt_schedule();
538

D
dzzxzz@gmail.com 已提交
539 540 541 542 543 544
            if (thread->error != RT_EOK)
            {
                return thread->error;
            }
        }
    }
545

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

D
dzzxzz@gmail.com 已提交
548
    return RT_EOK;
549
}
550
RTM_EXPORT(rt_sem_take);
551

C
chenjh 已提交
552

553
/**
C
chenjh 已提交
554
 * @brief    This function will try to take a semaphore, if the semaphore is unavailable, the thread returns immediately.
555
 *
C
chenjh 已提交
556 557 558
 * @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).
559
 *
C
chenjh 已提交
560 561 562 563 564 565
 * @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.
566 567 568
 */
rt_err_t rt_sem_trytake(rt_sem_t sem)
{
mysterywolf's avatar
mysterywolf 已提交
569
    return rt_sem_take(sem, RT_WAITING_NO);
570
}
571
RTM_EXPORT(rt_sem_trytake);
572

C
chenjh 已提交
573

574
/**
C
chenjh 已提交
575 576 577 578 579
 * @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.
580
 *
C
chenjh 已提交
581
 * @param    sem is a pointer to a semaphore object.
582
 *
C
chenjh 已提交
583 584
 * @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.
585 586 587
 */
rt_err_t rt_sem_release(rt_sem_t sem)
{
D
dzzxzz@gmail.com 已提交
588 589
    register rt_base_t temp;
    register rt_bool_t need_schedule;
590

591 592 593 594
    /* parameter check */
    RT_ASSERT(sem != RT_NULL);
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);

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

D
dzzxzz@gmail.com 已提交
597
    need_schedule = RT_FALSE;
598

D
dzzxzz@gmail.com 已提交
599 600
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
601

D
dzzxzz@gmail.com 已提交
602
    RT_DEBUG_LOG(RT_DEBUG_IPC, ("thread %s releases sem:%s, which value is: %d\n",
603 604 605
                                rt_thread_self()->name,
                                ((struct rt_object *)sem)->name,
                                sem->value));
606

D
dzzxzz@gmail.com 已提交
607 608 609
    if (!rt_list_isempty(&sem->parent.suspend_thread))
    {
        /* resume the suspended thread */
mysterywolf's avatar
mysterywolf 已提交
610
        _ipc_list_resume(&(sem->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
611 612 613
        need_schedule = RT_TRUE;
    }
    else
mysterywolf's avatar
mysterywolf 已提交
614
    {
mysterywolf's avatar
mysterywolf 已提交
615
        if(sem->value < RT_SEM_VALUE_MAX)
mysterywolf's avatar
mysterywolf 已提交
616 617 618 619 620 621 622 623 624
        {
            sem->value ++; /* increase value */
        }
        else
        {
            rt_hw_interrupt_enable(temp); /* enable interrupt */
            return -RT_EFULL; /* value overflowed */
        }
    }
625

D
dzzxzz@gmail.com 已提交
626 627
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
628

D
dzzxzz@gmail.com 已提交
629 630 631
    /* resume a thread, re-schedule */
    if (need_schedule == RT_TRUE)
        rt_schedule();
632

D
dzzxzz@gmail.com 已提交
633
    return RT_EOK;
634
}
635
RTM_EXPORT(rt_sem_release);
636

C
chenjh 已提交
637

638
/**
C
chenjh 已提交
639 640 641 642 643 644 645
 * @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.
646
 *
C
chenjh 已提交
647
 * @param    arg is the argument of the function to execute the command.
648
 *
C
chenjh 已提交
649 650
 * @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.
651
 */
B
bernard 已提交
652
rt_err_t rt_sem_control(rt_sem_t sem, int cmd, void *arg)
653
{
D
dzzxzz@gmail.com 已提交
654
    rt_ubase_t level;
655 656

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

D
dzzxzz@gmail.com 已提交
660 661
    if (cmd == RT_IPC_CMD_RESET)
    {
B
Bernard Xiong 已提交
662
        rt_ubase_t value;
663

D
dzzxzz@gmail.com 已提交
664
        /* get value */
B
Bernard Xiong 已提交
665
        value = (rt_ubase_t)arg;
D
dzzxzz@gmail.com 已提交
666 667
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
668

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

D
dzzxzz@gmail.com 已提交
672 673
        /* set new value */
        sem->value = (rt_uint16_t)value;
674

D
dzzxzz@gmail.com 已提交
675 676
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
677

D
dzzxzz@gmail.com 已提交
678
        rt_schedule();
679

D
dzzxzz@gmail.com 已提交
680 681
        return RT_EOK;
    }
682

D
dzzxzz@gmail.com 已提交
683
    return -RT_ERROR;
684
}
685
RTM_EXPORT(rt_sem_control);
C
chenjh 已提交
686 687

/**@}*/
688
#endif /* RT_USING_SEMAPHORE */
689 690 691

#ifdef RT_USING_MUTEX
/**
C
chenjh 已提交
692 693 694 695 696 697 698 699 700 701 702 703
 * @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.
704
 *
C
chenjh 已提交
705
 * @see      rt_mutex_create()
706
 *
C
chenjh 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720
 * @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.
 *           NOTE: The mutex flag can ONLY be RT_IPC_FLAG_PRIO. Using RT_IPC_FLAG_FIFO will seriously affect
 *           the real-time performance of the system.
 *
 * @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.
721
 */
D
dzzxzz 已提交
722
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
723
{
724 725
    (void)flag;

726
    /* parameter check */
D
dzzxzz@gmail.com 已提交
727
    RT_ASSERT(mutex != RT_NULL);
728

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

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

D
dzzxzz@gmail.com 已提交
735 736 737 738
    mutex->value = 1;
    mutex->owner = RT_NULL;
    mutex->original_priority = 0xFF;
    mutex->hold  = 0;
739

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

D
dzzxzz@gmail.com 已提交
743
    return RT_EOK;
744
}
745
RTM_EXPORT(rt_mutex_init);
746

C
chenjh 已提交
747

748
/**
C
chenjh 已提交
749
 * @brief    This function will detach a static mutex object.
750
 *
C
chenjh 已提交
751 752 753
 * @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.
754
 *
C
chenjh 已提交
755
 * @see      rt_mutex_delete()
756
 *
C
chenjh 已提交
757 758 759 760 761 762 763 764
 * @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.
765
 */
D
dzzxzz 已提交
766
rt_err_t rt_mutex_detach(rt_mutex_t mutex)
767
{
768
    /* parameter check */
D
dzzxzz@gmail.com 已提交
769
    RT_ASSERT(mutex != RT_NULL);
770 771
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
    RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent));
772

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

776
    /* detach mutex object */
D
dzzxzz@gmail.com 已提交
777
    rt_object_detach(&(mutex->parent.parent));
778

D
dzzxzz@gmail.com 已提交
779
    return RT_EOK;
780
}
781
RTM_EXPORT(rt_mutex_detach);
782 783 784

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
785 786 787 788 789 790
 * @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()
791
 *
C
chenjh 已提交
792
 * @param    name is a pointer to the name that given to the mutex.
793
 *
C
chenjh 已提交
794 795 796 797
 * @param    flag is the mutex flag, which determines the queuing way of how multiple threads wait
 *           when the mutex is not available.
 *           NOTE: The mutex flag can ONLY be RT_IPC_FLAG_PRIO. Using RT_IPC_FLAG_FIFO will seriously affect
 *           the real-time performance of the system.
798
 *
C
chenjh 已提交
799 800 801
 * @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.
802
 */
D
dzzxzz 已提交
803
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
804
{
D
dzzxzz@gmail.com 已提交
805
    struct rt_mutex *mutex;
806
    (void)flag;
807

D
dzzxzz@gmail.com 已提交
808
    RT_DEBUG_NOT_IN_INTERRUPT;
809

D
dzzxzz@gmail.com 已提交
810 811 812 813
    /* allocate object */
    mutex = (rt_mutex_t)rt_object_allocate(RT_Object_Class_Mutex, name);
    if (mutex == RT_NULL)
        return mutex;
814

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

D
dzzxzz@gmail.com 已提交
818 819 820 821
    mutex->value              = 1;
    mutex->owner              = RT_NULL;
    mutex->original_priority  = 0xFF;
    mutex->hold               = 0;
822

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

D
dzzxzz@gmail.com 已提交
826
    return mutex;
827
}
828
RTM_EXPORT(rt_mutex_create);
829

C
chenjh 已提交
830

831
/**
C
chenjh 已提交
832 833 834 835 836 837 838
 * @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()
839
 *
C
chenjh 已提交
840
 * @param    mutex is a pointer to a mutex object to be deleted.
841
 *
C
chenjh 已提交
842 843
 * @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.
844
 *
C
chenjh 已提交
845 846 847
 * @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.
848
 */
D
dzzxzz 已提交
849
rt_err_t rt_mutex_delete(rt_mutex_t mutex)
850
{
D
dzzxzz@gmail.com 已提交
851
    RT_DEBUG_NOT_IN_INTERRUPT;
852

853
    /* parameter check */
D
dzzxzz@gmail.com 已提交
854
    RT_ASSERT(mutex != RT_NULL);
855 856
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
    RT_ASSERT(rt_object_is_systemobject(&mutex->parent.parent) == RT_FALSE);
857

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

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

D
dzzxzz@gmail.com 已提交
864
    return RT_EOK;
865
}
866
RTM_EXPORT(rt_mutex_delete);
867
#endif /* RT_USING_HEAP */
868

C
chenjh 已提交
869

870
/**
C
chenjh 已提交
871 872 873 874 875 876 877 878 879 880 881
 * @brief    This function will take a mutex, if the mutex is unavailable, the thread shall wait for
 *           the mutex 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 mutex is unavailable. At this time, it will suspend the
 *           thread preparing to take the mutex.
 *           On the contrary, the rt_sem_release() function will increase the count value of sem->value by 1 each time.
 *
 * @see      rt_mutex_trytake()
 *
 * @param    mutex is a pointer to a mutex object.
882
 *
C
chenjh 已提交
883 884 885 886
 * @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.
887
 *
C
chenjh 已提交
888 889 890 891
 * @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.
892
 */
D
dzzxzz 已提交
893
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
894
{
D
dzzxzz@gmail.com 已提交
895 896
    register rt_base_t temp;
    struct rt_thread *thread;
897

B
bernard 已提交
898 899 900
    /* this function must not be used in interrupt even if time = 0 */
    RT_DEBUG_IN_THREAD_CONTEXT;

901
    /* parameter check */
902
    RT_ASSERT(mutex != RT_NULL);
903
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
904 905 906

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

D
dzzxzz@gmail.com 已提交
908 909
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
910

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

D
dzzxzz@gmail.com 已提交
913
    RT_DEBUG_LOG(RT_DEBUG_IPC,
914 915
                 ("mutex_take: current thread %s, mutex value: %d, hold: %d\n",
                  thread->name, mutex->value, mutex->hold));
916

D
dzzxzz@gmail.com 已提交
917 918 919 920 921
    /* reset thread error */
    thread->error = RT_EOK;

    if (mutex->owner == thread)
    {
mysterywolf's avatar
mysterywolf 已提交
922
        if(mutex->hold < RT_MUTEX_HOLD_MAX)
923 924 925 926 927 928 929 930 931
        {
            /* it's the same thread */
            mutex->hold ++;
        }
        else
        {
            rt_hw_interrupt_enable(temp); /* enable interrupt */
            return -RT_EFULL; /* value overflowed */
        }
D
dzzxzz@gmail.com 已提交
932 933 934
    }
    else
    {
935
#ifdef RT_USING_SIGNALS
B
bernard 已提交
936
__again:
937
#endif /* RT_USING_SIGNALS */
D
dzzxzz@gmail.com 已提交
938 939 940 941 942 943 944 945 946 947 948
        /* 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 已提交
949
            if(mutex->hold < RT_MUTEX_HOLD_MAX)
950 951 952 953 954 955 956 957
            {
                mutex->hold ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
D
dzzxzz@gmail.com 已提交
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
        }
        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",
976
                                            thread->name));
977

D
dzzxzz@gmail.com 已提交
978 979 980 981 982 983 984 985 986 987
                /* 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 已提交
988
                _ipc_list_suspend(&(mutex->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
989 990 991 992 993 994 995
                                    thread,
                                    mutex->parent.parent.flag);

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

D
dzzxzz@gmail.com 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
                    /* 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)
                {
1014
#ifdef RT_USING_SIGNALS
1015 1016
                    /* interrupt by signal, try it again */
                    if (thread->error == -RT_EINTR) goto __again;
1017
#endif /* RT_USING_SIGNALS */
B
bernard 已提交
1018

D
dzzxzz@gmail.com 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
                    /* 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;
1038
}
1039
RTM_EXPORT(rt_mutex_take);
1040

C
chenjh 已提交
1041

mysterywolf's avatar
mysterywolf 已提交
1042
/**
C
chenjh 已提交
1043 1044 1045 1046 1047 1048 1049 1050
 * @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 已提交
1051
 *
C
chenjh 已提交
1052
 * @param    mutex is a pointer to a mutex object.
mysterywolf's avatar
mysterywolf 已提交
1053
 *
C
chenjh 已提交
1054 1055
 * @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 已提交
1056 1057 1058 1059 1060 1061 1062
 */
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 已提交
1063

1064
/**
C
chenjh 已提交
1065 1066 1067 1068 1069
 * @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.
1070
 *
C
chenjh 已提交
1071
 * @param    mutex is a pointer to a mutex object.
1072
 *
C
chenjh 已提交
1073 1074
 * @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.
1075 1076 1077
 */
rt_err_t rt_mutex_release(rt_mutex_t mutex)
{
D
dzzxzz@gmail.com 已提交
1078 1079 1080
    register rt_base_t temp;
    struct rt_thread *thread;
    rt_bool_t need_schedule;
1081

1082 1083 1084 1085
    /* parameter check */
    RT_ASSERT(mutex != RT_NULL);
    RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);

D
dzzxzz@gmail.com 已提交
1086
    need_schedule = RT_FALSE;
1087

1088 1089
    /* only thread could release mutex because we need test the ownership */
    RT_DEBUG_IN_THREAD_CONTEXT;
1090

B
bernard 已提交
1091 1092 1093
    /* get current thread */
    thread = rt_thread_self();

D
dzzxzz@gmail.com 已提交
1094 1095
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
1096

D
dzzxzz@gmail.com 已提交
1097
    RT_DEBUG_LOG(RT_DEBUG_IPC,
1098 1099
                 ("mutex_release:current thread %s, mutex value: %d, hold: %d\n",
                  thread->name, mutex->value, mutex->hold));
1100

D
dzzxzz@gmail.com 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 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
    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",
1136
                                        thread->name));
1137

D
dzzxzz@gmail.com 已提交
1138 1139 1140
            /* set new owner and priority */
            mutex->owner             = thread;
            mutex->original_priority = thread->current_priority;
St0pH@CK's avatar
St0pH@CK 已提交
1141

mysterywolf's avatar
mysterywolf 已提交
1142
            if(mutex->hold < RT_MUTEX_HOLD_MAX)
1143 1144 1145 1146 1147 1148 1149 1150
            {
                mutex->hold ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
1151

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

D
dzzxzz@gmail.com 已提交
1155 1156 1157 1158
            need_schedule = RT_TRUE;
        }
        else
        {
mysterywolf's avatar
mysterywolf 已提交
1159
            if(mutex->value < RT_MUTEX_VALUE_MAX)
1160 1161 1162 1163 1164 1165 1166 1167 1168
            {
                /* increase value */
                mutex->value ++;
            }
            else
            {
                rt_hw_interrupt_enable(temp); /* enable interrupt */
                return -RT_EFULL; /* value overflowed */
            }
mysterywolf's avatar
mysterywolf 已提交
1169

D
dzzxzz@gmail.com 已提交
1170 1171 1172 1173 1174
            /* clear owner */
            mutex->owner             = RT_NULL;
            mutex->original_priority = 0xff;
        }
    }
1175

D
dzzxzz@gmail.com 已提交
1176 1177
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
1178

D
dzzxzz@gmail.com 已提交
1179 1180 1181
    /* perform a schedule */
    if (need_schedule == RT_TRUE)
        rt_schedule();
B
bernard.xiong 已提交
1182

D
dzzxzz@gmail.com 已提交
1183
    return RT_EOK;
1184
}
1185
RTM_EXPORT(rt_mutex_release);
1186

C
chenjh 已提交
1187

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

D
dzzxzz@gmail.com 已提交
1208
    return -RT_ERROR;
1209
}
1210
RTM_EXPORT(rt_mutex_control);
C
chenjh 已提交
1211 1212

/**@}*/
1213
#endif /* RT_USING_MUTEX */
1214 1215 1216

#ifdef RT_USING_EVENT
/**
C
chenjh 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
 * @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    value is the initial value for the event.
 *           If want to share resources, you should initialize the value as the number of available resources.
 *           If want to signal the occurrence of an event, you should initialize the value as 0.
 *
 * @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.
1246
 *
C
chenjh 已提交
1247 1248
 *               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).
1249
 *
C
chenjh 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258
 *               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.
1259
 */
D
dzzxzz 已提交
1260
rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
1261
{
1262
    /* parameter check */
D
dzzxzz@gmail.com 已提交
1263
    RT_ASSERT(event != RT_NULL);
1264

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

D
dzzxzz@gmail.com 已提交
1268 1269
    /* set parent flag */
    event->parent.parent.flag = flag;
1270

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

B
Bernard Xiong 已提交
1274
    /* initialize event */
D
dzzxzz@gmail.com 已提交
1275
    event->set = 0;
1276

D
dzzxzz@gmail.com 已提交
1277
    return RT_EOK;
1278
}
1279
RTM_EXPORT(rt_event_init);
1280

C
chenjh 已提交
1281

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

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

D
dzzxzz@gmail.com 已提交
1310 1311
    /* detach event object */
    rt_object_detach(&(event->parent.parent));
1312

D
dzzxzz@gmail.com 已提交
1313
    return RT_EOK;
1314
}
1315
RTM_EXPORT(rt_event_detach);
1316 1317 1318

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

D
dzzxzz@gmail.com 已提交
1350
    RT_DEBUG_NOT_IN_INTERRUPT;
1351

D
dzzxzz@gmail.com 已提交
1352 1353 1354 1355
    /* allocate object */
    event = (rt_event_t)rt_object_allocate(RT_Object_Class_Event, name);
    if (event == RT_NULL)
        return event;
1356

D
dzzxzz@gmail.com 已提交
1357 1358
    /* set parent */
    event->parent.parent.flag = flag;
1359

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

B
Bernard Xiong 已提交
1363
    /* initialize event */
D
dzzxzz@gmail.com 已提交
1364
    event->set = 0;
1365

D
dzzxzz@gmail.com 已提交
1366
    return event;
1367
}
1368
RTM_EXPORT(rt_event_create);
1369

C
chenjh 已提交
1370

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

D
dzzxzz@gmail.com 已提交
1396
    RT_DEBUG_NOT_IN_INTERRUPT;
1397

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

D
dzzxzz@gmail.com 已提交
1401 1402
    /* delete event object */
    rt_object_delete(&(event->parent.parent));
1403

D
dzzxzz@gmail.com 已提交
1404
    return RT_EOK;
1405
}
1406
RTM_EXPORT(rt_event_delete);
1407
#endif /* RT_USING_HEAP */
1408

C
chenjh 已提交
1409

1410
/**
C
chenjh 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419
 * @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.
1420
 *
C
chenjh 已提交
1421 1422
 * @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.
1423
 *
C
chenjh 已提交
1424 1425
 * @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.
1426 1427 1428
 */
rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
{
D
dzzxzz@gmail.com 已提交
1429 1430 1431 1432 1433 1434 1435 1436
    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);
1437 1438
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);

D
dzzxzz@gmail.com 已提交
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
    if (set == 0)
        return -RT_ERROR;

    need_schedule = RT_FALSE;

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

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

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

D
dzzxzz@gmail.com 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
    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 已提交
1474
                    /* save the received event set */
D
dzzxzz@gmail.com 已提交
1475 1476 1477 1478 1479 1480
                    thread->event_set = thread->event_set & event->set;

                    /* received an OR event */
                    status = RT_EOK;
                }
            }
1481 1482 1483 1484 1485 1486 1487
            else
            {
                /* enable interrupt */
                rt_hw_interrupt_enable(level);

                return -RT_EINVAL;
            }
D
dzzxzz@gmail.com 已提交
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

            /* 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;
1516
}
1517
RTM_EXPORT(rt_event_send);
1518

C
chenjh 已提交
1519

1520
/**
C
chenjh 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
 * @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.
1532
 *
C
chenjh 已提交
1533 1534 1535
 * @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):
1536
 *
C
chenjh 已提交
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
 *
 *               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.
1551
 */
D
dzzxzz@gmail.com 已提交
1552 1553 1554 1555 1556
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)
1557
{
D
dzzxzz@gmail.com 已提交
1558 1559 1560 1561
    struct rt_thread *thread;
    register rt_ubase_t level;
    register rt_base_t status;

G
Grissiom 已提交
1562
    RT_DEBUG_IN_THREAD_CONTEXT;
D
dzzxzz@gmail.com 已提交
1563 1564 1565

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

D
dzzxzz@gmail.com 已提交
1568 1569 1570
    if (set == 0)
        return -RT_ERROR;

B
Bernard Xiong 已提交
1571
    /* initialize status */
D
dzzxzz@gmail.com 已提交
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
    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;
    }
1594 1595 1596 1597 1598
    else
    {
        /* either RT_EVENT_FLAG_AND or RT_EVENT_FLAG_OR should be set */
        RT_ASSERT(0);
    }
D
dzzxzz@gmail.com 已提交
1599 1600 1601 1602

    if (status == RT_EOK)
    {
        /* set received event */
1603
        if (recved)
P
pathletboy 已提交
1604
            *recved = (event->set & set);
mysterywolf's avatar
mysterywolf 已提交
1605 1606

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

D
dzzxzz@gmail.com 已提交
1610 1611 1612 1613 1614 1615 1616 1617
        /* received event */
        if (option & RT_EVENT_FLAG_CLEAR)
            event->set &= ~set;
    }
    else if (timeout == 0)
    {
        /* no waiting */
        thread->error = -RT_ETIMEOUT;
1618

G
greed-island 已提交
1619 1620
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
1621

G
greed-island 已提交
1622
        return -RT_ETIMEOUT;
D
dzzxzz@gmail.com 已提交
1623 1624 1625 1626 1627 1628 1629 1630
    }
    else
    {
        /* fill thread event info */
        thread->event_set  = set;
        thread->event_info = option;

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

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

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

    return thread->error;
1671
}
1672
RTM_EXPORT(rt_event_recv);
1673

C
chenjh 已提交
1674

1675
/**
C
chenjh 已提交
1676 1677 1678 1679 1680
 * @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.
1681
 *
C
chenjh 已提交
1682
 * @param    cmd is a command word used to configure some attributions of the event.
1683
 *
C
chenjh 已提交
1684 1685 1686 1687
 * @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.
1688
 */
B
bernard 已提交
1689
rt_err_t rt_event_control(rt_event_t event, int cmd, void *arg)
1690
{
D
dzzxzz@gmail.com 已提交
1691
    rt_ubase_t level;
1692

1693 1694 1695
    /* parameter check */
    RT_ASSERT(event != RT_NULL);
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
B
Bernard Xiong 已提交
1696

D
dzzxzz@gmail.com 已提交
1697 1698 1699 1700
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
1701

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

B
Bernard Xiong 已提交
1705
        /* initialize event set */
D
dzzxzz@gmail.com 已提交
1706
        event->set = 0;
1707

D
dzzxzz@gmail.com 已提交
1708 1709
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
1710

D
dzzxzz@gmail.com 已提交
1711
        rt_schedule();
1712

D
dzzxzz@gmail.com 已提交
1713 1714
        return RT_EOK;
    }
1715

D
dzzxzz@gmail.com 已提交
1716
    return -RT_ERROR;
1717
}
B
Bernard Xiong 已提交
1718
RTM_EXPORT(rt_event_control);
C
chenjh 已提交
1719 1720

/**@}*/
1721
#endif /* RT_USING_EVENT */
1722 1723 1724

#ifdef RT_USING_MAILBOX
/**
C
chenjh 已提交
1725 1726 1727 1728 1729 1730 1731
 * @addtogroup mailbox
 */

/**@{*/

/**
 * @brief    Initialize a static mailbox object.
1732
 *
C
chenjh 已提交
1733 1734 1735
 * @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.
1736
 *
C
chenjh 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
 * @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    msgpoll is a pointer to the starting address of the memory space you allocated for the mailbox in advance.
 *           In other words, msgpoll is a pointer to the mailbox buffer of the starting address.
 *
 * @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.
1768
 */
D
dzzxzz@gmail.com 已提交
1769 1770 1771 1772 1773
rt_err_t rt_mb_init(rt_mailbox_t mb,
                    const char  *name,
                    void        *msgpool,
                    rt_size_t    size,
                    rt_uint8_t   flag)
1774
{
D
dzzxzz@gmail.com 已提交
1775
    RT_ASSERT(mb != RT_NULL);
1776

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

D
dzzxzz@gmail.com 已提交
1780 1781
    /* set parent flag */
    mb->parent.parent.flag = flag;
1782

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

B
Bernard Xiong 已提交
1786
    /* initialize mailbox */
1787
    mb->msg_pool   = (rt_ubase_t *)msgpool;
D
dzzxzz@gmail.com 已提交
1788 1789 1790 1791
    mb->size       = size;
    mb->entry      = 0;
    mb->in_offset  = 0;
    mb->out_offset = 0;
1792

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

D
dzzxzz@gmail.com 已提交
1796
    return RT_EOK;
1797
}
1798
RTM_EXPORT(rt_mb_init);
1799

C
chenjh 已提交
1800

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

D
dzzxzz@gmail.com 已提交
1826
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1827
    _ipc_list_resume_all(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
1828
    /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1829
    _ipc_list_resume_all(&(mb->suspend_sender_thread));
1830

D
dzzxzz@gmail.com 已提交
1831 1832
    /* detach mailbox object */
    rt_object_detach(&(mb->parent.parent));
1833

D
dzzxzz@gmail.com 已提交
1834
    return RT_EOK;
1835
}
1836
RTM_EXPORT(rt_mb_detach);
1837 1838 1839

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

D
dzzxzz@gmail.com 已提交
1874
    RT_DEBUG_NOT_IN_INTERRUPT;
1875

D
dzzxzz@gmail.com 已提交
1876 1877 1878 1879
    /* allocate object */
    mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
    if (mb == RT_NULL)
        return mb;
1880

D
dzzxzz@gmail.com 已提交
1881 1882
    /* set parent */
    mb->parent.parent.flag = flag;
1883

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

B
Bernard Xiong 已提交
1887
    /* initialize mailbox */
D
dzzxzz@gmail.com 已提交
1888
    mb->size     = size;
1889
    mb->msg_pool = (rt_ubase_t *)RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t));
D
dzzxzz@gmail.com 已提交
1890 1891 1892 1893
    if (mb->msg_pool == RT_NULL)
    {
        /* delete mailbox object */
        rt_object_delete(&(mb->parent.parent));
1894

D
dzzxzz@gmail.com 已提交
1895 1896 1897 1898 1899
        return RT_NULL;
    }
    mb->entry      = 0;
    mb->in_offset  = 0;
    mb->out_offset = 0;
1900

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

D
dzzxzz@gmail.com 已提交
1904
    return mb;
1905
}
1906
RTM_EXPORT(rt_mb_create);
1907

C
chenjh 已提交
1908

1909
/**
C
chenjh 已提交
1910 1911 1912 1913 1914
 * @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.
1915
 *
C
chenjh 已提交
1916
 * @see      rt_mb_detach()
1917
 *
C
chenjh 已提交
1918 1919 1920 1921 1922 1923 1924 1925
 * @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.
1926
 */
D
dzzxzz 已提交
1927
rt_err_t rt_mb_delete(rt_mailbox_t mb)
M
mbbill@gmail.com 已提交
1928
{
D
dzzxzz@gmail.com 已提交
1929
    RT_DEBUG_NOT_IN_INTERRUPT;
1930

D
dzzxzz@gmail.com 已提交
1931 1932
    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
1933 1934
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
    RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent) == RT_FALSE);
1935

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

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

1942 1943
    /* free mailbox pool */
    RT_KERNEL_FREE(mb->msg_pool);
1944

D
dzzxzz@gmail.com 已提交
1945 1946
    /* delete mailbox object */
    rt_object_delete(&(mb->parent.parent));
1947

D
dzzxzz@gmail.com 已提交
1948
    return RT_EOK;
1949
}
1950
RTM_EXPORT(rt_mb_delete);
1951
#endif /* RT_USING_HEAP */
1952

C
chenjh 已提交
1953

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

    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
1987
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
D
dzzxzz@gmail.com 已提交
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020

    /* 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;
        }

G
Grissiom 已提交
2021
        RT_DEBUG_IN_THREAD_CONTEXT;
D
dzzxzz@gmail.com 已提交
2022
        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2023
        _ipc_list_suspend(&(mb->suspend_sender_thread),
D
dzzxzz@gmail.com 已提交
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
                            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",
2034
                                        thread->name));
2035

D
dzzxzz@gmail.com 已提交
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 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
            /* 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 已提交
2075

mysterywolf's avatar
mysterywolf 已提交
2076
    if(mb->entry < RT_MB_ENTRY_MAX)
2077 2078 2079 2080 2081 2082 2083 2084 2085
    {
        /* increase message entry */
        mb->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
mysterywolf's avatar
mysterywolf 已提交
2086

D
dzzxzz@gmail.com 已提交
2087 2088 2089
    /* resume suspended thread */
    if (!rt_list_isempty(&mb->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2090
        _ipc_list_resume(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
2104
}
2105
RTM_EXPORT(rt_mb_send_wait);
2106

C
chenjh 已提交
2107

2108
/**
C
chenjh 已提交
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
 * @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.
2119
 *
C
chenjh 已提交
2120
 * @param    value is a value to the content of the mail you want to send.
2121
 *
C
chenjh 已提交
2122 2123
 * @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.
2124
 */
B
Bernard Xiong 已提交
2125
rt_err_t rt_mb_send(rt_mailbox_t mb, rt_ubase_t value)
2126
{
D
dzzxzz@gmail.com 已提交
2127
    return rt_mb_send_wait(mb, value, 0);
2128
}
2129
RTM_EXPORT(rt_mb_send);
2130

C
chenjh 已提交
2131

2132
/**
C
chenjh 已提交
2133 2134 2135 2136 2137 2138 2139 2140 2141
 * @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.
2142
 *
C
chenjh 已提交
2143
 * @param    value is the content of the mail you want to send.
2144
 *
C
chenjh 已提交
2145 2146
 * @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.
2147
 */
mysterywolf's avatar
update  
mysterywolf 已提交
2148
rt_err_t rt_mb_urgent(rt_mailbox_t mb, rt_ubase_t value)
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
{
    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 已提交
2161 2162 2163 2164 2165 2166
    if (mb->entry == mb->size)
    {
        rt_hw_interrupt_enable(temp);
        return -RT_EFULL;
    }

2167
    /* rewind to the previous position */
mysterywolf's avatar
update  
mysterywolf 已提交
2168
    if (mb->out_offset > 0)
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
    {
        mb->out_offset --;
    }
    else
    {
        mb->out_offset = mb->size - 1;
    }

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

mysterywolf's avatar
update  
mysterywolf 已提交
2180 2181
    /* increase message entry */
    mb->entry ++;
2182 2183 2184 2185

    /* resume suspended thread */
    if (!rt_list_isempty(&mb->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2186
        _ipc_list_resume(&(mb->parent.suspend_thread));
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202

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

2204
/**
C
chenjh 已提交
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
 * @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.
2216
 *
C
chenjh 已提交
2217
 * @param    timeout is a timeout period (unit: an OS tick).
2218
 *
C
chenjh 已提交
2219 2220
 * @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.
2221
 */
B
Bernard Xiong 已提交
2222
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
2223
{
D
dzzxzz@gmail.com 已提交
2224 2225 2226 2227 2228 2229
    struct rt_thread *thread;
    register rt_ubase_t temp;
    rt_uint32_t tick_delta;

    /* parameter check */
    RT_ASSERT(mb != RT_NULL);
2230
    RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
D
dzzxzz@gmail.com 已提交
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262

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

D
dzzxzz@gmail.com 已提交
2264 2265 2266
            return -RT_ETIMEOUT;
        }

G
Grissiom 已提交
2267
        RT_DEBUG_IN_THREAD_CONTEXT;
D
dzzxzz@gmail.com 已提交
2268
        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2269
        _ipc_list_suspend(&(mb->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
                            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",
2280
                                        thread->name));
2281

D
dzzxzz@gmail.com 已提交
2282 2283 2284 2285 2286 2287
            /* 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));
        }
2288

D
dzzxzz@gmail.com 已提交
2289 2290
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2291

D
dzzxzz@gmail.com 已提交
2292 2293
        /* re-schedule */
        rt_schedule();
2294

D
dzzxzz@gmail.com 已提交
2295 2296 2297 2298 2299 2300
        /* resume from suspend state */
        if (thread->error != RT_EOK)
        {
            /* return error */
            return thread->error;
        }
2301

D
dzzxzz@gmail.com 已提交
2302 2303
        /* disable interrupt */
        temp = rt_hw_interrupt_disable();
2304

D
dzzxzz@gmail.com 已提交
2305 2306 2307 2308 2309 2310 2311 2312 2313
        /* 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;
        }
    }
2314

D
dzzxzz@gmail.com 已提交
2315 2316
    /* fill ptr */
    *value = mb->msg_pool[mb->out_offset];
2317

D
dzzxzz@gmail.com 已提交
2318 2319 2320 2321
    /* increase output offset */
    ++ mb->out_offset;
    if (mb->out_offset >= mb->size)
        mb->out_offset = 0;
2322

D
dzzxzz@gmail.com 已提交
2323
    /* decrease message entry */
2324 2325 2326 2327
    if(mb->entry > 0)
    {
        mb->entry --;
    }
2328

D
dzzxzz@gmail.com 已提交
2329 2330 2331
    /* resume suspended thread */
    if (!rt_list_isempty(&(mb->suspend_sender_thread)))
    {
mysterywolf's avatar
mysterywolf 已提交
2332
        _ipc_list_resume(&(mb->suspend_sender_thread));
2333

D
dzzxzz@gmail.com 已提交
2334 2335
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2336

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

D
dzzxzz@gmail.com 已提交
2339
        rt_schedule();
2340

D
dzzxzz@gmail.com 已提交
2341 2342
        return RT_EOK;
    }
2343

D
dzzxzz@gmail.com 已提交
2344 2345
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
2346

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

D
dzzxzz@gmail.com 已提交
2349
    return RT_EOK;
2350
}
2351
RTM_EXPORT(rt_mb_recv);
2352

C
chenjh 已提交
2353

2354
/**
C
chenjh 已提交
2355 2356 2357 2358 2359 2360 2361
 * @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.
2362
 *
C
chenjh 已提交
2363
 * @param    arg is the argument of the function to execute the command.
2364
 *
C
chenjh 已提交
2365 2366
 * @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.
2367
 */
B
bernard 已提交
2368
rt_err_t rt_mb_control(rt_mailbox_t mb, int cmd, void *arg)
2369
{
D
dzzxzz@gmail.com 已提交
2370
    rt_ubase_t level;
2371 2372

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

D
dzzxzz@gmail.com 已提交
2376 2377 2378 2379
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
2380

D
dzzxzz@gmail.com 已提交
2381
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
2382
        _ipc_list_resume_all(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2383
        /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2384
        _ipc_list_resume_all(&(mb->suspend_sender_thread));
2385

D
dzzxzz@gmail.com 已提交
2386 2387 2388 2389
        /* re-init mailbox */
        mb->entry      = 0;
        mb->in_offset  = 0;
        mb->out_offset = 0;
2390

D
dzzxzz@gmail.com 已提交
2391 2392
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
2393

D
dzzxzz@gmail.com 已提交
2394
        rt_schedule();
2395

D
dzzxzz@gmail.com 已提交
2396 2397
        return RT_EOK;
    }
2398

D
dzzxzz@gmail.com 已提交
2399
    return -RT_ERROR;
2400
}
B
Bernard Xiong 已提交
2401
RTM_EXPORT(rt_mb_control);
C
chenjh 已提交
2402 2403

/**@}*/
2404
#endif /* RT_USING_MAILBOX */
2405 2406

#ifdef RT_USING_MESSAGEQUEUE
C
chenjh 已提交
2407 2408 2409 2410 2411 2412
/**
 * @addtogroup messagequeue
 */

/**@{*/

2413 2414
struct rt_mq_message
{
D
dzzxzz@gmail.com 已提交
2415
    struct rt_mq_message *next;
2416 2417
};

C
chenjh 已提交
2418

2419
/**
C
chenjh 已提交
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
 * @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.
2447
 *
C
chenjh 已提交
2448 2449
 *               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).
2450
 *
C
chenjh 已提交
2451 2452 2453 2454 2455 2456 2457 2458 2459
 *               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.
2460
 */
D
dzzxzz@gmail.com 已提交
2461 2462 2463 2464 2465 2466
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)
2467
{
D
dzzxzz@gmail.com 已提交
2468 2469
    struct rt_mq_message *head;
    register rt_base_t temp;
2470

D
dzzxzz@gmail.com 已提交
2471 2472
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2473

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

D
dzzxzz@gmail.com 已提交
2477 2478
    /* set parent flag */
    mq->parent.parent.flag = flag;
2479

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

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

D
dzzxzz@gmail.com 已提交
2486 2487 2488
    /* 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));
2489

B
Bernard Xiong 已提交
2490
    /* initialize message list */
D
dzzxzz@gmail.com 已提交
2491 2492
    mq->msg_queue_head = RT_NULL;
    mq->msg_queue_tail = RT_NULL;
2493

B
Bernard Xiong 已提交
2494
    /* initialize message empty list */
D
dzzxzz@gmail.com 已提交
2495 2496 2497 2498
    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 +
2499
                                        temp * (mq->msg_size + sizeof(struct rt_mq_message)));
2500
        head->next = (struct rt_mq_message *)mq->msg_queue_free;
D
dzzxzz@gmail.com 已提交
2501 2502
        mq->msg_queue_free = head;
    }
2503

D
dzzxzz@gmail.com 已提交
2504 2505
    /* the initial entry is zero */
    mq->entry = 0;
2506

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

D
dzzxzz@gmail.com 已提交
2510
    return RT_EOK;
2511
}
2512
RTM_EXPORT(rt_mq_init);
2513

C
chenjh 已提交
2514

2515
/**
C
chenjh 已提交
2516 2517 2518 2519 2520
 * @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.
2521
 *
C
chenjh 已提交
2522
 * @see      rt_mq_delete()
2523
 *
C
chenjh 已提交
2524 2525 2526 2527 2528 2529 2530 2531
 * @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.
2532 2533 2534
 */
rt_err_t rt_mq_detach(rt_mq_t mq)
{
D
dzzxzz@gmail.com 已提交
2535 2536
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2537 2538
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
    RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent));
2539

D
dzzxzz@gmail.com 已提交
2540
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2541
    _ipc_list_resume_all(&mq->parent.suspend_thread);
2542
    /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2543
    _ipc_list_resume_all(&(mq->suspend_sender_thread));
2544

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

D
dzzxzz@gmail.com 已提交
2548
    return RT_EOK;
2549
}
2550
RTM_EXPORT(rt_mq_detach);
2551 2552 2553

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

    RT_DEBUG_NOT_IN_INTERRUPT;

    /* 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 已提交
2604
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
2605
    _ipc_object_init(&(mq->parent));
D
dzzxzz@gmail.com 已提交
2606

B
Bernard Xiong 已提交
2607
    /* initialize message queue */
D
dzzxzz@gmail.com 已提交
2608 2609 2610 2611 2612 2613

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

    /* allocate message pool */
2614
    mq->msg_pool = RT_KERNEL_MALLOC((mq->msg_size + sizeof(struct rt_mq_message)) * mq->max_msgs);
D
dzzxzz@gmail.com 已提交
2615 2616
    if (mq->msg_pool == RT_NULL)
    {
2617
        rt_object_delete(&(mq->parent.parent));
D
dzzxzz@gmail.com 已提交
2618 2619 2620 2621

        return RT_NULL;
    }

B
Bernard Xiong 已提交
2622
    /* initialize message list */
D
dzzxzz@gmail.com 已提交
2623 2624 2625
    mq->msg_queue_head = RT_NULL;
    mq->msg_queue_tail = RT_NULL;

B
Bernard Xiong 已提交
2626
    /* initialize message empty list */
D
dzzxzz@gmail.com 已提交
2627 2628 2629 2630
    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 +
2631
                                        temp * (mq->msg_size + sizeof(struct rt_mq_message)));
2632
        head->next = (struct rt_mq_message *)mq->msg_queue_free;
D
dzzxzz@gmail.com 已提交
2633 2634 2635 2636 2637 2638
        mq->msg_queue_free = head;
    }

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

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

D
dzzxzz@gmail.com 已提交
2642
    return mq;
2643
}
2644
RTM_EXPORT(rt_mq_create);
2645

C
chenjh 已提交
2646

2647
/**
C
chenjh 已提交
2648
 * @brief    This function will delete a messagequeue object and release the memory.
2649
 *
C
chenjh 已提交
2650 2651 2652
 * @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.
2653
 *
C
chenjh 已提交
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
 * @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.
2665
 */
D
dzzxzz 已提交
2666
rt_err_t rt_mq_delete(rt_mq_t mq)
2667
{
D
dzzxzz@gmail.com 已提交
2668
    RT_DEBUG_NOT_IN_INTERRUPT;
2669

D
dzzxzz@gmail.com 已提交
2670 2671
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2672 2673
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
    RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent) == RT_FALSE);
2674

D
dzzxzz@gmail.com 已提交
2675
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2676
    _ipc_list_resume_all(&(mq->parent.suspend_thread));
2677
    /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2678
    _ipc_list_resume_all(&(mq->suspend_sender_thread));
2679

2680 2681
    /* free message queue pool */
    RT_KERNEL_FREE(mq->msg_pool);
2682

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

D
dzzxzz@gmail.com 已提交
2686
    return RT_EOK;
2687
}
2688
RTM_EXPORT(rt_mq_delete);
2689
#endif /* RT_USING_HEAP */
2690

C
chenjh 已提交
2691

2692
/**
C
chenjh 已提交
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
 * @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.
2709
 *
C
chenjh 已提交
2710
 * @param    size is the length of the message(Unit: Byte).
2711
 *
C
chenjh 已提交
2712 2713 2714 2715 2716 2717 2718 2719
 * @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.
2720
 */
2721 2722 2723 2724
rt_err_t rt_mq_send_wait(rt_mq_t     mq,
                         const void *buffer,
                         rt_size_t   size,
                         rt_int32_t  timeout)
2725
{
D
dzzxzz@gmail.com 已提交
2726 2727
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
2728 2729
    rt_uint32_t tick_delta;
    struct rt_thread *thread;
D
dzzxzz@gmail.com 已提交
2730

2731
    /* parameter check */
D
dzzxzz@gmail.com 已提交
2732
    RT_ASSERT(mq != RT_NULL);
2733
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
2734 2735 2736 2737 2738 2739 2740
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);

    /* greater than one message size */
    if (size > mq->msg_size)
        return -RT_ERROR;

2741 2742 2743 2744 2745
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();

D
dzzxzz@gmail.com 已提交
2746 2747 2748 2749 2750 2751
    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 */
2752
    msg = (struct rt_mq_message *)mq->msg_queue_free;
2753 2754
    /* for non-blocking call */
    if (msg == RT_NULL && timeout == 0)
D
dzzxzz@gmail.com 已提交
2755 2756 2757 2758 2759 2760
    {
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        return -RT_EFULL;
    }
2761 2762

    /* message queue is full */
2763
    while ((msg = (struct rt_mq_message *)mq->msg_queue_free) == RT_NULL)
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
    {
        /* 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;
        }

        RT_DEBUG_IN_THREAD_CONTEXT;
        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2779
        _ipc_list_suspend(&(mq->suspend_sender_thread),
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 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
                            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 已提交
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
    /* 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 已提交
2851
    if(mq->entry < RT_MQ_ENTRY_MAX)
2852 2853 2854 2855 2856 2857 2858 2859 2860
    {
        /* increase message entry */
        mq->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
D
dzzxzz@gmail.com 已提交
2861 2862 2863 2864

    /* resume suspended thread */
    if (!rt_list_isempty(&mq->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2865
        _ipc_list_resume(&(mq->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
2879
}
2880 2881
RTM_EXPORT(rt_mq_send_wait)

C
chenjh 已提交
2882

2883
/**
C
chenjh 已提交
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898
 * @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).
2899
 *
C
chenjh 已提交
2900 2901
 * @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.
2902
 *
C
chenjh 已提交
2903
 * @warning  This function can be called in interrupt context and thread context.
2904 2905 2906 2907 2908
 */
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);
}
2909
RTM_EXPORT(rt_mq_send);
2910

C
chenjh 已提交
2911

2912
/**
C
chenjh 已提交
2913 2914 2915 2916 2917 2918 2919 2920 2921
 * @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.
2922
 *
C
chenjh 已提交
2923
 * @param    buffer is the content of the message.
2924
 *
C
chenjh 已提交
2925 2926 2927 2928
 * @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.
2929
 */
2930
rt_err_t rt_mq_urgent(rt_mq_t mq, const void *buffer, rt_size_t size)
2931
{
D
dzzxzz@gmail.com 已提交
2932 2933
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
2934

2935
    /* parameter check */
D
dzzxzz@gmail.com 已提交
2936
    RT_ASSERT(mq != RT_NULL);
2937
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
2938 2939
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);
2940

D
dzzxzz@gmail.com 已提交
2941 2942 2943
    /* greater than one message size */
    if (size > mq->msg_size)
        return -RT_ERROR;
2944

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

D
dzzxzz@gmail.com 已提交
2947 2948
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
2949

D
dzzxzz@gmail.com 已提交
2950 2951 2952 2953 2954 2955 2956
    /* 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);
2957

D
dzzxzz@gmail.com 已提交
2958 2959 2960 2961
        return -RT_EFULL;
    }
    /* move free list pointer */
    mq->msg_queue_free = msg->next;
2962

D
dzzxzz@gmail.com 已提交
2963 2964
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
2965

D
dzzxzz@gmail.com 已提交
2966 2967
    /* copy buffer */
    rt_memcpy(msg + 1, buffer, size);
2968

D
dzzxzz@gmail.com 已提交
2969 2970
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
2971

D
dzzxzz@gmail.com 已提交
2972
    /* link msg to the beginning of message queue */
2973
    msg->next = (struct rt_mq_message *)mq->msg_queue_head;
D
dzzxzz@gmail.com 已提交
2974
    mq->msg_queue_head = msg;
2975

D
dzzxzz@gmail.com 已提交
2976 2977 2978
    /* if there is no tail */
    if (mq->msg_queue_tail == RT_NULL)
        mq->msg_queue_tail = msg;
2979

mysterywolf's avatar
mysterywolf 已提交
2980
    if(mq->entry < RT_MQ_ENTRY_MAX)
2981 2982 2983 2984 2985 2986 2987 2988 2989
    {
        /* increase message entry */
        mq->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
mysterywolf's avatar
mysterywolf 已提交
2990

D
dzzxzz@gmail.com 已提交
2991 2992 2993
    /* resume suspended thread */
    if (!rt_list_isempty(&mq->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2994
        _ipc_list_resume(&(mq->parent.suspend_thread));
2995

D
dzzxzz@gmail.com 已提交
2996 2997
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2998

D
dzzxzz@gmail.com 已提交
2999
        rt_schedule();
D
dzzxzz 已提交
3000

D
dzzxzz@gmail.com 已提交
3001 3002
        return RT_EOK;
    }
3003

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

D
dzzxzz@gmail.com 已提交
3007
    return RT_EOK;
3008
}
3009
RTM_EXPORT(rt_mq_urgent);
3010

C
chenjh 已提交
3011

3012
/**
C
chenjh 已提交
3013 3014 3015 3016 3017 3018 3019 3020
 * @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.
3021
 *
C
chenjh 已提交
3022
 * @param    buffer is the content of the message.
3023
 *
C
chenjh 已提交
3024 3025 3026 3027 3028 3029
 * @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.
3030
 */
D
dzzxzz@gmail.com 已提交
3031 3032 3033 3034
rt_err_t rt_mq_recv(rt_mq_t    mq,
                    void      *buffer,
                    rt_size_t  size,
                    rt_int32_t timeout)
3035
{
D
dzzxzz@gmail.com 已提交
3036 3037 3038 3039 3040
    struct rt_thread *thread;
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
    rt_uint32_t tick_delta;

3041
    /* parameter check */
D
dzzxzz@gmail.com 已提交
3042
    RT_ASSERT(mq != RT_NULL);
3043
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);

    /* 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)
    {
G
Grissiom 已提交
3067
        RT_DEBUG_IN_THREAD_CONTEXT;
D
dzzxzz@gmail.com 已提交
3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083

        /* 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 已提交
3084
        _ipc_list_suspend(&(mq->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094
                            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",
3095
                                        thread->name));
3096

D
dzzxzz@gmail.com 已提交
3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139
            /* 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 */
3140 3141 3142 3143
    if(mq->entry > 0)
    {
        mq->entry --;
    }
D
dzzxzz@gmail.com 已提交
3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155

    /* 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;
3156 3157 3158 3159

    /* resume suspended thread */
    if (!rt_list_isempty(&(mq->suspend_sender_thread)))
    {
mysterywolf's avatar
mysterywolf 已提交
3160
        _ipc_list_resume(&(mq->suspend_sender_thread));
3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171

        /* 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 已提交
3172 3173 3174 3175 3176 3177
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

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

    return RT_EOK;
3178
}
B
Bernard Xiong 已提交
3179
RTM_EXPORT(rt_mq_recv);
3180

C
chenjh 已提交
3181

3182
/**
C
chenjh 已提交
3183 3184 3185
 * @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.
3186
 *
C
chenjh 已提交
3187
 * @param    mq is a pointer to a messagequeue object.
3188
 *
C
chenjh 已提交
3189 3190 3191 3192 3193 3194
 * @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.
3195
 */
B
bernard 已提交
3196
rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg)
3197
{
D
dzzxzz@gmail.com 已提交
3198 3199
    rt_ubase_t level;
    struct rt_mq_message *msg;
3200

3201
    /* parameter check */
D
dzzxzz@gmail.com 已提交
3202
    RT_ASSERT(mq != RT_NULL);
3203
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
3204

D
dzzxzz@gmail.com 已提交
3205 3206 3207 3208
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
3209

D
dzzxzz@gmail.com 已提交
3210
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
3211
        _ipc_list_resume_all(&mq->parent.suspend_thread);
3212
        /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
3213
        _ipc_list_resume_all(&(mq->suspend_sender_thread));
3214

D
dzzxzz@gmail.com 已提交
3215 3216 3217 3218 3219
        /* 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;
3220

D
dzzxzz@gmail.com 已提交
3221 3222 3223 3224 3225
            /* 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;
3226

D
dzzxzz@gmail.com 已提交
3227 3228 3229 3230
            /* put message to free list */
            msg->next = (struct rt_mq_message *)mq->msg_queue_free;
            mq->msg_queue_free = msg;
        }
3231

D
dzzxzz@gmail.com 已提交
3232 3233
        /* clean entry */
        mq->entry = 0;
3234

D
dzzxzz@gmail.com 已提交
3235 3236
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
3237

D
dzzxzz@gmail.com 已提交
3238
        rt_schedule();
3239

D
dzzxzz@gmail.com 已提交
3240 3241
        return RT_EOK;
    }
3242

D
dzzxzz@gmail.com 已提交
3243
    return -RT_ERROR;
3244
}
D
dzzxzz@gmail.com 已提交
3245
RTM_EXPORT(rt_mq_control);
C
chenjh 已提交
3246 3247

/**@}*/
3248
#endif /* RT_USING_MESSAGEQUEUE */
D
dzzxzz 已提交
3249

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