包含头文件#include “rtai_sched.h”
1. 发送函数
int rt_mbx_send (MBX* mbx, void* msg, int msg_size);
//无条件发送消息
int rt_mbx_send_wp (MBX* mbx, void* msg, int msg_size);
//在不阻塞当期调用的任务的情况下,尽可能多的发送数据。消息长度是msg_size。
int rt_mbx_send_if (MBX* mbx, void* msg, int msg_size);
//发送一个消息, 要求在不阻塞当前调用任务的情况下,整个消息可以完整传递。
int rt_mbx_send_until (MBX* mbx, void* msg, int msg_size, RTIME time);
int rt_mbx_send_timed (MBX* mbx, void* msg, int msg_size, RTIME delay);
//rt_mbx_send_until和rt_mbx_send_timed发送消息msg_size大小的msg到邮箱mbx。调用者将被阻塞,直到所有字节的入消息队列、超时过期或发生错误。
函数返回
成功,未寄出的字节的数量被返回;失败,返回负值,负值定义如下:EINVAL mbx指向无效的mailbox(mbx points to not a valid mailbox).
2. 接收函数
int rt_mbx_receive (MBX* mbx, void* msg, int msg_size);
//无条件接受消息
int rt_mbx_receive_wp (MBX* mbx, void* msg, int msg_size);
//在不阻塞当期调用的任务的情况下,尽可能多的接收数据。
int rt_mbx_receive_if (MBX* mbx, void* msg, int msg_size);
//整个msg_size大小的消息能立刻获取到时,接收消息。
int rt_mbx_receive_until (MBX* mbx, void* msg, int msg_size, RTIME time);
int rt_mbx_receive_timed (MBX* mbx, void* msg, int msg_size, RTIME delay);
//调用者将被阻塞,直到所有字节的入消息队列、超时过期或发生错误。time是绝对值,Delay是相对于当前时间的相对值.
函数返回
成功,返回收到的字节数量。
失败,返回一个负值,定义如下:EINVAL mbx指向无效的mailbox(mbx points to not a valid mailbox).
3. 示例代码
/* messqueue.c*/
/* ------------ headers ------------------------------------------- */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include /* error codes */
#include
#include
#include
#include
#include
/* ------------ globals ------------------------------------------- */
/* turn on(1) or off(0) debugging */
const int DEBUG=1;
// fix include warning
//#include
//#include
int pthread_cancel_rt(pthread_t thread);
// linux threads id's
static int thread1;
static int thread2;
// realtime task structures
static RT_TASK *t1;
static RT_TASK *t2;
// message queue
static MBX *mesgQueueId;
#define MAX_MESSAGES 100
#define MAX_MESSAGE_LENGTH 50
/* ------------ functions ------------------------------------------- */
void taskOne(void *arg)
{
int retval;
char message[] = "Received message from taskOne";
/* make this thread LXRT soft realtime */
t1 = rt_task_init_schmod(nam2num("TASK1"), 0, 0, 0, SCHED_FIFO, 0xF);
mlockall(MCL_CURRENT | MCL_FUTURE);
// makes task hard real time (only when not developing/debugging)
if ( !DEBUG ) rt_make_hard_real_time();
rt_printk("Started taskOne\n");
/* send message */
retval = rt_mbx_send(mesgQueueId, message, sizeof(message));
if (0 != retval) {
if (-EINVAL == retval) {
rt_printk("mailbox is invalid\n");
} else {
/* unknown error */
rt_printk("Unknown mailbox error\n");
}
}
}
void taskTwo(void *arg)
{
int retval;
char msgBuf[MAX_MESSAGE_LENGTH];
/* make this thread LXRT soft realtime */
t2 = rt_task_init_schmod(nam2num("TASK2"), 0, 0, 0, SCHED_FIFO, 0xF);
mlockall(MCL_CURRENT | MCL_FUTURE);
// makes task hard real time (only when not developing/debugging)
if ( !DEBUG ) rt_make_hard_real_time();
rt_printk("Started taskTwo\n");
/* receive message */
retval = rt_mbx_receive_wp(mesgQueueId, msgBuf, 50);
if (-EINVAL == retval) {
rt_printk("mailbox is invalid\n");
} else {
rt_printk("message length=50-%d\n",retval);
rt_printk("message : %s\n",msgBuf);
}
}
void cleanup(void)
{
/* delete message queue */
rt_mbx_delete(mesgQueueId);
// task end themselves -> not necesssary to delete them
return;
}
/* ------------ main ------------------------------------------- */
int main(void)
{
printf("Start of main\n");
/* make this thread LXRT soft realtime */
rt_task_init_schmod(nam2num("TASK0"), 0, 0, 0, SCHED_FIFO, 0xF);
mlockall(MCL_CURRENT | MCL_FUTURE);
// set realtime timer to run in oneshot mode
rt_set_oneshot_mode();
// start realtime timer and scheduler
start_rt_timer(1);
/* create message queue */
mesgQueueId = rt_typed_mbx_init (nam2num("MSGQUEUE"), MAX_MESSAGES, FIFO_Q);
if (mesgQueueId == 0) {
printf("Error creating message queue\n");
return 1;
}
// create the linux threads
thread1 = rt_thread_create(taskOne, NULL, 10000);
thread2 = rt_thread_create(taskTwo, NULL, 10000);
// wait for end of program
printf("TYPE TO TERMINATE\n" );
getchar();
// cleanup
cleanup();
printf("Finished\n");
return 0;
}