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

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

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

#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
D
dzzxzz 已提交
61 62 63
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);
64
#endif /* RT_USING_HOOK */
65 66 67 68 69

/**
 * @addtogroup IPC
 */

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

/**
C
chenjh 已提交
73
 * @brief    This function will initialize an IPC object, such as semaphore, mutex, messagequeue and mailbox.
74
 *
C
chenjh 已提交
75
 * @note     Executing this function will complete an initialization of the suspend thread list of the ipc object.
76
 *
C
chenjh 已提交
77 78 79 80 81 82
 * @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.
83
 */
mysterywolf's avatar
mysterywolf 已提交
84
rt_inline rt_err_t _ipc_object_init(struct rt_ipc_object *ipc)
85
{
B
Bernard Xiong 已提交
86
    /* initialize ipc object */
D
dzzxzz@gmail.com 已提交
87
    rt_list_init(&(ipc->suspend_thread));
88

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

C
chenjh 已提交
92

93
/**
C
chenjh 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
 * @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.
112
 *
C
chenjh 已提交
113 114
 * @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.
115
 *
C
chenjh 已提交
116 117 118 119 120
 * @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()
121
 */
mysterywolf's avatar
mysterywolf 已提交
122 123 124
rt_inline rt_err_t _ipc_list_suspend(rt_list_t        *list,
                                     struct rt_thread *thread,
                                     rt_uint8_t        flag)
125
{
D
dzzxzz@gmail.com 已提交
126 127 128 129 130 131 132
    /* suspend thread */
    rt_thread_suspend(thread);

    switch (flag)
    {
    case RT_IPC_FLAG_FIFO:
        rt_list_insert_before(list, &(thread->tlist));
133
        break; /* RT_IPC_FLAG_FIFO */
D
dzzxzz@gmail.com 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

    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));
        }
161
        break;/* RT_IPC_FLAG_PRIO */
C
chenchaoqun 已提交
162 163

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

    return RT_EOK;
169 170
}

C
chenjh 已提交
171

172
/**
C
chenjh 已提交
173 174 175 176 177 178 179 180 181 182
 * @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.
183
 *
C
chenjh 已提交
184 185
 * @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.
186
 *
C
chenjh 已提交
187 188 189
 * @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(),
190
 */
