RT-Thread - IPC Communicate

RT-Thread 2018-11-28 4.2k

Mail Box#

  • 類似於 pipe,主要用來傳輸資料
  • 每一封郵件大小為 4 bytes(即 32 位元)

結構#

File: rtdef.h

642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#ifdef RT_USING_MAILBOX
/**
* mailbox structure
*/
struct rt_mailbox
{
struct rt_ipc_object parent; /**< inherit from ipc_object */

rt_uint32_t *msg_pool; /**< start address of message buffer */

rt_uint16_t size; /**< size of message pool */

rt_uint16_t entry; /**< index of messages in msg_pool */
rt_uint16_t in_offset; /**< input offset of the message buffer */
rt_uint16_t out_offset; /**< output offset of the message buffer */

rt_list_t suspend_sender_thread; /**< sender thread suspended on this mailbox */
};
typedef struct rt_mailbox *rt_mailbox_t;
#endif
  • msg_pool 指向郵件堆的起點,entry 紀錄總郵件的數量

File: ipc.c

建立 mail box#

動態記憶體管理#

功能 回傳值
建立 mail box mail box
*name size flag
名字 mail box 大小 FIFO / PRIO
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
/**
* This function will create a mailbox object from system resource
*
* @param name the name of mailbox
* @param size the size of mailbox
* @param flag the flag of mailbox
*
* @return the created mailbox, RT_NULL on error happen
*/
rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
{
rt_mailbox_t mb;

RT_DEBUG_NOT_IN_INTERRUPT;

/* allocate object */
mb = (rt_mailbox_t)rt_object_allocate(RT_Object_Class_MailBox, name);
if (mb == RT_NULL)
return mb;
  • 首先 allocate 一塊給 mailbox
1367
1368
1369
1370
1371
/* set parent */
mb->parent.parent.flag = flag;

/* init ipc object */
rt_ipc_object_init(&(mb->parent));
  • 填入 flag 及初始化
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
/* init mailbox */
mb->size = size;
mb->msg_pool = RT_KERNEL_MALLOC(mb->size * sizeof(rt_uint32_t));
if (mb->msg_pool == RT_NULL)
{
/* delete mailbox object */
rt_object_delete(&(mb->parent.parent));

return RT_NULL;
}
  • 因使用動態記憶體的緣故,需 allocate 一塊給郵件堆
  • 大小為一封一件的大小 * size
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
    mb->entry      = 0;
mb->in_offset = 0;
mb->out_offset = 0;

/* init an additional list of sender suspend thread */
rt_list_init(&(mb->suspend_sender_thread));

return mb;
}
RTM_EXPORT(rt_mb_create);
  • 最後初始化值及等待鏈

靜態記憶體管理#

功能 回傳值
初始化 mail box RT_EOK
mb *name *msgpool size flag
mail box 本體 名字 存放郵件的地方 mail box 大小 FIFO / PRIO
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
/**
* This function will initialize a mailbox and put it under control of resource
* management.
*
* @param mb the mailbox object
* @param name the name of mailbox
* @param msgpool the begin address of buffer to save received mail
* @param size the size of mailbox
* @param flag the flag of mailbox
*
* @return the operation status, RT_EOK on successful
*/
rt_err_t rt_mb_init(rt_mailbox_t mb,
const char *name,
void *msgpool,
rt_size_t size,
rt_uint8_t flag)
{
RT_ASSERT(mb != RT_NULL);

/* init object */
rt_object_init(&(mb->parent.parent), RT_Object_Class_MailBox, name);

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

/* init ipc object */
rt_ipc_object_init(&(mb->parent));
  • 這裡就不需要 allocate,可直接初始化來使用
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
    /* init mailbox */
mb->msg_pool = msgpool;
mb->size = size;
mb->entry = 0;
mb->in_offset = 0;
mb->out_offset = 0;

/* init an additional list of sender suspend thread */
rt_list_init(&(mb->suspend_sender_thread));

return RT_EOK;
}
RTM_EXPORT(rt_mb_init);
  • 一樣,郵件堆可直接拿來用,初始化值及等待鏈

