* @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() * * @param name is a pointer that given to the mailbox. * * @param size is the maximum number of mails in the mailbox. * For example, when mail
| 2451 | * @warning This function can ONLY be called from threads. |
| 2452 | */ |
| 2453 | rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag) |
| 2454 | { |
| 2455 | rt_mailbox_t mb; |
| 2456 | |
| 2457 | RT_ASSERT((flag == RT_IPC_FLAG_FIFO) || (flag == RT_IPC_FLAG_PRIO)); |
| 2458 | |
| 2459 | RT_DEBUG_NOT_IN_INTERRUPT; |
| 2460 | |
| 2461 | /* allocate object */ |
| 2462 | mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name); |
| 2463 | if (mb == RT_NULL) |
| 2464 | return mb; |
| 2465 | |
| 2466 | /* set parent */ |
| 2467 | mb->parent.parent.flag = flag; |
| 2468 | |
| 2469 | /* initialize ipc object */ |
| 2470 | _ipc_object_init(&(mb->parent)); |
| 2471 | |
| 2472 | /* initialize mailbox */ |
| 2473 | mb->size = (rt_uint16_t)size; |
| 2474 | mb->msg_pool = (rt_ubase_t *)RT_KERNEL_MALLOC(mb->size * sizeof(rt_ubase_t)); |
| 2475 | if (mb->msg_pool == RT_NULL) |
| 2476 | { |
| 2477 | /* delete mailbox object */ |
| 2478 | rt_object_delete(&(mb->parent.parent)); |
| 2479 | |
| 2480 | return RT_NULL; |
| 2481 | } |
| 2482 | mb->entry = 0; |
| 2483 | mb->in_offset = 0; |
| 2484 | mb->out_offset = 0; |
| 2485 | |
| 2486 | /* initialize an additional list of sender suspend thread */ |
| 2487 | rt_list_init(&(mb->suspend_sender_thread)); |
| 2488 | rt_spin_lock_init(&(mb->spinlock)); |
| 2489 | |
| 2490 | return mb; |
| 2491 | } |
| 2492 | RTM_EXPORT(rt_mb_create); |
| 2493 | |
| 2494 |