mysterywolf's avatar
mysterywolf 已提交
191
rt_inline rt_err_t _ipc_list_resume(rt_list_t *list)
192
{
D
dzzxzz@gmail.com 已提交
193
    struct rt_thread *thread;
194

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

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

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

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

C
chenjh 已提交
206

207
/**
C
chenjh 已提交
208 209 210 211 212
 * @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.
213
 *
C
chenjh 已提交
214 215 216 217
 * @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.
218 219
 *
 */
mysterywolf's avatar
mysterywolf 已提交
220
rt_inline rt_err_t _ipc_list_resume_all(rt_list_t *list)
221
{
D
dzzxzz@gmail.com 已提交
222 223 224
    struct rt_thread *thread;
    register rt_ubase_t temp;

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

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

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

    return RT_EOK;
248 249
}

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

252 253
#ifdef RT_USING_SEMAPHORE
/**
C
chenjh 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
 * @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.
270
 *
C
chenjh 已提交
271
 * @param    name is a pointer to the name you would like to give the semaphore.
272
 *
C
chenjh 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
 * @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.
295
 */
D
dzzxzz@gmail.com 已提交
296 297 298 299
rt_err_t rt_sem_init(rt_sem_t    sem,
                     const char *name,
                     rt_uint32_t value,
                     rt_uint8_t  flag)
300
{
D
dzzxzz@gmail.com 已提交
301
    RT_ASSERT(sem != RT_NULL);
302
    RT_ASSERT(value < 0x10000U);
303
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
304

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

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

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

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

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

C
chenjh 已提交
321

322
/**
C
chenjh 已提交
323 324 325 326 327
 * @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.
328
 *
C
chenjh 已提交
329
 * @see      rt_sem_delete()
330
 *
C
chenjh 已提交
331
 * @param    sem is a pointer to a semaphore object to be detached.
332
 *
C
chenjh 已提交
333 334 335 336 337 338
 * @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.
339
 */
D
dzzxzz 已提交
340
rt_err_t rt_sem_detach(rt_sem_t sem)
341
{
342
    /* parameter check */
D
dzzxzz@gmail.com 已提交
343
    RT_ASSERT(sem != RT_NULL);
344 345
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
    RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent));
346

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

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

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

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
359 360 361 362 363 364 365 366 367 368 369 370
 * @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.
371
 *
C
chenjh 已提交
372 373 374
 * @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:
375
 *
C
chenjh 已提交
376
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
377
 *
C
chenjh 已提交
378 379 380 381 382 383 384 385 386 387 388
 *               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.
389
 */
D
dzzxzz 已提交
390
rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)
391
{
D
dzzxzz@gmail.com 已提交
392
    rt_sem_t sem;
393

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

397 398
    RT_DEBUG_NOT_IN_INTERRUPT;

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

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

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

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

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

C
chenjh 已提交
417

418
/**
C
chenjh 已提交
419
 * @brief    This function will delete a semaphore object and release the memory space.
420
 *
C
chenjh 已提交
421 422 423
 * @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.
424
 *
C
chenjh 已提交
425
 * @see      rt_sem_detach()
426
 *
C
chenjh 已提交
427 428 429 430 431 432 433 434
 * @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.
435
 */
D
dzzxzz 已提交
436
rt_err_t rt_sem_delete(rt_sem_t sem)
437
{
438
    /* parameter check */
D
dzzxzz@gmail.com 已提交
439
    RT_ASSERT(sem != RT_NULL);
440 441
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
    RT_ASSERT(rt_object_is_systemobject(&sem->parent.parent) == RT_FALSE);
442

443 444
    RT_DEBUG_NOT_IN_INTERRUPT;

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

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

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

C
chenjh 已提交
456

457
/**
C
chenjh 已提交
458 459 460 461 462 463 464
 * @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.
465
 *
C
chenjh 已提交
466
 * @see      rt_sem_trytake()
467
 *
C
chenjh 已提交
468 469
 * @param    sem is a pointer to a semaphore object.
 *
S
Stanley 已提交
470
 * @param    timeout is a timeout period (unit: an OS tick). If the semaphore is unavailable, the thread will wait for
S
Stanley 已提交
471
 *           the semaphore up to the amount of time specified by this parameter.
S
Stanley 已提交
472 473 474 475 476 477
 *
 *           NOTE:
 *           If use Macro RT_WAITING_FOREVER to set this parameter, which means that when the
 *           message is unavailable in the queue, the thread will be waiting forever.
 *           If use macro RT_WAITING_NO to set this parameter, which means that this
 *           function is non-blocking and will return immediately.
C
chenjh 已提交
478 479 480 481 482
 *
 * @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.
483
 */
S
Stanley 已提交
484
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t timeout)
485
{
D
dzzxzz@gmail.com 已提交
486 487
    register rt_base_t temp;
    struct rt_thread *thread;
488

489
    /* parameter check */
D
dzzxzz@gmail.com 已提交
490
    RT_ASSERT(sem != RT_NULL);
491
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
H
Henson 已提交
492

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

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

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

D
dzzxzz@gmail.com 已提交
503 504 505 506 507 508 509 510 511 512 513
    if (sem->value > 0)
    {
        /* semaphore is available */
        sem->value --;

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
    }
    else
    {
        /* no waiting, return with timeout */
S
Stanley 已提交
514
        if (timeout == 0)
D
dzzxzz@gmail.com 已提交
515 516 517 518 519 520 521
        {
            rt_hw_interrupt_enable(temp);

            return -RT_ETIMEOUT;
        }
        else
        {
522 523 524
            /* current context checking */
            RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);

D
dzzxzz@gmail.com 已提交
525 526 527 528 529 530 531 532
            /* 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",
533
                                        thread->name));
534

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

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

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

D
dzzxzz@gmail.com 已提交
553 554
            /* enable interrupt */
            rt_hw_interrupt_enable(temp);
555

D
dzzxzz@gmail.com 已提交
556 557
            /* do schedule */
            rt_schedule();
558

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

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

D
dzzxzz@gmail.com 已提交
568
    return RT_EOK;
569
}
570
RTM_EXPORT(rt_sem_take);
571

C
chenjh 已提交
572

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

C
chenjh 已提交
593

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

611 612 613 614
    /* parameter check */
    RT_ASSERT(sem != RT_NULL);
    RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);

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

D
dzzxzz@gmail.com 已提交
617
    need_schedule = RT_FALSE;
618

D
dzzxzz@gmail.com 已提交
619 620
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
621

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

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

D
dzzxzz@gmail.com 已提交
646 647
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
648

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

D
dzzxzz@gmail.com 已提交
653
    return RT_EOK;
654
}
655
RTM_EXPORT(rt_sem_release);
656

C
chenjh 已提交
657

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

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

D
dzzxzz@gmail.com 已提交
680 681
    if (cmd == RT_IPC_CMD_RESET)
    {
B
Bernard Xiong 已提交
682
        rt_ubase_t value;
683

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

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

D
dzzxzz@gmail.com 已提交
692 693
        /* set new value */
        sem->value = (rt_uint16_t)value;
694

D
dzzxzz@gmail.com 已提交
695 696
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
697

D
dzzxzz@gmail.com 已提交
698
        rt_schedule();
699

D
dzzxzz@gmail.com 已提交
700 701
        return RT_EOK;
    }
702

D
dzzxzz@gmail.com 已提交
703
    return -RT_ERROR;
704
}
705
RTM_EXPORT(rt_sem_control);
C
chenjh 已提交
706 707

/**@}*/
708
#endif /* RT_USING_SEMAPHORE */
709 710 711

#ifdef RT_USING_MUTEX
/**
C
chenjh 已提交
712 713 714 715 716 717 718 719 720 721 722 723
 * @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.
724
 *
C
chenjh 已提交
725
 * @see      rt_mutex_create()
726
 *
C
chenjh 已提交
727 728 729 730 731 732 733
 * @param    mutex is a pointer to the mutex to initialize. It is assumed that storage for the mutex will be
 *           allocated in your application.
 *
 * @param    name is a pointer to the name that given to the mutex.
 *
 * @param    flag is the mutex flag, which determines the queuing way of how multiple threads wait
 *           when the mutex is not available.
mysterywolf's avatar
mysterywolf 已提交
734
 *           NOTE: This parameter has been obsoleted. It can be RT_IPC_FLAG_PRIO, RT_IPC_FLAG_FIFO or RT_NULL.
C
chenjh 已提交
735 736 737 738 739
 *
 * @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.
740
 */