刪除 mail box#

動態記憶體管理#

功能 回傳值 mb
刪除 mail box RT_EOK 欲刪除的 mail box
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
/**
* This function will delete a mailbox object and release the memory
*
* @param mb the mailbox object
*
* @return the error code
*/
rt_err_t rt_mb_delete(rt_mailbox_t mb)
{
RT_DEBUG_NOT_IN_INTERRUPT;

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

/* resume all suspended thread */
rt_ipc_list_resume_all(&(mb->parent.suspend_thread));

/* also resume all mailbox private suspended thread */
rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  • 首先將正在等待郵件的,與正在等待傳送的 thread 叫醒
1416
1417
1418
1419
1420
1421
1422
1423
1424
    /* free mailbox pool */
RT_KERNEL_FREE(mb->msg_pool);

/* delete mailbox object */
rt_object_delete(&(mb->parent.parent));

return RT_EOK;
}
RTM_EXPORT(rt_mb_delete);
  • 歸還郵件堆,最後刪除 mail box

靜態記憶體管理#

功能 回傳值 mb
刪除 mail box RT_EOK 欲刪除的 mail box
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
/**
* This function will detach a mailbox from resource management
*
* @param mb the mailbox object
*
* @return the operation status, RT_EOK on successful
*/
rt_err_t rt_mb_detach(rt_mailbox_t mb)
{
/* parameter check */
RT_ASSERT(mb != RT_NULL);
RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
RT_ASSERT(rt_object_is_systemobject(&mb->parent.parent));

/* resume all suspended thread */
rt_ipc_list_resume_all(&(mb->parent.suspend_thread));
/* also resume all mailbox private suspended thread */
rt_ipc_list_resume_all(&(mb->suspend_sender_thread));
  • 首先將正在等待郵件的,與正在等待傳送的 thread 叫醒
1339
1340
1341
1342
1343
1344
    /* detach mailbox object */
rt_object_detach(&(mb->parent.parent));

return RT_EOK;
}
RTM_EXPORT(rt_mb_detach);
  • 最後刪除 mail box(使用 detach

傳送郵件#

功能 回傳值
傳送郵件 RT_EOK
mb value timeout
欲傳送的 mailbox 郵件內容 等待時間(如果需要)
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
/**
* This function will send a mail to mailbox object. If the mailbox is full,
* current thread will be suspended until timeout.
*
* @param mb the mailbox object
* @param value the mail
* @param timeout the waiting time
*
* @return the error code
*/
rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
rt_uint32_t value,
rt_int32_t timeout)
{
struct rt_thread *thread;
register rt_ubase_t temp;
rt_uint32_t tick_delta;

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

/* 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;
}
  • 如果 mail box 滿了,且不等待 (timeout==0),回傳 FULL
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
RT_DEBUG_IN_THREAD_CONTEXT;
/* suspend current thread */
rt_ipc_list_suspend(&(mb->suspend_sender_thread),
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",
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));
}
  • 若要等待,將 thread 掛上等待鏈,啟動一個 timer
1503
1504
1505
1506
1507
/* enable interrupt */
rt_hw_interrupt_enable(temp);

/* re-schedule */
rt_schedule();
  • 再做一次調度
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
    /* 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;
}
}
  • 如跳回來,重新計算 timeout
1527
1528
1529
1530
1531
1532
1533
1534
/* 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;
/* increase message entry */
mb->entry ++;
  • 若可以寫入,將資料寫入,同時更新 offsetentry
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
    /* resume suspended thread */
if (!rt_list_isempty(&mb->parent.suspend_thread))
{
rt_ipc_list_resume(&(mb->parent.suspend_thread));

/* 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_send_wait);
  • 如果有人在等待寄信,叫醒他,做一次調度

  • 若是不想等待,可以使用 rt_mb_send
功能 回傳值
傳送郵件(不等待) RT_EOK
mb value
欲傳送的 mailbox 郵件內容
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
/**
* This function will send a mail to mailbox object, if there are threads
* suspended on mailbox object, it will be waked up. This function will return
* immediately, if you want blocking send, use rt_mb_send_wait instead.
*
* @param mb the mailbox object
* @param value the mail
*
* @return the error code
*/
rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value)
{
return rt_mb_send_wait(mb, value, 0);
}
RTM_EXPORT(rt_mb_send);
  • timeout == 0

