1 /* Copyright (C) 2002-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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/time.h>
24 #include <support/check.h>
25 #include <support/timespec.h>
26 #include <support/xthread.h>
27 #include <support/xtime.h>
28 
29 
30 #define CLOCK_USE_TIMEDJOIN (-1)
31 
32 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
33 
34 
35 static void *
tf(void * arg)36 tf (void *arg)
37 {
38   xpthread_mutex_lock (&lock);
39   xpthread_mutex_unlock (&lock);
40 
41   return (void *) 42l;
42 }
43 
44 
45 static int
do_test_clock(clockid_t clockid)46 do_test_clock (clockid_t clockid)
47 {
48   const clockid_t clockid_for_get =
49     (clockid == CLOCK_USE_TIMEDJOIN) ? CLOCK_REALTIME : clockid;
50 
51   xpthread_mutex_lock (&lock);
52   pthread_t th = xpthread_create (NULL, tf, NULL);
53 
54   void *status;
55   struct timespec timeout = timespec_add (xclock_now (clockid_for_get),
56                                           make_timespec (0, 200000000));
57 
58   int val;
59   if (clockid == CLOCK_USE_TIMEDJOIN)
60     val = pthread_timedjoin_np (th, &status, &timeout);
61   else
62     val = pthread_clockjoin_np (th, &status, clockid, &timeout);
63 
64   TEST_COMPARE (val, ETIMEDOUT);
65 
66   xpthread_mutex_unlock (&lock);
67 
68   while (1)
69     {
70       timeout = timespec_add (xclock_now (clockid_for_get),
71                               make_timespec (0, 200000000));
72 
73       if (clockid == CLOCK_USE_TIMEDJOIN)
74         val = pthread_timedjoin_np (th, &status, &timeout);
75       else
76         val = pthread_clockjoin_np (th, &status, clockid, &timeout);
77       if (val == 0)
78 	break;
79 
80       TEST_COMPARE (val, ETIMEDOUT);
81     }
82 
83   if (status != (void *) 42l)
84     FAIL_EXIT1 ("return value %p, expected %p\n", status, (void *) 42l);
85 
86   return 0;
87 }
88 
89 static int
do_test(void)90 do_test (void)
91 {
92   do_test_clock (CLOCK_USE_TIMEDJOIN);
93   do_test_clock (CLOCK_REALTIME);
94   do_test_clock (CLOCK_MONOTONIC);
95   return 0;
96 }
97 
98 #include <support/test-driver.c>
99