1 /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
33 size_t ps
= sysconf (_SC_PAGESIZE
);
34 char tmpfname
[] = "/tmp/tst-spin2.XXXXXX";
38 pthread_spinlock_t
*s
;
43 fd
= mkstemp (tmpfname
);
46 printf ("cannot open temporary file: %m\n");
50 /* Make sure it is always removed. */
53 /* Create one page of data. */
54 memset (data
, '\0', ps
);
56 /* Write the data to the file. */
57 if (write (fd
, data
, ps
) != (ssize_t
) ps
)
63 mem
= mmap (NULL
, ps
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
64 if (mem
== MAP_FAILED
)
66 printf ("mmap failed: %m\n");
70 s
= (pthread_spinlock_t
*) (((uintptr_t) mem
71 + __alignof (pthread_spinlock_t
))
72 & ~(__alignof (pthread_spinlock_t
) - 1));
75 if (pthread_spin_init (s
, PTHREAD_PROCESS_SHARED
) != 0)
77 puts ("spin_init failed");
81 if (pthread_spin_lock (s
) != 0)
83 puts ("spin_lock failed");
87 err
= pthread_spin_trylock (s
);
90 puts ("1st spin_trylock succeeded");
93 else if (err
!= EBUSY
)
95 puts ("1st spin_trylock didn't return EBUSY");
99 err
= pthread_spin_unlock (s
);
102 puts ("parent: spin_unlock failed");
106 err
= pthread_spin_trylock (s
);
109 puts ("2nd spin_trylock failed");
115 puts ("going to fork now");
119 puts ("fork failed");
124 /* Play some lock ping-pong. It's our turn to unlock first. */
127 puts ("child: *p != 0");
131 if (pthread_spin_unlock (s
) != 0)
133 puts ("child: 1st spin_unlock failed");
141 if (pthread_spin_lock (s
) != 0)
143 puts ("parent: 2nd spin_lock failed");
147 puts ("waiting for child");
149 waitpid (pid
, NULL
, 0);
151 puts ("parent done");
157 #define TEST_FUNCTION do_test ()
158 #include "../test-skeleton.c"