D
dzzxzz 已提交
741
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
742
{
mysterywolf's avatar
mysterywolf 已提交
743 744
    /* flag parameter has been obsoleted */
    RT_UNUSED(flag);
745

746
    /* parameter check */
D
dzzxzz@gmail.com 已提交
747
    RT_ASSERT(mutex != RT_NULL);
748

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

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

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

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

D
dzzxzz@gmail.com 已提交
763
    return RT_EOK;
764
}
765
RTM_EXPORT(rt_mutex_init);
766

C
chenjh 已提交
767

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

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

796
    /* detach mutex object */
D
dzzxzz@gmail.com 已提交
797
    rt_object_detach(&(mutex->parent.parent));
798

D
dzzxzz@gmail.com 已提交
799
    return RT_EOK;
800
}
801
RTM_EXPORT(rt_mutex_detach);
802 803 804

#ifdef RT_USING_HEAP
/**
C
chenjh 已提交
805 806 807 808 809 810
 * @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()
811
 *
C
chenjh 已提交
812
 * @param    name is a pointer to the name that given to the mutex.
813
 *
C
chenjh 已提交
814 815
 * @param    flag is the mutex flag, which determines the queuing way of how multiple threads wait
 *           when the mutex is not available.
mysterywolf's avatar
mysterywolf 已提交
816
 *           NOTE: This parameter has been obsoleted. It can be RT_IPC_FLAG_PRIO, RT_IPC_FLAG_FIFO or RT_NULL.
817
 *
C
chenjh 已提交
818 819 820
 * @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.
821
 */
D
dzzxzz 已提交
822
rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
823
{
D
dzzxzz@gmail.com 已提交
824
    struct rt_mutex *mutex;
mysterywolf's avatar
mysterywolf 已提交
825 826 827

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

D
dzzxzz@gmail.com 已提交
829
    RT_DEBUG_NOT_IN_INTERRUPT;
830

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

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

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

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

D
dzzxzz@gmail.com 已提交
847
    return mutex;
848
}
849
RTM_EXPORT(rt_mutex_create);
850

C
chenjh 已提交
851

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

877 878
    RT_DEBUG_NOT_IN_INTERRUPT;

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

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

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

C
chenjh 已提交
890

891
/**
C
chenjh 已提交
892 893 894
 * @brief    This function will take a mutex, if the mutex is unavailable, the thread shall wait for
 *           the mutex up to a specified time.
 *
J
jihongbin 已提交
895 896
 * @note     When this function is called, the count value of the mutex->value will decrease 1 until it is equal to 0.
 *           When the mutex->value is 0, it means that the mutex is unavailable. At this time, it will suspend the
C
chenjh 已提交
897
 *           thread preparing to take the mutex.
J
jihongbin 已提交
898
 *           On the contrary, the rt_mutex_release() function will increase the count value of mutex->value by 1 each time.
C
chenjh 已提交
899 900 901 902
 *
 * @see      rt_mutex_trytake()
 *
 * @param    mutex is a pointer to a mutex object.
903
 *
S
Stanley 已提交
904
 * @param    timeout is a timeout period (unit: an OS tick). If the mutex is unavailable, the thread will wait for
C
chenjh 已提交
905 906 907
 *           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.
908
 *
C
chenjh 已提交
909 910 911 912
 * @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.
913
 */
S
Stanley 已提交
914
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t timeout)
915
{
D
dzzxzz@gmail.com 已提交
916 917
    register rt_base_t temp;
    struct rt_thread *thread;
918

B
bernard 已提交
919
    /* this function must not be used in interrupt even if time = 0 */
920
    /* current context checking */
H
Henson 已提交
921
    RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
922

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

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

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

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

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

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

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

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

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

                /* has waiting time, start thread timer */
S
Stanley 已提交
1012
                if (timeout > 0)
D
dzzxzz@gmail.com 已提交
1013 1014
                {
                    RT_DEBUG_LOG(RT_DEBUG_IPC,
1015 1016
                                 ("mutex_take: start the timer of thread:%s\n",
                                  thread->name));
1017

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

                /* enable interrupt */
                rt_hw_interrupt_enable(temp);

                /* do schedule */
                rt_schedule();

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

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

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

    return RT_EOK;
1052
}
1053
RTM_EXPORT(rt_mutex_take);
1054

C
chenjh 已提交
1055

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

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

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

D
dzzxzz@gmail.com 已提交
1100
    need_schedule = RT_FALSE;
1101

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

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

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

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

D
dzzxzz@gmail.com 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
    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",
1150
                                        thread->name));
1151

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

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

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

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

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

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

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

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

C
chenjh 已提交
1201

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

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

/**@}*/
1227
#endif /* RT_USING_MUTEX */
1228 1229 1230

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

/**@{*/

