1 /* Copyright (C) 2007-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18 #include <errno.h>
19 #include <pthread.h>
20 #include <semaphore.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25
26
27 static int
do_test(void)28 do_test (void)
29 {
30 sem_t s;
31 if (sem_init (&s, 0, 0) == -1)
32 {
33 puts ("sem_init failed");
34 return 1;
35 }
36
37 struct timeval tv;
38 if (gettimeofday (&tv, NULL) != 0)
39 {
40 puts ("gettimeofday failed");
41 return 1;
42 }
43
44 struct timespec ts;
45 TIMEVAL_TO_TIMESPEC (&tv, &ts);
46
47 /* Set ts to yesterday. */
48 ts.tv_sec -= 86400;
49
50 int type_before;
51 if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &type_before) != 0)
52 {
53 puts ("first pthread_setcanceltype failed");
54 return 1;
55 }
56
57 errno = 0;
58 if (TEMP_FAILURE_RETRY (sem_timedwait (&s, &ts)) != -1)
59 {
60 puts ("sem_timedwait succeeded");
61 return 1;
62 }
63 if (errno != ETIMEDOUT)
64 {
65 printf ("sem_timedwait return errno = %d instead of ETIMEDOUT\n",
66 errno);
67 return 1;
68 }
69
70 int type_after;
71 if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &type_after) != 0)
72 {
73 puts ("second pthread_setcanceltype failed");
74 return 1;
75 }
76 if (type_after != PTHREAD_CANCEL_DEFERRED)
77 {
78 puts ("sem_timedwait changed cancellation type");
79 return 1;
80 }
81
82 return 0;
83 }
84
85 #define TEST_FUNCTION do_test ()
86 #include "../test-skeleton.c"
87