接受郵件#

功能 回傳值
接受郵件 RT_EOK
mb *value timeout
欲收信的 mailbox 郵件內容 等待時間(如果需要)
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
/**
* 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.
*
* @param mb the mailbox object
* @param value the received mail will be saved in
* @param timeout the waiting time
*
* @return the error code
*/
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout)
{
struct rt_thread *thread;
register rt_ubase_t temp;
rt_uint32_t tick_delta;

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

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

return -RT_ETIMEOUT;
}
  • 如果 mail box 沒東西,且不等待,回傳 TIMEOUT
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
RT_DEBUG_IN_THREAD_CONTEXT;
/* suspend current thread */
rt_ipc_list_suspend(&(mb->parent.suspend_thread),
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",
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));
}
  • 若要等待,將 thread 掛上等待鏈,啟動一個 timer
1651
1652
1653
1654
1655
/* enable interrupt */
rt_hw_interrupt_enable(temp);

/* re-schedule */
rt_schedule();
  • 再做一次調度
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
    /* 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;
}
}
  • 如跳回來,重新計算 timeout
1675
1676
1677
1678
1679
1680
1681
1682
1683
/* fill ptr */
*value = mb->msg_pool[mb->out_offset];

/* increase output offset */
++ mb->out_offset;
if (mb->out_offset >= mb->size)
mb->out_offset = 0;
/* decrease message entry */
mb->entry --;
  • 若可以讀取,將資料寫入,同時更新 offsetentry
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
    /* resume suspended thread */
if (!rt_list_isempty(&(mb->suspend_sender_thread)))
{
rt_ipc_list_resume(&(mb->suspend_sender_thread));

/* enable interrupt */
rt_hw_interrupt_enable(temp);

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

rt_schedule();

return RT_EOK;
}

/* enable interrupt */
rt_hw_interrupt_enable(temp);

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

return RT_EOK;
}
RTM_EXPORT(rt_mb_recv);
  • 如果有人在等待收信,叫醒他,做一次調度

Message Queue#

  • 特性:可接受不固定長度的訊息

結構#

File: rtdef.h

663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#ifdef RT_USING_MESSAGEQUEUE
/**
* message queue structure
*/
struct rt_messagequeue
{
struct rt_ipc_object parent; /**< inherit from ipc_object */

void *msg_pool; /**< start address of message queue */

rt_uint16_t msg_size; /**< message size of each message */
rt_uint16_t max_msgs; /**< max number of messages */

rt_uint16_t entry; /**< index of messages in the queue */

void *msg_queue_head; /**< list head */
void *msg_queue_tail; /**< list tail */
void *msg_queue_free; /**< pointer indicated the free node of queue */
};
typedef struct rt_messagequeue *rt_mq_t;
#endif

File: ipc.c

建立 message queue#

動態記憶體管理#