/**
 * @brief    The function will initialize a static event object.
 *
 * @note     For the static event object, its memory space is allocated by the compiler during compiling,
 *           and shall placed on the read-write data segment or on the uninitialized data segment.
 *           By contrast, the rt_event_create() function will allocate memory space automatically
 *           and initialize the event.
 *
 * @see      rt_event_create()
 *
 * @param    event is a pointer to the event to initialize. It is assumed that storage for the event
 *           will be allocated in your application.
 *
 * @param    name is a pointer to the name that given to the event.
 *
 * @param    flag is the event flag, which determines the queuing way of how multiple threads wait
 *           when the event is not available.
 *           The event flag can be ONE of the following values:
 *
 *               RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
1256
 *
C
chenjh 已提交
1257 1258
 *               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).
1259
 *
C
chenjh 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268
 *               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.
1269
 */
D
dzzxzz 已提交
1270
rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
1271
{
1272
    /* parameter check */
D
dzzxzz@gmail.com 已提交
1273
    RT_ASSERT(event != RT_NULL);
1274
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1275

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

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

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

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

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

C
chenjh 已提交
1292

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

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

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

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

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

1361
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1362 1363

    RT_DEBUG_NOT_IN_INTERRUPT;
H
HelloByeAll 已提交
1364

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

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

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

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

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

C
chenjh 已提交
1383

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

D
dzzxzz@gmail.com 已提交
1409
    RT_DEBUG_NOT_IN_INTERRUPT;
1410

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

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

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

C
chenjh 已提交
1422

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

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

    need_schedule = RT_FALSE;

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

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

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

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

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

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

            /* 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;
1529
}
1530
RTM_EXPORT(rt_event_send);
1531

C
chenjh 已提交
1532

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

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

1579
    /* current context checking */
H
Henson 已提交
1580
    RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
1581

D
dzzxzz@gmail.com 已提交
1582 1583 1584
    if (set == 0)
        return -RT_ERROR;

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

    if (status == RT_EOK)
    {
        /* set received event */
1617
        if (recved)
P
pathletboy 已提交
1618
            *recved = (event->set & set);
mysterywolf's avatar
mysterywolf 已提交
1619 1620

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

D
dzzxzz@gmail.com 已提交
1624 1625 1626 1627 1628 1629 1630 1631
        /* received event */
        if (option & RT_EVENT_FLAG_CLEAR)
            event->set &= ~set;
    }
    else if (timeout == 0)
    {
        /* no waiting */
        thread->error = -RT_ETIMEOUT;
1632

G
greed-island 已提交
1633 1634
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
1635

G
greed-island 已提交
1636
        return -RT_ETIMEOUT;
D
dzzxzz@gmail.com 已提交
1637 1638 1639 1640 1641 1642 1643 1644
    }
    else
    {
        /* fill thread event info */
        thread->event_set  = set;
        thread->event_info = option;

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

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

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

    return thread->error;
1685
}
1686
RTM_EXPORT(rt_event_recv);
1687

C
chenjh 已提交
1688

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

1707 1708 1709
    /* parameter check */
    RT_ASSERT(event != RT_NULL);
    RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
B
Bernard Xiong 已提交
1710

D
dzzxzz@gmail.com 已提交
1711 1712 1713 1714
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
1715

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

B
Bernard Xiong 已提交
1719
        /* initialize event set */
D
dzzxzz@gmail.com 已提交
1720
        event->set = 0;
1721

D
dzzxzz@gmail.com 已提交
1722 1723
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
1724

D
dzzxzz@gmail.com 已提交
1725
        rt_schedule();
1726

D
dzzxzz@gmail.com 已提交
1727 1728
        return RT_EOK;
    }
1729

D
dzzxzz@gmail.com 已提交
1730
    return -RT_ERROR;
1731
}
B
Bernard Xiong 已提交
1732
RTM_EXPORT(rt_event_control);
C
chenjh 已提交
1733 1734

/**@}*/
1735
#endif /* RT_USING_EVENT */
1736 1737 1738

#ifdef RT_USING_MAILBOX
/**
C
chenjh 已提交
1739 1740 1741 1742 1743 1744 1745
 * @addtogroup mailbox
 */

/**@{*/

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

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

D
dzzxzz@gmail.com 已提交
1792 1793
    /* set parent flag */
    mb->parent.parent.flag = flag;
1794

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

B
Bernard Xiong 已提交
1798
    /* initialize mailbox */
1799
    mb->msg_pool   = (rt_ubase_t *)msgpool;
D
dzzxzz@gmail.com 已提交
1800 1801 1802 1803
    mb->size       = size;
    mb->entry      = 0;
    mb->in_offset  = 0;
    mb->out_offset = 0;
1804

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

D
dzzxzz@gmail.com 已提交
1808
    return RT_EOK;
1809
}
1810
RTM_EXPORT(rt_mb_init);
1811

C
chenjh 已提交
1812

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

D
dzzxzz@gmail.com 已提交
1838
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1839
    _ipc_list_resume_all(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
1840
    /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
1841
    _ipc_list_resume_all(&(mb->suspend_sender_thread));
1842

D
dzzxzz@gmail.com 已提交
1843 1844
    /* detach mailbox object */
    rt_object_detach(&(mb->parent.parent));
1845

D
dzzxzz@gmail.com 已提交
1846
    return RT_EOK;
1847
}
1848
RTM_EXPORT(rt_mb_detach);
1849 1850 1851

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

1886
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
1887

1888 1889
    RT_DEBUG_NOT_IN_INTERRUPT;

D
dzzxzz@gmail.com 已提交
1890 1891 1892 1893
    /* allocate object */
    mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
    if (mb == RT_NULL)
        return mb;
