]>
Commit | Line | Data |
---|---|---|
6d7e8eda | 1 | /* Copyright (C) 2003-2023 Free Software Foundation, Inc. |
61623643 | 2 | This file is part of the GNU C Library. |
61623643 UD |
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 | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
61623643 UD |
17 | |
18 | #include <errno.h> | |
19 | #include <pthread.h> | |
903ae060 | 20 | #include <stdint.h> |
61623643 | 21 | #include <stdio.h> |
903ae060 | 22 | #include <stdlib.h> |
61623643 UD |
23 | #include <string.h> |
24 | #include <unistd.h> | |
25 | #include <sys/mman.h> | |
903ae060 | 26 | #include <sys/time.h> |
61623643 | 27 | #include <sys/wait.h> |
ce5b73a7 MC |
28 | #include <support/check.h> |
29 | #include <support/timespec.h> | |
30 | #include <support/xunistd.h> | |
2313ab15 | 31 | #include <support/xthread.h> |
61623643 | 32 | |
71eeae03 AZ |
33 | #ifdef ENABLE_PP |
34 | #include "tst-tpp.h" | |
35 | #endif | |
36 | ||
61623643 | 37 | |
9d20e22e MC |
38 | /* A bogus clock value that tells run_test to use pthread_mutex_timedlock |
39 | rather than pthread_mutex_clocklock. */ | |
40 | #define CLOCK_USE_TIMEDLOCK (-1) | |
41 | ||
42 | static void | |
2313ab15 | 43 | do_test_clock (clockid_t clockid, int tmo_result) |
61623643 | 44 | { |
9d20e22e MC |
45 | const clockid_t clockid_for_get = |
46 | (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; | |
61623643 UD |
47 | size_t ps = sysconf (_SC_PAGESIZE); |
48 | char tmpfname[] = "/tmp/tst-mutex9.XXXXXX"; | |
49 | char data[ps]; | |
50 | void *mem; | |
51 | int fd; | |
52 | pthread_mutex_t *m; | |
53 | pthread_mutexattr_t a; | |
54 | pid_t pid; | |
61623643 UD |
55 | |
56 | fd = mkstemp (tmpfname); | |
57 | if (fd == -1) | |
ce5b73a7 | 58 | FAIL_EXIT1 ("cannot open temporary file: %m\n"); |
61623643 UD |
59 | |
60 | /* Make sure it is always removed. */ | |
61 | unlink (tmpfname); | |
62 | ||
63 | /* Create one page of data. */ | |
64 | memset (data, '\0', ps); | |
65 | ||
66 | /* Write the data to the file. */ | |
ce5b73a7 | 67 | xwrite (fd, data, ps); |
61623643 | 68 | |
ce5b73a7 | 69 | mem = xmmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd); |
61623643 UD |
70 | |
71 | m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t)) | |
72 | & ~(__alignof (pthread_mutex_t) - 1)); | |
61623643 | 73 | |
ce5b73a7 | 74 | TEST_COMPARE (pthread_mutexattr_init (&a), 0); |
61623643 | 75 | |
ce5b73a7 | 76 | TEST_COMPARE (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED), 0); |
61623643 | 77 | |
ce5b73a7 | 78 | TEST_COMPARE (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE), 0); |
61623643 | 79 | |
71eeae03 | 80 | #if defined ENABLE_PI |
ce5b73a7 | 81 | TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0); |
71eeae03 AZ |
82 | #elif defined ENABLE_PP |
83 | TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_PROTECT), 0); | |
84 | TEST_COMPARE (pthread_mutexattr_setprioceiling (&a, 6), 0); | |
df47504c UD |
85 | #endif |
86 | ||
87 | int e; | |
88 | if ((e = pthread_mutex_init (m, &a)) != 0) | |
89 | { | |
90 | #ifdef ENABLE_PI | |
91 | if (e == ENOTSUP) | |
ce5b73a7 | 92 | FAIL_UNSUPPORTED ("PI mutexes unsupported"); |
df47504c | 93 | #endif |
ce5b73a7 | 94 | FAIL_EXIT1 ("mutex_init failed"); |
61623643 UD |
95 | } |
96 | ||
ce5b73a7 | 97 | TEST_COMPARE (pthread_mutex_lock (m), 0); |
61623643 | 98 | |
ce5b73a7 | 99 | TEST_COMPARE (pthread_mutexattr_destroy (&a), 0); |
61623643 UD |
100 | |
101 | puts ("going to fork now"); | |
ce5b73a7 MC |
102 | pid = xfork (); |
103 | if (pid == 0) | |
61623643 UD |
104 | { |
105 | if (pthread_mutex_trylock (m) == 0) | |
ce5b73a7 | 106 | FAIL_EXIT1 ("child: mutex_trylock succeeded"); |
61623643 UD |
107 | |
108 | if (pthread_mutex_unlock (m) == 0) | |
ce5b73a7 MC |
109 | FAIL_EXIT1 ("child: mutex_unlock succeeded"); |
110 | ||
9d20e22e | 111 | const struct timespec ts = timespec_add (xclock_now (clockid_for_get), |
ce5b73a7 MC |
112 | make_timespec (0, 500000000)); |
113 | ||
9d20e22e | 114 | if (clockid == CLOCK_USE_TIMEDLOCK) |
2313ab15 | 115 | TEST_COMPARE (pthread_mutex_timedlock (m, &ts), tmo_result); |
9d20e22e | 116 | else |
2313ab15 | 117 | TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), tmo_result); |
61623643 UD |
118 | |
119 | alarm (1); | |
120 | ||
121 | pthread_mutex_lock (m); | |
122 | ||
123 | puts ("child: mutex_lock returned"); | |
124 | ||
125 | exit (0); | |
126 | } | |
127 | ||
128 | sleep (2); | |
129 | ||
130 | int status; | |
131 | if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) | |
ce5b73a7 | 132 | FAIL_EXIT1 ("waitpid failed"); |
61623643 | 133 | if (! WIFSIGNALED (status)) |
ce5b73a7 MC |
134 | FAIL_EXIT1 ("child not killed by signal"); |
135 | TEST_COMPARE (WTERMSIG (status), SIGALRM); | |
9d20e22e | 136 | } |
61623643 | 137 | |
9d20e22e MC |
138 | static int |
139 | do_test (void) | |
140 | { | |
71eeae03 AZ |
141 | #ifdef ENABLE_PP |
142 | init_tpp_test (); | |
143 | #endif | |
144 | ||
2313ab15 AZ |
145 | int monotonic_result = |
146 | #ifdef ENABLE_PI | |
147 | support_mutex_pi_monotonic () ? ETIMEDOUT : EINVAL; | |
148 | #else | |
149 | ETIMEDOUT; | |
74f418b2 | 150 | #endif |
2313ab15 AZ |
151 | |
152 | do_test_clock (CLOCK_USE_TIMEDLOCK, ETIMEDOUT); | |
153 | do_test_clock (CLOCK_REALTIME, ETIMEDOUT); | |
154 | do_test_clock (CLOCK_MONOTONIC, monotonic_result); | |
61623643 | 155 | return 0; |
61623643 UD |
156 | } |
157 | ||
ce5b73a7 | 158 | #include <support/test-driver.c> |