功能 回傳值
建立 message queue message queue
*name msg_size max_msgs flag
名字 一封訊息的大小 訊息數上限 FIFO / PRIO
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
/**
* This function will create a message queue object from system resource
*
* @param name the name of message queue
* @param msg_size the size of message
* @param max_msgs the maximum number of message in queue
* @param flag the flag of message queue
*
* @return the created message queue, RT_NULL on error happen
*/
rt_mq_t rt_mq_create(const char *name,
rt_size_t msg_size,
rt_size_t max_msgs,
rt_uint8_t flag)
{
struct rt_messagequeue *mq;
struct rt_mq_message *head;
register rt_base_t temp;

RT_DEBUG_NOT_IN_INTERRUPT;

/* allocate object */
mq = (rt_mq_t)rt_object_allocate(RT_Object_Class_MessageQueue, name);
if (mq == RT_NULL)
return mq;

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

/* init ipc object */
rt_ipc_object_init(&(mq->parent));
  • 首先要一塊物件給 message queue,並同時填入 flag 及初始化
1883
1884
1885
1886
1887
/* init message queue */

/* get correct message size */
mq->msg_size = RT_ALIGN(msg_size, RT_ALIGN_SIZE);
mq->max_msgs = max_msgs;
  • 設定訊息的大小,與訊息數量的上限

RT_ALGIN 目的在對齊訊息的大小,根據不同板子所定義不同的 RT_ALIGN_SIZE 會有所差別

#define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))

  • 如傳進來的是 RT_ALGIN(7,8) 則結果是 8
  • 如傳進來的是 RT_ALGIN(13,4) 則結果是 16
  • 即結果為大於後值的最小倍數
1888
1889
1890
1891
1892
1893
1894
1895
/* allocate message pool */
mq->msg_pool = RT_KERNEL_MALLOC((mq->msg_size + sizeof(struct rt_mq_message)) * mq->max_msgs);
if (mq->msg_pool == RT_NULL)
{
rt_mq_delete(mq);

return RT_NULL;
}
  • 接著需 allocate 適當的記憶體存放訊息
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
/* init message list */
mq->msg_queue_head = RT_NULL;
mq->msg_queue_tail = RT_NULL;

/* init message empty list */
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 +
temp * (mq->msg_size + sizeof(struct rt_mq_message)));
head->next = mq->msg_queue_free;
mq->msg_queue_free = head;
}
  • 先將頭尾設為空,再一塊一塊的將 msg_pool 插在 free list 的第一顆
1909
1910
1911
1912
1913
1914
    /* the initial entry is zero */
mq->entry = 0;

return mq;
}
RTM_EXPORT(rt_mq_create);
  • 最後設定 entry 為 0

靜態記憶體管理#

功能 回傳值
初始化 message queue RT_EOK
mq *name *msgpool
message queue 本體 名字 存放訊息的位址
msg_size pool_size flag
一封訊息的大小 存放訊息的大小 FIFO / PRIO
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
/**
* This function will initialize a message queue and put it under control of
* resource management.
*
* @param mq the message object
* @param name the name of message queue
* @param msgpool the beginning address of buffer to save messages
* @param msg_size the maximum size of message
* @param pool_size the size of buffer to save messages
* @param flag the flag of message queue
*
* @return the operation status, RT_EOK on successful
*/
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)
{
struct rt_mq_message *head;
register rt_base_t temp;

/* parameter check */
RT_ASSERT(mq != RT_NULL);

/* init object */
rt_object_init(&(mq->parent.parent), RT_Object_Class_MessageQueue, name);

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

/* init ipc object */
rt_ipc_object_init(&(mq->parent));
  • 這裡就不需要去要一塊物件,直接拿來用即可
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
    /* set messasge pool */
mq->msg_pool = msgpool;

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

/* init message list */
mq->msg_queue_head = RT_NULL;
mq->msg_queue_tail = RT_NULL;

/* init message empty list */
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 +
temp * (mq->msg_size + sizeof(struct rt_mq_message)));
head->next = mq->msg_queue_free;
mq->msg_queue_free = head;
}

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

return RT_EOK;
}
RTM_EXPORT(rt_mq_init);
  • 其餘的動作皆與上面相同

刪除 message queue#

動態記憶體管理#

功能 回傳值 mq
刪除 message queue RT_EOK 欲刪除的 message queue
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
/**
* This function will delete a message queue object and release the memory
*
* @param mq the message queue object
*
* @return the error code
*/
rt_err_t rt_mq_delete(rt_mq_t mq)
{
RT_DEBUG_NOT_IN_INTERRUPT;

/* parameter check */
RT_ASSERT(mq != RT_NULL);
RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent) == RT_FALSE);

/* resume all suspended thread */
rt_ipc_list_resume_all(&(mq->parent.suspend_thread));
  • 先把正在等待收訊息的 thread 叫醒