1894

D
dzzxzz@gmail.com 已提交
1895 1896
    /* set parent */
    mb->parent.parent.flag = flag;
1897

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

B
Bernard Xiong 已提交
1901
    /* initialize mailbox */
D
dzzxzz@gmail.com 已提交
1902
    mb->size     = size;
1903
    mb->msg_pool = (rt_ubase_t *)RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t));
D
dzzxzz@gmail.com 已提交
1904 1905 1906 1907
    if (mb->msg_pool == RT_NULL)
    {
        /* delete mailbox object */
        rt_object_delete(&(mb->parent.parent));
1908

D
dzzxzz@gmail.com 已提交
1909 1910 1911 1912 1913
        return RT_NULL;
    }
    mb->entry      = 0;
    mb->in_offset  = 0;
    mb->out_offset = 0;
1914

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

D
dzzxzz@gmail.com 已提交
1918
    return mb;
1919
}
1920
RTM_EXPORT(rt_mb_create);
1921

C
chenjh 已提交
1922

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

1948 1949
    RT_DEBUG_NOT_IN_INTERRUPT;

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

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

1956 1957
    /* free mailbox pool */
    RT_KERNEL_FREE(mb->msg_pool);
1958

D
dzzxzz@gmail.com 已提交
1959 1960
    /* delete mailbox object */
    rt_object_delete(&(mb->parent.parent));
1961

D
dzzxzz@gmail.com 已提交
1962
    return RT_EOK;
1963
}
1964
RTM_EXPORT(rt_mb_delete);
1965
#endif /* RT_USING_HEAP */
1966

C
chenjh 已提交
1967

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

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

2003
    /* current context checking */
H
Henson 已提交
2004
    RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2005

D
dzzxzz@gmail.com 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();

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

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

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

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

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

            return -RT_EFULL;
        }

        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2039
        _ipc_list_suspend(&(mb->suspend_sender_thread),
D
dzzxzz@gmail.com 已提交
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
                            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",
2050
                                        thread->name));
2051

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

mysterywolf's avatar
mysterywolf 已提交
2092
    if(mb->entry < RT_MB_ENTRY_MAX)
2093 2094 2095 2096 2097 2098 2099 2100 2101
    {
        /* increase message entry */
        mb->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
mysterywolf's avatar
mysterywolf 已提交
2102

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

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
2120
}
2121
RTM_EXPORT(rt_mb_send_wait);
2122

C
chenjh 已提交
2123

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

C
chenjh 已提交
2147

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

2183
    /* rewind to the previous position */
mysterywolf's avatar
update  
mysterywolf 已提交
2184
    if (mb->out_offset > 0)
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
    {
        mb->out_offset --;
    }
    else
    {
        mb->out_offset = mb->size - 1;
    }

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

mysterywolf's avatar
update  
mysterywolf 已提交
2196 2197
    /* increase message entry */
    mb->entry ++;
2198 2199 2200 2201

    /* resume suspended thread */
    if (!rt_list_isempty(&mb->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
2202
        _ipc_list_resume(&(mb->parent.suspend_thread));
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218

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

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

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

2248
    /* current context checking */
H
Henson 已提交
2249
    RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2250

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

D
dzzxzz@gmail.com 已提交
2283 2284 2285 2286
            return -RT_ETIMEOUT;
        }

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

D
dzzxzz@gmail.com 已提交
2300 2301 2302 2303 2304 2305
            /* 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));
        }
2306

D
dzzxzz@gmail.com 已提交
2307 2308
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2309

D
dzzxzz@gmail.com 已提交
2310 2311
        /* re-schedule */
        rt_schedule();
2312

D
dzzxzz@gmail.com 已提交
2313 2314 2315 2316 2317 2318
        /* resume from suspend state */
        if (thread->error != RT_EOK)
        {
            /* return error */
            return thread->error;
        }
2319

D
dzzxzz@gmail.com 已提交
2320 2321
        /* disable interrupt */
        temp = rt_hw_interrupt_disable();
2322

D
dzzxzz@gmail.com 已提交
2323 2324 2325 2326 2327 2328 2329 2330 2331
        /* 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;
        }
    }
2332

D
dzzxzz@gmail.com 已提交
2333 2334
    /* fill ptr */
    *value = mb->msg_pool[mb->out_offset];
2335

D
dzzxzz@gmail.com 已提交
2336 2337 2338 2339
    /* increase output offset */
    ++ mb->out_offset;
    if (mb->out_offset >= mb->size)
        mb->out_offset = 0;
2340

D
dzzxzz@gmail.com 已提交
2341
    /* decrease message entry */
2342 2343 2344 2345
    if(mb->entry > 0)
    {
        mb->entry --;
    }
2346

D
dzzxzz@gmail.com 已提交
2347 2348 2349
    /* resume suspended thread */
    if (!rt_list_isempty(&(mb->suspend_sender_thread)))
    {
mysterywolf's avatar
mysterywolf 已提交
2350
        _ipc_list_resume(&(mb->suspend_sender_thread));
2351

D
dzzxzz@gmail.com 已提交
2352 2353
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
2354

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

D
dzzxzz@gmail.com 已提交
2357
        rt_schedule();
2358

D
dzzxzz@gmail.com 已提交
2359 2360
        return RT_EOK;
    }
2361

D
dzzxzz@gmail.com 已提交
2362 2363
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
2364

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

D
dzzxzz@gmail.com 已提交
2367
    return RT_EOK;
2368
}
2369
RTM_EXPORT(rt_mb_recv);
2370

