(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / sysdeps / x86_64 / pspinlock.c
blobe1b2a668410534ac4b2634daa341b3e0ea62f376
1 /* POSIX spinlock implementation. x86-64 version.
2 Copyright (C) 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include "internals.h"
24 /* This implementation is similar to the one used in the Linux kernel.
25 But the kernel is byte instructions for the memory access. This is
26 faster but unusable here. The problem is that only 128
27 threads/processes could use the spinlock at the same time. If (by
28 a design error in the program) a thread/process would hold the
29 spinlock for a time long enough to accumulate 128 waiting
30 processes, the next one will find a positive value in the spinlock
31 and assume it is unlocked. We cannot accept that. */
33 int
34 __pthread_spin_lock (pthread_spinlock_t *lock)
36 asm volatile
37 ("\n"
38 "1:\n\t"
39 "lock; decl %0\n\t"
40 "js 2f\n\t"
41 ".section .text.spinlock,\"ax\"\n"
42 "2:\n\t"
43 "cmpl $0,%0\n\t"
44 "rep; nop\n\t"
45 "jle 2b\n\t"
46 "jmp 1b\n\t"
47 ".previous"
48 : "=m" (*lock));
49 return 0;
51 weak_alias (__pthread_spin_lock, pthread_spin_lock)
54 int
55 __pthread_spin_trylock (pthread_spinlock_t *lock)
57 int oldval;
59 asm volatile
60 ("xchgl %0,%1"
61 : "=r" (oldval), "=m" (*lock)
62 : "0" (0));
63 return oldval > 0 ? 0 : EBUSY;
65 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
68 int
69 __pthread_spin_unlock (pthread_spinlock_t *lock)
71 asm volatile
72 ("movl $1,%0"
73 : "=m" (*lock));
74 return 0;
76 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
79 int
80 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
82 /* We can ignore the `pshared' parameter. Since we are busy-waiting
83 all processes which can access the memory location `lock' points
84 to can use the spinlock. */
85 *lock = 1;
86 return 0;
88 weak_alias (__pthread_spin_init, pthread_spin_init)
91 int
92 __pthread_spin_destroy (pthread_spinlock_t *lock)
94 /* Nothing to do. */
95 return 0;
97 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)