1938
1939
1940
1941
1942
1943
1944
1945
1946
    /* free message queue pool */
RT_KERNEL_FREE(mq->msg_pool);

/* delete message queue object */
rt_object_delete(&(mq->parent.parent));

return RT_EOK;
}
RTM_EXPORT(rt_mq_delete);
  • 接著 free msg_pool,並刪除物件

靜態記憶體管理#

功能 回傳值 mq
刪除 message queue RT_EOK 欲刪除的 message queue
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
/**
* This function will detach a message queue object from resource management
*
* @param mq the message queue object
*
* @return the operation status, RT_EOK on successful
*/
rt_err_t rt_mq_detach(rt_mq_t mq)
{
/* parameter check */
RT_ASSERT(mq != RT_NULL);
RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
RT_ASSERT(rt_object_is_systemobject(&mq->parent.parent));

/* resume all suspended thread */
rt_ipc_list_resume_all(&mq->parent.suspend_thread);

/* detach message queue object */
rt_object_detach(&(mq->parent.parent));

return RT_EOK;
}
RTM_EXPORT(rt_mq_detach);
  • 這裡的 msg_pool 就不需要 free

傳送訊息#

功能 回傳值
傳送訊息 RT_EOK
mq *buffer size
欲傳送的 message queue 訊息資料 訊息大小
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
/**
* This function will send a message to message queue object, if there are
* threads suspended on message queue object, it will be waked up.
*
* @param mq the message queue object
* @param buffer the message
* @param size the size of buffer
*
* @return the error code
*/
rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size)
{
register rt_ubase_t temp;
struct rt_mq_message *msg;

/* parameter check */
RT_ASSERT(mq != RT_NULL);
RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
RT_ASSERT(buffer != RT_NULL);
RT_ASSERT(size != 0);

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

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 */
msg = (struct rt_mq_message *)mq->msg_queue_free;
/* message queue is full */
if (msg == RT_NULL)
{
/* enable interrupt */
rt_hw_interrupt_enable(temp);

return -RT_EFULL;
}
  • 首先確定 message queue 沒滿(即 free list 不為空)
  • 如果滿了,回傳 FULL
1990
1991
/* move free list pointer */
mq->msg_queue_free = msg->next;
  • 接著 free list 往下一顆走
1992
1993
1994
1995
1996
1997
1998
/* 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);
  • 將訊息填入從 free list 拿的一顆(msg),這顆待會是新的尾巴(設定 next = NULL
1999
2000
2001
2002
2003
2004
2005
2006
/* 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;
}
  • 如果尾巴不為空(也就是 message queue 有東西),將原本的尾巴指向 msg
2007
2008
2009
2010
2011
/* 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;
  • 設定新的尾巴
  • 如果頭為空(也就是 message queue 為空),設定新的頭
2012
2013
/* increase message entry */
mq->entry ++;
  • 最後更新 entry
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
    /* resume suspended thread */
if (!rt_list_isempty(&mq->parent.suspend_thread))
{
rt_ipc_list_resume(&(mq->parent.suspend_thread));

/* 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_mq_send);
  • 如果有人在等待接收訊息,叫醒他

傳送緊急訊息#

  • 與上面不同的是:這裡將新訊息插入第一顆
功能 回傳值
傳送緊急訊息 RT_EOK
mq *buffer size
欲傳送的 message queue 訊息資料 訊息大小
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
/**
* This function will send an urgent message to message queue object, which
* means the message will be inserted to the head of message queue. If there
* are threads suspended on message queue object, it will be waked up.
*
* @param mq the message queue object
* @param buffer the message
* @param size the size of buffer
*
* @return the error code
*/
rt_err_t rt_mq_urgent(rt_mq_t mq, void *buffer, rt_size_t size)
{
register rt_ubase_t temp;
struct rt_mq_message *msg;

/* parameter check */
RT_ASSERT(mq != RT_NULL);
RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
RT_ASSERT(buffer != RT_NULL);
RT_ASSERT(size != 0);

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

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 */
msg = (struct rt_mq_message *)mq->msg_queue_free;
/* message queue is full */
if (msg == RT_NULL)
{
/* enable interrupt */
rt_hw_interrupt_enable(temp);

return -RT_EFULL;
}
/* move free list pointer */
mq->msg_queue_free = msg->next;

/* enable interrupt */
rt_hw_interrupt_enable(temp);

/* copy buffer */
rt_memcpy(msg + 1, buffer, size);
  • 因為要插在第一顆,next 就不用設定為空了
2088
2089
2090
2091
2092
2093
/* disable interrupt */
temp = rt_hw_interrupt_disable();

/* link msg to the beginning of message queue */
msg->next = mq->msg_queue_head;
mq->msg_queue_head = msg;
  • 這裡就將新訊息插在第一顆
2097
2098
2099
/* if there is no tail */
if (mq->msg_queue_tail == RT_NULL)
mq->msg_queue_tail = msg;
  • 如果原本的 message queue 為空,設定新的尾巴
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
    /* increase message entry */
mq->entry ++;

/* resume suspended thread */
if (!rt_list_isempty(&mq->parent.suspend_thread))
{
rt_ipc_list_resume(&(mq->parent.suspend_thread));

/* 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_mq_urgent);
  • 其他的動作皆相同

接收訊息#

功能 回傳值
接收訊息 RT_EOK
mq *buffer size timeout
欲訊息 message queue 訊息存放處 訊息存放處大小 等待時間(如果需要)
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
/**
* This function will receive a message from message queue object, if there is
* no message in message queue object, the thread shall wait for a specified
* time.
*
* @param mq the message queue object
* @param buffer the received message will be saved in
* @param size the size of buffer
* @param timeout the waiting time
*
* @return the error code
*/
rt_err_t rt_mq_recv(rt_mq_t mq,
void *buffer,
rt_size_t size,
rt_int32_t timeout)
{
struct rt_thread *thread;
register rt_ubase_t temp;
struct rt_mq_message *msg;
rt_uint32_t tick_delta;

/* parameter check */
RT_ASSERT(mq != RT_NULL);
RT_ASSERT(rt_object_get_type(&mq->parent.parent) == RT_Object_Class_MessageQueue);
RT_ASSERT(buffer != RT_NULL);
RT_ASSERT(size != 0);

/* initialize delta tick */
tick_delta = 0;
/* get current thread */
thread = rt_thread_self();
RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mq->parent.parent)));

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

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

return -RT_ETIMEOUT;
}

/* message queue is empty */
while (mq->entry == 0)
{
RT_DEBUG_IN_THREAD_CONTEXT;

/* 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;
}
  • 如果 message queue 為空,且不等待,回傳 TIMEOUT
2186
2187
2188
2189
/* suspend current thread */
rt_ipc_list_suspend(&(mq->parent.suspend_thread),
thread,
mq->parent.parent.flag);
  • 如要等待,將 thread 掛在等待鏈上
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
/* 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",
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));
}
  • 並啟動一個 timer
2205
2206
2207
2208
2209
/* enable interrupt */
rt_hw_interrupt_enable(temp);

/* re-schedule */
rt_schedule();
  • 開始等待,做一次調度
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
    /* 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;
}
}
  • 如跳回來,重新計算 timeout
2229
2230
2231
2232
2233
2234
2235
2236
/* 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;
  • 如果 message queue 有資料,拿第一顆,同時更新 head(tail,如果需要)
2237
2238
/* decrease message entry */
mq->entry --;
  • 更新 entry
2239
2240
2241
2242
2243
/* enable interrupt */
rt_hw_interrupt_enable(temp);

/* copy message */
rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size);
  • 接著複製找到的訊息
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
    /* 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;
/* enable interrupt */
rt_hw_interrupt_enable(temp);

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

return RT_EOK;
}
RTM_EXPORT(rt_mq_recv);
  • 最後將 msg 插入 free list 的頭