(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / sysdeps / i386 / pspinlock.c
blob6a700939570473c956ee6e99803bae5d1f40fe52
1 /* POSIX spinlock implementation. x86 version.
2 Copyright (C) 2000, 2002 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 License as
7 published by the Free Software Foundation; either version 2.1 of the
8 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; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include "internals.h"
23 #include "kernel-features.h"
26 /* This implementation is similar to the one used in the Linux kernel.
27 But the kernel is byte instructions for the memory access. This is
28 faster but unusable here. The problem is that only 128
29 threads/processes could use the spinlock at the same time. If (by
30 a design error in the program) a thread/process would hold the
31 spinlock for a time long enough to accumulate 128 waiting
32 processes, the next one will find a positive value in the spinlock
33 and assume it is unlocked. We cannot accept that. */
35 int
36 __pthread_spin_lock (pthread_spinlock_t *lock)
38 asm volatile
39 ("\n"
40 "1:\n\t"
41 "lock; decl %0\n\t"
42 "js 2f\n\t"
43 ".section .text.spinlock,\"ax\"\n"
44 "2:\n\t"
45 "cmpl $0,%0\n\t"
46 "rep; nop\n\t"
47 "jle 2b\n\t"
48 "jmp 1b\n\t"
49 ".previous"
50 : "=m" (*lock));
51 return 0;
53 weak_alias (__pthread_spin_lock, pthread_spin_lock)
56 int
57 __pthread_spin_trylock (pthread_spinlock_t *lock)
59 int oldval;
61 asm volatile
62 ("xchgl %0,%1"
63 : "=r" (oldval), "=m" (*lock)
64 : "0" (0));
65 return oldval > 0 ? 0 : EBUSY;
67 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
70 int
71 __pthread_spin_unlock (pthread_spinlock_t *lock)
73 asm volatile
74 ("movl $1,%0"
75 : "=m" (*lock));
76 return 0;
78 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
81 int
82 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
84 /* We can ignore the `pshared' parameter. Since we are busy-waiting
85 all processes which can access the memory location `lock' points
86 to can use the spinlock. */
87 *lock = 1;
88 return 0;
90 weak_alias (__pthread_spin_init, pthread_spin_init)
93 int
94 __pthread_spin_destroy (pthread_spinlock_t *lock)
96 /* Nothing to do. */
97 return 0;
99 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)
101 #ifndef __ASSUME_SET_THREAD_AREA_SYSCALL
102 int __have_no_set_thread_area;
103 #endif