]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-mutex5.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-mutex5.c
CommitLineData
04277e02 1/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
76a50749
UD
18
19#include <errno.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <time.h>
23#include <unistd.h>
24#include <sys/time.h>
e401d5ca 25#include <stdint.h>
68cc2935 26#include <config.h>
ce5b73a7
MC
27#include <support/check.h>
28#include <support/timespec.h>
76a50749
UD
29
30
2c0b891a
UD
31#ifndef TYPE
32# define TYPE PTHREAD_MUTEX_NORMAL
33#endif
34
9d20e22e
MC
35/* A bogus clock value that tells run_test to use
36 pthread_mutex_timedlock rather than pthread_mutex_clocklock. */
37#define CLOCK_USE_TIMEDLOCK (-1)
2c0b891a 38
76a50749 39static int
9d20e22e 40do_test_clock (clockid_t clockid, const char *fnname)
76a50749
UD
41{
42 pthread_mutex_t m;
2c0b891a 43 pthread_mutexattr_t a;
9d20e22e
MC
44 const clockid_t clockid_for_get =
45 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
2c0b891a 46
ce5b73a7
MC
47 TEST_COMPARE (pthread_mutexattr_init (&a), 0);
48 TEST_COMPARE (pthread_mutexattr_settype (&a, TYPE), 0);
76a50749 49
df47504c 50#ifdef ENABLE_PI
ce5b73a7 51 TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0);
df47504c
UD
52#endif
53
ce5b73a7 54 int err = pthread_mutex_init (&m, &a);
df47504c
UD
55 if (err != 0)
56 {
57#ifdef ENABLE_PI
58 if (err == ENOTSUP)
ce5b73a7 59 FAIL_UNSUPPORTED ("PI mutexes unsupported");
df47504c 60#endif
ce5b73a7 61 FAIL_EXIT1 ("mutex_init failed");
76a50749
UD
62 }
63
ce5b73a7
MC
64 TEST_COMPARE (pthread_mutexattr_destroy (&a), 0);
65 TEST_COMPARE (pthread_mutex_lock (&m), 0);
76a50749 66 if (pthread_mutex_trylock (&m) == 0)
ce5b73a7 67 FAIL_EXIT1 ("mutex_trylock succeeded");
76a50749 68
ce5b73a7 69 /* Wait 2 seconds. */
9d20e22e 70 struct timespec ts_timeout = timespec_add (xclock_now (clockid_for_get),
ce5b73a7 71 make_timespec (2, 0));
76a50749 72
9d20e22e
MC
73 if (clockid == CLOCK_USE_TIMEDLOCK)
74 TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), ETIMEDOUT);
75 else
76 TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout),
77 ETIMEDOUT);
78 TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get);
1c82b97f 79
1c82b97f 80 /* The following makes the ts value invalid. */
ce5b73a7 81 ts_timeout.tv_nsec += 1000000000;
1c82b97f 82
9d20e22e
MC
83 if (clockid == CLOCK_USE_TIMEDLOCK)
84 TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), EINVAL);
85 else
86 TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), EINVAL);
ce5b73a7 87 TEST_COMPARE (pthread_mutex_unlock (&m), 0);
1c82b97f 88
ce5b73a7 89 const struct timespec ts_start = xclock_now (CLOCK_REALTIME);
76a50749 90
ce5b73a7
MC
91 /* Wait 2 seconds. */
92 ts_timeout = timespec_add (ts_start, make_timespec (2, 0));
1c82b97f 93
9d20e22e
MC
94 if (clockid == CLOCK_USE_TIMEDLOCK)
95 TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), 0);
96 else
97 TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), 0);
1c82b97f 98
9d20e22e 99 const struct timespec ts_end = xclock_now (clockid_for_get);
1c82b97f
UD
100
101 /* Check that timedlock didn't delay. We use a limit of 0.1 secs. */
ce5b73a7
MC
102 TEST_TIMESPEC_BEFORE (ts_end,
103 timespec_add (ts_start, make_timespec (0, 100000000)));
1c82b97f 104
ce5b73a7
MC
105 TEST_COMPARE (pthread_mutex_unlock (&m), 0);
106 TEST_COMPARE (pthread_mutex_destroy (&m), 0);
76a50749
UD
107
108 return 0;
109}
110
9d20e22e
MC
111static int do_test (void)
112{
113 do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock");
114 do_test_clock (CLOCK_REALTIME, "clocklock(realtime)");
115 do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)");
116 return 0;
117}
118
ce5b73a7 119#include <support/test-driver.c>