1 /* Copyright (C) 2002-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
32 size_t ps
= sysconf (_SC_PAGESIZE
);
33 char tmpfname
[] = "/tmp/tst-spin2.XXXXXX";
37 pthread_spinlock_t
*s
;
42 fd
= mkstemp (tmpfname
);
45 printf ("cannot open temporary file: %m\n");
49 /* Make sure it is always removed. */
52 /* Create one page of data. */
53 memset (data
, '\0', ps
);
55 /* Write the data to the file. */
56 if (write (fd
, data
, ps
) != (ssize_t
) ps
)
62 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
63 if (mem
== MAP_FAILED
)
65 printf ("mmap failed: %m\n");
69 s
= (pthread_spinlock_t
*) (((uintptr_t) mem
70 + __alignof (pthread_spinlock_t
))
71 & ~(__alignof (pthread_spinlock_t
) - 1));
74 if (pthread_spin_init (s
, PTHREAD_PROCESS_SHARED
) != 0)
76 puts ("spin_init failed");
80 if (pthread_spin_lock (s
) != 0)
82 puts ("spin_lock failed");
86 err
= pthread_spin_trylock (s
);
89 puts ("1st spin_trylock succeeded");
92 else if (err
!= EBUSY
)
94 puts ("1st spin_trylock didn't return EBUSY");
98 err
= pthread_spin_unlock (s
);
101 puts ("parent: spin_unlock failed");
105 err
= pthread_spin_trylock (s
);
108 puts ("2nd spin_trylock failed");
114 puts ("going to fork now");
118 puts ("fork failed");
123 /* Play some lock ping-pong. It's our turn to unlock first. */
126 puts ("child: *p != 0");
130 if (pthread_spin_unlock (s
) != 0)
132 puts ("child: 1st spin_unlock failed");
140 if (pthread_spin_lock (s
) != 0)
142 puts ("parent: 2nd spin_lock failed");
146 puts ("waiting for child");
148 waitpid (pid
, NULL
, 0);
150 puts ("parent done");
156 #define TEST_FUNCTION do_test ()
157 #include "../test-skeleton.c"