C
chenjh 已提交
2371

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

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

D
dzzxzz@gmail.com 已提交
2394 2395 2396 2397
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
2398

D
dzzxzz@gmail.com 已提交
2399
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
2400
        _ipc_list_resume_all(&(mb->parent.suspend_thread));
D
dzzxzz@gmail.com 已提交
2401
        /* also resume all mailbox private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2402
        _ipc_list_resume_all(&(mb->suspend_sender_thread));
2403

D
dzzxzz@gmail.com 已提交
2404 2405 2406 2407
        /* re-init mailbox */
        mb->entry      = 0;
        mb->in_offset  = 0;
        mb->out_offset = 0;
2408

D
dzzxzz@gmail.com 已提交
2409 2410
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
2411

D
dzzxzz@gmail.com 已提交
2412
        rt_schedule();
2413

D
dzzxzz@gmail.com 已提交
2414 2415
        return RT_EOK;
    }
2416

D
dzzxzz@gmail.com 已提交
2417
    return -RT_ERROR;
2418
}
B
Bernard Xiong 已提交
2419
RTM_EXPORT(rt_mb_control);
C
chenjh 已提交
2420 2421

/**@}*/
2422
#endif /* RT_USING_MAILBOX */
2423 2424

#ifdef RT_USING_MESSAGEQUEUE
C
chenjh 已提交
2425 2426 2427 2428 2429 2430
/**
 * @addtogroup messagequeue
 */

/**@{*/

2431 2432
struct rt_mq_message
{
D
dzzxzz@gmail.com 已提交
2433
    struct rt_mq_message *next;
2434 2435
};

C
chenjh 已提交
2436

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

D
dzzxzz@gmail.com 已提交
2489 2490
    /* parameter check */
    RT_ASSERT(mq != RT_NULL);
2491
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
2492

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

D
dzzxzz@gmail.com 已提交
2496 2497
    /* set parent flag */
    mq->parent.parent.flag = flag;
2498

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

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

D
dzzxzz@gmail.com 已提交
2505 2506 2507
    /* 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));
2508

B
Bernard Xiong 已提交
2509
    /* initialize message list */
D
dzzxzz@gmail.com 已提交
2510 2511
    mq->msg_queue_head = RT_NULL;
    mq->msg_queue_tail = RT_NULL;
2512

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

D
dzzxzz@gmail.com 已提交
2523 2524
    /* the initial entry is zero */
    mq->entry = 0;
2525

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

D
dzzxzz@gmail.com 已提交
2529
    return RT_EOK;
2530
}
2531
RTM_EXPORT(rt_mq_init);
2532

C
chenjh 已提交
2533

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

D
dzzxzz@gmail.com 已提交
2559
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2560
    _ipc_list_resume_all(&mq->parent.suspend_thread);
2561
    /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2562
    _ipc_list_resume_all(&(mq->suspend_sender_thread));
2563

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

D
dzzxzz@gmail.com 已提交
2567
    return RT_EOK;
2568
}
2569
RTM_EXPORT(rt_mq_detach);
2570 2571 2572

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

2613
    RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO));
2614 2615

    RT_DEBUG_NOT_IN_INTERRUPT;
H
HelloByeAll 已提交
2616

D
dzzxzz@gmail.com 已提交
2617 2618 2619 2620 2621 2622 2623 2624
    /* 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 已提交
2625
    /* initialize ipc object */
mysterywolf's avatar
mysterywolf 已提交
2626
    _ipc_object_init(&(mq->parent));
D
dzzxzz@gmail.com 已提交
2627

B
Bernard Xiong 已提交
2628
    /* initialize message queue */
D
dzzxzz@gmail.com 已提交
2629 2630 2631 2632 2633 2634

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

    /* allocate message pool */
2635
    mq->msg_pool = RT_KERNEL_MALLOC((mq->msg_size + sizeof(struct rt_mq_message)) * mq->max_msgs);
D
dzzxzz@gmail.com 已提交
2636 2637
    if (mq->msg_pool == RT_NULL)
    {
2638
        rt_object_delete(&(mq->parent.parent));
D
dzzxzz@gmail.com 已提交
2639 2640 2641 2642

        return RT_NULL;
    }

B
Bernard Xiong 已提交
2643
    /* initialize message list */
D
dzzxzz@gmail.com 已提交
2644 2645 2646
    mq->msg_queue_head = RT_NULL;
    mq->msg_queue_tail = RT_NULL;

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

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

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

D
dzzxzz@gmail.com 已提交
2663
    return mq;
2664
}
2665
RTM_EXPORT(rt_mq_create);
2666

C
chenjh 已提交
2667

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

2694 2695
    RT_DEBUG_NOT_IN_INTERRUPT;

D
dzzxzz@gmail.com 已提交
2696
    /* resume all suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2697
    _ipc_list_resume_all(&(mq->parent.suspend_thread));
2698
    /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
2699
    _ipc_list_resume_all(&(mq->suspend_sender_thread));
2700

2701 2702
    /* free message queue pool */
    RT_KERNEL_FREE(mq->msg_pool);
