]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 2002-2020 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 <stdint.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <unistd.h> | |
26 | #include <sys/mman.h> | |
27 | #include <sys/wait.h> | |
28 | ||
29 | ||
b910f788 RM |
30 | static int |
31 | do_test (void) | |
76a50749 UD |
32 | { |
33 | size_t ps = sysconf (_SC_PAGESIZE); | |
34 | char tmpfname[] = "/tmp/tst-spin2.XXXXXX"; | |
35 | char data[ps]; | |
36 | void *mem; | |
37 | int fd; | |
38 | pthread_spinlock_t *s; | |
39 | pid_t pid; | |
40 | char *p; | |
41 | int err; | |
42 | ||
43 | fd = mkstemp (tmpfname); | |
44 | if (fd == -1) | |
45 | { | |
46 | printf ("cannot open temporary file: %m\n"); | |
b910f788 | 47 | return 1; |
76a50749 UD |
48 | } |
49 | ||
50 | /* Make sure it is always removed. */ | |
51 | unlink (tmpfname); | |
52 | ||
53 | /* Create one page of data. */ | |
54 | memset (data, '\0', ps); | |
55 | ||
56 | /* Write the data to the file. */ | |
f9657e88 | 57 | if (write (fd, data, ps) != (ssize_t) ps) |
76a50749 UD |
58 | { |
59 | puts ("short write"); | |
b910f788 | 60 | return 1; |
76a50749 UD |
61 | } |
62 | ||
63 | mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
64 | if (mem == MAP_FAILED) | |
65 | { | |
66 | printf ("mmap failed: %m\n"); | |
b910f788 | 67 | return 1; |
76a50749 UD |
68 | } |
69 | ||
70 | s = (pthread_spinlock_t *) (((uintptr_t) mem | |
71 | + __alignof (pthread_spinlock_t)) | |
72 | & ~(__alignof (pthread_spinlock_t) - 1)); | |
73 | p = (char *) (s + 1); | |
74 | ||
75 | if (pthread_spin_init (s, PTHREAD_PROCESS_SHARED) != 0) | |
76 | { | |
77 | puts ("spin_init failed"); | |
b910f788 | 78 | return 1; |
76a50749 UD |
79 | } |
80 | ||
81 | if (pthread_spin_lock (s) != 0) | |
82 | { | |
83 | puts ("spin_lock failed"); | |
b910f788 | 84 | return 1; |
76a50749 UD |
85 | } |
86 | ||
87 | err = pthread_spin_trylock (s); | |
88 | if (err == 0) | |
89 | { | |
f78deea6 | 90 | puts ("1st spin_trylock succeeded"); |
b910f788 | 91 | return 1; |
76a50749 UD |
92 | } |
93 | else if (err != EBUSY) | |
94 | { | |
f78deea6 | 95 | puts ("1st spin_trylock didn't return EBUSY"); |
b910f788 | 96 | return 1; |
f78deea6 UD |
97 | } |
98 | ||
99 | err = pthread_spin_unlock (s); | |
100 | if (err != 0) | |
101 | { | |
102 | puts ("parent: spin_unlock failed"); | |
b910f788 | 103 | return 1; |
f78deea6 UD |
104 | } |
105 | ||
106 | err = pthread_spin_trylock (s); | |
107 | if (err != 0) | |
108 | { | |
109 | puts ("2nd spin_trylock failed"); | |
b910f788 | 110 | return 1; |
76a50749 UD |
111 | } |
112 | ||
113 | *p = 0; | |
114 | ||
115 | puts ("going to fork now"); | |
116 | pid = fork (); | |
117 | if (pid == -1) | |
118 | { | |
119 | puts ("fork failed"); | |
b910f788 | 120 | return 1; |
76a50749 UD |
121 | } |
122 | else if (pid == 0) | |
123 | { | |
124 | /* Play some lock ping-pong. It's our turn to unlock first. */ | |
125 | if ((*p)++ != 0) | |
126 | { | |
127 | puts ("child: *p != 0"); | |
b910f788 | 128 | return 1; |
76a50749 UD |
129 | } |
130 | ||
131 | if (pthread_spin_unlock (s) != 0) | |
132 | { | |
133 | puts ("child: 1st spin_unlock failed"); | |
b910f788 | 134 | return 1; |
76a50749 UD |
135 | } |
136 | ||
137 | puts ("child done"); | |
138 | } | |
139 | else | |
140 | { | |
141 | if (pthread_spin_lock (s) != 0) | |
142 | { | |
143 | puts ("parent: 2nd spin_lock failed"); | |
b910f788 | 144 | return 1; |
76a50749 UD |
145 | } |
146 | ||
147 | puts ("waiting for child"); | |
148 | ||
149 | waitpid (pid, NULL, 0); | |
150 | ||
151 | puts ("parent done"); | |
152 | } | |
153 | ||
b910f788 | 154 | return 0; |
76a50749 | 155 | } |
b910f788 RM |
156 | |
157 | #define TEST_FUNCTION do_test () | |
158 | #include "../test-skeleton.c" |