]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/pthread/tst-mutex5.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / pthread / tst-mutex5.c
CommitLineData
2b778ceb 1/* Copyright (C) 2002-2021 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 29
71eeae03
AZ
30#ifdef ENABLE_PP
31#include "tst-tpp.h"
32#endif
76a50749 33
2c0b891a
UD
34#ifndef TYPE
35# define TYPE PTHREAD_MUTEX_NORMAL
36#endif
37
9d20e22e
MC
38/* A bogus clock value that tells run_test to use
39 pthread_mutex_timedlock rather than pthread_mutex_clocklock. */
40#define CLOCK_USE_TIMEDLOCK (-1)
2c0b891a 41
76a50749 42static int
9d20e22e 43do_test_clock (clockid_t clockid, const char *fnname)
76a50749
UD
44{
45 pthread_mutex_t m;
2c0b891a 46 pthread_mutexattr_t a;
9d20e22e
MC
47 const clockid_t clockid_for_get =
48 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
2c0b891a 49
ce5b73a7
MC
50 TEST_COMPARE (pthread_mutexattr_init (&a), 0);
51 TEST_COMPARE (pthread_mutexattr_settype (&a, TYPE), 0);
76a50749 52
71eeae03 53#if defined ENABLE_PI
ce5b73a7 54 TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0);
71eeae03
AZ
55#elif defined ENABLE_PP
56 TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_PROTECT), 0);
57 TEST_COMPARE (pthread_mutexattr_setprioceiling (&a, 6), 0);
df47504c
UD
58#endif
59
ce5b73a7 60 int err = pthread_mutex_init (&m, &a);
df47504c
UD
61 if (err != 0)
62 {
63#ifdef ENABLE_PI
64 if (err == ENOTSUP)
ce5b73a7 65 FAIL_UNSUPPORTED ("PI mutexes unsupported");
df47504c 66#endif
ce5b73a7 67 FAIL_EXIT1 ("mutex_init failed");
76a50749
UD
68 }
69
ce5b73a7
MC
70 TEST_COMPARE (pthread_mutexattr_destroy (&a), 0);
71 TEST_COMPARE (pthread_mutex_lock (&m), 0);
76a50749 72 if (pthread_mutex_trylock (&m) == 0)
ce5b73a7 73 FAIL_EXIT1 ("mutex_trylock succeeded");
76a50749 74
ce5b73a7 75 /* Wait 2 seconds. */
9d20e22e 76 struct timespec ts_timeout = timespec_add (xclock_now (clockid_for_get),
ce5b73a7 77 make_timespec (2, 0));
76a50749 78
9d20e22e
MC
79 if (clockid == CLOCK_USE_TIMEDLOCK)
80 TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), ETIMEDOUT);
81 else
82 TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout),
83 ETIMEDOUT);
84 TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get);
1c82b97f 85
1c82b97f 86 /* The following makes the ts value invalid. */
ce5b73a7 87 ts_timeout.tv_nsec += 1000000000;
1c82b97f 88
9d20e22e
MC
89 if (clockid == CLOCK_USE_TIMEDLOCK)
90 TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), EINVAL);
91 else
92 TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), EINVAL);
ce5b73a7 93 TEST_COMPARE (pthread_mutex_unlock (&m), 0);
1c82b97f 94
ce5b73a7 95 const struct timespec ts_start = xclock_now (CLOCK_REALTIME);
76a50749 96
ce5b73a7
MC
97 /* Wait 2 seconds. */
98 ts_timeout = timespec_add (ts_start, make_timespec (2, 0));
1c82b97f 99
9d20e22e
MC
100 if (clockid == CLOCK_USE_TIMEDLOCK)
101 TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), 0);
102 else
103 TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), 0);
1c82b97f 104
9d20e22e 105 const struct timespec ts_end = xclock_now (clockid_for_get);
1c82b97f
UD
106
107 /* Check that timedlock didn't delay. We use a limit of 0.1 secs. */
ce5b73a7
MC
108 TEST_TIMESPEC_BEFORE (ts_end,
109 timespec_add (ts_start, make_timespec (0, 100000000)));
1c82b97f 110
ce5b73a7
MC
111 TEST_COMPARE (pthread_mutex_unlock (&m), 0);
112 TEST_COMPARE (pthread_mutex_destroy (&m), 0);
76a50749
UD
113
114 return 0;
115}
116
9d20e22e
MC
117static int do_test (void)
118{
71eeae03
AZ
119#ifdef ENABLE_PP
120 init_tpp_test ();
121#endif
122
9d20e22e
MC
123 do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock");
124 do_test_clock (CLOCK_REALTIME, "clocklock(realtime)");
74f418b2 125#ifndef ENABLE_PI
9d20e22e 126 do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)");
74f418b2 127#endif
9d20e22e
MC
128 return 0;
129}
130
ce5b73a7 131#include <support/test-driver.c>