2703

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

D
dzzxzz@gmail.com 已提交
2707
    return RT_EOK;
2708
}
2709
RTM_EXPORT(rt_mq_delete);
2710
#endif /* RT_USING_HEAP */
2711

C
chenjh 已提交
2712

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

2752
    /* parameter check */
D
dzzxzz@gmail.com 已提交
2753
    RT_ASSERT(mq != RT_NULL);
2754
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
2755 2756 2757
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);

2758
    /* current context checking */
H
Henson 已提交
2759
    RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2760

D
dzzxzz@gmail.com 已提交
2761 2762 2763 2764
    /* greater than one message size */
    if (size > mq->msg_size)
        return -RT_ERROR;

2765 2766 2767 2768 2769
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();

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

        return -RT_EFULL;
    }
2785 2786

    /* message queue is full */
2787
    while ((msg = (struct rt_mq_message *)mq->msg_queue_free) == RT_NULL)
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
    {
        /* reset error number in thread */
        thread->error = RT_EOK;

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

            return -RT_EFULL;
        }

        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
2802
        _ipc_list_suspend(&(mq->suspend_sender_thread),
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
                            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 已提交
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
    /* 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 已提交
2874
    if(mq->entry < RT_MQ_ENTRY_MAX)
2875 2876 2877 2878 2879 2880 2881 2882 2883
    {
        /* increase message entry */
        mq->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
D
dzzxzz@gmail.com 已提交
2884 2885 2886 2887

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

        /* enable interrupt */
        rt_hw_interrupt_enable(temp);

        rt_schedule();

        return RT_EOK;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

    return RT_EOK;
2902
}
2903 2904
RTM_EXPORT(rt_mq_send_wait)

C
chenjh 已提交
2905

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

C
chenjh 已提交
2934

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

2958
    /* parameter check */
D
dzzxzz@gmail.com 已提交
2959
    RT_ASSERT(mq != RT_NULL);
2960
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
2961 2962
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);
2963

D
dzzxzz@gmail.com 已提交
2964 2965 2966
    /* greater than one message size */
    if (size > mq->msg_size)
        return -RT_ERROR;
2967

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

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

D
dzzxzz@gmail.com 已提交
2973 2974 2975 2976 2977 2978 2979
    /* 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);
2980

D
dzzxzz@gmail.com 已提交
2981 2982 2983 2984
        return -RT_EFULL;
    }
    /* move free list pointer */
    mq->msg_queue_free = msg->next;
2985

D
dzzxzz@gmail.com 已提交
2986 2987
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
2988

D
dzzxzz@gmail.com 已提交
2989 2990
    /* copy buffer */
    rt_memcpy(msg + 1, buffer, size);
2991

D
dzzxzz@gmail.com 已提交
2992 2993
    /* disable interrupt */
    temp = rt_hw_interrupt_disable();
2994

D
dzzxzz@gmail.com 已提交
2995
    /* link msg to the beginning of message queue */
2996
    msg->next = (struct rt_mq_message *)mq->msg_queue_head;
D
dzzxzz@gmail.com 已提交
2997
    mq->msg_queue_head = msg;
2998

D
dzzxzz@gmail.com 已提交
2999 3000 3001
    /* if there is no tail */
    if (mq->msg_queue_tail == RT_NULL)
        mq->msg_queue_tail = msg;
3002

mysterywolf's avatar
mysterywolf 已提交
3003
    if(mq->entry < RT_MQ_ENTRY_MAX)
3004 3005 3006 3007 3008 3009 3010 3011 3012
    {
        /* increase message entry */
        mq->entry ++;
    }
    else
    {
        rt_hw_interrupt_enable(temp); /* enable interrupt */
        return -RT_EFULL; /* value overflowed */
    }
mysterywolf's avatar
mysterywolf 已提交
3013

D
dzzxzz@gmail.com 已提交
3014 3015 3016
    /* resume suspended thread */
    if (!rt_list_isempty(&mq->parent.suspend_thread))
    {
mysterywolf's avatar
mysterywolf 已提交
3017
        _ipc_list_resume(&(mq->parent.suspend_thread));
3018

D
dzzxzz@gmail.com 已提交
3019 3020
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
3021

D
dzzxzz@gmail.com 已提交
3022
        rt_schedule();
D
dzzxzz 已提交
3023

D
dzzxzz@gmail.com 已提交
3024 3025
        return RT_EOK;
    }
3026

D
dzzxzz@gmail.com 已提交
3027 3028
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);
3029

D
dzzxzz@gmail.com 已提交
3030
    return RT_EOK;
3031
}
3032
RTM_EXPORT(rt_mq_urgent);
3033

C
chenjh 已提交
3034

3035
/**
C
chenjh 已提交
3036 3037 3038 3039 3040 3041 3042 3043
 * @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.
3044
 *
C
chenjh 已提交
3045
 * @param    buffer is the content of the message.
3046
 *
C
chenjh 已提交
3047 3048
 * @param    size is the length of the message(Unit: Byte).
 *
S
Stanley 已提交
3049 3050
 * @param    timeout is a timeout period (unit: an OS tick). If the message is unavailable, the thread will wait for
 *           the message in the queue up to the amount of time specified by this parameter.
S
Stanley 已提交
3051 3052 3053
 *
 *           NOTE:
 *           If use Macro RT_WAITING_FOREVER to set this parameter, which means that when the
S
Stanley 已提交
3054
 *           message is unavailable in the queue, the thread will be waiting forever.
S
Stanley 已提交
3055 3056
 *           If use macro RT_WAITING_NO to set this parameter, which means that this
 *           function is non-blocking and will return immediately.
C
chenjh 已提交
3057 3058 3059
 *
 * @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.
3060
 */
