1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2010-11-17 Bernard first version
9 */
10
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <pthread.h>
16 #include <mqueue.h>
17
18 #define MQ_NAME_1 "testmsg1"
19 #define MQ_NAME_2 "testmsg2"
20 #define MSG_SIZE 128
21 #define MAX_MSG 3
22
23 const char *s_msg_ptr[] = {"msg test 1", "msg test 2", "msg test 3"};
24 char r_msg_ptr_1[MAX_MSG][MSG_SIZE];
25 char r_msg_ptr_2[MAX_MSG][MSG_SIZE];
26 pthread_t send1, send2, rev1, rev2;
27
send_1(void * mq)28 int * send_1(void * mq)
29 {
30 int i;
31 mqd_t mq1 = *(mqd_t *)mq;
32
33 printf("Enter into send_1 \n");
34 for (i = 0; i < MAX_MSG; i++ ) {
35 if ( -1 == mq_send(mq1, s_msg_ptr[i], MSG_SIZE, i)) {
36 perror("mq_send doesn't return success \n");
37 pthread_exit((void *)1);
38 }
39 printf("[%d] send '%s' in thread send_1. \n", i+1, s_msg_ptr[i]);
40 }
41 pthread_exit((void *)0);
42
43 }
44
send_2(void * mq)45 int * send_2(void * mq)
46 {
47 int i;
48 mqd_t mq2 = *(mqd_t *)mq;
49
50 printf("Enter into send_2 \n");
51 for (i = 0; i < MAX_MSG; i++ ) {
52 if ( -1 == mq_send(mq2, s_msg_ptr[i], MSG_SIZE, i)) {
53 perror("mq_send doesn't return success \n");
54 pthread_exit((void *)1);
55 }
56 printf("[%d] send '%s' in thread send_2. \n", i+1, s_msg_ptr[i]);
57 }
58 pthread_exit((void *)0);
59 }
60
receive_1(void * mq)61 int * receive_1(void * mq)
62 {
63 int i;
64 mqd_t mq1 = *(mqd_t *)mq;
65
66 printf("Enter into receive_1 \n");
67 for (i = 0; i< MAX_MSG; i++) {
68 if ( -1 == mq_receive(mq1, r_msg_ptr_1[i], MSG_SIZE, NULL) ) {
69 perror("mq_receive doesn't return success \n");
70 pthread_exit((void *)1);
71 }
72 printf("[%d] receive '%s' in thread receive_1. \n", i+1, r_msg_ptr_1[i]);
73 }
74 pthread_exit((void *)0);
75 }
receive_2(void * mq)76 int * receive_2(void * mq)
77 {
78 int i;
79 mqd_t mq2 = *(mqd_t *)mq;
80
81 printf("Enter into receive_2 \n");
82 for (i = 0; i< MAX_MSG; i++) {
83 if ( -1 == mq_receive(mq2, r_msg_ptr_2[i], MSG_SIZE, NULL) ) {
84 perror("mq_receive doesn't return success \n");
85 pthread_exit((void *)1);
86 }
87 printf("[%d] receive '%s' in thread receive_2. \n", i+1, r_msg_ptr_2[i]);
88 }
89 pthread_exit((void *)0);
90 }
91
libc_mq()92 int libc_mq()
93 {
94 mqd_t mq1 = 0, mq2 = 0;
95 struct mq_attr mqstat;
96 int oflag = O_CREAT|O_RDWR;
97
98 memset(&mqstat, 0, sizeof(mqstat));
99 mqstat.mq_maxmsg = MAX_MSG;
100 mqstat.mq_msgsize = MSG_SIZE;
101 mqstat.mq_flags = 0;
102
103 if( ((mqd_t) -1) == (mq1 = mq_open(MQ_NAME_1,oflag,0777, &mqstat)) ) {
104 printf("mq_open doesn't return success \n");
105 return -1;
106 }
107 if( ((mqd_t) -1) == (mq2 = mq_open(MQ_NAME_2,oflag,0777, &mqstat)) ) {
108 printf("mq_open doesn't return success \n");
109 return -1;
110 }
111 pthread_create(&send1, NULL, (void *)send_1, (void *)&mq1);
112 pthread_create(&send2, NULL, (void *)send_2, (void *)&mq2);
113 pthread_create(&rev1, NULL, (void *)receive_1, (void *)&mq1);
114 pthread_create(&rev2, NULL, (void *)receive_2, (void *)&mq2);
115 pthread_join(send1, NULL);
116 pthread_join(send2, NULL);
117 pthread_join(rev1, NULL);
118 pthread_join(rev2, NULL);
119
120 mq_close(mq1);
121 mq_close(mq2);
122 mq_unlink(MQ_NAME_1);
123 mq_unlink(MQ_NAME_2);
124
125 printf("PASSED\n");
126 return 0;
127 }
128 #include <finsh.h>
129 FINSH_FUNCTION_EXPORT(libc_mq, posix mqueue test);
130