OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libpthread / linuxthreads / sysdeps / i386 / pspinlock.c
1 /* POSIX spinlock implementation.  x86 version.
2    Copyright (C) 2000, 2002, 2006 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
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 License as
7    published by the Free Software Foundation; either version 2.1 of the
8    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
16    License along with the GNU C Library; see the file COPYING.LIB.  If
17    not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include "internals.h"
22 #include <bits/kernel-features.h>
23
24
25 /* This implementation is similar to the one used in the Linux kernel.
26    But the kernel is byte instructions for the memory access.  This is
27    faster but unusable here.  The problem is that only 128
28    threads/processes could use the spinlock at the same time.  If (by
29    a design error in the program) a thread/process would hold the
30    spinlock for a time long enough to accumulate 128 waiting
31    processes, the next one will find a positive value in the spinlock
32    and assume it is unlocked.  We cannot accept that.  */
33
34 int
35 __pthread_spin_lock (pthread_spinlock_t *lock)
36 {
37   __asm__ __volatile__
38     ("\n"
39      "1:\n\t"
40      "lock; decl %0\n\t"
41      "js 2f\n\t"
42      ".section .text.spinlock,\"ax\"\n"
43      "2:\n\t"
44      "cmpl $0,%0\n\t"
45      "rep; nop\n\t"
46      "jle 2b\n\t"
47      "jmp 1b\n\t"
48      ".previous"
49      : "=m" (*lock));
50   return 0;
51 }
52 weak_alias (__pthread_spin_lock, pthread_spin_lock)
53
54
55 int
56 __pthread_spin_trylock (pthread_spinlock_t *lock)
57 {
58   int oldval;
59
60   __asm__ __volatile__
61     ("xchgl %0,%1"
62      : "=r" (oldval), "=m" (*lock)
63      : "0" (0));
64   return oldval > 0 ? 0 : EBUSY;
65 }
66 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
67
68
69 int
70 __pthread_spin_unlock (pthread_spinlock_t *lock)
71 {
72   __asm__ __volatile__
73     ("movl $1,%0"
74      : "=m" (*lock));
75   return 0;
76 }
77 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
78
79
80 int
81 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
82 {
83   /* We can ignore the `pshared' parameter.  Since we are busy-waiting
84      all processes which can access the memory location `lock' points
85      to can use the spinlock.  */
86   *lock = 1;
87   return 0;
88 }
89 weak_alias (__pthread_spin_init, pthread_spin_init)
90
91
92 int
93 __pthread_spin_destroy (pthread_spinlock_t *lock)
94 {
95   /* Nothing to do.  */
96   return 0;
97 }
98 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)
99
100 #ifndef __ASSUME_SET_THREAD_AREA_SYSCALL
101 int __have_no_set_thread_area;
102 #endif