D
dzzxzz@gmail.com 已提交
3061 3062 3063 3064
rt_err_t rt_mq_recv(rt_mq_t    mq,
                    void      *buffer,
                    rt_size_t  size,
                    rt_int32_t timeout)
3065
{
D
dzzxzz@gmail.com 已提交
3066 3067 3068 3069 3070
    struct rt_thread *thread;
    register rt_ubase_t temp;
    struct rt_mq_message *msg;
    rt_uint32_t tick_delta;

3071
    /* parameter check */
D
dzzxzz@gmail.com 已提交
3072
    RT_ASSERT(mq != RT_NULL);
3073
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
D
dzzxzz@gmail.com 已提交
3074 3075 3076
    RT_ASSERT(buffer != RT_NULL);
    RT_ASSERT(size != 0);

3077
    /* current context checking */
H
Henson 已提交
3078
    RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
3079

D
dzzxzz@gmail.com 已提交
3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114
    /* initialize delta tick */
    tick_delta = 0;
    /* get current thread */
    thread = rt_thread_self();
    RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));

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

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

        return -RT_ETIMEOUT;
    }

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

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

            thread->error = -RT_ETIMEOUT;

            return -RT_ETIMEOUT;
        }

        /* suspend current thread */
mysterywolf's avatar
mysterywolf 已提交
3115
        _ipc_list_suspend(&(mq->parent.suspend_thread),
D
dzzxzz@gmail.com 已提交
3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
                            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",
3126
                                        thread->name));
3127

D
dzzxzz@gmail.com 已提交
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
            /* 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 */
3171 3172 3173 3174
    if(mq->entry > 0)
    {
        mq->entry --;
    }
D
dzzxzz@gmail.com 已提交
3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186

    /* 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;
3187 3188 3189 3190

    /* resume suspended thread */
    if (!rt_list_isempty(&(mq->suspend_sender_thread)))
    {
mysterywolf's avatar
mysterywolf 已提交
3191
        _ipc_list_resume(&(mq->suspend_sender_thread));
3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202

        /* 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 已提交
3203 3204 3205 3206 3207 3208
    /* enable interrupt */
    rt_hw_interrupt_enable(temp);

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

    return RT_EOK;
3209
}
B
Bernard Xiong 已提交
3210
RTM_EXPORT(rt_mq_recv);
3211

C
chenjh 已提交
3212

3213
/**
C
chenjh 已提交
3214 3215 3216
 * @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.
3217
 *
C
chenjh 已提交
3218
 * @param    mq is a pointer to a messagequeue object.
3219
 *
C
chenjh 已提交
3220 3221 3222 3223 3224 3225
 * @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.
3226
 */
B
bernard 已提交
3227
rt_err_t rt_mq_control(rt_mq_t mq, int cmd, void *arg)
3228
{
D
dzzxzz@gmail.com 已提交
3229 3230
    rt_ubase_t level;
    struct rt_mq_message *msg;
3231

3232
    /* parameter check */
D
dzzxzz@gmail.com 已提交
3233
    RT_ASSERT(mq != RT_NULL);
3234
    RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
3235

D
dzzxzz@gmail.com 已提交
3236 3237 3238 3239
    if (cmd == RT_IPC_CMD_RESET)
    {
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
3240

D
dzzxzz@gmail.com 已提交
3241
        /* resume all waiting thread */
mysterywolf's avatar
mysterywolf 已提交
3242
        _ipc_list_resume_all(&mq->parent.suspend_thread);
3243
        /* also resume all message queue private suspended thread */
mysterywolf's avatar
mysterywolf 已提交
3244
        _ipc_list_resume_all(&(mq->suspend_sender_thread));
3245

D
dzzxzz@gmail.com 已提交
3246 3247 3248 3249 3250
        /* 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;
3251

D
dzzxzz@gmail.com 已提交
3252 3253 3254 3255 3256
            /* 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;
3257

D
dzzxzz@gmail.com 已提交
3258 3259 3260 3261
            /* put message to free list */
            msg->next = (struct rt_mq_message *)mq->msg_queue_free;
            mq->msg_queue_free = msg;
        }
3262

D
dzzxzz@gmail.com 已提交
3263 3264
        /* clean entry */
        mq->entry = 0;
3265

D
dzzxzz@gmail.com 已提交
3266 3267
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
3268

D
dzzxzz@gmail.com 已提交
3269
        rt_schedule();
3270

D
dzzxzz@gmail.com 已提交
3271 3272
        return RT_EOK;
    }
3273

D
dzzxzz@gmail.com 已提交
3274
    return -RT_ERROR;
3275
}
D
dzzxzz@gmail.com 已提交
3276
RTM_EXPORT(rt_mq_control);
C
chenjh 已提交
3277 3278

/**@}*/
3279
#endif /* RT_USING_MESSAGEQUEUE */
D
dzzxzz 已提交
3280

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