1 /* Copyright (C) 2004-2015 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, see
17 <http://www.gnu.org/licenses/>. */
28 static pthread_barrier_t b
;
30 /* Cleanup handling test. */
39 #define TF_MQ_RECEIVE 0L
40 #define TF_MQ_TIMEDRECEIVE 1L
42 #define TF_MQ_TIMEDSEND 3L
44 static const char *names
[]
45 = { "mq_receive", "mq_timedreceive", "mq_send", "mq_timedsend" };
48 static struct timespec never
;
53 int r
= pthread_barrier_wait (&b
);
54 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
56 puts ("tf: barrier_wait failed");
60 pthread_cleanup_push (cl
, NULL
);
67 TEMP_FAILURE_RETRY (mq_send (q
, &c
, 1, 1));
70 TEMP_FAILURE_RETRY (mq_timedsend (q
, &c
, 1, 1, &never
));
73 TEMP_FAILURE_RETRY (mq_receive (q
, &c
, 1, NULL
));
75 case TF_MQ_TIMEDRECEIVE
:
76 TEMP_FAILURE_RETRY (mq_timedreceive (q
, &c
, 1, NULL
, &never
));
80 pthread_cleanup_pop (0);
82 printf ("tf: %s returned\n", names
[(long) arg
]);
87 #define TEST_FUNCTION do_test ()
91 char name
[sizeof "/tst-mqueue8-" + sizeof (pid_t
) * 3];
92 snprintf (name
, sizeof (name
), "/tst-mqueue8-%u", getpid ());
94 struct mq_attr attr
= { .mq_maxmsg
= 1, .mq_msgsize
= 1 };
95 q
= mq_open (name
, O_CREAT
| O_EXCL
| O_RDWR
, 0600, &attr
);
99 printf ("mq_open failed with: %m\n");
103 if (mq_unlink (name
) != 0)
105 printf ("mq_unlink failed with: %m\n");
109 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
111 puts ("barrier_init failed");
115 if (clock_gettime (CLOCK_REALTIME
, &never
) == 0)
119 never
.tv_sec
= time (NULL
) + 100;
124 for (long l
= TF_MQ_RECEIVE
; l
<= TF_MQ_TIMEDSEND
; ++l
)
129 if (pthread_create (&th
, NULL
, tf
, (void *) l
) != 0)
131 printf ("1st %s create failed\n", names
[l
]);
136 int r
= pthread_barrier_wait (&b
);
137 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
139 puts ("barrier_wait failed");
144 struct timespec ts
= { .tv_sec
= 0, .tv_nsec
= 100000000 };
145 while (nanosleep (&ts
, &ts
) != 0)
148 printf ("going to cancel %s in-time\n", names
[l
]);
149 if (pthread_cancel (th
) != 0)
151 printf ("1st cancel of %s failed\n", names
[l
]);
157 if (pthread_join (th
, &status
) != 0)
159 printf ("1st join of %s failed\n", names
[l
]);
163 if (status
!= PTHREAD_CANCELED
)
165 printf ("1st %s thread not canceled\n", names
[l
]);
172 printf ("%s cleanup handler not called\n", names
[l
]);
178 printf ("%s cleanup handler called more than once\n", names
[l
]);
183 printf ("in-time %s cancellation succeeded\n", names
[l
]);
187 if (pthread_create (&th
, NULL
, tf
, (void *) l
) != 0)
189 printf ("2nd %s create failed\n", names
[l
]);
194 printf ("going to cancel %s early\n", names
[l
]);
195 if (pthread_cancel (th
) != 0)
197 printf ("2nd cancel of %s failed\n", names
[l
]);
202 r
= pthread_barrier_wait (&b
);
203 if (r
!= 0 && r
!= PTHREAD_BARRIER_SERIAL_THREAD
)
205 puts ("barrier_wait failed");
210 if (pthread_join (th
, &status
) != 0)
212 printf ("2nd join of %s failed\n", names
[l
]);
216 if (status
!= PTHREAD_CANCELED
)
218 printf ("2nd %s thread not canceled\n", names
[l
]);
225 printf ("%s cleanup handler not called\n", names
[l
]);
231 printf ("%s cleanup handler called more than once\n", names
[l
]);
236 printf ("early %s cancellation succeeded\n", names
[l
]);
238 if (l
== TF_MQ_TIMEDRECEIVE
)
240 /* mq_receive and mq_timedreceive are tested on empty mq.
241 For mq_send and mq_timedsend we need to make it full.
242 If this fails, there is no point in doing further testing. */
244 if (mq_send (q
, &c
, 1, 1) != 0)
246 printf ("mq_send failed: %m\n");
253 if (mq_close (q
) != 0)
255 printf ("mq_close failed: %m\n");
262 # define TEST_FUNCTION 0
265 #include "../test-skeleton.c"