OSDN Git Service

2003-05-28 Jeff Johnston <jjohnstn@redhat.com>
[pf3gnuchains/pf3gnuchains3x.git] / newlib / libc / sys / linux / linuxthreads / machine / i386 / pspinlock.c
1 /* POSIX spinlock implementation.  x86 version.
2    Copyright (C) 2000 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 Library General Public License as
7    published by the Free Software Foundation; either version 2 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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library 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.  */
19
20 #include <errno.h>
21 #include <pthread.h>
22 #include "internals.h"
23
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.  */
32
33 int
34 __pthread_spin_lock (pthread_spinlock_t *lock)
35 {
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;
50 }
51 weak_alias (__pthread_spin_lock, pthread_spin_lock)
52
53
54 int
55 __pthread_spin_trylock (pthread_spinlock_t *lock)
56 {
57   int oldval;
58
59   asm volatile
60     ("xchgl %0,%1"
61      : "=r" (oldval), "=m" (*lock)
62      : "0" (0));
63   return oldval > 0 ? 0 : EBUSY;
64 }
65 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
66
67
68 int
69 __pthread_spin_unlock (pthread_spinlock_t *lock)
70 {
71   asm volatile
72     ("movl $1,%0"
73      : "=m" (*lock));
74   return 0;
75 }
76 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
77
78
79 int
80 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
81 {
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;
87 }
88 weak_alias (__pthread_spin_init, pthread_spin_init)
89
90
91 int
92 __pthread_spin_destroy (pthread_spinlock_t *lock)
93 {
94   /* Nothing to do.  */
95   return 0;
96 }
97 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)