| 1 | /* pthread_timedjoin_np, pthread_clockjoin_np NULL timeout test. |
| 2 | Copyright (C) 2019-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <support/check.h> |
| 26 | #include <support/timespec.h> |
| 27 | #include <support/xthread.h> |
| 28 | #include <support/xtime.h> |
| 29 | |
| 30 | |
| 31 | #define CLOCK_USE_TIMEDJOIN (-1) |
| 32 | |
| 33 | |
| 34 | static void * |
| 35 | tf (void *arg) |
| 36 | { |
| 37 | struct timespec ts = make_timespec(s: 0, ns: 100000); |
| 38 | nanosleep(requested_time: &ts, NULL); |
| 39 | |
| 40 | return (void *) 42l; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /* Check that pthread_timedjoin_np and pthread_clockjoin_np wait "forever" if |
| 45 | * passed a timeout parameter of NULL. We can't actually wait forever, but we |
| 46 | * can be sure that we did at least wait for some time by checking the exit |
| 47 | * status of the thread. */ |
| 48 | static int |
| 49 | do_test_clock (clockid_t clockid) |
| 50 | { |
| 51 | pthread_t th = xpthread_create (NULL, thread_func: tf, NULL); |
| 52 | |
| 53 | void *status; |
| 54 | int val = (clockid == CLOCK_USE_TIMEDJOIN) |
| 55 | ? pthread_timedjoin_np (th: th, thread_return: &status, NULL) |
| 56 | : pthread_clockjoin_np (th: th, thread_return: &status, clockid: clockid, NULL); |
| 57 | TEST_COMPARE (val, 0); |
| 58 | |
| 59 | if (status != (void *) 42l) |
| 60 | FAIL_EXIT1 ("return value %p, expected %p\n" , status, (void *) 42l); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int |
| 66 | do_test (void) |
| 67 | { |
| 68 | do_test_clock (CLOCK_USE_TIMEDJOIN); |
| 69 | do_test_clock (CLOCK_REALTIME); |
| 70 | do_test_clock (CLOCK_MONOTONIC); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | #include <support/test-driver.c> |
| 75 | |