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
29 static pthread_barrier_t b;
31 /* Cleanup handling test. */
40 #define TF_MQ_RECEIVE 0L
41 #define TF_MQ_TIMEDRECEIVE 1L
43 #define TF_MQ_TIMEDSEND 3L
45 static const char *names[]
46 = { "mq_receive", "mq_timedreceive", "mq_send", "mq_timedsend" };
49 static struct timespec never;
54 int r = pthread_barrier_wait (&b);
55 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
57 puts ("tf: barrier_wait failed");
61 pthread_cleanup_push (cl, NULL);
68 TEMP_FAILURE_RETRY (mq_send (q, &c, 1, 1));
71 TEMP_FAILURE_RETRY (mq_timedsend (q, &c, 1, 1, &never));
74 TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL));
76 case TF_MQ_TIMEDRECEIVE:
77 TEMP_FAILURE_RETRY (mq_timedreceive (q, &c, 1, NULL, &never));
81 pthread_cleanup_pop (0);
83 printf ("tf: %s returned\n", names[(long) arg]);
88 #define TEST_FUNCTION do_test ()
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);
100 printf ("mq_open failed with: %m\n");
104 if (mq_unlink (name) != 0)
106 printf ("mq_unlink failed with: %m\n");
110 if (pthread_barrier_init (&b, NULL, 2) != 0)
112 puts ("barrier_init failed");
116 if (clock_gettime (CLOCK_REALTIME, &never) == 0)
120 never.tv_sec = time (NULL) + 100;
125 for (long l = TF_MQ_RECEIVE; l <= TF_MQ_TIMEDSEND; ++l)
130 if (pthread_create (&th, NULL, tf, (void *) l) != 0)
132 printf ("1st %s create failed\n", names[l]);
137 int r = pthread_barrier_wait (&b);
138 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
140 puts ("barrier_wait failed");
145 struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
146 while (nanosleep (&ts, &ts) != 0)
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]);
158 if (pthread_join (th, &status) != 0)
160 printf ("1st join of %s failed\n", names[l]);
164 if (status != PTHREAD_CANCELED)
166 printf ("1st %s thread not canceled\n", names[l]);
173 printf ("%s cleanup handler not called\n", names[l]);
179 printf ("%s cleanup handler called more than once\n", names[l]);
184 printf ("in-time %s cancellation succeeded\n", names[l]);
188 if (pthread_create (&th, NULL, tf, (void *) l) != 0)
190 printf ("2nd %s create failed\n", names[l]);
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]);
203 r = pthread_barrier_wait (&b);
204 if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
206 puts ("barrier_wait failed");
211 if (pthread_join (th, &status) != 0)
213 printf ("2nd join of %s failed\n", names[l]);
217 if (status != PTHREAD_CANCELED)
219 printf ("2nd %s thread not canceled\n", names[l]);
226 printf ("%s cleanup handler not called\n", names[l]);
232 printf ("%s cleanup handler called more than once\n", names[l]);
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. */
245 if (mq_send (q, &c, 1, 1) != 0)
247 printf ("mq_send failed: %m\n");
254 if (mq_close (q) != 0)
256 printf ("mq_close failed: %m\n");
263 # define TEST_FUNCTION 0
266 #include "../test-skeleton.c"