* malloc/mcheck.c (reallochook): If size==0, free the block.
[glibc/pb-stable.git] / rt / tst-mqueue8.c
blob7e902aa6018091ba6ac1cb125d394f3f3b006c09
1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <mqueue.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #if _POSIX_THREADS
27 # include <pthread.h>
29 static pthread_barrier_t b;
31 /* Cleanup handling test. */
32 static int cl_called;
34 static void
35 cl (void *arg)
37 ++cl_called;
40 #define TF_MQ_RECEIVE 0L
41 #define TF_MQ_TIMEDRECEIVE 1L
42 #define TF_MQ_SEND 2L
43 #define TF_MQ_TIMEDSEND 3L
45 static const char *names[]
46 = { "mq_receive", "mq_timedreceive", "mq_send", "mq_timedsend" };
48 static mqd_t q;
49 static struct timespec never;
51 static void *
52 tf (void *arg)
54 int r = pthread_barrier_wait (&b);
55 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
57 puts ("tf: barrier_wait failed");
58 exit (1);
61 pthread_cleanup_push (cl, NULL);
63 char c = ' ';
65 switch ((long) arg)
67 case TF_MQ_SEND:
68 TEMP_FAILURE_RETRY (mq_send (q, &c, 1, 1));
69 break;
70 case TF_MQ_TIMEDSEND:
71 TEMP_FAILURE_RETRY (mq_timedsend (q, &c, 1, 1, &never));
72 break;
73 case TF_MQ_RECEIVE:
74 TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
75 break;
76 case TF_MQ_TIMEDRECEIVE:
77 TEMP_FAILURE_RETRY (mq_timedreceive (q, &c, 1, NULL, &never));
78 break;
81 pthread_cleanup_pop (0);
83 printf ("tf: %s returned\n", names[(long) arg]);
85 exit (1);
88 #define TEST_FUNCTION do_test ()
89 static int
90 do_test (void)
92 char name[sizeof "/tst-mqueue8-" + sizeof (pid_t) * 3];
93 snprintf (name, sizeof (name), "/tst-mqueue8-%u", getpid ());
95 struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
96 q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
98 if (q == (mqd_t) -1)
100 printf ("mq_open failed with: %m\n");
101 return 0;
104 if (mq_unlink (name) != 0)
106 printf ("mq_unlink failed with: %m\n");
107 return 1;
110 if (pthread_barrier_init (&b, NULL, 2) != 0)
112 puts ("barrier_init failed");
113 return 1;
116 if (clock_gettime (CLOCK_REALTIME, &never) == 0)
117 never.tv_sec += 100;
118 else
120 never.tv_sec = time (NULL) + 100;
121 never.tv_nsec = 0;
124 int result = 0;
125 for (long l = TF_MQ_RECEIVE; l <= TF_MQ_TIMEDSEND; ++l)
127 cl_called = 0;
129 pthread_t th;
130 if (pthread_create (&th, NULL, tf, (void *) l) != 0)
132 printf ("1st %s create failed\n", names[l]);
133 result = 1;
134 continue;
137 int r = pthread_barrier_wait (&b);
138 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
140 puts ("barrier_wait failed");
141 result = 1;
142 continue;
145 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
146 while (nanosleep (&ts, &ts) != 0)
147 continue;
149 printf ("going to cancel %s in-time\n", names[l]);
150 if (pthread_cancel (th) != 0)
152 printf ("1st cancel of %s failed\n", names[l]);
153 result = 1;
154 continue;
157 void *status;
158 if (pthread_join (th, &status) != 0)
160 printf ("1st join of %s failed\n", names[l]);
161 result = 1;
162 continue;
164 if (status != PTHREAD_CANCELED)
166 printf ("1st %s thread not canceled\n", names[l]);
167 result = 1;
168 continue;
171 if (cl_called == 0)
173 printf ("%s cleanup handler not called\n", names[l]);
174 result = 1;
175 continue;
177 if (cl_called > 1)
179 printf ("%s cleanup handler called more than once\n", names[l]);
180 result = 1;
181 continue;
184 printf ("in-time %s cancellation succeeded\n", names[l]);
186 cl_called = 0;
188 if (pthread_create (&th, NULL, tf, (void *) l) != 0)
190 printf ("2nd %s create failed\n", names[l]);
191 result = 1;
192 continue;
195 printf ("going to cancel %s early\n", names[l]);
196 if (pthread_cancel (th) != 0)
198 printf ("2nd cancel of %s failed\n", names[l]);
199 result = 1;
200 continue;
203 r = pthread_barrier_wait (&b);
204 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
206 puts ("barrier_wait failed");
207 result = 1;
208 continue;
211 if (pthread_join (th, &status) != 0)
213 printf ("2nd join of %s failed\n", names[l]);
214 result = 1;
215 continue;
217 if (status != PTHREAD_CANCELED)
219 printf ("2nd %s thread not canceled\n", names[l]);
220 result = 1;
221 continue;
224 if (cl_called == 0)
226 printf ("%s cleanup handler not called\n", names[l]);
227 result = 1;
228 continue;
230 if (cl_called > 1)
232 printf ("%s cleanup handler called more than once\n", names[l]);
233 result = 1;
234 continue;
237 printf ("early %s cancellation succeeded\n", names[l]);
239 if (l == TF_MQ_TIMEDRECEIVE)
241 /* mq_receive and mq_timedreceive are tested on empty mq.
242 For mq_send and mq_timedsend we need to make it full.
243 If this fails, there is no point in doing further testing. */
244 char c = ' ';
245 if (mq_send (q, &c, 1, 1) != 0)
247 printf ("mq_send failed: %m\n");
248 result = 1;
249 break;
254 if (mq_close (q) != 0)
256 printf ("mq_close failed: %m\n");
257 result = 1;
260 return result;
262 #else
263 # define TEST_FUNCTION 0
264 #endif
266 #include "../test-skeleton.c"